Package nux.xom.binary

Examples of nux.xom.binary.BinaryXMLCodec


   *            a number in the range 0..9
   * @return a streaming serializer that writes bnux binary XML
   */
  public StreamingSerializer createBinaryXMLSerializer(OutputStream out, int zlibCompressionLevel) {
//    BinaryXMLCodec codec = XOMUtil.getBinaryXMLCodec();
    BinaryXMLCodec codec = new BinaryXMLCodec();
    return codec.createStreamingSerializer(out, zlibCompressionLevel);
  }
View Full Code Here


 
            if (input == null && baseURI == null)
              throw new IllegalArgumentException("input and baseURI must not both be null");
            if (input == null) input = baseURI.toURL().openStream();
            try {
              doc = new BinaryXMLCodec().deserialize(input, filter.createNodeFactory(null, myTransform));
              if (baseURI != null) doc.setBaseURI(baseURI.toASCIIString());
            } finally {
              input.close(); // do what SAX XML parsers do
            }
          }
View Full Code Here

    int compressionLevel = Integer.getInteger("nux.xom.tests.BinaryXMLConverter.compressionLevel", 0).intValue();
    int runs = Integer.getInteger("nux.xom.tests.BinaryXMLConverter.runs", 1).intValue();
    boolean readOnly = Boolean.getBoolean("nux.xom.tests.BinaryXMLConverter.readOnly");
   
    StreamingSerializerFactory factory = new StreamingSerializerFactory();
    BinaryXMLCodec codec = new BinaryXMLCodec();

    if (args.length == 0) { // simply convert from System.in to System.out
      InputStream in = new BufferedInputStream(System.in);
      if (codec.isBnuxDocument(in)) {
        StreamingSerializer ser = factory.createXMLSerializer(System.out, "UTF-8");
        NodeFactory redirector = XOMUtil.getRedirectingNodeFactory(ser);
        codec.deserialize(in, redirector);
      } else { // it's an XML document (or rubbish)
        StreamingSerializer ser = factory.createBinaryXMLSerializer(System.out, compressionLevel);
        NodeFactory redirector = XOMUtil.getRedirectingNodeFactory(ser);
        new Builder(redirector).build(in);
      }
      return;
    }
   
    for (int run=0; run < runs; run++) {
      long s = System.currentTimeMillis();
      for (int i=0; i < args.length; i++) {
        long start, end;
        String fileName = args[i];
        File file = new File(fileName);
        if (file.isDirectory()) continue; // ignore
        System.out.print(fileName + " --> ");
        InputStream in = new FileInputStream(file);       
        OutputStream out = null;
       
        if (fileName.endsWith(".bnux")) {
          NodeFactory redirector = null;
          if (readOnly) {
            redirector = XOMUtil.getNullNodeFactory();
          } else {
            String destFileName = fileName.substring(0, fileName.length() - ".bnux".length());
            System.out.print(destFileName);
            out = new FileOutputStream(destFileName);
            StreamingSerializer ser = factory.createXMLSerializer(out, "UTF-8");
            redirector = XOMUtil.getRedirectingNodeFactory(ser);
          }
         
          start = System.currentTimeMillis();
         
          codec.deserialize(in, redirector); // perform conversion       
        }
        else { // it's a textual XML document
          NodeFactory redirector = null;
          if (readOnly) {
            redirector = XOMUtil.getNullNodeFactory();
          } else {
            String destFileName = fileName + ".bnux";
            System.out.print(destFileName);
            out = new FileOutputStream(destFileName);
            StreamingSerializer ser = codec.createStreamingSerializer(out, compressionLevel);
            redirector = XOMUtil.getRedirectingNodeFactory(ser);
          }

          start = System.currentTimeMillis();
         
View Full Code Here

    fileData = null;
    fileData = FileUtil.toByteArray(new FileInputStream(file));
   
    if (mode.startsWith("bnux")) {
      doc = new Builder().build(new ByteArrayInputStream(fileData));
      data = new BinaryXMLCodec().serialize(doc, compressionLevel);
      if (!cmd.equals("deser")) {
        doc = codec.deserialize(data); // use "interned" strings
        data = null;
      }
      if (cmd.equals("deser")) {
View Full Code Here

  }
 
  // called once per mode (i.e. VM invocation)
  private void init() throws Exception {   // TODO: merge with constructor?
    // bnux and XOM
    codec = new BinaryXMLCodec();
    bnuxFactory = null;
    if (mode.startsWith("bnux")) {
      if (mode.indexOf("NNF") >= 0) {
        bnuxFactory = XOMUtil.getNullNodeFactory();
      }
View Full Code Here

    System.out.print("Now reading " + fileName);
    long start = System.currentTimeMillis();
    Document doc;
    if (fileName.endsWith(".bnux")) { // it's a binary xml file
      byte data[] = FileUtil.toByteArray(new FileInputStream(fileName));
      doc = new BinaryXMLCodec().deserialize(data);
    }
    else { // it's a standard textual XML file
      doc = new Builder().build(new File(fileName));
    }
    long end = System.currentTimeMillis();
View Full Code Here

//      System.setProperty("nu.xom.Verifier.checkPCDATA", "false");
      System.setProperty("nu.xom.Verifier.checkURI", "false");
    }
    final boolean testCompressionLevels = true;
   
    BinaryXMLCodec codec = new BinaryXMLCodec();
    for (int j=5; j < args.length; j++) {
      if (ignore(args[j])) continue;
      File file = new File(args[j]);
      if (file.isDirectory()) continue;
      System.out.println("now processing " + file);
     
      Document doc = new Builder().build(file);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      for (int p=0; p < iterations; p++) {
        System.out.println("compressionLevel=" + compressionLevel);
        codec.serialize(doc, compressionLevel, out);
        if (testCompressionLevels) { // alternate on each iteration
          compressionLevel = (compressionLevel + 1) % 10;
        }
      }
     
      InputStream in = new ByteArrayInputStream(out.toByteArray());
      for (int p=0; p < iterations; p++) {
        Document doc2 = codec.deserialize(in, null);
        // check correctness
        if (cmd.equals("test")) {
          if (! Arrays.equals(XOMUtil.toCanonicalXML(doc), XOMUtil.toCanonicalXML(doc2))) {
            System.err.println("Canonical XML Mismatch: ");
            System.err.println("expected: " + doc.toXML());
View Full Code Here

        staxOutputFactory = createXMLOutputFactory(mode);
      }

     
      // bnux and XOM
      BinaryXMLCodec codec = new BinaryXMLCodec();
      NodeFactory bnuxFactory = null;
      if (mode.startsWith("bnux")) {
        if (mode.indexOf("NNF") >= 0) {
          bnuxFactory = XOMUtil.getNullNodeFactory();
        }
      }

      Builder builder = new Builder();
//      Builder builder = BuilderPool.GLOBAL_POOL.getBuilder(false);
      if (mode.equals("xom-V")) {
        builder = new Builder(new NodeFactory() {});
      }
      if (mode.equals("xom-NNF")) {
        builder = new Builder(XOMUtil.getNullNodeFactory());
      }
     
      // saxon
      StaticQueryContext context = null;
      Transformer saxonSerializer = null;
      if (mode.equals("saxon")) {
        context = new StaticQueryContext(new Configuration());
        String clazz = "net.sf.saxon.TransformerFactoryImpl";
        System.setProperty("javax.xml.transform.TransformerFactory", clazz);
        saxonSerializer = TransformerFactory.newInstance().newTransformer();
      }
     
      // DOM
      DocumentBuilder domBuilder = null;
      Transformer domSerializer = null;
      if (mode.equals("dom")) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion", Boolean.FALSE);
        domBuilder = factory.newDocumentBuilder();
        String clazz = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
//        String clazz = "org.apache.xalan.processor.TransformerFactoryImpl";
        System.setProperty("javax.xml.transform.TransformerFactory", clazz);
        domSerializer = TransformerFactory.newInstance().newTransformer();
        System.err.println(domSerializer.getClass().getName());
      }
     
      // FastInfoSet
      Object fiSerializer = null;
//      XMLStreamWriter fiStaxSerializer = null;
      Builder fiBuilder = null
      Method fiMethod = null;
     
      XMLStreamReader fistaxReader = null;
      Method fistaxMethod = null;
      if (mode.startsWith("fi")) {
        NodeFactory factory = null;
        if (mode.indexOf("NNF") >= 0) factory = XOMUtil.getNullNodeFactory();
        XMLReader parser = (XMLReader) Class.forName("com.sun.xml.fastinfoset.sax.SAXDocumentParser").newInstance();
        fiBuilder = new Builder(parser, false, factory);
       
        if (mode.indexOf("stax") >= 0) {
          fiSerializer = (XMLStreamWriter) Class.forName("com.sun.xml.fastinfoset.stax.StAXDocumentSerializer").newInstance();       
        } else {
          fiSerializer = (ContentHandler) Class.forName("com.sun.xml.fastinfoset.sax.SAXDocumentSerializer").newInstance();
        }
       
        if (mode.startsWith("fi1")) { // enable "full indexing"
          Method method;
          method = fiSerializer.getClass().getMethod(
              "setAttributeValueSizeLimit", new Class[] {Integer.TYPE});
          method.invoke(fiSerializer, new Object[] {new Integer(Integer.MAX_VALUE)});
          method = fiSerializer.getClass().getMethod(
              "setCharacterContentChunkSizeLimit", new Class[] {Integer.TYPE});
          method.invoke(fiSerializer, new Object[] {new Integer(Integer.MAX_VALUE)});
        }
       
        fiMethod = fiSerializer.getClass().getMethod(
            "setOutputStream", new Class[] { OutputStream.class});
       
        if (mode.indexOf("stax") >= 0) {
          fistaxReader = (XMLStreamReader) Class.forName("com.sun.xml.fastinfoset.stax.StAXDocumentParser").newInstance();
          fistaxMethod = fistaxReader.getClass().getMethod(
              "setInputStream", new Class[] { InputStream.class});
        }
      }
     
      // StAX
      Builder staxBuilder = null;
      if (mode.indexOf("stax") >= 0) {
        NodeFactory factory = null;
        if (mode.indexOf("NNF") >= 0) factory = XOMUtil.getNullNodeFactory();
        staxBuilder = StaxUtil.createBuilder(staxInputFactory, factory);
      }
     
      for (int j=5; j < args.length; j++) {
        try {
          File[] files = IOTestUtil.listXMLFiles(args[j]);
          for (int k=0; k < files.length; k++) {
            File file = files[k];
            if (bogus(file) || ignore(file) || file.isDirectory()) {
              System.out.println("\n" + ": IGNORING " + file + " ...");
              continue;
            }
           
            System.out.println("now processing " + file);
  //          System.out.print(".");
           
            // prepare
            // file:/path/to/file --> file:///path/to/file
            String baseURI = file.toURI().toASCIIString();
            if (baseURI.startsWith("file:/")) {
              baseURI = baseURI.substring("file:/".length());
              if (!baseURI.startsWith("//")) baseURI = "//" + baseURI;
              baseURI = "file:/" + baseURI;
            }
  //          System.out.println("baseURI: " + baseURI);
            Document doc = new Builder().build(file);
           
            byte[] data = codec.serialize(doc, compressionLevel);
            if (!cmd.equals("test")) doc = new BinaryXMLCodec().deserialize(data); // use "interned" strings
            byte[] fileData = FileUtil.toByteArray(new FileInputStream(file));
            long fileLength = file.length();
            int encodedSize = 0;
           
            org.w3c.dom.Document domDoc = null;
            if (mode.equals("dom")) {
              domDoc = domBuilder.parse(file);
            }
           
            NodeInfo saxonDoc = null;
            if (mode.equals("saxon")) {
              saxonDoc = context.buildDocument(new StreamSource(new ByteArrayInputStream(fileData)));
            }
   
            if (mode.startsWith("fi")) {
              if (mode.indexOf("stax") >= 0) {
//                data = serializeWithStax(doc, staxOutputFactory);
                data = serializeWithFastInfosetStax(doc, (XMLStreamWriter)fiSerializer, fiMethod, new ByteArrayOutputStream());
              } else {
                data = serializeWithFastInfoset(doc, (ContentHandler)fiSerializer, fiMethod, new ByteArrayOutputStream());
              }
            }
   
            // run the benchmark
            long start = System.currentTimeMillis();
            for (int i = 0; i < iterations; i++) {
              try {
                // serialize
                if (cmd.equals("ser") || cmd.equals("serdeser") || cmd.equals("test")) {
                  ByteArrayOutputStream out = createOutputStream(cmd.equals("ser"));
                  if (mode.startsWith("bnux")) {
                    if (mode.indexOf("stream") < 0) {
                      codec.serialize(doc, compressionLevel, out);
                      data = out.toByteArray();
                    } else {
                      data = serializeWithStreamingBnux(doc, compressionLevel, out);
                    }
                  } else if (mode.startsWith("xom")) {
                    if (mode.indexOf("stax") >= 0) {
                      data = serializeWithStax(doc, staxOutputFactory, out);
                    } else if (mode.indexOf("stream") < 0) {
                      data = serializeWithXOM(doc, out);
                    } else {
                      data = serializeWithStreamingXOM(doc, out);
                    }
                  } else if (mode.equals("saxon")) {
                    saxonSerializer.transform(saxonDoc, new StreamResult(out));
                    data = out.toByteArray();
                  } else if (mode.equals("dom")) {
                    domSerializer.transform(new DOMSource(domDoc), new StreamResult(out));
                    data = out.toByteArray();
                  } else if (mode.startsWith("fi")) {
                    if (mode.indexOf("stax") >= 0) {
//                      data = serializeWithStax(doc, staxOutputFactory);
                      data = serializeWithFastInfosetStax(doc, (XMLStreamWriter)fiSerializer, fiMethod, out);
                    } else {
                      data = serializeWithFastInfoset(doc, (ContentHandler)fiSerializer, fiMethod, out);
                    }
                  } else {
                    throw new IllegalArgumentException("illegal mode");
                  }
                  checksum += data.length;
                }
                encodedSize = data.length;
                doneEncoded += encodedSize;
               
                // deserialize
                Document doc2 = null;
                if (cmd.equals("deser") || cmd.equals("serdeser") || cmd.equals("test")) {
                  if (mode.startsWith("bnux")) {
                    doc2 = codec.deserialize(new ByteArrayInputStream(data), bnuxFactory);
                  } else if (mode.startsWith("xom") && mode.indexOf("stax") >= 0) {
                    doc2 = staxBuilder.build(new ByteArrayInputStream(fileData));         
                  } else if (mode.startsWith("xom")) {
                    if (mode.indexOf("stream") < 0) {
                      doc2 = builder.build(new ByteArrayInputStream(fileData), baseURI);
View Full Code Here

TOP

Related Classes of nux.xom.binary.BinaryXMLCodec

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.