deepdiff.scope.XMLAttributeDiffUnit Maven / Gradle / Ivy
The newest version!
/*
* 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 attribute on an XML element
*/
public class XMLAttributeDiffUnit implements DiffUnit {
private XMLNodeDiffUnit elementUnit;
private String name;
private Node an1;
private Node an2;
/**
* Initializes a new XMLAttributeDiffUnit object.
*
* @param elementUnit the DiffUnit for the element
* @param name the name of the attribute
* @param an1 the attribute node on the left
* @param an2 the attribute node on the right
*/
public XMLAttributeDiffUnit(XMLNodeDiffUnit elementUnit, String name,
Node an1, Node an2) {
// TODO: clean up DiffUnit abstraction
// That way, non-File and InputStream based units don't have to
// implement meaningless methods
this.elementUnit = elementUnit;
this.name = name;
this.an1 = an1;
this.an2 = an2;
}
/**
* Returns whether the attribute on the left exists
*
* @return whether the attribute on the left exists
*/
public boolean leftExists() {
return an1 != null;
}
/**
* Returns whether the attribute on the right exists
*
* @return whether the attribute on the right exists
*/
public boolean rightExists() {
return an2 != null;
}
/**
* Returns false, since an attribute node can never have children
*
* @return false
*/
public boolean leftIsDir() {
return false;
}
/**
* Returns false, since an attribute node can never have children
*
* @return false
*/
public boolean rightIsDir() {
return false;
}
/**
* Always throws an exception, since attributes do not contain content
*
* @return never
*
* @throws FileNotFoundException always
*/
public InputStream getLeftInputStream() throws FileNotFoundException {
throw new FileNotFoundException("Attributes do not contain content");
}
/**
* Always throws an exception, since attributes do not contain content
*
* @return never
*
* @throws FileNotFoundException always
*/
public InputStream getRightInputStream() throws FileNotFoundException {
throw new FileNotFoundException("Attributes do not contain content");
}
/**
* Returns the scoped path for the unit. This includes the scoped path for
* the element, followed by an "at" sign and the name of the attribute
*
* @return the scoped path for the unit
*
* @see XMLNodeDiffUnit#getScopedPath()
*/
public String getScopedPath() {
String elementPath = elementUnit.getScopedPath();
String name = null;
if (an1 != null) {
name = an1.getNodeName();
} else if (an2 != null) {
name = an2.getNodeName();
}
String scopedPath = elementPath + "/@" + name;
return scopedPath;
}
/**
* Returns the attribute node on the left
*
* @return the attribute node on the left
*/
public Node getLeftAttributeNode() {
return an1;
}
/**
* Returns the attribute node on the right
*
* @return the attribute node on the right
*/
public Node getRightAttributeNode() {
return an2;
}
/**
* Returns the attribute name
*
* @return the attribute name
*/
public String getAttributeName() {
return name;
}
}