下面列出了javax.xml.xpath.XPathFunction#org.apache.xpath.objects.XObject 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Evaluates an XPath expression to an XObject.
* @param contextNode the node to start searching from
* @param str a valid XPath string
* @param a prefix resolver to use for resolving namespace prefixes, or null
* @return an XObject, which can be used to obtain a string, number, nodelist, etc (should never be {@code null})
* @throws TransformerException if a syntax or other error occurs
*/
private static XObject evaluateXPath(final DomNode contextNode,
final String str, final PrefixResolver prefixResolver) throws TransformerException {
final XPathContext xpathSupport = new XPathContext();
final Node xpathExpressionContext;
if (contextNode.getNodeType() == Node.DOCUMENT_NODE) {
xpathExpressionContext = ((Document) contextNode).getDocumentElement();
}
else {
xpathExpressionContext = contextNode;
}
PrefixResolver resolver = prefixResolver;
if (resolver == null) {
resolver = new HtmlUnitPrefixResolver(xpathExpressionContext);
}
final boolean caseSensitive = contextNode.getPage().hasCaseSensitiveTagNames();
final XPathAdapter xpath = new XPathAdapter(str, null, resolver, null, caseSensitive);
final int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}
/**
* Evaluates an XPath expression to an XObject.
* @param contextNode the node to start searching from
* @param str a valid XPath string
* @param a prefix resolver to use for resolving namespace prefixes, or null
* @return an XObject, which can be used to obtain a string, number, nodelist, etc (should never be {@code null})
* @throws TransformerException if a syntax or other error occurs
*/
private static XObject evaluateXPath(final DomNode contextNode,
final String str, final PrefixResolver prefixResolver) throws TransformerException {
final XPathContext xpathSupport = new XPathContext();
final Node xpathExpressionContext;
if (contextNode.getNodeType() == Node.DOCUMENT_NODE) {
xpathExpressionContext = ((Document) contextNode).getDocumentElement();
}
else {
xpathExpressionContext = contextNode;
}
PrefixResolver resolver = prefixResolver;
if (resolver == null) {
resolver = new HtmlUnitPrefixResolver(xpathExpressionContext);
}
final boolean caseSensitive = contextNode.getPage().hasCaseSensitiveTagNames();
final boolean attributeCaseSensitive = caseSensitive
|| contextNode.getPage().getWebClient()
.getBrowserVersion().hasFeature(XPATH_ATTRIBUTE_CASE_SENSITIVE);
final XPathAdapter xpath = new XPathAdapter(str, null, resolver, null, caseSensitive, attributeCaseSensitive);
final int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}
protected boolean evaluate(InputSource inputSource) {
try {
DocumentBuilder dbuilder = createDocumentBuilder();
Document doc = dbuilder.parse(inputSource);
//An XPath expression could return a true or false value instead of a node.
//eval() is a better way to determine the boolean value of the exp.
//For compliance with legacy behavior where selecting an empty node returns true,
//selectNodeIterator is attempted in case of a failure.
CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
XObject result = cachedXPathAPI.eval(doc, xpath);
if (result.bool())
return true;
else {
NodeIterator iterator = cachedXPathAPI.selectNodeIterator(doc, xpath);
return (iterator.nextNode() != null);
}
} catch (Throwable e) {
return false;
}
}
/**
* Generate the EXSLT function return value, and assign it to the variable
* index slot assigned for it in ElemExsltFunction compose().
*
*/
public void execute(TransformerImpl transformer) throws TransformerException
{
XPathContext context = transformer.getXPathContext();
// Verify that result has not already been set by another result
// element. Recursion is allowed: intermediate results are cleared
// in the owner ElemExsltFunction execute().
if (transformer.currentFuncResultSeen()) {
throw new TransformerException("An EXSLT function cannot set more than one result!");
}
int sourceNode = context.getCurrentNode();
// Set the return value;
XObject var = getValue(transformer, sourceNode);
transformer.popCurrentFuncResult();
transformer.pushCurrentFuncResult(var);
}
/**
* Internal -- push the global variables from the Stylesheet onto
* the context's runtime variable stack.
* <p>If we encounter a variable
* that is already defined in the variable stack, we ignore it. This
* is because the second variable definition will be at a lower import
* precedence. Presumably, global"variables at the same import precedence
* with the same name will have been caught during the recompose process.
* <p>However, if we encounter a parameter that is already defined in the
* variable stack, we need to see if this is a parameter whose value was
* supplied by a setParameter call. If so, we need to "receive" the one
* already in the stack, ignoring this one. If it is just an earlier
* xsl:param or xsl:variable definition, we ignore it using the same
* reasoning as explained above for the variable.
*
* @param contextNode The root of the source tree, can't be null.
*
* @throws TransformerException
*/
protected void pushGlobalVars(int contextNode) throws TransformerException
{
XPathContext xctxt = m_xcontext;
VariableStack vs = xctxt.getVarStack();
StylesheetRoot sr = getStylesheet();
Vector vars = sr.getVariablesAndParamsComposed();
int i = vars.size();
vs.link(i);
while (--i >= 0)
{
ElemVariable v = (ElemVariable) vars.elementAt(i);
// XObject xobj = v.getValue(this, contextNode);
XObject xobj = new XUnresolvedVariable(v, contextNode, this,
vs.getStackFrame(), 0, true);
if(null == vs.elementAt(i))
vs.setGlobalVariable(i, xobj);
}
}
/**
* Execute the function. The function must return
* a valid object.
* @param xctxt The current execution context.
* @return A valid XObject.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
int context = getArg0AsNode(xctxt);
XObject val;
if (DTM.NULL != context)
{
DTM dtm = xctxt.getDTM(context);
String qname = dtm.getNodeNameX(context);
val = (null == qname) ? XString.EMPTYSTRING : new XString(qname);
}
else
{
val = XString.EMPTYSTRING;
}
return val;
}
/**
* Execute this pattern step, including predicates.
*
*
* @param xctxt XPath runtime context.
* @param currentNode The current node context.
*
* @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
* {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt, int currentNode)
throws javax.xml.transform.TransformerException
{
DTM dtm = xctxt.getDTM(currentNode);
if (dtm != null)
{
int expType = dtm.getExpandedTypeID(currentNode);
return execute(xctxt, currentNode, dtm, expType);
}
return NodeTest.SCORE_NONE;
}
/**
* Evaluate XPath string to an XObject.
* XPath namespace prefixes are resolved from the namespaceNode.
* The implementation of this is a little slow, since it creates
* a number of objects each time it is called. This could be optimized
* to keep the same objects around, but then thread-safety issues would arise.
*
* @param contextNode The node to start searching from.
* @param str A valid XPath string.
* @param prefixResolver Will be called if the parser encounters namespace
* prefixes, to resolve the prefixes to URLs.
* @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
* @see org.apache.xpath.objects.XObject
* @see org.apache.xpath.objects.XNull
* @see org.apache.xpath.objects.XBoolean
* @see org.apache.xpath.objects.XNumber
* @see org.apache.xpath.objects.XString
* @see org.apache.xpath.objects.XRTreeFrag
*
* @throws TransformerException
*/
public XObject eval(
Node contextNode, String str, PrefixResolver prefixResolver)
throws TransformerException
{
// Since we don't have a XML Parser involved here, install some default support
// for things like namespaces, etc.
// (Changed from: XPathContext xpathSupport = new XPathContext();
// because XPathContext is weak in a number of areas... perhaps
// XPathContext should be done away with.)
// Create the XPath object.
XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
// Create an XPathContext that doesn't support pushing and popping of
// variable resolution scopes. Sufficient for simple XPath 1.0 expressions.
XPathContext xpathSupport = new XPathContext(false);
// Execute the XPath, and have it return the result
int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}
/**
* Execute an expression in the XPath runtime context, and return the
* result of the expression.
*
*
* @param xctxt The XPath runtime context.
*
* @return The result of the expression in the form of a <code>XObject</code>.
*
* @throws javax.xml.transform.TransformerException if a runtime exception
* occurs.
*/
public void executeCharsToContentHandler(XPathContext xctxt,
ContentHandler handler)
throws javax.xml.transform.TransformerException,
org.xml.sax.SAXException
{
if(Arg0IsNodesetExpr())
{
int node = getArg0AsNode(xctxt);
if(DTM.NULL != node)
{
DTM dtm = xctxt.getDTM(node);
dtm.dispatchCharactersEvents(node, handler, true);
}
}
else
{
XObject obj = execute(xctxt);
obj.dispatchCharactersEvents(handler);
}
}
/**
* Execute the function. The function must return
* a valid object.
* @param xctxt The current execution context.
* @return A valid XObject.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
DTMIterator nodes = m_arg0.asIterator(xctxt, xctxt.getCurrentNode());
double sum = 0.0;
int pos;
while (DTM.NULL != (pos = nodes.nextNode()))
{
DTM dtm = nodes.getDTM(pos);
XMLString s = dtm.getStringValue(pos);
if (null != s)
sum += s.toDouble();
}
nodes.detach();
return new XNumber(sum);
}
/**
* Get a local variable or parameter in the current stack frame.
*
*
* @param xctxt The XPath context, which must be passed in order to
* lazy evaluate variables.
*
* @param index Local variable index relative to the current stack
* frame bottom.
*
* @return The value of the variable.
*
* @throws TransformerException
*/
public XObject getLocalVariable(XPathContext xctxt, int index, boolean destructiveOK)
throws TransformerException
{
index += _currentFrameBottom;
XObject val = _stackFrames[index];
if(null == val)
throw new TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VARIABLE_ACCESSED_BEFORE_BIND, null),
xctxt.getSAXLocator());
// "Variable accessed before it is bound!", xctxt.getSAXLocator());
// Lazy execution of variables.
if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE)
return (_stackFrames[index] = val.execute(xctxt));
return destructiveOK ? val : val.getFresh();
}
/**
* Execute the function. The function must return
* a valid object.
* @param xctxt The current execution context.
* @return A valid XObject.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
// DTMIterator nl = m_arg0.asIterator(xctxt, xctxt.getCurrentNode());
// // We should probably make a function on the iterator for this,
// // as a given implementation could optimize.
// int i = 0;
//
// while (DTM.NULL != nl.nextNode())
// {
// i++;
// }
// nl.detach();
DTMIterator nl = m_arg0.asIterator(xctxt, xctxt.getCurrentNode());
int i = nl.getLength();
nl.detach();
return new XNumber((double) i);
}
/**
* Get a local variable or parameter in the current stack frame.
*
*
* @param xctxt The XPath context, which must be passed in order to
* lazy evaluate variables.
*
* @param index Local variable index relative to the current stack
* frame bottom.
*
* @return The value of the variable.
*
* @throws TransformerException
*/
public XObject getLocalVariable(XPathContext xctxt, int index)
throws TransformerException
{
index += _currentFrameBottom;
XObject val = _stackFrames[index];
if(null == val)
throw new TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VARIABLE_ACCESSED_BEFORE_BIND, null),
xctxt.getSAXLocator());
// "Variable accessed before it is bound!", xctxt.getSAXLocator());
// Lazy execution of variables.
if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE)
return (_stackFrames[index] = val.execute(xctxt));
return val;
}
/**
* Evaluates an XPath expression from the specified node, returning the resultant nodes.
*
* @param <T> the type class
* @param node the node to start searching from
* @param xpathExpr the XPath expression
* @param resolver the prefix resolver to use for resolving namespace prefixes, or null
* @return the list of objects found
*/
@SuppressWarnings("unchecked")
public static <T> List<T> getByXPath(final DomNode node, final String xpathExpr,
final PrefixResolver resolver) {
if (xpathExpr == null) {
throw new IllegalArgumentException("Null is not a valid XPath expression");
}
PROCESS_XPATH_.set(Boolean.TRUE);
final List<T> list = new ArrayList<>();
try {
final XObject result = evaluateXPath(node, xpathExpr, resolver);
if (result instanceof XNodeSet) {
final NodeList nodelist = ((XNodeSet) result).nodelist();
for (int i = 0; i < nodelist.getLength(); i++) {
list.add((T) nodelist.item(i));
}
}
else if (result instanceof XNumber) {
list.add((T) Double.valueOf(result.num()));
}
else if (result instanceof XBoolean) {
list.add((T) Boolean.valueOf(result.bool()));
}
else if (result instanceof XString) {
list.add((T) result.str());
}
else {
throw new RuntimeException("Unproccessed " + result.getClass().getName());
}
}
catch (final Exception e) {
throw new RuntimeException("Could not retrieve XPath >" + xpathExpr + "< on " + node, e);
}
finally {
PROCESS_XPATH_.set(Boolean.FALSE);
}
return list;
}
/**
* Evaluates an XPath expression from the specified node, returning the resultant nodes.
*
* @param <T> the type class
* @param node the node to start searching from
* @param xpathExpr the XPath expression
* @param resolver the prefix resolver to use for resolving namespace prefixes, or null
* @return the list of objects found
*/
@SuppressWarnings("unchecked")
public static <T> List<T> getByXPath(final DomNode node, final String xpathExpr, final PrefixResolver resolver) {
if (xpathExpr == null) {
throw new NullPointerException("Null is not a valid XPath expression");
}
PROCESS_XPATH_.set(Boolean.TRUE);
final List<T> list = new ArrayList<>();
try {
final XObject result = evaluateXPath(node, xpathExpr, resolver);
if (result instanceof XNodeSet) {
final NodeList nodelist = ((XNodeSet) result).nodelist();
for (int i = 0; i < nodelist.getLength(); i++) {
list.add((T) nodelist.item(i));
}
}
else if (result instanceof XNumber) {
list.add((T) Double.valueOf(result.num()));
}
else if (result instanceof XBoolean) {
list.add((T) Boolean.valueOf(result.bool()));
}
else if (result instanceof XString) {
list.add((T) result.str());
}
else {
throw new RuntimeException("Unproccessed " + result.getClass().getName());
}
}
catch (final Exception e) {
throw new RuntimeException("Could not retrieve XPath >" + xpathExpr + "< on " + node, e);
}
finally {
PROCESS_XPATH_.set(Boolean.FALSE);
}
return list;
}
@Override
public Object selectObject(Node contextNode, String expression) throws JRException {
try {
Object value;
XObject object = xpathAPI.eval(contextNode, expression);
switch (object.getType()) {
case XObject.CLASS_NODESET:
value = object.nodeset().nextNode();
break;
case XObject.CLASS_BOOLEAN:
value = object.bool();
break;
case XObject.CLASS_NUMBER:
value = object.num();
break;
default:
value = object.str();
break;
}
return value;
} catch (TransformerException e) {
throw
new JRException(
EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
new Object[]{expression},
e);
}
}
/**
* Execute the function. The function must return
* a valid object.
* @param xctxt The current execution context.
* @return A valid XObject.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
int context = getArg0AsNode(xctxt);
if(DTM.NULL == context)
return XString.EMPTYSTRING;
DTM dtm = xctxt.getDTM(context);
String s = (context != DTM.NULL) ? dtm.getLocalName(context) : "";
if(s.startsWith("#") || s.equals("xmlns"))
return XString.EMPTYSTRING;
return new XString(s);
}
/**
* Given a result tree fragment, walk the tree and
* output it to the SerializationHandler.
*
* @param obj Result tree fragment object
* @param support XPath context for the result tree fragment
*
* @throws org.xml.sax.SAXException
*/
public static void outputResultTreeFragment(
SerializationHandler handler,
XObject obj,
XPathContext support)
throws org.xml.sax.SAXException
{
int doc = obj.rtf();
DTM dtm = support.getDTM(doc);
if (null != dtm)
{
for (int n = dtm.getFirstChild(doc);
DTM.NULL != n;
n = dtm.getNextSibling(n))
{
handler.flushPending();
// I think. . . . This used to have a (true) arg
// to flush prefixes, will that cause problems ???
if (dtm.getNodeType(n) == DTM.ELEMENT_NODE
&& dtm.getNamespaceURI(n) == null)
handler.startPrefixMapping("", "");
dtm.dispatchToEvents(n, handler);
}
}
}
/**
* Test whether a specified node is visible in the logical view of a
* TreeWalker or NodeIterator. This function will be called by the
* implementation of TreeWalker and NodeIterator; it is not intended to
* be called directly from user code.
* @param n The node to check to see if it passes the filter or not.
* @return a constant to determine whether the node is accepted,
* rejected, or skipped, as defined above .
*/
public short acceptNode(int n)
{
XPathContext xctxt = m_lpi.getXPathContext();
try
{
xctxt.pushCurrentNode(n);
XObject score = execute(xctxt, n);
// System.out.println("\n::acceptNode - score: "+score.num()+"::");
if (score != NodeTest.SCORE_NONE)
{
if (getPredicateCount() > 0)
{
countProximityPosition(0);
if (!executePredicates(n, xctxt))
return DTMIterator.FILTER_SKIP;
}
return DTMIterator.FILTER_ACCEPT;
}
}
catch (javax.xml.transform.TransformerException se)
{
// TODO: Fix this.
throw new RuntimeException(se.getMessage());
}
finally
{
xctxt.popCurrentNode();
}
return DTMIterator.FILTER_SKIP;
}
private Object getResultAsType( XObject resultObject, QName returnType )
throws javax.xml.transform.TransformerException {
// XPathConstants.STRING
if ( returnType.equals( XPathConstants.STRING ) ) {
return resultObject.str();
}
// XPathConstants.NUMBER
if ( returnType.equals( XPathConstants.NUMBER ) ) {
return new Double ( resultObject.num());
}
// XPathConstants.BOOLEAN
if ( returnType.equals( XPathConstants.BOOLEAN ) ) {
return new Boolean( resultObject.bool());
}
// XPathConstants.NODESET ---ORdered, UNOrdered???
if ( returnType.equals( XPathConstants.NODESET ) ) {
return resultObject.nodelist();
}
// XPathConstants.NODE
if ( returnType.equals( XPathConstants.NODE ) ) {
NodeIterator ni = resultObject.nodeset();
//Return the first node, or null
return ni.nextNode();
}
String fmsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
new Object[] { returnType.toString()});
throw new IllegalArgumentException( fmsg );
}
/**
* Execute a variable declaration and push it onto the variable stack.
* @see <a href="http://www.w3.org/TR/xslt#variables">variables in XSLT Specification</a>
*
* @param transformer non-null reference to the the current transform-time state.
*
* @throws TransformerException
*/
public void execute(TransformerImpl transformer) throws TransformerException
{
int sourceNode = transformer.getXPathContext().getCurrentNode();
XObject var = getValue(transformer, sourceNode);
// transformer.getXPathContext().getVarStack().pushVariable(m_qname, var);
transformer.getXPathContext().getVarStack().setLocalVariable(m_index, var);
}
/**
* Execute a variable declaration and push it onto the variable stack.
* @see <a href="http://www.w3.org/TR/xslt#variables">variables in XSLT Specification</a>
*
* @param transformer non-null reference to the the current transform-time state.
*
* @throws TransformerException
*/
public void execute(TransformerImpl transformer) throws TransformerException
{
VariableStack vars = transformer.getXPathContext().getVarStack();
if(!vars.isLocalSet(m_index))
{
int sourceNode = transformer.getXPathContext().getCurrentNode();
XObject var = getValue(transformer, sourceNode);
// transformer.getXPathContext().getVarStack().pushVariable(m_qname, var);
transformer.getXPathContext().getVarStack().setLocalVariable(m_index, var);
}
}
/**
* Execute the function. The function must return
* a valid object.
* @param xctxt The current execution context.
* @return A valid XObject.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
String s1 = m_arg0.execute(xctxt).str();
String s2 = m_arg1.execute(xctxt).str();
int index = s1.indexOf(s2);
return (-1 == index)
? XString.EMPTYSTRING : new XString(s1.substring(0, index));
}
/**
* Evaluate XPath string to an XObject.
* XPath namespace prefixes are resolved from the namespaceNode.
* The implementation of this is a little slow, since it creates
* a number of objects each time it is called. This could be optimized
* to keep the same objects around, but then thread-safety issues would arise.
*
* @param contextNode The node to start searching from.
* @param str A valid XPath string.
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
* @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
* @see org.apache.xpath.objects.XObject
* @see org.apache.xpath.objects.XNull
* @see org.apache.xpath.objects.XBoolean
* @see org.apache.xpath.objects.XNumber
* @see org.apache.xpath.objects.XString
* @see org.apache.xpath.objects.XRTreeFrag
*
* @throws TransformerException
*/
public XObject eval(Node contextNode, String str, Node namespaceNode)
throws TransformerException
{
// Since we don't have a XML Parser involved here, install some default support
// for things like namespaces, etc.
// (Changed from: XPathContext xpathSupport = new XPathContext();
// because XPathContext is weak in a number of areas... perhaps
// XPathContext should be done away with.)
// Create an object to resolve namespace prefixes.
// XPath namespaces are resolved from the input context node's document element
// if it is a root node, or else the current context node (for lack of a better
// resolution space, given the simplicity of this sample code).
PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
(namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
? ((Document) namespaceNode).getDocumentElement() : namespaceNode);
// Create the XPath object.
XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
// Execute the XPath, and have it return the result
// return xpath.execute(xpathSupport, contextNode, prefixResolver);
int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}
/**
* Test a node to see if it matches the given node test.
*
* @param xctxt XPath runtime context.
*
* @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
* {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt)
throws javax.xml.transform.TransformerException
{
int context = xctxt.getCurrentNode();
DTMIterator nl = m_functionExpr.asIterator(xctxt, context);
XNumber score = SCORE_NONE;
if (null != nl)
{
int n;
while (DTM.NULL != (n = nl.nextNode()))
{
score = (n == context) ? SCORE_OTHER : SCORE_NONE;
if (score == SCORE_OTHER)
{
context = n;
break;
}
}
nl.detach();
}
return score;
}
/**
* Use an XPath string to select a nodelist.
* XPath namespace prefixes are resolved from the namespaceNode.
*
* @param contextNode The node to start searching from.
* @param str A valid XPath string.
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
* @return A NodeIterator, should never be null.
*
* @throws TransformerException
*/
public NodeIterator selectNodeIterator(
Node contextNode, String str, Node namespaceNode)
throws TransformerException
{
// Execute the XPath, and have it return the result
XObject list = eval(contextNode, str, namespaceNode);
// Have the XObject return its result as a NodeSetDTM.
return list.nodeset();
}
/**
* Execute an expression in the XPath runtime context, and return the
* result of the expression.
*
*
* @param xctxt The XPath runtime context.
* @param currentNode The currentNode.
* @param dtm The DTM of the current node.
* @param expType The expanded type ID of the current node.
*
* @return The result of the expression in the form of a <code>XObject</code>.
*
* @throws javax.xml.transform.TransformerException if a runtime exception
* occurs.
*/
public XObject execute(
XPathContext xctxt, int currentNode, DTM dtm, int expType)
throws javax.xml.transform.TransformerException
{
if (m_whatToShow == NodeTest.SHOW_BYFUNCTION)
{
if (null != m_relativePathPattern)
{
return m_relativePathPattern.execute(xctxt);
}
else
return NodeTest.SCORE_NONE;
}
XObject score;
score = super.execute(xctxt, currentNode, dtm, expType);
if (score == NodeTest.SCORE_NONE)
return NodeTest.SCORE_NONE;
if (getPredicateCount() != 0)
{
if (!executePredicates(xctxt, dtm, currentNode))
return NodeTest.SCORE_NONE;
}
if (null != m_relativePathPattern)
return m_relativePathPattern.executeRelativePathPattern(xctxt, dtm,
currentNode);
return score;
}
/**
* Execute the function. The function must return
* a valid object.
* @param xctxt The current execution context.
* @return A valid XObject.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
int whereNode = getArg0AsNode(xctxt);
String fileLocation = null;
if (DTM.NULL != whereNode)
{
DTM dtm = xctxt.getDTM(whereNode);
// %REVIEW%
if (DTM.DOCUMENT_FRAGMENT_NODE == dtm.getNodeType(whereNode))
{
whereNode = dtm.getFirstChild(whereNode);
}
if (DTM.NULL != whereNode)
{
fileLocation = dtm.getDocumentBaseURI();
// int owner = dtm.getDocument();
// fileLocation = xctxt.getSourceTreeManager().findURIFromDoc(owner);
}
}
return new XString((null != fileLocation) ? fileLocation : "");
}
/**
* Execute the function. The function must return
* a valid object.
* @param xctxt The current execution context.
* @return A valid XObject.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
final XObject obj = m_arg0.execute(xctxt);
final double val= obj.num();
if (val >= -0.5 && val < 0) return new XNumber(-0.0);
if (val == 0.0) return new XNumber(val);
return new XNumber(java.lang.Math.floor(val
+ 0.5));
}
/**
* Test a node to see if it matches any of the patterns in the union.
*
* @param xctxt XPath runtime context.
*
* @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
* {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
* {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
XObject bestScore = null;
int n = m_patterns.length;
for (int i = 0; i < n; i++)
{
XObject score = m_patterns[i].execute(xctxt);
if (score != NodeTest.SCORE_NONE)
{
if (null == bestScore)
bestScore = score;
else if (score.num() > bestScore.num())
bestScore = score;
}
}
if (null == bestScore)
{
bestScore = NodeTest.SCORE_NONE;
}
return bestScore;
}