Java Code Examples for org.w3c.dom.Node.DOCUMENT_NODE

The following are Jave code examples for showing how to use DOCUMENT_NODE of the org.w3c.dom.Node class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: OpenJSharp   File: RangeImpl.java   Source Code and License Vote up 6 votes
/**
 * Finds the root container for the given node and determines
 * if that root container is legal with respect to the
 * DOM 2 specification.  At present, that means the root
 * container must be either an attribute, a document,
 * or a document fragment.
 */
private boolean hasLegalRootContainer( Node node )
{
        if ( node==null )
                return false;

        Node rootContainer = getRootContainer( node );
        switch( rootContainer.getNodeType() )
        {
        case Node.ATTRIBUTE_NODE:
        case Node.DOCUMENT_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
                return true;
        }
        return false;
}
 
Example 2
Project: OpenJSharp   File: DOMDocumentSerializer.java   Source Code and License Vote up 6 votes
/**
 * Serialize a {@link Node}.
 *
 * @param n the node to serialize.
 */
public final void serialize(Node n) throws IOException {
    switch (n.getNodeType()) {
        case Node.DOCUMENT_NODE:
            serialize((Document)n);
            break;
        case Node.ELEMENT_NODE:
            serializeElementAsDocument(n);
            break;
        case Node.COMMENT_NODE:
            serializeComment(n);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            serializeProcessingInstruction(n);
            break;
    }
}
 
Example 3
Project: convertigo-engine   File: ParameterUtils.java   Source Code and License Vote up 6 votes
private static String nodeToString(Node node) {
	int nodeType = node.getNodeType();
	switch (nodeType) {
		case Node.DOCUMENT_NODE:
			return XMLUtils.prettyPrintElement(((Document) node).getDocumentElement(), true, false);
		case Node.ELEMENT_NODE:
			return XMLUtils.prettyPrintElement((Element) node, true, false);
		case Node.CDATA_SECTION_NODE:
		case Node.TEXT_NODE:
			int len = node.getChildNodes().getLength();
			return ((len<2) ? node.getNodeValue():XMLUtils.getNormalizedText(node));
		case Node.ATTRIBUTE_NODE:
			return node.getNodeValue();
		default:
			return null;
	}
}
 
Example 4
Project: openjdk-jdk10   File: StreamHeader.java   Source Code and License Vote up 6 votes
public void writeTo(SOAPMessage saaj) throws SOAPException {
    try {
        // TODO what about in-scope namespaces
        // Not very efficient consider implementing a stream buffer
        // processor that produces a DOM node from the buffer.
        TransformerFactory tf = XmlUtil.newTransformerFactory(true);
        Transformer t = tf.newTransformer();
        XMLStreamBufferSource source = new XMLStreamBufferSource(_mark);
        DOMResult result = new DOMResult();
        t.transform(source, result);
        Node d = result.getNode();
        if(d.getNodeType() == Node.DOCUMENT_NODE)
            d = d.getFirstChild();
        SOAPHeader header = saaj.getSOAPHeader();
        if(header == null)
            header = saaj.getSOAPPart().getEnvelope().addHeader();
        Node node = header.getOwnerDocument().importNode(d, true);
        header.appendChild(node);
    } catch (Exception e) {
        throw new SOAPException(e);
    }
}
 
Example 5
Project: openjdk-jdk10   File: DOMSerializerImpl.java   Source Code and License Vote up 5 votes
private String _getXmlEncoding(Node node) {
    Document doc = (node.getNodeType() == Node.DOCUMENT_NODE)
            ? (Document) node : node.getOwnerDocument();
    if (doc != null) {
        try {
            return doc.getXmlEncoding();
        } // The VM ran out of memory or there was some other serious problem. Re-throw.
        catch (VirtualMachineError | ThreadDeath vme) {
            throw vme;
        } // Ignore all other exceptions and errors
        catch (Throwable t) {
        }
    }
    return null;
}
 
