Java Code Examples for org.w3c.dom.Node#isEqualNode()

The following examples show how to use org.w3c.dom.Node#isEqualNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParentNode.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * DOM Level 3 WD- Experimental.
 * Override inherited behavior from NodeImpl to support deep equal.
 */
public boolean isEqualNode(Node arg) {
    if (!super.isEqualNode(arg)) {
        return false;
    }
    // there are many ways to do this test, and there isn't any way
    // better than another. Performance may vary greatly depending on
    // the implementations involved. This one should work fine for us.
    Node child1 = getFirstChild();
    Node child2 = arg.getFirstChild();
    while (child1 != null && child2 != null) {
        if (!child1.isEqualNode(child2)) {
            return false;
        }
        child1 = child1.getNextSibling();
        child2 = child2.getNextSibling();
    }
    if (child1 != child2) {
        return false;
    }
    return true;
}
 
Example 2
Source File: XMLCompareUtils.java    From XPagesExtensionLibrary with Apache License 2.0 6 votes vote down vote up
/**
 * This method will compare the attributes of a given src element with the attributes of a given dest element.
 * Both must contain the same attributes, but you can specify a String array of attribute names whose values
 * are ignored. Both elements must have the ignore attributes, their contents can be different and they will
 * still be considered equal.
 * @param src - the src element whose attributes are to be compared
 * @param dest - the dest element whose attributes are to be compared
 * @param ignore - the string array of attributes whose values are to be ignored during the compare.
 * @return true if the attributes of both nodes meet the criteria of being equal, false otherwise.
 */
private static boolean compareAttributes (Element src, Element dest, String[] ignore) {
    NamedNodeMap srcAttrs = src.getAttributes();
    
    if (srcAttrs.getLength() != dest.getAttributes().getLength())
        return false;
    
    for (int ctr=0; ctr<srcAttrs.getLength(); ctr++) {
        Node srcAttr = srcAttrs.item(ctr);
        
        String name = srcAttr.getNodeName();
        if (Arrays.binarySearch(ignore, name) < 0) {
            Node destAttr = dest.getAttributeNode(name);
            if (destAttr == null || !srcAttr.isEqualNode(destAttr)) {
                return false;
            }
        }
    }
    
    return true;
}
 
Example 3
Source File: SVGNamedNodeMap.java    From latexdraw with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean equals(final Object obj) {
	if(this == obj) {
		return true;
	}
	if(!(obj instanceof SVGNamedNodeMap)) {
		return false;
	}

	final SVGNamedNodeMap map = (SVGNamedNodeMap) obj;
	boolean ok = true;
	int i;
	final int size = getLength();

	if(size != map.getLength()) {
		return false;
	}

	for(i = 0; i < size && ok; i++) {
		final Node item = item(i);
		ok = item != null && item.isEqualNode(map.item(i));
	}

	return ok;
}
 
Example 4
Source File: EnvelopedSignatureBuilder.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected void incorporateSignatureDom(Node parentNodeOfSignature) {
    if (params.getXPathElementPlacement() == null || Utils.isStringEmpty(params.getXPathLocationString())) {
	parentNodeOfSignature.appendChild(signatureDom);
	return;
    }

    switch (params.getXPathElementPlacement()) {
	    case XPathAfter:
		    // root element referenced by XPath
		    if (parentNodeOfSignature.isEqualNode(documentDom.getDocumentElement())) { 
			    // append signature at end of document
			    parentNodeOfSignature.appendChild(signatureDom);
			    
		    } else {
			    // insert signature before next sibling or as last child
			    // if no sibling exists
			    Node parent = parentNodeOfSignature.getParentNode();
			    parent.insertBefore(signatureDom, parentNodeOfSignature.getNextSibling());
		    }

		    break;
	    case XPathFirstChildOf:
		    parentNodeOfSignature.insertBefore(signatureDom, parentNodeOfSignature.getFirstChild());
		    break;
	    default:
		    parentNodeOfSignature.appendChild(signatureDom);
		    break;
    }
}
 
Example 5
Source File: DavResourceUtils.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * XMLのレスポンスに期待するノード情報と同じ情報が含まれるかどうかをチェックする.
 * @param res PROPFINDレスポンス
 * @param tagName チェック対象となるタグ名
 * @param expectedNode 期待するノード情報
 */
