Package nu.xom

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


          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

      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

      "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

    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

        }
        ParentNode parent = elem.getParent();
        if (parent == null) throwTamperedWithParent();
        currents[i] = parent; // recurse up
       
        Nodes nodes = receivers[i].finishMakingElement(elem);
        if (nodes.size()==1 && nodes.get(0)==elem) { // same node? (common case)
          if (parent instanceof Document) hasRootElement[i] = true;
          continue; // optimization: no need to remove and then readd same element
        }

        if (parent.getChildCount()-1 < 0) throwTamperedWithParent();       
        if (parent instanceof Element) { // can't remove root element
          parent.removeChild(parent.getChildCount()-1);
        }
        appendNodes(parent, nodes, i);
      } catch (RuntimeException e) {
        onException(i, e);
      }
    }
   
    if (unused.getParent() instanceof Document) {
      return new Nodes(unused); // XOM documents must have a root element
    }
    return NONE;
  }
View Full Code Here

  public Nodes makeAttribute(String qname, String namespaceURI, String value, Attribute.Type type) {
    for (int i=0; i < receivers.length; i++) {
      if (exceptions[i] != null) continue; // ignore failed factory
      if (!addAttributesAndNamespaces[i]) continue;
      try {
        Nodes nodes = receivers[i].makeAttribute(
            qname, namespaceURI, value, type);
        appendNodes(currents[i], nodes, i);
      } catch (RuntimeException e) {
        onException(i, e);
      }
View Full Code Here

  /** {@inheritDoc} */
  public Nodes makeComment(String data) {
    for (int i=0; i < receivers.length; i++) {
      if (exceptions[i] != null) continue; // ignore failed factory
      try {
        Nodes nodes = receivers[i].makeText(data);
        appendNodes(currents[i], nodes, i);
      } catch (RuntimeException e) {
        onException(i, e);
      }
    }
View Full Code Here

  /** {@inheritDoc} */
  public Nodes makeDocType(String rootElementName, String publicID, String systemID) {
    for (int i=0; i < receivers.length; i++) {
      if (exceptions[i] != null) continue; // ignore failed factory
      try {
        Nodes nodes = receivers[i].makeDocType(
          rootElementName, publicID, systemID);
        appendNodes(currents[i], nodes, i);
      } catch (RuntimeException e) {
        onException(i, e);
      }
View Full Code Here

  /** {@inheritDoc} */
  public Nodes makeProcessingInstruction(String target, String data) {
    for (int i=0; i < receivers.length; i++) {
      if (exceptions[i] != null) continue; // ignore failed factory
      try {
        Nodes nodes = receivers[i].makeProcessingInstruction(target, data);
        appendNodes(currents[i], nodes, i);
      } catch (RuntimeException e) {
        onException(i, e);
      }
    }
View Full Code Here

TOP

Related Classes of nu.xom.Nodes

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.