Class ElementSelectors
- java.lang.Object
-
- org.xmlunit.diff.ElementSelectors
-
public final class ElementSelectors extends java.lang.Object
Common ElementSelector implementations.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ElementSelectors.CanBeComparedPredicate
static interface
ElementSelectors.ConditionalSelectorBuilder
Allows to build complexElementSelector
s by combining simpler blocks.static interface
ElementSelectors.ConditionalSelectorBuilderThen
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
ATTRIBUTES_MUST_NOT_BE_NULL
private static java.lang.String
ATTRIBUTES_MUST_NOT_CONTAIN_NULL_VALUES
static ElementSelector
byName
Elements with the same local name (and namespace URI - if any) can be compared.static ElementSelector
byNameAndAllAttributes
Elements with the same local name (and namespace URI - if any) and attribute values for all attributes can be compared.static ElementSelector
byNameAndText
Elements with the same local name (and namespace URI - if any) and nested text (if any) can be compared.static ElementSelector
Default
Always returns true, i.e.private static java.lang.String
SELECTORS_MUST_NOT_BE_NULL
String Constants.(package private) static Mapper<org.w3c.dom.Node,XPathContext.NodeInfo>
TO_NODE_INFO
Maps Nodes to their NodeInfo equivalent.
-
Constructor Summary
Constructors Modifier Constructor Description private
ElementSelectors()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ElementSelector
and(ElementSelector... selectors)
Accepts two elements if all of the given ElementSelectors do.private static boolean
bothNullOrEqual(java.lang.Object o1, java.lang.Object o2)
static ElementSelector
byNameAndAttributes(java.lang.String... attribs)
Elements with the same local name (and namespace URI - if any) and attribute values for the given attribute names can be compared.static ElementSelector
byNameAndAttributes(javax.xml.namespace.QName... attribs)
Elements with the same local name (and namespace URI - if any) and attribute values for the given attribute names can be compared.static ElementSelector
byNameAndAttributesControlNS(java.lang.String... attribs)
Elements with the same local name (and namespace URI - if any) and attribute values for the given attribute names can be compared.static ElementSelector
byXPath(java.lang.String xpath, java.util.Map<java.lang.String,java.lang.String> prefix2Uri, ElementSelector childSelector)
Selects two elements as matching if the child elements selected via XPath match using the given childSelector.static ElementSelector
byXPath(java.lang.String xpath, ElementSelector childSelector)
Selects two elements as matching if the child elements selected via XPath match using the given childSelector.static ElementSelectors.ConditionalSelectorBuilder
conditionalBuilder()
Allows to build complexElementSelector
s by combining simpler blocks.static ElementSelector
conditionalSelector(Predicate<? super org.w3c.dom.Element> predicate, ElementSelector es)
Applies the wrapped ElementSelector's logic if and only if the control element matches the given predicate.(package private) static Predicate<org.w3c.dom.Element>
elementNamePredicate(java.lang.String expectedName)
(package private) static Predicate<org.w3c.dom.Element>
elementNamePredicate(javax.xml.namespace.QName expectedName)
private static boolean
mapsEqualForKeys(java.util.Map<javax.xml.namespace.QName,java.lang.String> control, java.util.Map<javax.xml.namespace.QName,java.lang.String> test, java.lang.Iterable<javax.xml.namespace.QName> keys)
static ElementSelector
not(ElementSelector es)
Negates another ElementSelector.static ElementSelector
or(ElementSelector... selectors)
Accepts two elements if at least one of the given ElementSelectors does.static ElementSelector
selectorForElementNamed(java.lang.String expectedName, ElementSelector es)
Applies the wrapped ElementSelector's logic if and only if the control element has the given (local) name.static ElementSelector
selectorForElementNamed(javax.xml.namespace.QName expectedName, ElementSelector es)
Applies the wrapped ElementSelector's logic if and only if the control element has the given name.static ElementSelector
xor(ElementSelector es1, ElementSelector es2)
Accepts two elements if exactly on of the given ElementSelectors does.
-
-
-
Field Detail
-
Default
public static final ElementSelector Default
Always returns true, i.e. each element can be compared to each other element.Generally this means elements will be compared in document order.
-
byName
public static final ElementSelector byName
Elements with the same local name (and namespace URI - if any) can be compared.
-
byNameAndText
public static final ElementSelector byNameAndText
Elements with the same local name (and namespace URI - if any) and nested text (if any) can be compared.
-
byNameAndAllAttributes
public static final ElementSelector byNameAndAllAttributes
Elements with the same local name (and namespace URI - if any) and attribute values for all attributes can be compared.
-
SELECTORS_MUST_NOT_BE_NULL
private static final java.lang.String SELECTORS_MUST_NOT_BE_NULL
String Constants.- See Also:
- Constant Field Values
-
ATTRIBUTES_MUST_NOT_CONTAIN_NULL_VALUES
private static final java.lang.String ATTRIBUTES_MUST_NOT_CONTAIN_NULL_VALUES
- See Also:
- Constant Field Values
-
ATTRIBUTES_MUST_NOT_BE_NULL
private static final java.lang.String ATTRIBUTES_MUST_NOT_BE_NULL
- See Also:
- Constant Field Values
-
TO_NODE_INFO
static final Mapper<org.w3c.dom.Node,XPathContext.NodeInfo> TO_NODE_INFO
Maps Nodes to their NodeInfo equivalent.
-
-
Method Detail
-
byNameAndAttributes
public static ElementSelector byNameAndAttributes(java.lang.String... attribs)
Elements with the same local name (and namespace URI - if any) and attribute values for the given attribute names can be compared.Attributes are only searched for in the null namespace.
-
byNameAndAttributesControlNS
public static ElementSelector byNameAndAttributesControlNS(java.lang.String... attribs)
Elements with the same local name (and namespace URI - if any) and attribute values for the given attribute names can be compared.Namespace URIs of attributes are those of the attributes on the control element or the null namespace if they don't exist.
-
byNameAndAttributes
public static ElementSelector byNameAndAttributes(javax.xml.namespace.QName... attribs)
Elements with the same local name (and namespace URI - if any) and attribute values for the given attribute names can be compared.
-
not
public static ElementSelector not(ElementSelector es)
Negates another ElementSelector.
-
or
public static ElementSelector or(ElementSelector... selectors)
Accepts two elements if at least one of the given ElementSelectors does.There is an important difference between using
or(org.xmlunit.diff.ElementSelector...)
to combine multipleElementSelector
s and usingDefaultNodeMatcher
's constructor with multipleElementSelector
s:Consider
ElementSelector
se1
ande2
and two control and test nodes each. Assumee1
would match the first control node to the second test node and vice versa if used alone, whilee2
would match the nodes in order (the first control node to the first test and so on).or(org.xmlunit.diff.ElementSelector...)
creates a combinedElementSelector
that is willing to match the first control node to both of the test nodes - and the same for the second control node. Since nodes are compared in order when possible the result will be the same as runninge2
alone.DefaultNodeMatcher
with twoElementSelector
s will consult theElementSelector
s separately and only invokee2
if there are any nodes not matched bye1
at all. In this case the result will be the same as runninge1
alone.
-
and
public static ElementSelector and(ElementSelector... selectors)
Accepts two elements if all of the given ElementSelectors do.
-
xor
public static ElementSelector xor(ElementSelector es1, ElementSelector es2)
Accepts two elements if exactly on of the given ElementSelectors does.
-
conditionalSelector
public static ElementSelector conditionalSelector(Predicate<? super org.w3c.dom.Element> predicate, ElementSelector es)
Applies the wrapped ElementSelector's logic if and only if the control element matches the given predicate.
-
selectorForElementNamed
public static ElementSelector selectorForElementNamed(java.lang.String expectedName, ElementSelector es)
Applies the wrapped ElementSelector's logic if and only if the control element has the given (local) name.
-
selectorForElementNamed
public static ElementSelector selectorForElementNamed(javax.xml.namespace.QName expectedName, ElementSelector es)
Applies the wrapped ElementSelector's logic if and only if the control element has the given name.
-
byXPath
public static ElementSelector byXPath(java.lang.String xpath, ElementSelector childSelector)
Selects two elements as matching if the child elements selected via XPath match using the given childSelector.The xpath expression should yield elements. Two elements match if a DefaultNodeMatcher applied to the selected children finds matching pairs for all children.
- Parameters:
xpath
- XPath expression applied in the context of the elements to chose from that selects the children to compare.childSelector
- ElementSelector to apply to the selected children.
-
byXPath
public static ElementSelector byXPath(java.lang.String xpath, java.util.Map<java.lang.String,java.lang.String> prefix2Uri, ElementSelector childSelector)
Selects two elements as matching if the child elements selected via XPath match using the given childSelector.The xpath expression should yield elements. Two elements match if a DefaultNodeMatcher applied to the selected children finds matching pairs for all children.
- Parameters:
xpath
- XPath expression applied in the context of the elements to chose from that selects the children to compare.prefix2Uri
- maps from prefix to namespace URI.childSelector
- ElementSelector to apply to the selected children.
-
conditionalBuilder
public static ElementSelectors.ConditionalSelectorBuilder conditionalBuilder()
Allows to build complexElementSelector
s by combining simpler blocks.All pairs created by the
when*
/thenUse
pairs are evaluated in order until one returns true, finally thedefault
, if any, is consulted.
-
bothNullOrEqual
private static boolean bothNullOrEqual(java.lang.Object o1, java.lang.Object o2)
-
mapsEqualForKeys
private static boolean mapsEqualForKeys(java.util.Map<javax.xml.namespace.QName,java.lang.String> control, java.util.Map<javax.xml.namespace.QName,java.lang.String> test, java.lang.Iterable<javax.xml.namespace.QName> keys)
-
elementNamePredicate
static Predicate<org.w3c.dom.Element> elementNamePredicate(java.lang.String expectedName)
-
elementNamePredicate
static Predicate<org.w3c.dom.Element> elementNamePredicate(javax.xml.namespace.QName expectedName)
-
-