Package org.custommonkey.xmlunit
Class SimpleXpathEngine
- java.lang.Object
-
- org.custommonkey.xmlunit.SimpleXpathEngine
-
- All Implemented Interfaces:
XMLConstants
,XpathEngine
,XSLTConstants
public class SimpleXpathEngine extends java.lang.Object implements XpathEngine, XSLTConstants
Simple class for accessing the Nodes matched by an Xpath expression, or evaluating the String value of an Xpath expression. Uses acopy-of
orvalue-of
XSL template (as appropriate) to execute the Xpath. This is not an efficient method for accessing XPaths but it is portable across underlying transform implementations. (Yes I know Jaxen is too, but this approach seemed to be the simplest thing that could possibly work...)
-
-
Field Summary
Fields Modifier and Type Field Description private NamespaceContext
ctx
-
Fields inherited from interface org.custommonkey.xmlunit.XMLConstants
CLOSE_NODE, END_CDATA, END_COMMENT, END_PROCESSING_INSTRUCTION, NULL_NS_URI, OPEN_END_NODE, OPEN_START_NODE, START_CDATA, START_COMMENT, START_DOCTYPE, START_PROCESSING_INSTRUCTION, W3C_XML_SCHEMA_INSTANCE_NO_NAMESPACE_SCHEMA_LOCATION_ATTR, W3C_XML_SCHEMA_INSTANCE_NS_URI, W3C_XML_SCHEMA_INSTANCE_SCHEMA_LOCATION_ATTR, W3C_XML_SCHEMA_NS_URI, XML_DECLARATION, XMLNS_PREFIX, XPATH_ATTRIBUTE_IDENTIFIER, XPATH_CHARACTER_NODE_IDENTIFIER, XPATH_COMMENT_IDENTIFIER, XPATH_NODE_INDEX_END, XPATH_NODE_INDEX_START, XPATH_PROCESSING_INSTRUCTION_IDENTIFIER, XPATH_SEPARATOR
-
Fields inherited from interface org.custommonkey.xmlunit.XSLTConstants
JAVA5_XSLTC_FACTORY_NAME, XSLT_END, XSLT_IDENTITY_TEMPLATE, XSLT_START, XSLT_START_NO_VERSION, XSLT_STRIP_COMMENTS_TEMPLATE, XSLT_STRIP_WHITESPACE, XSLT_XML_OUTPUT_NOINDENT
-
-
Constructor Summary
Constructors Constructor Description SimpleXpathEngine()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
evaluate(java.lang.String select, org.w3c.dom.Document document)
Evaluate the result of executing the specified xpath syntaxselect
expression on the specified documentprivate java.lang.String
getCopyTransformation(java.lang.String select)
org.w3c.dom.NodeList
getMatchingNodes(java.lang.String select, org.w3c.dom.Document document)
Execute the specified xpath syntaxselect
expression on the specified document and return the list of nodes (could have length zero) that matchprivate java.lang.String
getNamespaceDeclarations()
returns namespace declarations for all namespaces known to the current context.private java.lang.String
getValueTransformation(java.lang.String select)
protected org.w3c.dom.Document
getXPathResultAsDocument(java.lang.String select, org.w3c.dom.Document document)
Execute the copy-of transform and return the resulting Document.protected org.w3c.dom.Node
getXPathResultNode(java.lang.String select, org.w3c.dom.Document document)
Testable method to execute the copy-of transform and return the root node of the resulting Document.private java.lang.StringBuilder
getXSLTBase()
What every XSL transform needsprivate void
performTransform(java.lang.String xslt, org.w3c.dom.Document document, javax.xml.transform.Result result)
Perform the actual transformation work requiredvoid
setNamespaceContext(NamespaceContext ctx)
Establish a namespace context.
-
-
-
Field Detail
-
ctx
private NamespaceContext ctx
-
-
Method Detail
-
getXSLTBase
private java.lang.StringBuilder getXSLTBase()
What every XSL transform needs
-
getCopyTransformation
private java.lang.String getCopyTransformation(java.lang.String select)
- Parameters:
select
- an xpath syntaxselect
expression- Returns:
- the
copy-of
transformation
-
getValueTransformation
private java.lang.String getValueTransformation(java.lang.String select)
- Parameters:
select
- an xpath syntaxselect
expression- Returns:
- the
value-of
transformation
-
performTransform
private void performTransform(java.lang.String xslt, org.w3c.dom.Document document, javax.xml.transform.Result result) throws javax.xml.transform.TransformerException, ConfigurationException, XpathException
Perform the actual transformation work required- Parameters:
xslt
-document
-result
-- Throws:
XpathException
javax.xml.transform.TransformerException
ConfigurationException
-
getXPathResultNode
protected org.w3c.dom.Node getXPathResultNode(java.lang.String select, org.w3c.dom.Document document) throws ConfigurationException, javax.xml.transform.TransformerException, XpathException
Testable method to execute the copy-of transform and return the root node of the resulting Document.- Parameters:
select
- the XPath expressiondocument
- the XML source to apply the expression to- Returns:
- the root node of the Document created by the copy-of transform.
- Throws:
ConfigurationException
- if the underlying implementation doesjavax.xml.transform.TransformerException
- if the underlying implementation doesXpathException
- if the underlying implementation does
-
getXPathResultAsDocument
protected org.w3c.dom.Document getXPathResultAsDocument(java.lang.String select, org.w3c.dom.Document document) throws ConfigurationException, javax.xml.transform.TransformerException, XpathException
Execute the copy-of transform and return the resulting Document. Used for XMLTestCase comparison- Parameters:
select
- the XPath expressiondocument
- the XML source to apply the expression to- Returns:
- the Document created by the copy-of transform.
- Throws:
ConfigurationException
- if the underlying implementation doesjavax.xml.transform.TransformerException
- if the underlying implementation doesXpathException
- if the underlying implementation does
-
getMatchingNodes
public org.w3c.dom.NodeList getMatchingNodes(java.lang.String select, org.w3c.dom.Document document) throws ConfigurationException, XpathException
Execute the specified xpath syntaxselect
expression on the specified document and return the list of nodes (could have length zero) that match- Specified by:
getMatchingNodes
in interfaceXpathEngine
- Parameters:
select
- the XPath expressiondocument
- the XML source to apply the expression to- Returns:
- matching nodes
- Throws:
XpathException
- if the underlying implementation doesConfigurationException
-
evaluate
public java.lang.String evaluate(java.lang.String select, org.w3c.dom.Document document) throws ConfigurationException, XpathException
Evaluate the result of executing the specified xpath syntaxselect
expression on the specified document- Specified by:
evaluate
in interfaceXpathEngine
- Parameters:
select
- the XPath expressiondocument
- the XML source to apply the expression to- Returns:
- evaluated result
- Throws:
XpathException
- if the underlying implementation doesConfigurationException
-
setNamespaceContext
public void setNamespaceContext(NamespaceContext ctx)
Description copied from interface:XpathEngine
Establish a namespace context.- Specified by:
setNamespaceContext
in interfaceXpathEngine
- Parameters:
ctx
- the NamespaceContext
-
getNamespaceDeclarations
private java.lang.String getNamespaceDeclarations()
returns namespace declarations for all namespaces known to the current context.
-
-