public static void assertEqualsNodeInResXml(TResponse res, String tagName, Node expectedNode) {
    Document propfind = res.bodyAsXml();
    NodeList list;
    list = propfind.getElementsByTagName(tagName);
    for (int i = 0; i < list.getLength(); i++) {
        Node item = list.item(i);
        if (item.isEqualNode(expectedNode)) {
            return;
        }
    }
    // 指定されたタグが含まれない場合はテスト失敗とする
    fail();
}
 
Example 6
Source File: NodeImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private boolean namedNodeMapsEqual(NamedNodeMap a, NamedNodeMap b) {
    if (a.getLength() != b.getLength()) {
        return false;
    }
    for (int i = 0; i < a.getLength(); i++) {
        Node aNode = a.item(i);
        Node bNode = aNode.getLocalName() == null
                ? b.getNamedItem(aNode.getNodeName())
                : b.getNamedItemNS(aNode.getNamespaceURI(), aNode.getLocalName());
        if (bNode == null || !aNode.isEqualNode(bNode)) {
            return false;
        }
    }
    return true;
}
 
Example 7
Source File: XMLCompareUtils.java    From XPagesExtensionLibrary with Apache License 2.0 4 votes vote down vote up
/**
   * This method will check that two nodes are equal. 
   * For nodes to be considered equal.
   * 		The nodes must be of the same type
* 		The following node properties must be equal: 
*			nodeName, 
*			localName, 
*			namespaceURI, 
*			prefix, 
*			nodeValue
*		The attributes NamedNodeMaps must be equal (attributes in ignoreAttrs are ignored)
*		The childNodes NodeLists are equal. i.e. both nodes have exactly the same children, but in any order
*			Any child nodes that are in the ignoreContentsOfNodes array will be considered equal as long they are present in both. 
*			The contents of nodes in ignoreNodes can be different
   * @param srcNode
   * @param destNode
   * @param ignoreContentsOfNodes
   * @param ignoreAttrs
   * @return
   */
  private static boolean areIndividualNodeEqual(Node srcNode, Node destNode, String[] ignoreContentsOfNodes, String[] ignoreAttrs){
  	if(null != srcNode && null !=destNode){
   	//try normal node compare first. Takes all the effort out if this check passes, but we will fail this
   	//test a lot of the time.
   	if(srcNode.isEqualNode(destNode)){
   		return true;
   	}
   
   	//verify that the node types are the same
   	if(srcNode.getNodeType() == destNode.getNodeType()){
   	
    	String srcNodeName = srcNode.getNodeName();
    	String srcLocalName = srcNode.getLocalName();
    	String srcNamespaceURI = srcNode.getNamespaceURI();
    	String srcPrefix = srcNode.getPrefix();
    	String srcNodeValue = srcNode.getNodeValue();
    	
    	String destNodeName = destNode.getNodeName();
    	String destLocalName = destNode.getLocalName();
    	String destNamespaceURI = destNode.getNamespaceURI();
    	String destPrefix = destNode.getPrefix();
    	String destNodeValue = destNode.getNodeValue();
   	
    	//verify that the node properties are all the same
    	if(StringUtil.equals(srcNodeName, destNodeName)
    			&& StringUtil.equals(srcLocalName, destLocalName)
    			&& StringUtil.equals(srcNamespaceURI, destNamespaceURI)
    			&& StringUtil.equals(srcPrefix, destPrefix)
    			&& StringUtil.equals(srcNodeValue, destNodeValue)
    			){
    		//if these are element Nodes check their attributes (only element nodes have attributes). 
    		if(srcNode instanceof Element && destNode instanceof Element){
    			//if this is one of our ignore nodes we pass automatically. Otherwise we pass to compare attributes
    			//to see if the attributes match given our array of ignore attributes
	    		if((Arrays.binarySearch(ignoreContentsOfNodes, srcNodeName) >= 0 
    				|| compareAttributes((Element)srcNode, (Element)destNode, ignoreAttrs))){
	    			//we have passed the attributes test. 
	    			return true;
	    		}
    		}
    		else{
    			//if the nodes are not elements, then they do not have attributes to check, so they must be equal
    			return true;
    		}
    		
    	}
   	}
  	}
  	return false;
  }