Package nu.xom

Examples of nu.xom.Element


  /** sets the XPath value of the given node to the given value */
  private static void setValue(Node node, String value) {
    if (node instanceof Document) {
      // remove all children except root element (XOM docs must have a root element)
      Document doc = (Document) node;
      Element root = doc.getRootElement();
      for (int k = doc.getChildCount(); --k >= 0; ) {
        if (doc.getChild(k) != root) doc.removeChild(k);
      }
      node = root; // replace root element's content
    }

    if (node instanceof Element) {
      Element elem = (Element) node;
      elem.removeChildren();
      elem.appendChild(value);
    } else if (node instanceof Attribute) {
      ((Attribute) node).setValue(value);
    } else if (node instanceof Text) {
      ((Text) node).setValue(value);
    } else if (node instanceof Comment) {
View Full Code Here


    return new Attribute(
      node.getDisplayName(), node.getURI(), node.getStringValue());     
  }
 
  private Document convertDocumentNodeInfo(NodeInfo node) {
    Document doc = new Document(new Element("fakeRoot"));
    doc.setBaseURI(node.getBaseURI());
   
    boolean hasRootElement = false;
    int i = 0;
    NodeInfo next;
View Full Code Here

    return doc;
  }
 
  private Element convertElementNodeInfo(NodeInfo node) {
    if (DEBUG) System.err.println("converting element=" + node.getDisplayName());
    Element elem = new Element(node.getDisplayName(), node.getURI());
    NodeInfo next;
   
    // Append attributes
    AxisIterator iter = node.iterateAxis(Axis.ATTRIBUTE);
    ArrayList prefixes = null;
    while ((next = (NodeInfo) iter.next()) != null) {
      elem.addAttribute((Attribute) convertNodeInfo(next));
     
      // keep track of attributes with prefixes, so we can avoid adding costly
      // additional namespaces declarations below. This safes potentially vast
      // amounts of memory due too XOM's expensive NS declaration storage scheme.
      String prefix = next.getPrefix();
      if (prefix.length() != 0) { // e.g. SOAP
         if (prefixes == null) prefixes = new ArrayList(1);
         prefixes.add(prefix);
      }
    }
   
    // Append namespace declarations (avoids unnecessary redeclarations).
    // For background see net.sf.saxon.om.NamespaceIterator and
    // net.sf.saxon.om.NamespaceDeclarationsImpl
    int[] namespaces = node.getDeclaredNamespaces(NodeInfo.EMPTY_NAMESPACE_LIST);
    int i = 0;
    int nsCode;
    NamePool pool = null;
    while (i < namespaces.length && (nsCode = namespaces[i]) != -1) {
      short uriCode = (short) (nsCode & 0xffff);
      if (uriCode != 0) { // it is not an undeclaration
        if (pool == null) pool = node.getNamePool();
        String uri = pool.getURIFromURICode(uriCode);
        String prefix = pool.getPrefixFromNamespaceCode(nsCode);
       
        if (prefixes != null && prefixes.contains(prefix)) {
          if (DEBUG) System.err.println(
            "Safely ignoring additional namespace declaration for prefix="
            + prefix + ", uri=" + uri);
        }
        else {
          if (DEBUG) System.err.println(
            "Adding additional namespace declaration for prefix="
            + prefix + ", uri=" + uri);
          elem.addNamespaceDeclaration(prefix, uri);
        }
      }
      i++;
    }
    prefixes = null; // help gc
    namespaces = null; // help gc
   
//    // Append namespace declarations (would introduce lots of redundant redeclarations)
//    iter = node.iterateAxis(Axis.NAMESPACE);
//    while ((next = (NodeInfo) iter.next()) != null) {
//      String prefix = next.getLocalPart();
//      String uri = next.getStringValue();
//      if (DEBUG) System.err.println("converted prefix=" + prefix + ", uri=" + uri);
//      elem.addNamespaceDeclaration(prefix, uri);
//    }
   
    // Append children (recursively)
    iter = node.iterateAxis(Axis.CHILD);
    while ((next = (NodeInfo) iter.next()) != null) {
      elem.appendChild(convertNodeInfo(next));
    }
   
    return elem;
  }
View Full Code Here

  }
 
  /** Converts saxon's atomic value to XOM. */
  private Node convertAtomicValue(AtomicValue value) {
    if (DEBUG) System.err.println("atomicValue.getClass="+value.getClass().getName());
    Element elem = new Element(ATOMIC_VALUE)// copy to avoid reverification
    elem.getAttribute(0).setValue(getItemType(value)); // e.g. "xs:integer"
    Text text = ((Text)elem.getChild(0));
    text.setValue(value.getStringValue()); // e.g. "123"
    return elem;
  }
View Full Code Here

    }
  }
 
  static boolean isAtomicValue(Node node) {
    if (node instanceof Element) {
      Element elem = (Element) node;
      return elem.getLocalName().equals("atomic-value") &&
        elem.getNamespaceURI().equals("http://dsd.lbl.gov/nux");
    }
    return false;
  }
View Full Code Here

    throw new ConformanceException(
      "\nexpected='" + expected + "', \nactual  ='" + actual + "'");   
  }
 
  private String normalize(String text) {
    Element wrapper = new Element("dummy");
    wrapper.appendChild(text);
    XOMUtil.Normalizer.COLLAPSE.normalize(wrapper); // ???
    return wrapper.getValue();
  }
View Full Code Here

      for (int i=0; i < parent.getChildCount(); i++) {
        toStatisticsString(parent.getChild(i), stats);
      }
      if (node instanceof Element) {
        stats.elements++;
        Element elem = (Element) node;
        value = elem.getQualifiedName();
        stats.tagChars += value.length();
        for (int j=0; j < elem.getAttributeCount(); j++) {
          toStatisticsString(elem.getAttribute(j), stats);
        }
        // TODO: include additional namespace declarations?
      }
    }
    else {
View Full Code Here

  private static void toDebugString(ParentNode node, int depth, StringBuffer result) {
    String indent = TABS.substring(0, depth); // substring sharing
   
    // print attributes
    if (node instanceof Element) {
      Element elem = (Element) node;
      for (int i=0; i < elem.getAttributeCount(); i++) {
        result.append(indent);
        result.append(elem.getAttribute(i).toString());
        result.append('\n');
      }
    }
   
    // omitting namespace declarations for now
View Full Code Here

        log("", new Text(text));
        return child.makeText(text);
      }
 
      public Element makeRootElement(String name, String namespace) {
        log("startRoot", new Element(name, namespace));
        level++;
        return child.makeRootElement(name, namespace);
      }
 
      public Element startMakingElement(String name, String namespace) {
        log("start", new Element(name, namespace));
        Element elem = child.startMakingElement(name, namespace);
        if (elem == null)
          log("SKIP ", new Element(name, namespace));
        else
          level++;
        return elem;
      }
     
View Full Code Here

      public Nodes makeProcessingInstruction(String target, String data) {
        return NONE;
      }
     
      public Element makeRootElement(String name, String namespace) {
        return new Element(name, namespace);
      }
 
      public Nodes makeText(String text) {
        return NONE;
      }
 
      public Element startMakingElement(String name, String namespace) {
        return null;
      }
     
      public Document startMakingDocument() {
        return new Document(new Element("dummy")); // unused dummy
      }
    };
  }
View Full Code Here

TOP

Related Classes of nu.xom.Element

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.