Example 6
Project: java-tools   File: MFDOMWriter.java   Source Code and License Vote up 5 votes
public static void write(IEnumerable what, Node where) throws Exception
{
	Document owner = (where.getNodeType() == Node.DOCUMENT_NODE) ? (Document) where : where.getOwnerDocument();
	for (IEnumerator en = what.enumerator(); en.moveNext();)
	{ 
		if (en.current() instanceof IMFNode)
		{
			IMFNode el = (IMFNode)en.current();
			if ((el.getNodeKind() & IMFNode.MFNodeKind_Element) != 0)
			{
				Element xel = appendElement(owner, where, el.getNamespaceURI(), el.getLocalName(), el.getPrefix());
				write(el.select(IMFNode.MFQueryKind_All, null), xel);
			}
			if ((el.getNodeKind() & IMFNode.MFNodeKind_Attribute) != 0)
			{
				appendAttribute(owner, where, el.getNamespaceURI(), el.getLocalName(), el.getPrefix(), getValue(el, where));
			}
			else if ((el.getNodeKind() & IMFNode.MFNodeKind_Comment) != 0)
			{
				where.appendChild(owner.createComment(getValue(el,where)));
			}
			else if ((el.getNodeKind() & IMFNode.MFNodeKind_ProcessingInstruction) != 0)
			{
				where.appendChild(owner.createProcessingInstruction(el.getLocalName(), getValue(el,where)));
			}
			else if ((el.getNodeKind() & IMFNode.MFNodeKind_CData) != 0)
			{
				where.appendChild(owner.createCDATASection(getValue(el,where)));
			}
			else if ((el.getNodeKind() & IMFNode.MFNodeKind_Text) != 0)
			{
				where.appendChild(owner.createTextNode(getValue(el,where)));
			}
		}
		else
		{
			where.appendChild(owner.createTextNode(getValue(en.current(), where)));
		}
	}
}
 
Example 7
Project: OpenJSharp   File: DOMHelper.java   Source Code and License Vote up 5 votes
/**
 * Retrieve the text content of a DOM subtree, appending it into a
 * user-supplied FastStringBuffer object. Note that attributes are
 * not considered part of the content of an element.
 * <p>
 * There are open questions regarding whitespace stripping.
 * Currently we make no special effort in that regard, since the standard
 * DOM doesn't yet provide DTD-based information to distinguish
 * whitespace-in-element-context from genuine #PCDATA. Note that we
 * should probably also consider xml:space if/when we address this.
 * DOM Level 3 may solve the problem for us.
 *
 * @param node Node whose subtree is to be walked, gathering the
 * contents of all Text or CDATASection nodes.
 * @param buf FastStringBuffer into which the contents of the text
 * nodes are to be concatenated.
 */
public static void getNodeData(Node node, FastStringBuffer buf)
{

  switch (node.getNodeType())
  {
  case Node.DOCUMENT_FRAGMENT_NODE :
  case Node.DOCUMENT_NODE :
  case Node.ELEMENT_NODE :
  {
    for (Node child = node.getFirstChild(); null != child;
            child = child.getNextSibling())
    {
      getNodeData(child, buf);
    }
  }
  break;
  case Node.TEXT_NODE :
  case Node.CDATA_SECTION_NODE :
    buf.append(node.getNodeValue());
    break;
  case Node.ATTRIBUTE_NODE :
    buf.append(node.getNodeValue());
    break;
  case Node.PROCESSING_INSTRUCTION_NODE :
    // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);
    break;
  default :
    // ignore
    break;
  }
}
 
Example 8
Project: OpenJSharp   File: LogicalMessageImpl.java   Source Code and License Vote up 5 votes
public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
    Node n = dom.getNode();
    if(n.getNodeType()== Node.DOCUMENT_NODE) {
        n = ((Document)n).getDocumentElement();
    }
    return new DOMMessage(binding.getSOAPVersion(), headers, (Element)n, attachments);
}
 
