Java Code Examples for org.w3c.dom.Node.normalize()

The following are Jave code examples for showing how to use normalize() 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: Equella   File: PropBagEx.java   Source Code and License Vote up 6 votes
/**
 * Appends the child nodes of another propbag into the node located by
 * szNodeName
 * 
 * @param szNodeName The path to insert into
 * @param xml The propbag that we want the children of
 */
public void appendChildren(final String szNodeName, final PropBagEx xml)
{
	checkNotAttribute(szNodeName);

	ensureRoot();
	xml.ensureRoot();
	Node oNode = getNodeHelper(szNodeName, false, false);
	if( oNode == null )
	{
		createNode(szNodeName, BLANK);
		oNode = getNodeHelper(szNodeName, false, false);
	}

	final Document doc = oNode.getOwnerDocument();
	Node child = xml.m_elRoot.getFirstChild();
	while( child != null )
	{
		oNode.appendChild(importNode(doc, child, true));
		child = child.getNextSibling();
	}

	// we might have pushed two text nodes together, normalise it
	oNode.normalize();
}
 
Example 2
Project: hybris-integration-intellij-idea-plugin   File: ValidateContextImpl.java   Source Code and License Vote up 5 votes
@Override
public void endElement(final String uri, final String localName, final String qName) {
    final Node last = this.myElements.removeLast();
    this.markLocation(last, END_LOC);

    if (this.myElements.isEmpty()) {
        last.normalize();
    }
}
 
Example 3
Project: OpenJSharp   File: ElementImpl.java   Source Code and License Vote up 4 votes
/**
 * In "normal form" (as read from a source file), there will never be two
 * Text children in succession. But DOM users may create successive Text
 * nodes in the course of manipulating the document. Normalize walks the
 * sub-tree and merges adjacent Texts, as if the DOM had been written out
 * and read back in again. This simplifies implementation of higher-level
 * functions that may want to assume that the document is in standard form.
 * <p>
 * To normalize a Document, normalize its top-level Element child.
 * <p>
 * As of PR-DOM-Level-1-19980818, CDATA -- despite being a subclass of
 * Text -- is considered "markup" and will _not_ be merged either with
 * normal Text or with other CDATASections.
 */
public void normalize() {
    // No need to normalize if already normalized.
    if (isNormalized()) {
        return;
    }
    if (needsSyncChildren()) {
        synchronizeChildren();
    }
    ChildNode kid, next;
    for (kid = firstChild; kid != null; kid = next) {
        next = kid.nextSibling;

        // If kid is a text node, we need to check for one of two
        // conditions:
        //   1) There is an adjacent text node
        //   2) There is no adjacent text node, but kid is
        //      an empty text node.
        if ( kid.getNodeType() == Node.TEXT_NODE )
        {
            // If an adjacent text node, merge it with kid
            if ( next!=null && next.getNodeType() == Node.TEXT_NODE )
            {
                ((Text)kid).appendData(next.getNodeValue());
                removeChild( next );
                next = kid; // Don't advance; there might be another.
            }
            else
            {
                // If kid is empty, remove it
                if ( kid.getNodeValue() == null || kid.getNodeValue().length() == 0 ) {
                    removeChild( kid );
                }
            }
        }

        // Otherwise it might be an Element, which is handled recursively
        else if (kid.getNodeType() == Node.ELEMENT_NODE) {
            kid.normalize();
        }
    }

    // We must also normalize all of the attributes
    if ( attributes!=null )
    {
        for( int i=0; i<attributes.getLength(); ++i )
        {
            Node attr = attributes.item(i);
            attr.normalize();
        }
    }

    // changed() will have occurred when the removeChild() was done,
    // so does not have to be reissued.

    isNormalized(true);
}
 
Example 4
Project: openjdk-jdk10   File: ElementImpl.java   Source Code and License Vote up 4 votes
/**
 * In "normal form" (as read from a source file), there will never be two
 * Text children in succession. But DOM users may create successive Text
 * nodes in the course of manipulating the document. Normalize walks the
 * sub-tree and merges adjacent Texts, as if the DOM had been written out
 * and read back in again. This simplifies implementation of higher-level
 * functions that may want to assume that the document is in standard form.
 * <p>
 * To normalize a Document, normalize its top-level Element child.
 * <p>
 * As of PR-DOM-Level-1-19980818, CDATA -- despite being a subclass of Text
 * -- is considered "markup" and will _not_ be merged either with normal
 * Text or with other CDATASections.
 */
public void normalize() {
    // No need to normalize if already normalized.
    if (isNormalized()) {
        return;
    }
    if (needsSyncChildren()) {
        synchronizeChildren();
    }
    ChildNode kid, next;
    for (kid = firstChild; kid != null; kid = next) {
        next = kid.nextSibling;

        // If kid is a text node, we need to check for one of two
        // conditions:
        //   1) There is an adjacent text node
        //   2) There is no adjacent text node, but kid is
        //      an empty text node.
        if (kid.getNodeType() == Node.TEXT_NODE) {
            // If an adjacent text node, merge it with kid
            if (next != null && next.getNodeType() == Node.TEXT_NODE) {
                ((Text) kid).appendData(next.getNodeValue());
                removeChild(next);
                next = kid; // Don't advance; there might be another.
            } else {
                // If kid is empty, remove it
                if (kid.getNodeValue() == null || kid.getNodeValue().length() == 0) {
                    removeChild(kid);
                }
            }
        } // Otherwise it might be an Element, which is handled recursively
        else if (kid.getNodeType() == Node.ELEMENT_NODE) {
            kid.normalize();
        }
    }

    // We must also normalize all of the attributes
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); ++i) {
            Node attr = attributes.item(i);
            attr.normalize();
        }
    }

    // changed() will have occurred when the removeChild() was done,
    // so does not have to be reissued.
    isNormalized(true);
}