Package nu.xom

Examples of nu.xom.Builder


//  private  double  totalMeasurePct;
//  private double  lastNoteInMeasureDuration;  //  adjusted duration of the
                        //  last note in the measure
 
  public MusicXmlParser()
  {  xomBuilder = new Builder();
    dictionaryMap = new HashMap<String, Object>();
    JFugueDefinitions.populateDictionary(dictionaryMap);
    beats = 1;
    divisions = 1;
    curVoice = -1;
View Full Code Here


              String value = arg.substring(i+1);
              if (false && value.startsWith("doc(") && value.endsWith(")")) {
                try {
                  value = value.substring("doc(".length()-1);
                  value = value.substring(1, value.length()-1);
                  variables.put(name, new Builder().build(new File(value)));
                } catch (Exception e) {
                  throw new UsageException(e);
                }
              } else {
                variables.put(name, value);
View Full Code Here

          StreamingTransform myTransform = new StreamingTransform() {
            public Nodes transform(Element subtree) {
              return XQueryUtil.xquery(subtree, filterQuery);
            }
          };
          return new Builder(parser, validate, filter.createNodeFactory(null, myTransform));    
        }
      };
      builderPool = new BuilderPool(config, builderFactory);
    }

    // prepare DocumentFactory and DocumentPool
    DocumentFactory docFactory = new DocumentFactory() {
      public Document createDocument(InputStream input, URI baseURI)
          throws ParsingException, IOException {
        long start = System.currentTimeMillis();
        Document doc;
        if (baseURI != null && baseURI.getPath().endsWith(".bnux")) {
          if (filter == null) {
            doc = getBinaryXMLFactory().createDocument(input, baseURI);
          } else {
            StreamingTransform myTransform = new StreamingTransform() {
              public Nodes transform(Element subtree) {
                return XQueryUtil.xquery(subtree, filterQuery);
              }
            };
 
            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
            }
          }
        } else {
          doc = super.createDocument(input, baseURI);
        }
        if (xinclude) {
          try {
            XIncluder.resolveInPlace(doc, newBuilder());
          } catch (XIncludeException e) {
            throw new ParsingException(e.getMessage(), e);
          }
        }
        if (stripWhitespace) XOMUtil.Normalizer.STRIP.normalize(doc);
        long end = System.currentTimeMillis();
        if (isBench || explain) System.out.println(baseURI + " parse [ms]=" + (end-start));
        return doc;
      }
     
      protected Builder newBuilder() {
        if (validate.equals("wf")) {
          return builderPool.getBuilder(false);
        } else if (validate.equals("dtd")) {
          if (schema == null) return builderPool.getBuilder(true);
          EntityResolver resolver;
          try {
            resolver = new BuilderFactory().createResolver(
                  new FileInputStream(schema));
          } catch (IOException e) {
            throw new UsageException(e);
          }
          return builderPool.getDTDBuilder(resolver);
        } else if (validate.equals("schema")) {
          HashMap map = new HashMap();
          if (schema != null) map.put(schema, namespace);
//          return new BuilderFactory().createW3CBuilder(map);
          return builderPool.getW3CBuilder(map);
        } else if (validate.equals("relaxng")) {
          if (schema == null) throw new UsageException(
              "Missing required argument --schema");
          return builderPool.getMSVBuilder(schema.toURI());
        } else if (validate.equals("html")) {
          XMLReader parser;
          try {
            parser = (XMLReader) Class.forName("org.ccil.cowan.tagsoup.Parser").newInstance();
          } catch (Exception e) {
            throw new UsageException(e);
          }
          return new Builder(parser);
        } else {
          throw new UsageException("Illegal validate option: " + validate);
        }
      }
    };
View Full Code Here

        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();
         
          new Builder(redirector).build(file); // perform conversion       
        }
       
        end = System.currentTimeMillis();
        System.out.println(" [ms=" + (end-start) + "]. ");
        in.close();