Example 9
Project: convertigo-engine   File: TwsTreeWalker.java   Source Code and License Vote up 5 votes
/**
 * Use isSameNode() for testing node identity if the DOM implementation
 * supports DOM Level 3 core and it isn't the Xerces implementation.
 */
private boolean useIsSameNode(Node node) {
    if (node instanceof NodeImpl) {
        return false;
    }
    Document doc = node.getNodeType() == Node.DOCUMENT_NODE 
        ? (Document) node : node.getOwnerDocument();
    return (doc != null && doc.getImplementation().hasFeature("Core", "3.0"));
}
 
Example 10
Project: openjdk-jdk10   File: XPathAPI.java   Source Code and License Vote up 5 votes
/**
 *  Evaluate XPath string to an XObject.
 *  XPath namespace prefixes are resolved from the namespaceNode.
 *  The implementation of this is a little slow, since it creates
 *  a number of objects each time it is called.  This could be optimized
 *  to keep the same objects around, but then thread-safety issues would arise.
 *
 *  @param contextNode The node to start searching from.
 *  @param str A valid XPath string.
 *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
 *  @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
 *  @see com.sun.org.apache.xpath.internal.objects.XObject
 *  @see com.sun.org.apache.xpath.internal.objects.XNull
 *  @see com.sun.org.apache.xpath.internal.objects.XBoolean
 *  @see com.sun.org.apache.xpath.internal.objects.XNumber
 *  @see com.sun.org.apache.xpath.internal.objects.XString
 *  @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
 *
 * @throws TransformerException
 */
public static XObject eval(Node contextNode, String str, Node namespaceNode)
        throws TransformerException
{

  // Since we don't have a XML Parser involved here, install some default support
  // for things like namespaces, etc.
  // (Changed from: XPathContext xpathSupport = new XPathContext();
  //    because XPathContext is weak in a number of areas... perhaps
  //    XPathContext should be done away with.)
  XPathContext xpathSupport = new XPathContext();

  // Create an object to resolve namespace prefixes.
  // XPath namespaces are resolved from the input context node's document element
  // if it is a root node, or else the current context node (for lack of a better
  // resolution space, given the simplicity of this sample code).
  PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
    (namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
    ? ((Document) namespaceNode).getDocumentElement() : namespaceNode);

  // Create the XPath object.
  XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);

  // Execute the XPath, and have it return the result
  // return xpath.execute(xpathSupport, contextNode, prefixResolver);
  int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);

  return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}
 
Example 11
Project: openjdk-jdk10   File: DOMSubTreeData.java   Source Code and License Vote up 4 votes
/**
 * Recursively traverses the subtree, and returns an XPath-equivalent
 * node-set of all nodes traversed, excluding any comment nodes,
 * if specified.
 *
 * @param node the node to traverse
 * @param nodeSet the set of nodes traversed so far
 * @param the previous sibling node
 */
@SuppressWarnings("fallthrough")
private void nodeSetMinusCommentNodes(Node node, List<Node> nodeSet,
                                      Node prevSibling)
{
    switch (node.getNodeType()) {
        case Node.ELEMENT_NODE :
            NamedNodeMap attrs = node.getAttributes();
            if (attrs != null) {
                for (int i = 0, len = attrs.getLength(); i < len; i++) {
                    nodeSet.add(attrs.item(i));
                }
            }
            nodeSet.add(node);
            Node pSibling = null;
            for (Node child = node.getFirstChild(); child != null;
                child = child.getNextSibling()) {
                nodeSetMinusCommentNodes(child, nodeSet, pSibling);
                pSibling = child;
            }
            break;
        case Node.DOCUMENT_NODE :
            pSibling = null;
            for (Node child = node.getFirstChild(); child != null;
                child = child.getNextSibling()) {
                nodeSetMinusCommentNodes(child, nodeSet, pSibling);
                pSibling = child;
            }
            break;
        case Node.TEXT_NODE :
        case Node.CDATA_SECTION_NODE:
            // emulate XPath which only returns the first node in
            // contiguous text/cdata nodes
            if (prevSibling != null &&
                (prevSibling.getNodeType() == Node.TEXT_NODE ||
                 prevSibling.getNodeType() == Node.CDATA_SECTION_NODE)) {
                return;
            }
            nodeSet.add(node);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE :
            nodeSet.add(node);
            break;
        case Node.COMMENT_NODE:
            if (withComments) {
                nodeSet.add(node);
            }
    }
}
 
