Package nu.xom

Examples of nu.xom.Nodes


    private Nodes transformMatch(Element elem) {
//      if (DEBUG) System.err.println("found match at level=" + level + ":"
//              + XOMUtil.toPrettyXML(elem));
      level--;
      if (transform == null) return super.finishMakingElement(elem);
      Nodes results = transform.transform(elem);

      // prevent potential nu.xom.MultipleParentException by detaching
      for (int i = results.size(); --i >= 0; ) {
        Node node = results.get(i);
        if (node != elem) node.detach();
      }
      return results;
    }
View Full Code Here


    this.results = results;
    this.config = config;
  }
 
  public Nodes toNodes() throws XQueryException {
    Nodes nodes = new Nodes();
    Node next;
    while ((next = next()) != null) {
      nodes.append(next);
    }
    return nodes;
  }
View Full Code Here

    File expectedDir = new File(rootDir,
      XQueryUtil.xquery(catalog, ns + "ns:test-suite/@ResultOffsetPath").get(0).getValue());
    File queryDir = new File(rootDir,
      XQueryUtil.xquery(catalog, ns + "ns:test-suite/@XQueryQueryOffsetPath").get(0).getValue());
    File testSourcesDir = new File(rootDir, "TestSources");   
    Nodes testCases = XQueryUtil.xquery(catalog, ns + "//ns:test-case");
   
    for (int i=0; i < testCases.size(); i++) {
      Node testCase = testCases.get(i);

//      String groupTitle = XQueryUtil.xquery(testCase, ns + "../ns:GroupInfo/ns:title").get(0).getValue();
      String path = XQueryUtil.xquery(testCase, "@FilePath").get(0).getValue();
      File query = new File(new File(queryDir, path),
        XQueryUtil.xquery(testCase, ns + "ns:query/@name").get(0).getValue() + ".xq");
      String squery = readQuery(query);
      System.out.println(i + ": " + query + " ...");
     
      if (XQueryUtil.xquery(testCase, ns + "ns:spec-citation[@section-pointer='id-validate']").size() > 0) {
        System.out.println("    ************* IGNORED SCHEMA AWARE FUNCTIONALITY *****");
        continue; // ignore validate() function (nux is not schema aware)
      }
         
      if (squery == null) {
        System.out.println("    ************* IGNORED *****");
        continue;
      }
           
      Nodes inputs = XQueryUtil.xquery(testCase, ns + "ns:input-file");
      Map vars = new HashMap();
      for (int j=0; j < inputs.size(); j++) {
        File input = new File(testSourcesDir, inputs.get(j).getValue() + ".xml");
        String varName = ((Element) inputs.get(j)).getAttributeValue("variable");
        Document inputDoc = buildDocument(input);
//        System.out.println(inputDoc.getBaseURI());
        if (true) XOMUtil.Normalizer.STRIP.normalize(inputDoc);
        vars.put(varName, inputDoc);
      }

      Nodes expectedErrors = XQueryUtil.xquery(testCase, ns + "ns:expected-error");         
      Nodes expectedOutputs = XQueryUtil.xquery(testCase, ns + "ns:output-file");
      boolean inspect = false;
      for (int k=0; !inspect && k < expectedOutputs.size(); k++) {
        String compare = ((Element)expectedOutputs.get(k)).getAttributeValue("compare");
        if ("Inspect".equals(compare)) inspect = true;
      }
       
      Nodes results = null;
      try { // here's where the query is actually executed
        XQuery xquery = new XQuery(squery, testSourcesDir.toURI());
//        XQuery xquery = new XQuery(squery, query.toURI());
//        XQuery xquery = XQueryPool.GLOBAL_POOL.getXQuery(squery, query.toURI());
        results = xquery.execute(null, null, vars).toNodes();
View Full Code Here

              if (morpher != null && doc2 != null) {
                doc2 = new Document(doc2); // immutable for multiple iterations
              }
             
              // run the query
              Nodes results;
              if (xomXPath) {
                if (doc2 == null) throw new UsageException(
                  "A context node is required by XOM's XPath engine, but missing.");
                results = doc2.query((String)query);
              } else if (xquery != null) {
                results = xquery.execute(doc2, null, variables).toNodes();
              } else {
                results = new Nodes(); // disable XQuery for benchmarking
                results.append(doc2);
              }
             
              if (morpher != null) {
                // interpret --query as select, interpret --update as morpher
                for (int k=0; doc2 == null && k < results.size(); k++) {
                  doc2 = results.get(k).getDocument();
                }
                XQueryUtil.update(results, morpher, null);
               
                // serialize modified document if there is one
                results = new Nodes();
                if (doc2 != null) results.append(doc2);
              }
             
              // serialize results onto output, if any
              File f = (File) outputFiles.get(j);
              OutputStream out = System.out;
View Full Code Here

     * nu.xom.Serializer
     */
    ResultSequenceSerializer serializer = new ResultSequenceSerializer();
    serializer.setIndent(4);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Nodes nodes = new Nodes();
    nodes.append(node);
    String xml;
    try {
      serializer.write(nodes, out);
      xml = out.toString("UTF-8"); // safe: UTF-8 support is required by JDK spec
    } catch (IOException e) {
View Full Code Here

   *
   * @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

   *
   * @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

   *
   * @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

     * 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

      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

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.