]> matita.cs.unibo.it Git - helm.git/blob - helm/xsltd/xaland-java-DOM/xaland_DOM.java
ocaml 3.09 transition
[helm.git] / helm / xsltd / xaland-java-DOM / xaland_DOM.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 org.w3c.dom.Document;
26 import java.net.*;
27 import java.io.*;
28
29 public class xaland_DOM {
30    public static void main(String argv[]) throws  java.io.IOException, java.net.MalformedURLException, org.xml.sax.SAXException
31    {
32       int port    = Integer.parseInt(argv[0]);
33       int port2   = Integer.parseInt(argv[1]);
34       String xsl1 = argv[2];
35       String xsl2 = argv[3];
36       String theory_xsl1 = argv[4];
37       String theory_xsl2 = argv[5];
38
39       XSLTProcessor theory_processor =
40        XSLTProcessorFactory.getProcessor(new org.apache.xalan.xpath.xdom.XercesLiaison());
41       StylesheetRoot theory_style1 =
42          theory_processor.processStylesheet(theory_xsl1);
43       theory_processor.reset();
44       StylesheetRoot theory_style2 =
45          theory_processor.processStylesheet(theory_xsl2);
46       theory_processor.setStylesheet(theory_style2);
47
48       XSLTProcessor processor =
49        XSLTProcessorFactory.getProcessor(new org.apache.xalan.xpath.xdom.XercesLiaison());
50       StylesheetRoot style1 = processor.processStylesheet(xsl1);
51       processor.reset();
52       StylesheetRoot style2 = processor.processStylesheet(xsl2);
53       processor.setStylesheet(style2);
54
55       DatagramSocket socket = new DatagramSocket(port);
56
57       System.out.println("Demon activated on input port " + port +
58        " and output port " + port2);
59       while(true) {
60          System.out.print("Ready...");
61
62          /* Warning: the packet must be a fresh one! */
63          DatagramPacket packet = new DatagramPacket(new byte[1024],1024);
64          socket.receive(packet);
65          byte data[] = packet.getData();
66          int datalen = packet.getLength();
67          String received = new String(data,0,datalen);
68
69          int first = received.indexOf(' ');
70          int last  = received.lastIndexOf(' ');
71          String mode = received.substring(0,first);
72          String input = received.substring(first+1,last);
73          String output = received.substring(last+1);
74
75          System.out.println("request received! Parameters are");
76          System.out.println("Mode: " + mode + " ");
77          System.out.println("Input file: \"" + input + "\"");
78          System.out.println("Output file: \"" + output  + "\"\n");
79
80          if ((new File(output)).exists()) {
81             System.out.println("Using cached version\n");
82          } else {
83             FileOutputStream fout = new FileOutputStream(output);
84             if (mode.equals("cic")) {
85 Document content_doc = new org.apache.xerces.dom.DocumentImpl();
86 XSLTResultTarget content = new XSLTResultTarget(content_doc);
87 /*
88                processor.setDocumentHandler(style2.getSAXSerializer(fout));
89                XSLTResultTarget content = new XSLTResultTarget(processor);
90 */
91 System.out.print("Prima...\n");
92                style1.process(new XSLTInputSource(input), content);
93 System.out.print("Durante...\n");
94                style2.process(new XSLTInputSource(content_doc),new XSLTResultTarget(fout));
95 System.out.print("Dopo...\n");
96             } else if (mode.equals("theory")) {
97                theory_processor.setDocumentHandler(
98                   theory_style2.getSAXSerializer(fout));
99                XSLTResultTarget content =
100                   new XSLTResultTarget(theory_processor);
101                theory_style1.process(new XSLTInputSource(input), content);
102             }
103          }
104
105          InetAddress address = InetAddress.getLocalHost();
106          DatagramSocket socket2 = new DatagramSocket();
107
108          byte buf[] = new byte[0];
109          DatagramPacket packet2 = new DatagramPacket(buf,0,address,port2);
110
111          socket2.send(packet2);
112       }
113    }
114 }