Example 12
Project: patent-crawler   File: DOMBuilder.java   Source Code and License Vote up 4 votes
/**
 * Tell if the current node is outside the document element.
 * 
 * @return true if the current node is outside the document element.
 */
private boolean isOutsideDocElem() {
    return (null == m_docFrag)
            && m_elemStack.size() == 0
            && (null == m_currentNode || m_currentNode.getNodeType() == Node.DOCUMENT_NODE);
}
 
Example 13
Project: jdk8u-jdk   File: XMLUtils.java   Source Code and License Vote up 4 votes
/**
 * This is the work horse for {@link #circumventBug2650}.
 *
 * @param node
 * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
 * Namespace axis resolution is not XPath compliant </A>
 */
@SuppressWarnings("fallthrough")
private static void circumventBug2650internal(Node node) {
    Node parent = null;
    Node sibling = null;
    final String namespaceNs = Constants.NamespaceSpecNS;
    do {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE :
            Element element = (Element) node;
            if (!element.hasChildNodes()) {
                break;
            }
            if (element.hasAttributes()) {
                NamedNodeMap attributes = element.getAttributes();
                int attributesLength = attributes.getLength();

                for (Node child = element.getFirstChild(); child!=null;
                    child = child.getNextSibling()) {

                    if (child.getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    Element childElement = (Element) child;

                    for (int i = 0; i < attributesLength; i++) {
                        Attr currentAttr = (Attr) attributes.item(i);
                        if (!namespaceNs.equals(currentAttr.getNamespaceURI())) {
                            continue;
                        }
                        if (childElement.hasAttributeNS(namespaceNs,
                                                        currentAttr.getLocalName())) {
                            continue;
                        }
                        childElement.setAttributeNS(namespaceNs,
                                                    currentAttr.getName(),
                                                    currentAttr.getNodeValue());
                    }
                }
            }
        case Node.ENTITY_REFERENCE_NODE :
        case Node.DOCUMENT_NODE :
            parent = node;
            sibling = node.getFirstChild();
            break;
        }
        while ((sibling == null) && (parent != null)) {
            sibling = parent.getNextSibling();
            parent = parent.getParentNode();
        }
        if (sibling == null) {
            return;
        }

        node = sibling;
        sibling = node.getNextSibling();
    } while (true);
}
 
Example 14
Project: OpenJSharp   File: XMLUtils.java   Source Code and License Vote up 4 votes
/**
 * This is the work horse for {@link #circumventBug2650}.
 *
 * @param node
 * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
 * Namespace axis resolution is not XPath compliant </A>
 */
@SuppressWarnings("fallthrough")
private static void circumventBug2650internal(Node node) {
    Node parent = null;
    Node sibling = null;
    final String namespaceNs = Constants.NamespaceSpecNS;
    do {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE :
            Element element = (Element) node;
            if (!element.hasChildNodes()) {
                break;
            }
            if (element.hasAttributes()) {
                NamedNodeMap attributes = element.getAttributes();
                int attributesLength = attributes.getLength();

                for (Node child = element.getFirstChild(); child!=null;
                    child = child.getNextSibling()) {

                    if (child.getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    Element childElement = (Element) child;

                    for (int i = 0; i < attributesLength; i++) {
                        Attr currentAttr = (Attr) attributes.item(i);
                        if (!namespaceNs.equals(currentAttr.getNamespaceURI())) {
                            continue;
                        }
                        if (childElement.hasAttributeNS(namespaceNs,
                                                        currentAttr.getLocalName())) {
                            continue;
                        }
                        childElement.setAttributeNS(namespaceNs,
                                                    currentAttr.getName(),
                                                    currentAttr.getNodeValue());
                    }
                }
            }
        case Node.ENTITY_REFERENCE_NODE :
        case Node.DOCUMENT_NODE :
            parent = node;
            sibling = node.getFirstChild();
            break;
        }
        while ((sibling == null) && (parent != null)) {
            sibling = parent.getNextSibling();
            parent = parent.getParentNode();
        }
        if (sibling == null) {
            return;
        }

        node = sibling;
        sibling = node.getNextSibling();
    } while (true);
}
 
