Package nux.xom.pool

Examples of nux.xom.pool.BuilderPool


  private DocumentPool createDocumentPool(final boolean isBench) {
    // prepare BuilderPool
    PoolConfig config = new PoolConfig();
    if (noBuilderPool) config.setMaxEntries(0);
    final BuilderPool builderPool;
   
    if (filter == null) {
      builderPool = new BuilderPool(config, new BuilderFactory());
    } else {
      BuilderFactory builderFactory = new BuilderFactory() {
        protected Builder newBuilder(XMLReader parser, boolean validate) {
          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) {
View Full Code Here


   
    byte[] b = new byte[0];
    if (memory && !fileName.equals("nofile")) b = FileUtil.toByteArray(new FileInputStream(fileName));
    final byte[] bytes = b;
   
    final BuilderPool pool = !nullNodeFactory ? BuilderPool.GLOBAL_POOL : new BuilderPool(new PoolConfig(),
        new BuilderFactory() {
          protected NodeFactory createNodeFactory() {
            return XOMUtil.getNullNodeFactory();
          }
        }
      );
     
    // warmup hotspot VM
    for (int i=0; i < 100; i++) {
      new Builder(false);
      new Builder(true);
      pool.getW3CBuilder(map);     
    }
    System.gc();
    Thread.sleep(1000); // give hotspot some time to optimize

    // run the benchmark
    long start = System.currentTimeMillis();
    for (int i = 0; i < numThreads; i++) { // stress gc on threads and ThreadLocal via container-like scenario
      Thread thread = new Thread() {
        int checksum = 0; // make dead-code elimination impossible for hotspot vm
        public void run() {
          try {
            for (int i=0; i < runs; i++) { // user code inside multi-threaded container
              bench();
            }
          } catch (Exception e) { throw new RuntimeException(e); }       
        }
        private void bench() throws Exception {
          Builder builder;
          if (validate.equals("validateschema")) {
            builder = pool.getW3CBuilder(map);
          }
          else {
            builder = isPooled ?
              pool.getBuilder(validate.equals("validate")) :
              new Builder(validate.equals("validate"))
          }
          checksum += builder.hashCode();
          if (! fileName.equals("nofile")) {
            Document doc = memory ?
View Full Code Here

TOP

Related Classes of nux.xom.pool.BuilderPool

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.