]> matita.cs.unibo.it Git - helm.git/blob - helm/xsltd/xaland-java/xaland.java
ocaml 3.09 transition
[helm.git] / helm / xsltd / xaland-java / xaland.java
1 // Copyright (C) 2000, HELM Team.
2 // 
3 // This file is part of HELM, an Hypertextual, Electronic
4 // Library of Mathematics, developed at the Computer Science
5 // Department, University of Bologna, Italy.
6 // 
7 // HELM is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 // 
12 // HELM is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 // 
17 // You should have received a copy of the GNU General Public License
18 // along with HELM; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 // 
21 // For details, see the HELM World-Wide-Web page,
22 // http://cs.unibo.it/helm/.
23
24 import org.apache.xalan.xslt.*;
25 import java.net.*;
26 import java.io.*;
27
28 public class xaland {
29    public static void main(String argv[]) throws  java.io.IOException, java.net.MalformedURLException, org.xml.sax.SAXException
30    {
31       int port    = Integer.parseInt(argv[0]);
32       int port2   = Integer.parseInt(argv[1]);
33       String xsl1 = argv[2];
34       String xsl2 = argv[3];
35       String theory_xsl1 = argv[4];
36       String theory_xsl2 = argv[5];
37
38       XSLTProcessor theory_processor =
39        XSLTProcessorFactory.getProcessor(new org.apache.xalan.xpath.xdom.XercesLiaison());
40       StylesheetRoot theory_style1 =
41          theory_processor.processStylesheet(theory_xsl1);
42       theory_processor.reset();
43       StylesheetRoot theory_style2 =
44          theory_processor.processStylesheet(theory_xsl2);
45       theory_processor.setStylesheet(theory_style2);
46
47       XSLTProcessor processor =
48        XSLTProcessorFactory.getProcessor(new org.apache.xalan.xpath.xdom.XercesLiaison());
49       StylesheetRoot style1 = processor.processStylesheet(xsl1);
50       processor.reset();
51       StylesheetRoot style2 = processor.processStylesheet(xsl2);
52       processor.setStylesheet(style2);
53
54       DatagramSocket socket = new DatagramSocket(port);
55
56       System.out.println("Demon activated on input port " + port +
57        " and output port " + port2);
58       while(true) {
59          System.out.print("Ready...");
60
61          /* Warning: the packet must be a fresh one! */
62          DatagramPacket packet = new DatagramPacket(new byte[1024],1024);
63          socket.receive(packet);
64          byte data[] = packet.getData();
65          int datalen = packet.getLength();
66          String received = new String(data,0,datalen);
67
68          int first = received.indexOf(' ');
69          int last  = received.lastIndexOf(' ');
70          String mode = received.substring(0,first);
71          String input = received.substring(first+1,last);
72          String output = received.substring(last+1);
73
74          System.out.println("request received! Parameters are");
75          System.out.println("Mode: " + mode + " ");
76          System.out.println("Input file: \"" + input + "\"");
77          System.out.println("Output file: \"" + output  + "\"\n");
78
79          if ((new File(output)).exists()) {
80             System.out.println("Using cached version\n");
81          } else {
82             FileOutputStream fout = new FileOutputStream(output);
83             if (mode.equals("cic")) {
84                processor.setDocumentHandler(style2.getSAXSerializer(fout));
85                XSLTResultTarget content = new XSLTResultTarget(processor);
86                style1.process(new XSLTInputSource(input), content);
87             } else if (mode.equals("theory")) {
88                theory_processor.setDocumentHandler(
89                   theory_style2.getSAXSerializer(fout));
90                XSLTResultTarget content =
91                   new XSLTResultTarget(theory_processor);
92                theory_style1.process(new XSLTInputSource(input), content);
93             }
94          }
95
96          InetAddress address = InetAddress.getLocalHost();
97          DatagramSocket socket2 = new DatagramSocket();
98
99          byte buf[] = new byte[0];
100          DatagramPacket packet2 = new DatagramPacket(buf,0,address,port2);
101
102          socket2.send(packet2);
103       }
104    }
105 }