Examples of Nodes


Examples of nu.xom.Nodes

   *
   * @return a node factory
   */
  public static NodeFactory getIgnoreWhitespaceOnlyTextNodeFactory() {
    return new NodeFactory() {
      private final Nodes NONE = new Nodes();
     
      public Nodes makeText(String text) {
        return Normalizer.isWhitespaceOnly(text) ?
          NONE :
          super.makeText(text);  
View Full Code Here

Examples of nu.xom.Nodes

   *
   * @return a node factory
   */
  public static NodeFactory getTextTrimmingNodeFactory() {
    return new NodeFactory() {
      private final Nodes NONE = new Nodes();
     
      public Nodes makeText(String text) {
        text = Normalizer.trim(text);
        return text.length() == 0 ? NONE : super.makeText(text);
      }
View Full Code Here

Examples of nu.xom.Nodes

   *
   * @return a node factory
   */
  public static NodeFactory getNullNodeFactory() {
    return new NodeFactory() {
      private final Nodes NONE = new Nodes();
           
      public Nodes makeAttribute(String name, String URI, String value, Attribute.Type type) {
        return NONE;
      }
 
View Full Code Here

Examples of nu.xom.Nodes

     * by the Builder, and only then calls serializer.writeStartTag(Elem).
     */
    return new NodeFactory() {
     
      private Element buffer = null;
      private final Nodes NONE = new Nodes();
      private final NodeBuilder nodeBuilder = new NodeBuilder();
           
      public Nodes makeAttribute(String name, String namespace,
          String value, Attribute.Type type) {
       
        buffer.addAttribute(
          nodeBuilder.createAttribute(name, namespace, value, type));
//        buffer.addAttribute(
//          new Attribute(name, namespace, value, type));
        return NONE;
      }
 
      public Nodes makeComment(String data) {
        flush();
        try {
          serializer.write(new Comment(data));
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
        return NONE;
      }
 
      public Nodes makeDocType(String rootElementName, String publicID, String systemID) {
        flush();
        try {
          serializer.write(new DocType(rootElementName, publicID, systemID));
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
        return NONE;
      }
 
      public Nodes makeProcessingInstruction(String target, String data) {
        flush();
        try {
          serializer.write(new ProcessingInstruction(target, data));
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
        return NONE;
      }
     
      public Nodes makeText(String text) {
        flush();
        try {
          serializer.write(new Text(text));
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
        return NONE;
      }
     
      public Element startMakingElement(String name, String namespace) {
        flush();
//        buffer = new Element(name, namespace);
        buffer = nodeBuilder.createElement(name, namespace);
        return buffer;
      }
     
      public Nodes finishMakingElement(Element element) {
        flush();
        try {
          serializer.writeEndTag();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
       
        if (element.getParent() instanceof Document) {
          return new Nodes(element);
        }
        return NONE;
      }
     
      public Document startMakingDocument() {
View Full Code Here

Examples of nu.xom.Nodes

      boolean hasRootElement = false;
      int k = 0;
     
      for (int i=0; i < doc.getChildCount(); i++) {
        Node child = doc.getChild(i);
        Nodes nodes;
        if (child instanceof Element) {
          Element elem = (Element) child;
          Element root = factory.makeRootElement(
              elem.getQualifiedName(), elem.getNamespaceURI());
          if (root == null) {
            throw new NullPointerException("Factory failed to create root element.");
          }
          result.setRootElement(root);
          appendNamespaces(elem, root);
          appendAttributes(elem, factory, root);
          build(elem, factory, root);
          nodes = factory.finishMakingElement(root);
        } else if (child instanceof Comment) {
          nodes = factory.makeComment(child.getValue());
        } else if (child instanceof ProcessingInstruction) {
          ProcessingInstruction pi = (ProcessingInstruction) child;
          nodes = factory.makeProcessingInstruction(
            pi.getTarget(), pi.getValue());
        } else if (child instanceof DocType) {
          DocType docType = (DocType) child;
          nodes = factory.makeDocType(
              docType.getRootElementName(),
              docType.getPublicID(),
              docType.getSystemID());
        } else {
          throw new IllegalArgumentException("Unrecognized node type");
        }
       
        // append nodes:
        for (int j=0; j < nodes.size(); j++) {
          Node node = nodes.get(j);
          if (node instanceof Element) { // replace fake root with real root
            if (hasRootElement) {
              throw new IllegalAddException(
                "Factory returned multiple root elements");
            }
View Full Code Here

Examples of nu.xom.Nodes

      return result;
    }
   
    private static void build(Element parent, NodeFactory factory, Element result) {
      for (int i=0; i < parent.getChildCount(); i++) {
        Nodes nodes;
        Node child = parent.getChild(i);
        if (child instanceof Element) {
          Element elem = (Element) child;
          Element copy = factory.startMakingElement(
              elem.getQualifiedName(), elem.getNamespaceURI());
         
          if (copy != null) {
            result.appendChild(copy);
            result = copy;
            appendNamespaces(elem, result);
            appendAttributes(elem, factory, result);
          }

          build(elem, factory, result); // recurse down
         
          if (copy == null) continue; // skip element
          result = (Element) copy.getParent(); // recurse up
          nodes = factory.finishMakingElement(copy);
          if (nodes.size()==1 && nodes.get(0)==copy) { // same node? (common case)
            continue; // optimization: no need to remove and then readd same element
          }       
          if (result.getChildCount()-1 < 0) {
            throw new XMLException("Factory has tampered with a parent pointer " +
              "of ancestor-or-self in finishMakingElement()");
View Full Code Here

Examples of nu.xom.Nodes

          expected = new Object[] { "" };
        }
        else { // it is a document containing zero or more XPath queries
          Document queries = readDocument(args[q]); // e.g. "xpath/queries1.xml"
         
          Nodes paths = XQueryUtil.xquery(queries, "/paths/document/path");   
          contexts = new Node[paths.size()];
          selects = new String[paths.size()];
          types = new int[paths.size()];
          expected = new Object[paths.size()];
         
          // precompute all the info necessary to run the bench
          for (int i=0; i < paths.size(); i++) {
            Element path = (Element) paths.get(i);
            Attribute ctxAttr = path.getAttribute("context");
           
            contexts[i] = ctxAttr == null ? doc : XQueryUtil.xquery(doc, ctxAttr.getValue()).get(0);
            selects[i] = path.getAttribute("select").getValue();
            types[i] = 0;
            if (path.getAttribute("type") != null) {
              String[] flavours = {"count", "string", "double", "boolean"};
              types[i] = java.util.Arrays.asList(flavours).indexOf(path.getAttribute("type").getValue());
            }
            expected[i] = path.getValue();
          }
        }
       
        // for each query
        for (int i=0; i < selects.length; i++) {
          int actualRuns = runs;
          if (selects[i].equals("//*[contains(string(.),'Capulet')]")) {
//            actualRuns = Math.min(runs, 10); // this one would take too long
            continue; // ignore
          }
          System.out.print("query = " + selects[i] + "  ");
          if (actualRuns == 1) System.out.println(
            "\nexplain = \n" + new XQuery(selects[i], null).explain());
          XQuery xquery = new XQuery(selects[i], null);
                 
          // now execute the query N times and measure execution time
          long start = System.currentTimeMillis();
          IS_BENCHMARK = true;
          Nodes results = run(contexts[i], selects[i], actualRuns, mode, types[i], xquery);
          IS_BENCHMARK = false;
          long end = System.currentTimeMillis();
         
          if (check && results != null) { // found the right results?
            for (int j=0; j < results.size(); j++) {
              System.out.println("node " + j + ": " + results.get(j).toXML());
            }
            Node first = results.size() == 0 ? null : results.get(0)
            Object actual = null;
            switch (types[i]) {
              case 0 : actual = String.valueOf(results.size()); break;
              case 1 : actual = first == null ? "" : first.getValue(); break;
              case 2 : actual = first == null ? "0.0" : new Double(first.getValue()).toString(); break;
              case 3 : actual = first == null ? "false" : first.getValue().equals("true") ? "true" : "false"; break;
              default: throw new IllegalStateException();
            }       
View Full Code Here

Examples of nu.xom.Nodes

      if (k < repeats-1) Thread.sleep(3000); // give hotspot VM some time to finish compilation
    }
  }
 
  private static Nodes run(Node contextNode, String query, int runs, int mode, int type, XQuery xquery) throws Exception {
    Nodes results = null;
    for (int run=0; run < runs; run++) {
      switch (mode) {
        case -1 : // Nux with static query
//          dummy += xquery.execute(contextNode).next().getBaseURI().length();
          results = xquery.execute(contextNode).toNodes();
View Full Code Here

Examples of nu.xom.Nodes

      "return element {node-name($e)} {" +
        "attribute percent {round-half-to-even(100.0 * $e/@instances div /*/@descendants-or-self, 1)}," +
        "attribute instances {$e/@instances}, " +
        "attribute xpath {$e/saxon:path()}" +
      "}";
    Nodes nodes = XQueryUtil.xquery(summary, query);
    ResultSequenceSerializer rser = new ResultSequenceSerializer();
    rser.setIndent(4);
    rser.write(nodes, System.out);   
  }
View Full Code Here

Examples of nu.xom.Nodes

    if (elem.getParent() instanceof Document) { // done with root element?
      current = (Element) current.removeChild(0);
      int total = Integer.parseInt(
        current.getAttributeValue("descendants-or-self"));
      addAccumulatedPercentages(current, total);     
      return new Nodes(current);
    }
    return NONE;
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.