Example 15
Project: OpenJSharp   File: DOMSerializerImpl.java   Source Code and License Vote up 4 votes
private void prepareForSerialization(XMLSerializer ser, Node node) {
    ser.reset();
    ser.features = features;
    ser.fDOMErrorHandler = fErrorHandler;
    ser.fNamespaces = (features & NAMESPACES) != 0;
    ser.fNamespacePrefixes = (features & NSDECL) != 0;
    ser._format.setOmitComments((features & COMMENTS)==0);
    ser._format.setOmitXMLDeclaration((features & XMLDECL) == 0);
    ser._format.setIndenting((features & FORMAT_PRETTY_PRINT) != 0);

    if ((features & WELLFORMED) != 0) {
        // REVISIT: this is inefficient implementation of well-formness. Instead, we should check
        // well-formness as we serialize the tree
        Node next, root;
        root = node;
        Method versionChanged;
        boolean verifyNames = true;
        Document document =(node.getNodeType() == Node.DOCUMENT_NODE)
                ? (Document) node
                : node.getOwnerDocument();
        try {
            versionChanged = document.getClass().getMethod("isXMLVersionChanged()", new Class[] {});
            if (versionChanged != null) {
                verifyNames = ((Boolean)versionChanged.invoke(document, (Object[]) null)).booleanValue();
            }
        } catch (Exception e) {
            //no way to test the version...
            //ignore the exception
        }
        if (node.getFirstChild() != null) {
            while (node != null) {
                verify(node, verifyNames, false);
                // Move down to first child
                next = node.getFirstChild();
                // No child nodes, so walk tree
                while (next == null) {
                  // Move to sibling if possible.
                  next = node.getNextSibling();
                  if (next == null) {
                      node = node.getParentNode();
                      if (root == node){
                          next = null;
                          break;
                      }
                      next = node.getNextSibling();
                  }
                }
                node = next;
            }
        }
        else {
            verify(node, verifyNames, false);
        }
    }
}
 
Example 16
Project: openjdk-jdk10   File: ReferenceSubTreeData.java   Source Code and License Vote up 4 votes
/**
 * Recursively traverses the subtree, and returns an XPath-equivalent
 * node-set of all nodes traversed, excluding any comment nodes,
 * if specified.
 *
 * @param node the node to traverse
 * @param nodeSet the set of nodes traversed so far
 * @param the previous sibling node
 */
@SuppressWarnings("fallthrough")
private void nodeSetMinusCommentNodes(Node node, List<Node> nodeSet,
                                      Node prevSibling)
{
    switch (node.getNodeType()) {
        case Node.ELEMENT_NODE :
            nodeSet.add(node);
            NamedNodeMap attrs = node.getAttributes();
            if (attrs != null) {
                for (int i = 0, len = attrs.getLength(); i < len; i++) {
                    nodeSet.add(attrs.item(i));
                }
            }
            Node pSibling = null;
            for (Node child = node.getFirstChild(); child != null;
                child = child.getNextSibling()) {
                nodeSetMinusCommentNodes(child, nodeSet, pSibling);
                pSibling = child;
            }
            break;
        case Node.DOCUMENT_NODE :
            pSibling = null;
            for (Node child = node.getFirstChild(); child != null;
                child = child.getNextSibling()) {
                nodeSetMinusCommentNodes(child, nodeSet, pSibling);
                pSibling = child;
            }
            break;
        case Node.TEXT_NODE :
        case Node.CDATA_SECTION_NODE:
            // emulate XPath which only returns the first node in
            // contiguous text/cdata nodes
            if (prevSibling != null &&
                (prevSibling.getNodeType() == Node.TEXT_NODE ||
                 prevSibling.getNodeType() == Node.CDATA_SECTION_NODE)) {
                return;
            }
            nodeSet.add(node);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE :
            nodeSet.add(node);
            break;
        case Node.COMMENT_NODE:
            if (withComments) {
                nodeSet.add(node);
            }
    }
}
 
