Package nu.xom

Examples of nu.xom.Nodes


    boolean hasRootElement = false;
    int i = 0;
   
    // add children of document, retaining the exact same order found in input
    while (src.remaining() > 0) {
      Nodes nodes;
      int type = src.get(); // look ahead
      if (DEBUG) System.err.println("reading type = " + toString(type));
      switch (type & 0x07) { // three low bits indicate node type
        case TEXT: {
          throw new BinaryParsingException("Unreachable text");
        }
        case ATTRIBUTE: {
          throw new BinaryParsingException("Unreachable attribute");
        }
        case BEGIN_ELEMENT: {
          if (factory.getClass() == NodeFactory.class) { // fast path
            Element root = readStartTag(src, type);
            readElement(src, root, input); // reads entire subtree
            nodes = new Nodes(root);
          } else { // slow path
            Element root = readStartTagF(src, type, true);
            if (root == null) {
              throw new NullPointerException("Factory failed to create root element.");
            }
            doc.setRootElement(root);
            readElementF(src, root, input);
            nodes = factory.finishMakingElement(root);
          }
          break;
        }
        case END_ELEMENT: {
          throw new BinaryParsingException("Unreachable end of element");
        }
        case COMMENT: {
          nodes = readCommentF(src, type);
          break;
        }
        case NAMESPACE_DECLARATION: {
          throw new BinaryParsingException("Unreachable namespace declaration");
        }
        case PROCESSING_INSTRUCTION: {
          nodes = readProcessingInstructionF(src);
          break;
        }
        case DOC_TYPE: {
          nodes = readDocTypeF(src);
          break;
        }
        default: {
          throw new BinaryParsingException("Illegal node type code=" + 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


    final FastStack stack = new FastStack();
    stack.push(current);
    boolean addAttributesAndNamespaces = true;
   
    while (true) {
      Nodes nodes = null;
      int type = src.get(); // look ahead
//      if (DEBUG) System.err.println("reading type = " + toString(type));

      switch (type & 0x07) { // three low bits indicate node type
        case TEXT: {
          nodes = readTextF(src, type);
          break;
        }
        case ATTRIBUTE: {
          Element elem = addAttributesAndNamespaces ? current : null;
          nodes = readAttributeF(src, elem, type);
          break;
        }
        case BEGIN_ELEMENT: {
          Element elem = readStartTagF(src, type, false);
          stack.push(elem); // even if it's null
          if (elem != null) {
            current.insertChild(elem, current.getChildCount());
            current = elem; // recurse down
          }
          addAttributesAndNamespaces = elem != null;
          continue;
        }
        case END_ELEMENT: {
          Element elem = stack.pop();
          if (elem == null) {
            continue; // skip element
          }
          ParentNode parent = elem.getParent();
          if (parent == null) throwTamperedWithParent();
          if (parent instanceof Document) {
            return; // we're done with the root element
          }
         
          current = (Element) parent; // recurse up
          nodes = factory.finishMakingElement(elem);
                    
          if (nodes.size()==1 && nodes.get(0)==elem) { // same node? (common case)
            continue; // optimization: no need to remove and then readd same element
          }
         
          if (current.getChildCount()-1 < 0) throwTamperedWithParent();       
          current.removeChild(current.getChildCount()-1);
View Full Code Here

      case XMLStreamConstants.CDATA:
      case XMLStreamConstants.ENTITY_REFERENCE:
      case XMLStreamConstants.CHARACTERS:
        return readText();
      case XMLStreamConstants.DTD: {
        Nodes nodes = null;
        try {
          nodes = readDocType(new NodeFactory());
        } catch (XMLStreamException e) {
          StaxUtil.wrapException(e);
        }
        if (nodes.size() > 0) return nodes.get(0);
        return null; // unsupported extended DTD API
      }
      case XMLStreamConstants.ENTITY_DECLARATION:
        return null; // ignore
      case XMLStreamConstants.NOTATION_DECLARATION:
View Full Code Here

    String systemID = symbols[src.getInt()];
    if (DOCTYPE_NULL_ID.equals(systemID)) systemID = null;
    String internalDTDSubset = symbols[src.getInt()];
    if (internalDTDSubset.length() == 0) internalDTDSubset = null;
   
    Nodes nodes = factory.makeDocType(rootElementName, publicID, systemID);
    for (int i=0; i < nodes.size(); i++) {
      if (nodes.get(i) instanceof DocType) {
        DocType docType = (DocType) nodes.get(i);
        if (docType.getInternalDTDSubset().length() == 0) {
          try {
            docType.setInternalDTDSubset(internalDTDSubset);
          } catch (IllegalAccessError e) {
            ; // ignore; setInternalDTDSubset() is private in xom < 1.1
View Full Code Here

    boolean done = false;
    int i = 0;
   
    while (!done && reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
     
      Nodes nodes;
//      if (DEBUG) System.out.println(toString(reader.getEventType()));
      switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
          if (hasRootElement) throw new IllegalAddException(
            "StAX reader must not return multiple root elements");

          if (factory.getClass() == NodeFactory.class) { // fast path
            if (nodeBuilder == null) nodeBuilder = new NodeBuilder();
            Element root = readStartTag();
            addAttributes(root);
            addNamespaceDeclarations(root);
            readElement(root); // reads entire subtree
            nodes = new Nodes(root);
          } else { // slow path     
            Element root = readStartTagF(true);
            if (root == null) {
              throw new NullPointerException(
                "Factory failed to create root element.");
            }
            doc.setRootElement(root);
            addAttributesF(root);
            addNamespaceDeclarations(root);
            readElementF(root); // read entire subtree
            nodes = factory.finishMakingElement(root);
          }
          reader.require(XMLStreamConstants.END_ELEMENT, null, null);
          if (isFragmentMode) done = true;
          break;
        }
        case XMLStreamConstants.END_ELEMENT:
          throw new IllegalAddException(
            "A document must not have more than one root element");
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
          nodes = factory.makeProcessingInstruction(
              reader.getPITarget(), reader.getPIData());
          break;
        case XMLStreamConstants.CHARACTERS:
          nodes = NONE; // ignore text in prolog/epilog
          break;
        case XMLStreamConstants.COMMENT:
          nodes = factory.makeComment(reader.getText());
          break;
        case XMLStreamConstants.SPACE:
          nodes = NONE; // ignore text in prolog/epilog
          break;
        case XMLStreamConstants.START_DOCUMENT:
          nodes = NONE; // has already been handled previously
          break;
        case XMLStreamConstants.END_DOCUMENT:
          throw new IllegalStateException("unreachable");
        case XMLStreamConstants.CDATA:
          nodes = NONE; // ignore text in prolog/epilog
          break;
        case XMLStreamConstants.ATTRIBUTE:
          throw new IllegalAddException(
            "Illegal attribute in prolog/epilog");
        case XMLStreamConstants.NAMESPACE:
          throw new IllegalAddException(
            "Illegal namespace declaration in prolog/epilog");
        case XMLStreamConstants.DTD:
          nodes = readDocType(factory); // FIXME
          break;
        case XMLStreamConstants.ENTITY_DECLARATION:
          nodes = NONE; // ignore (missing StAX support)
          break;
        case XMLStreamConstants.NOTATION_DECLARATION:
          nodes = NONE; // ignore (missing StAX support)
          break;
        case XMLStreamConstants.ENTITY_REFERENCE:
          nodes = NONE; // ignore text in prolog/epilog
          break;
        default:
          throw new XMLException("Unrecognized Stax event type: "
              + reader.getEventType());
      }
     
      // 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

   
    final ArrayList stack = new ArrayList();
    stack.add(current); // push

    while (true) {
      Nodes nodes;
      switch (reader.next()) { 
        case XMLStreamConstants.START_ELEMENT: {
          Element elem = readStartTagF(false);
          stack.add(elem); // push even if it's null
          if (elem != null) {
            current.appendChild(elem);
            addAttributesF(elem);
            addNamespaceDeclarations(elem);
            current = elem; // recurse down
          }
          continue;
        }
        case XMLStreamConstants.END_ELEMENT: {
          Element elem = (Element) stack.remove(stack.size()-1); // pop
          if (elem == null) {
            continue; // skip element
          }
          ParentNode parent = elem.getParent();
          if (parent == null) throwTamperedWithParent();
          if (parent instanceof Document) {
            return; // we're done with the root element
          }
         
          current = (Element) parent; // recurse up
          nodes = factory.finishMakingElement(elem);
                    
          if (nodes.size()==1 && nodes.get(0)==elem) { // same node? (common case)
            continue; // optimization: no need to remove and then readd same element
          }
         
          if (current.getChildCount()-1 < 0) throwTamperedWithParent();       
          current.removeChild(current.getChildCount()-1);
View Full Code Here

    String publicID = (String) invoke(info, "getDTDPublicId");
    if (publicID == MISSING_StAX2) return NONE;
    String systemID = (String) invoke(info, "getDTDSystemId");
    if (systemID == MISSING_StAX2) return NONE;
   
    Nodes nodes = nodeFactory.makeDocType(rootName, publicID, systemID);
    for (int k=0; k < nodes.size(); k++) {
      Node node = nodes.get(k);
      if (node instanceof DocType) {
        DocType docType = (DocType) node;
        if (docType.getInternalDTDSubset().length() == 0) {
          // xom >= 1.1 only
          String subset = (String) invoke(info, "getDTDInternalSubset");
View Full Code Here

  private Nodes buildSequence() throws ParsingException {
    // approach a)
    // assert: we are currently positioned *before* the first event, with the
    // behaviour of all methods initially undefined, except for reader.hasNext()
    try {
      Nodes results = new Nodes();
      while (reader.hasNext()) {
        Node node;
        switch (reader.next()) {
          case XMLStreamConstants.START_DOCUMENT:
            node = build(); // FIXME don't auto-close reader?
            break;
          case XMLStreamConstants.START_ELEMENT:
            node = buildFragment();
            break;
          default:
            node = buildNode();
            break;
        }
        results.append(node);
      }
      return results;
    } catch (XMLStreamException e) {
      StaxUtil.wrapException(e);
      return null; // unreachable
View Full Code Here

     * <code>nodes</code> does not contain duplicates wrt. node identity.
     */
    HashSet identities = null;
    for (int i=nodes.size(); --i >= 0; ) {
      Node node = nodes.get(i);
      Nodes results = EMPTY;
      if (morpher != null) {
        try {
          results = morpher.execute(node, null, variables).toNodes();
        } catch (XQueryException e) { // part of the "convenience"
          throw new RuntimeException(e);
        }
      }
     
      int size = results.size();
      if (size == 0) { // pure delete?
        node.detach();
        continue; // not really needed; just for clarity
      }
     
      if (size == 1 && node == results.get(0)) {
        continue; // nothing to do (replace X with X)
      }

      ParentNode parent = node.getParent();
      StringBuffer atomics = null;
      boolean isInitialized = false;
      int position = 0;
      if (size > 1) {
        if (identities == null) {
          identities = new HashSet();
        } else {
          identities.clear();
        }
      }
     
      for (int j=0; j < size; j++) {
        Node result = results.get(j);       
        if (DefaultResultSequence.isAtomicValue(result)) { // concat atomic values
          String value = result.getValue();
          if (atomics == null) {
            atomics = new StringBuffer(value.length());
          } else {
View Full Code Here

      } catch (XQueryException e) { // part of the "convenience"
        throw new RuntimeException(e);
      }
    }
   
    Nodes nodes = xquery(contextNode, select);
    update(nodes, xmorpher, null);
  }
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.