]> matita.cs.unibo.it Git - helm.git/blob - helm/xsltd/xaland-java/xaland.java.prima_del_loro_baco_ma_dopo_i_reset
ocaml 3.09 transition
[helm.git] / helm / xsltd / xaland-java / xaland.java.prima_del_loro_baco_ma_dopo_i_reset
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 = XSLTProcessorFactory.getProcessor();
23       StylesheetRoot theory_style1 =
24          theory_processor.processStylesheet(theory_xsl1);
25       theory_processor.reset();
26       StylesheetRoot theory_style2 =
27          theory_processor.processStylesheet(theory_xsl2);
28       theory_processor.setStylesheet(theory_style2);
29
30       XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
31       StylesheetRoot style1 = processor.processStylesheet(xsl1);
32       processor.reset();
33       StylesheetRoot style2 = processor.processStylesheet(xsl2);
34       processor.setStylesheet(style2);
35
36       DatagramSocket socket = new DatagramSocket(port);
37
38       System.out.println("Demon activated on input port " + port +
39        " and output port " + port2);
40       while(true) {
41          System.out.print("Ready...");
42
43          /* Warning: the packet must be a fresh one! */
44          DatagramPacket packet = new DatagramPacket(new byte[1024],1024);
45          socket.receive(packet);
46          byte data[] = packet.getData();
47          int datalen = packet.getLength();
48          String received = new String(data,0,datalen);
49
50          int first = received.indexOf(' ');
51          int last  = received.lastIndexOf(' ');
52          String mode = received.substring(0,first);
53          String input = received.substring(first+1,last);
54          String output = received.substring(last+1);
55
56          System.out.println("request received! Parameters are");
57          System.out.println("Mode: " + mode + " ");
58          System.out.println("Input file: \"" + input + "\"");
59          System.out.println("Output file: \"" + output  + "\"\n");
60
61          if ((new File(output)).exists()) {
62             System.out.println("Using cached version\n");
63          } else {
64             FileOutputStream fout = new FileOutputStream(output);
65             if (mode.equals("cic")) {
66                processor.setDocumentHandler(style2.getSAXSerializer(fout));
67                XSLTResultTarget content = new XSLTResultTarget(processor);
68                style1.process(new XSLTInputSource(input), content);
69             } else if (mode.equals("theory")) {
70                theory_processor.setDocumentHandler(
71                   theory_style2.getSAXSerializer(fout));
72                XSLTResultTarget content =
73                   new XSLTResultTarget(theory_processor);
74                theory_style1.process(new XSLTInputSource(input), content);
75             }
76          }
77
78          InetAddress address = InetAddress.getLocalHost();
79          DatagramSocket socket2 = new DatagramSocket();
80
81          byte buf[] = new byte[0];
82          DatagramPacket packet2 = new DatagramPacket(buf,0,address,port2);
83
84          socket2.send(packet2);
85       }
86    }
87 }