Example 17
Project: OpenJSharp   File: DOM2DTM.java   Source Code and License Vote up 4 votes
/**
   * Retrieve the text content of a DOM subtree, appending it into a
   * user-supplied FastStringBuffer object. Note that attributes are
   * not considered part of the content of an element.
   * <p>
   * There are open questions regarding whitespace stripping.
   * Currently we make no special effort in that regard, since the standard
   * DOM doesn't yet provide DTD-based information to distinguish
   * whitespace-in-element-context from genuine #PCDATA. Note that we
   * should probably also consider xml:space if/when we address this.
   * DOM Level 3 may solve the problem for us.
   * <p>
   * %REVIEW% Note that as a DOM-level operation, it can be argued that this
   * routine _shouldn't_ perform any processing beyond what the DOM already
   * does, and that whitespace stripping and so on belong at the DTM level.
   * If you want a stripped DOM view, wrap DTM2DOM around DOM2DTM.
   *
   * @param node Node whose subtree is to be walked, gathering the
   * contents of all Text or CDATASection nodes.
   */
  protected static void dispatchNodeData(Node node,
                                         org.xml.sax.ContentHandler ch,
                                         int depth)
            throws org.xml.sax.SAXException
  {

    switch (node.getNodeType())
    {
    case Node.DOCUMENT_FRAGMENT_NODE :
    case Node.DOCUMENT_NODE :
    case Node.ELEMENT_NODE :
    {
      for (Node child = node.getFirstChild(); null != child;
              child = child.getNextSibling())
      {
        dispatchNodeData(child, ch, depth+1);
      }
    }
    break;
    case Node.PROCESSING_INSTRUCTION_NODE : // %REVIEW%
    case Node.COMMENT_NODE :
      if(0 != depth)
        break;
        // NOTE: Because this operation works in the DOM space, it does _not_ attempt
        // to perform Text Coalition. That should only be done in DTM space.
    case Node.TEXT_NODE :
    case Node.CDATA_SECTION_NODE :
    case Node.ATTRIBUTE_NODE :
      String str = node.getNodeValue();
      if(ch instanceof CharacterNodeHandler)
      {
        ((CharacterNodeHandler)ch).characters(node);
      }
      else
      {
        ch.characters(str.toCharArray(), 0, str.length());
      }
      break;
//    /* case Node.PROCESSING_INSTRUCTION_NODE :
//      // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);
//      break; */
    default :
      // ignore
      break;
    }
  }
 
Example 18
Project: jdk8u-jdk   File: DOMSubTreeData.java   Source Code and License Vote up 4 votes
/**
 * Recursively traverses the subtree, and returns an XPath-equivalent
 * node-set of all nodes traversed, excluding any comment nodes,
 * if specified.
 *
 * @param node the node to traverse
 * @param nodeSet the set of nodes traversed so far
 * @param the previous sibling node
 */
