]> matita.cs.unibo.it Git - helm.git/blob - helm/interface/xaland-java/xaland.java
9eda83124f873aba29fbc822e82a5bead580a0f4
[helm.git] / helm / interface / xaland-java / xaland.java
1 import org.apache.xalan.xslt.*;
2 import java.net.*;
3 import java.io.*;
4
5 public class xaland {
6    public static void Transform(StylesheetRoot style, String xmlSourceURL, String OutputURL) throws java.io.IOException, java.net.MalformedURLException, org.xml.sax.SAXException
7    {
8       XSLTInputSource xmlSource = new XSLTInputSource (xmlSourceURL);
9       XSLTResultTarget xmlResult = new XSLTResultTarget (OutputURL);
10       style.process(xmlSource, xmlResult);
11    }
12
13    public static void main(String argv[]) throws  java.io.IOException, java.net.MalformedURLException, org.xml.sax.SAXException
14    {
15       int port    = Integer.parseInt(argv[0]);
16       int port2   = Integer.parseInt(argv[1]);
17       String xsl1 = argv[2];
18       String xsl2 = argv[3];
19       String theory_xsl1 = argv[4];
20       String theory_xsl2 = argv[5];
21
22       XSLTProcessor theory_processor =
23        XSLTProcessorFactory.getProcessor(new org.apache.xalan.xpath.xdom.XercesLiaison());
24       StylesheetRoot theory_style1 =
25          theory_processor.processStylesheet(theory_xsl1);
26       theory_processor.reset();
27       StylesheetRoot theory_style2 =
28          theory_processor.processStylesheet(theory_xsl2);
29       theory_processor.setStylesheet(theory_style2);
30
31       XSLTProcessor processor =
32        XSLTProcessorFactory.getProcessor(new org.apache.xalan.xpath.xdom.XercesLiaison());
33       StylesheetRoot style1 = processor.processStylesheet(xsl1);
34       processor.reset();
35       StylesheetRoot style2 = processor.processStylesheet(xsl2);
36       processor.setStylesheet(style2);
37
38       DatagramSocket socket = new DatagramSocket(port);
39
40       System.out.println("Demon activated on input port " + port +
41        " and output port " + port2);
42       while(true) {
43          System.out.print("Ready...");
44
45          /* Warning: the packet must be a fresh one! */
46          DatagramPacket packet = new DatagramPacket(new byte[1024],1024);
47          socket.receive(packet);
48          byte data[] = packet.getData();
49          int datalen = packet.getLength();
50          String received = new String(data,0,datalen);
51
52          int first = received.indexOf(' ');
53          int last  = received.lastIndexOf(' ');
54          String mode = received.substring(0,first);
55          String input = received.substring(first+1,last);
56          String output = received.substring(last+1);
57
58          System.out.println("request received! Parameters are");
59          System.out.println("Mode: " + mode + " ");
60          System.out.println("Input file: \"" + input + "\"");
61          System.out.println("Output file: \"" + output  + "\"\n");
62
63          if ((new File(output)).exists()) {
64             System.out.println("Using cached version\n");
65          } else {
66             FileOutputStream fout = new FileOutputStream(output);
67             if (mode.equals("cic")) {
68                processor.setDocumentHandler(style2.getSAXSerializer(fout));
69                XSLTResultTarget content = new XSLTResultTarget(processor);
70                style1.process(new XSLTInputSource(input), content);
71             } else if (mode.equals("theory")) {
72                theory_processor.setDocumentHandler(
73                   theory_style2.getSAXSerializer(fout));
74                XSLTResultTarget content =
75                   new XSLTResultTarget(theory_processor);
76                theory_style1.process(new XSLTInputSource(input), content);
77             }
78          }
79
80          InetAddress address = InetAddress.getLocalHost();
81          DatagramSocket socket2 = new DatagramSocket();
82
83          byte buf[] = new byte[0];
84          DatagramPacket packet2 = new DatagramPacket(buf,0,address,port2);
85
86          socket2.send(packet2);
87       }
88    }
89 }