deepdiff.scope.XMLNodeDiffUnit Maven / Gradle / Ivy
/*
* Copyright 2011 DeepDiff Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package deepdiff.scope;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.w3c.dom.Node;
import deepdiff.core.DiffUnit;
/**
* A DiffUnit for an XML node
*/
public class XMLNodeDiffUnit implements DiffUnit {
private DiffUnit parentUnit;
private int index;
private Node n1;
private Node n2;
/**
* Initializes a new XMLNodeDiffUnit object.
*
* @param parentUnit the DiffUnit for the parent (either another node, or
* the document)
* @param index the index of the node among its siblings
* @param n1 the node on the left
* @param n2 the node on the right
*/
public XMLNodeDiffUnit(DiffUnit parentUnit, int index, Node n1, Node n2) {
this.parentUnit = parentUnit;
this.index = index;
this.n1 = n1;
this.n2 = n2;
}
/**
* Returns whether the node on the left exists
*
* @return whether the node on the left exists
*/
public boolean leftExists() {
return n1 != null;
}
/**
* Returns whether the node on the right exists
*
* @return whether the node on the right exists
*/
public boolean rightExists() {
return n2 != null;
}
/**
* Returns whether the left node can contain children
*
* @return whether the left node can contain children
*/
public boolean leftIsDir() {
return canContainChildren(n1);
}
/**
* Returns whether the right node can contain children
*
* @return whether the right node can contain children
*/
public boolean rightIsDir() {
return canContainChildren(n2);
}
/**
* Returns whether the specified node can contain children
*
* @param node the node to test
*
* @return whether the specified node can contain children
*/
private static boolean canContainChildren(Node node) {
if (node == null) {
return false;
}
int nodeType = node.getNodeType();
switch (nodeType) {
case Node.DOCUMENT_NODE:
case Node.DOCUMENT_FRAGMENT_NODE:
case Node.ELEMENT_NODE:
return true;
default:
return false;
}
}
/**
* Throws exception always; not supported
*
* @return never
*
* @throws FileNotFoundException always
*/
public InputStream getLeftInputStream() throws FileNotFoundException {
throw new FileNotFoundException(
"Treating XML nodes as a stream is not supported");
}
/**
* Throws exception always; not supported
*
* @return never
*
* @throws FileNotFoundException always
*/
public InputStream getRightInputStream() throws FileNotFoundException {
throw new FileNotFoundException(
"Treating XML nodes as a stream is not supported");
}
/**
* Returns the scoped path of the unit. This includes the path of the
* parent, the index of the node among its sibilings, and the node name.
*
* @return the scoped path of the unit
*
* @see XMLDocumentDiffUnit#getScopedPath()
*/
public String getScopedPath() {
String parentPath = parentUnit.getScopedPath();
String name = null;
if (n1 != null) {
name = n1.getNodeName();
} else if (n2 != null) {
name = n2.getNodeName();
}
String scopedPath = parentPath + "/[" + index + "]" + name;
return scopedPath;
}
/**
* Returns the node on the left
*
* @return the node on the left
*/
public Node getLeftNode() {
return n1;
}
/**
* Returns the node on the right
*
* @return the node on the right
*/
public Node getRightNode() {
return n2;
}
}