@SuppressWarnings("fallthrough")
private void nodeSetMinusCommentNodes(Node node, List<Node> nodeSet,
                                      Node prevSibling)
{
    switch (node.getNodeType()) {
        case Node.ELEMENT_NODE :
            NamedNodeMap attrs = node.getAttributes();
            if (attrs != null) {
                for (int i = 0, len = attrs.getLength(); i < len; i++) {
                    nodeSet.add(attrs.item(i));
                }
            }
            nodeSet.add(node);
            Node pSibling = null;
            for (Node child = node.getFirstChild(); child != null;
                child = child.getNextSibling()) {
                nodeSetMinusCommentNodes(child, nodeSet, pSibling);
                pSibling = child;
            }
            break;
        case Node.DOCUMENT_NODE :
            pSibling = null;
            for (Node child = node.getFirstChild(); child != null;
                child = child.getNextSibling()) {
                nodeSetMinusCommentNodes(child, nodeSet, pSibling);
                pSibling = child;
            }
            break;
        case Node.TEXT_NODE :
        case Node.CDATA_SECTION_NODE:
            // emulate XPath which only returns the first node in
            // contiguous text/cdata nodes
            if (prevSibling != null &&
                (prevSibling.getNodeType() == Node.TEXT_NODE ||
                 prevSibling.getNodeType() == Node.CDATA_SECTION_NODE)) {
                return;
            }
            nodeSet.add(node);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE :
            nodeSet.add(node);
            break;
        case Node.COMMENT_NODE:
            if (withComments) {
                nodeSet.add(node);
            }
    }
}
 
Example 19
Project: openjdk-jdk10   File: RangeImpl.java   Source Code and License Vote up 4 votes
public void surroundContents(Node newParent)
    throws DOMException, RangeException
{
    if (newParent==null) return;
    int type = newParent.getNodeType();

    if (fDocument.errorChecking) {
        if (fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if (type == Node.ATTRIBUTE_NODE
                || type == Node.ENTITY_NODE
                || type == Node.NOTATION_NODE
                || type == Node.DOCUMENT_TYPE_NODE
                || type == Node.DOCUMENT_NODE
                || type == Node.DOCUMENT_FRAGMENT_NODE)
        {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
    }

    Node realStart = fStartContainer;
    Node realEnd = fEndContainer;
    if (fStartContainer.getNodeType() == Node.TEXT_NODE) {
        realStart = fStartContainer.getParentNode();
    }
    if (fEndContainer.getNodeType() == Node.TEXT_NODE) {
        realEnd = fEndContainer.getParentNode();
    }

    if (realStart != realEnd) {
            throw new RangeExceptionImpl(
            RangeException.BAD_BOUNDARYPOINTS_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "BAD_BOUNDARYPOINTS_ERR", null));
    }

    DocumentFragment frag = extractContents();
    insertNode(newParent);
    newParent.appendChild(frag);
    selectNode(newParent);
}
 
Example 20
Project: openjdk-jdk10   File: DOM3TreeWalker.java   Source Code and License Vote up 4 votes
/**
 * Start processing given node
 *
 * @param node Node to process
 *
 * @throws org.xml.sax.SAXException
 */
protected void startNode(Node node) throws org.xml.sax.SAXException {
    if (node instanceof Locator) {
        Locator loc = (Locator) node;
        fLocator.setColumnNumber(loc.getColumnNumber());
        fLocator.setLineNumber(loc.getLineNumber());
        fLocator.setPublicId(loc.getPublicId());
        fLocator.setSystemId(loc.getSystemId());
    } else {
        fLocator.setColumnNumber(0);
        fLocator.setLineNumber(0);
    }

    switch (node.getNodeType()) {
        case Node.DOCUMENT_TYPE_NODE :
            serializeDocType((DocumentType) node, true);
            break;
        case Node.COMMENT_NODE :
            serializeComment((Comment) node);
            break;
        case Node.DOCUMENT_FRAGMENT_NODE :
            // Children are traversed
            break;
        case Node.DOCUMENT_NODE :
            break;
        case Node.ELEMENT_NODE :
            serializeElement((Element) node, true);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE :
            serializePI((ProcessingInstruction) node);
            break;
        case Node.CDATA_SECTION_NODE :
            serializeCDATASection((CDATASection) node);
            break;
        case Node.TEXT_NODE :
            serializeText((Text) node);
            break;
        case Node.ENTITY_REFERENCE_NODE :
            serializeEntityReference((EntityReference) node, true);
            break;
        default :
            }
}