]> matita.cs.unibo.it Git - helm.git/blob - helm/xsltd/xaland-java2/xaland2.java
ocaml 3.09 transition
[helm.git] / helm / xsltd / xaland-java2 / xaland2.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 java.net.*;
25 import java.io.*;
26
27 // Imported TraX classes
28 import org.apache.trax.Processor;
29 import org.apache.trax.Templates;
30 import org.apache.trax.Transformer;
31 import org.apache.trax.Result;
32 import org.apache.trax.ProcessorException;
33 import org.apache.trax.ProcessorFactoryException;
34 import org.apache.trax.TransformException;
35
36 // Imported SAX classes
37 import org.xml.sax.InputSource;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.Parser;
40 import org.xml.sax.helpers.ParserAdapter;
41 import org.xml.sax.helpers.XMLReaderFactory;
42 import org.xml.sax.XMLReader;
43 import org.xml.sax.ContentHandler;
44 import org.xml.sax.ext.LexicalHandler;
45
46 // Imported DOM classes
47 import org.w3c.dom.Node;
48
49 // Imported Serializer classes
50 import org.apache.serialize.OutputFormat;
51 import org.apache.serialize.Serializer;
52 import org.apache.serialize.SerializerFactory;
53
54 // Imported JAVA API for XML Parsing 1.0 classes
55 import javax.xml.parsers.DocumentBuilder;
56 import javax.xml.parsers.DocumentBuilderFactory;
57 import javax.xml.parsers.ParserConfigurationException;
58
59
60 public class xaland2 {
61    public static void main(String argv[]) throws  IOException, MalformedURLException, SAXException, ParserConfigurationException
62    {
63       int port    = Integer.parseInt(argv[0]);
64       int port2   = Integer.parseInt(argv[1]);
65       String xsl1 = argv[2];
66       String xsl2 = argv[3];
67       String theory_xsl1 = argv[4];
68       String theory_xsl2 = argv[5];
69
70       Processor theory_processor = Processor.newInstance("xslt");
71       Templates theory_style1 = theory_processor.process(new InputSource(theory_xsl1));
72       Transformer theory_transformer1 = theory_style1.newTransformer();
73
74       Templates theory_style2 = theory_processor.process(new InputSource(theory_xsl2));
75       Transformer theory_transformer2 = theory_style2.newTransformer();
76
77
78       Processor processor = Processor.newInstance("xslt");
79       Templates style1 = processor.process(new InputSource(xsl1));
80       Transformer transformer1 = style1.newTransformer();
81
82       Templates style2 = processor.process(new InputSource(xsl2));
83       Transformer transformer2 = style2.newTransformer();
84
85
86       DatagramSocket socket = new DatagramSocket(port);
87
88       System.out.println("Demon activated on input port " + port +
89        " and output port " + port2);
90       while(true) {
91          System.out.print("Ready...");
92
93          /* Warning: the packet must be a fresh one! */
94          DatagramPacket packet = new DatagramPacket(new byte[1024],1024);
95          socket.receive(packet);
96          byte data[] = packet.getData();
97          int datalen = packet.getLength();
98          String received = new String(data,0,datalen);
99
100          int first = received.indexOf(' ');
101          int last  = received.lastIndexOf(' ');
102          String mode = received.substring(0,first);
103          String input = received.substring(first+1,last);
104          String output = received.substring(last+1);
105
106          System.out.println("request received! Parameters are");
107          System.out.println("Mode: " + mode + " ");
108          System.out.println("Input file: \"" + input + "\"");
109          System.out.println("Output file: \"" + output  + "\"\n");
110
111          if ((new File(output)).exists()) {
112             System.out.println("Using cached version\n");
113          } else {
114             FileOutputStream fout = new FileOutputStream(output);
115             if (mode.equals("cic")) {
116                XMLReader reader = XMLReaderFactory.createXMLReader();
117                ContentHandler chandler = transformer1.getInputContentHandler();
118                reader.setContentHandler(chandler);
119                if (chandler instanceof LexicalHandler)
120                 reader.setProperty("http://xml.org/sax/properties/lexical-handler", chandler);
121                else
122                 reader.setProperty("http://xml.org/sax/properties/lexical-handler", null);
123
124                transformer1.setContentHandler(transformer2.getInputContentHandler());
125                Serializer serializer = SerializerFactory.getSerializer("xml");
126                serializer.setOutputStream(fout);
127                transformer2.setContentHandler(serializer.asContentHandler());
128
129                reader.parse(input);
130             } else if (mode.equals("theory")) {
131                XMLReader reader = XMLReaderFactory.createXMLReader();
132                ContentHandler chandler = theory_transformer1.getInputContentHandler();
133                reader.setContentHandler(chandler);
134                if (chandler instanceof LexicalHandler)
135                 reader.setProperty("http://xml.org/sax/properties/lexical-handler", chandler);
136                else
137                 reader.setProperty("http://xml.org/sax/properties/lexical-handler", null);
138
139                theory_transformer1.setContentHandler(theory_transformer2.getInputContentHandler());
140                Serializer serializer = SerializerFactory.getSerializer("xml");
141                serializer.setOutputStream(fout);
142                theory_transformer2.setContentHandler(serializer.asContentHandler());
143
144                reader.parse(input);
145              }
146          }
147
148          InetAddress address = InetAddress.getLocalHost();
149          DatagramSocket socket2 = new DatagramSocket();
150
151          byte buf[] = new byte[0];
152          DatagramPacket packet2 = new DatagramPacket(buf,0,address,port2);
153
154          socket2.send(packet2);
155       }
156    }
157 }