View Full Code Here

    doc = null;
    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")) {
        doc = null;
      }
      fileData = null;
    }

    if (mode.startsWith("xom")) {
      if (!cmd.equals("deser")) {
        doc = new Builder().build(new ByteArrayInputStream(fileData));
      }
    }
   
    domDoc = null;
    if (mode.equals("dom")) {
      if (!cmd.equals("deser")) {
        domDoc = domBuilder.parse(new ByteArrayInputStream(fileData));
      }
    }
   
    saxonDoc = null;
    if (mode.equals("saxon")) {
      if (!cmd.equals("deser")) {
        saxonDoc = context.buildDocument(new StreamSource(new ByteArrayInputStream(fileData)));
      }
    }

    if (mode.startsWith("fi")) {
      doc = new Builder().build(new ByteArrayInputStream(fileData));
      if (cmd.equals("deser")) {
        if (mode.indexOf("stax") >= 0) {
//          data = serializeWithStax(doc, staxOutputFactory);
          data = serializeWithFastInfosetStax(doc, (XMLStreamWriter)fiSerializer, fiMethod, new ByteArrayOutputStream());
        } else {
View Full Code Here

      if (mode.indexOf("NNF") >= 0) {
        bnuxFactory = XOMUtil.getNullNodeFactory();
      }
    }
   
    builder = new Builder();
    if (mode.indexOf("pool") >= 0) {
      builder = BuilderPool.GLOBAL_POOL.getBuilder(false);
    }
   
    if (mode.equals("xom-V")) {
      builder = new Builder(new NodeFactory() {});
    } else if (mode.equals("xom-V-pool")) {
      builder = new BuilderFactory() {
        protected Builder newBuilder(XMLReader parser, boolean validate) {
          return new Builder(parser, false, new NodeFactory() {});    
        }
      }.createBuilder(false);
    }
   
    if (mode.equals("xom-NNF")) {
      builder = new Builder(XOMUtil.getNullNodeFactory());
    } else if (mode.equals("xom-NNF-pool")) {
      builder = new BuilderFactory() {
        protected Builder newBuilder(XMLReader parser, boolean validate) {
          return new Builder(parser, false, XOMUtil.getNullNodeFactory());    
        }
      }.createBuilder(false);
    }
   
    // saxon
    context = null;
    saxonSerializer = null;
    if (mode.equals("saxon")) {
      context = new StaticQueryContext(new Configuration());
      saxonSerializer = createIdentityTransform(
          new String[] {"net.sf.saxon.TransformerFactoryImpl"});
    }
   
    // DOM
    domBuilder = null;
    domSerializer = null;
    if (mode.equals("dom")) {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(true);
      try {
        factory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion", Boolean.FALSE);
      } catch (IllegalArgumentException e) {
        // crimson does not implement this attribute
      }
      domBuilder = factory.newDocumentBuilder();
      domSerializer = createIdentityTransform(new String[] {
          "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl",
          "org.apache.xalan.processor.TransformerFactoryImpl"});
      System.err.println(domSerializer.getClass().getName());
    }
   
    // FastInfoSet
    fiBuilder = null
    fiSerializer = null;
    fiMethod = 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();
View Full Code Here

    for (int j=0; j < threads; j++) {
      final int t = j;
      Runnable runner = new Runnable() {
        public void run() {
          try {
            Builder builder = new Builder();
            int i = 0;
            while (i < files.length) {
              System.out.println("t="+ t + ", index="+i);
              Document doc = builder.build(files[i]);
              Object key = files[i];
              pool.putDocument(key, doc);
              i++;
            }
            System.out.println("done");
View Full Code Here

   
    try {
      if (input == null && baseURI == null)
        throw new IllegalArgumentException("input and baseURI must not both be null");
     
      Builder builder = newBuilder();
      if (baseURI == null)
        return builder.build(input);
      else
        return builder.build(input, baseURI.toASCIIString());     
    }
    finally { // better safe than sorry
      if (input != null) input.close();
    }
  }
View Full Code Here

  public static void main(String[] args) throws Exception {
    new XQueryUpdateTest().run(args);
  }
 
  private void run(String[] args) throws Exception {
    Document doc = new Builder().build(new File("samples/data/articles.xml"));
    System.out.println("input=\n" + XOMUtil.toPrettyXML(doc));
//    update(doc, "//article[@name='chair']", "(. , ., .)", "make copies");
    update(doc, "//node() | //@*", ".", "identity transform");
    update(doc, "//@*", "()", "delete all attributes");
    update(doc, "//article[@name='chair']", "()", "delete all chairs");
View Full Code Here

   * <pre>
   * java nux.xom.tests.XQueryBenchmark data/romeo.xml 100 data/romeo100.xml
   * </pre>
   */
  public static void generateTestData(String[] args) throws Exception {
    Document doc = new Builder().build(new File(args[0])); // e.g. "romeo.xml"
    int times = Integer.parseInt(args[1]);    // e.g. 100
    Elements children = doc.getRootElement().getChildElements();
    for (int k=0; k < times; k++) {
      for (int i=0; i < children.size(); i++) {
        doc.getRootElement().appendChild(children.get(i).copy());
View Full Code Here

TOP

Related Classes of nu.xom.Builder

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.