X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Finterface%2Fxaland-java2%2Fxaland2.java;fp=helm%2Finterface%2Fxaland-java2%2Fxaland2.java;h=9d91d37fbd5de0afaa3a2437d039f13e2d04f455;hb=54889b5fb4ea40df04165bcb88324c0fd271cd72;hp=0000000000000000000000000000000000000000;hpb=0ff06c9ed5e1f61dbf38c06ff0885cec794d7250;p=helm.git diff --git a/helm/interface/xaland-java2/xaland2.java b/helm/interface/xaland-java2/xaland2.java new file mode 100644 index 000000000..9d91d37fb --- /dev/null +++ b/helm/interface/xaland-java2/xaland2.java @@ -0,0 +1,134 @@ +import java.net.*; +import java.io.*; + +// Imported TraX classes +import org.apache.trax.Processor; +import org.apache.trax.Templates; +import org.apache.trax.Transformer; +import org.apache.trax.Result; +import org.apache.trax.ProcessorException; +import org.apache.trax.ProcessorFactoryException; +import org.apache.trax.TransformException; + +// Imported SAX classes +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.Parser; +import org.xml.sax.helpers.ParserAdapter; +import org.xml.sax.helpers.XMLReaderFactory; +import org.xml.sax.XMLReader; +import org.xml.sax.ContentHandler; +import org.xml.sax.ext.LexicalHandler; + +// Imported DOM classes +import org.w3c.dom.Node; + +// Imported Serializer classes +import org.apache.serialize.OutputFormat; +import org.apache.serialize.Serializer; +import org.apache.serialize.SerializerFactory; + +// Imported JAVA API for XML Parsing 1.0 classes +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + + +public class xaland2 { + public static void main(String argv[]) throws IOException, MalformedURLException, SAXException, ParserConfigurationException + { + int port = Integer.parseInt(argv[0]); + int port2 = Integer.parseInt(argv[1]); + String xsl1 = argv[2]; + String xsl2 = argv[3]; + String theory_xsl1 = argv[4]; + String theory_xsl2 = argv[5]; + + Processor theory_processor = Processor.newInstance("xslt"); + Templates theory_style1 = theory_processor.process(new InputSource(theory_xsl1)); + Transformer theory_transformer1 = theory_style1.newTransformer(); + + Templates theory_style2 = theory_processor.process(new InputSource(theory_xsl2)); + Transformer theory_transformer2 = theory_style2.newTransformer(); + + + Processor processor = Processor.newInstance("xslt"); + Templates style1 = processor.process(new InputSource(xsl1)); + Transformer transformer1 = style1.newTransformer(); + + Templates style2 = processor.process(new InputSource(xsl2)); + Transformer transformer2 = style2.newTransformer(); + + + DatagramSocket socket = new DatagramSocket(port); + + System.out.println("Demon activated on input port " + port + + " and output port " + port2); + while(true) { + System.out.print("Ready..."); + + /* Warning: the packet must be a fresh one! */ + DatagramPacket packet = new DatagramPacket(new byte[1024],1024); + socket.receive(packet); + byte data[] = packet.getData(); + int datalen = packet.getLength(); + String received = new String(data,0,datalen); + + int first = received.indexOf(' '); + int last = received.lastIndexOf(' '); + String mode = received.substring(0,first); + String input = received.substring(first+1,last); + String output = received.substring(last+1); + + System.out.println("request received! Parameters are"); + System.out.println("Mode: " + mode + " "); + System.out.println("Input file: \"" + input + "\""); + System.out.println("Output file: \"" + output + "\"\n"); + + if ((new File(output)).exists()) { + System.out.println("Using cached version\n"); + } else { + FileOutputStream fout = new FileOutputStream(output); + if (mode.equals("cic")) { + XMLReader reader = XMLReaderFactory.createXMLReader(); + ContentHandler chandler = transformer1.getInputContentHandler(); + reader.setContentHandler(chandler); + if (chandler instanceof LexicalHandler) + reader.setProperty("http://xml.org/sax/properties/lexical-handler", chandler); + else + reader.setProperty("http://xml.org/sax/properties/lexical-handler", null); + + transformer1.setContentHandler(transformer2.getInputContentHandler()); + Serializer serializer = SerializerFactory.getSerializer("xml"); + serializer.setOutputStream(fout); + transformer2.setContentHandler(serializer.asContentHandler()); + + reader.parse(input); + } else if (mode.equals("theory")) { + XMLReader reader = XMLReaderFactory.createXMLReader(); + ContentHandler chandler = theory_transformer1.getInputContentHandler(); + reader.setContentHandler(chandler); + if (chandler instanceof LexicalHandler) + reader.setProperty("http://xml.org/sax/properties/lexical-handler", chandler); + else + reader.setProperty("http://xml.org/sax/properties/lexical-handler", null); + + theory_transformer1.setContentHandler(theory_transformer2.getInputContentHandler()); + Serializer serializer = SerializerFactory.getSerializer("xml"); + serializer.setOutputStream(fout); + theory_transformer2.setContentHandler(serializer.asContentHandler()); + + reader.parse(input); + } + } + + InetAddress address = InetAddress.getLocalHost(); + DatagramSocket socket2 = new DatagramSocket(); + + byte buf[] = new byte[0]; + DatagramPacket packet2 = new DatagramPacket(buf,0,address,port2); + + socket2.send(packet2); + } + } +}