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