XPathpublic class XPath extends Object implements Serializable, ExpressionOwnerThe XPath class wraps an expression object and provides general services
for execution of that expression. |
Fields Summary |
---|
private Expression | m_mainExpThe top of the expression tree. | String | m_patternStringThe pattern string, mainly kept around for diagnostic purposes. | public static final int | SELECTRepresents a select type expression. | public static final int | MATCHRepresents a match type expression. | private static final boolean | DEBUG_MATCHESSet to true to get diagnostic messages about the result of
match pattern testing. | public static final double | MATCH_SCORE_NONEThe match score if no match is made. | public static final double | MATCH_SCORE_QNAMEThe match score if the pattern has the form
of a QName optionally preceded by an @ character. | public static final double | MATCH_SCORE_NSWILDThe match score if the pattern pattern has the form NCName:*. | public static final double | MATCH_SCORE_NODETESTThe match score if the pattern consists of just a NodeTest. | public static final double | MATCH_SCORE_OTHERThe match score if the pattern consists of something
other than just a NodeTest or just a qname. |
Constructors Summary |
---|
public XPath(String exprString, SourceLocator locator, PrefixResolver prefixResolver, int type, ErrorListener errorListener)Construct an XPath object.
(Needs review -sc) This method initializes an XPathParser/
Compiler and compiles the expression.
if(null == errorListener)
errorListener = new com.sun.org.apache.xml.internal.utils.DefaultErrorHandler();
m_patternString = exprString;
XPathParser parser = new XPathParser(errorListener, locator);
Compiler compiler = new Compiler(errorListener, locator);
if (SELECT == type)
parser.initXPath(compiler, exprString, prefixResolver);
else if (MATCH == type)
parser.initMatchPattern(compiler, exprString, prefixResolver);
else
throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_CANNOT_DEAL_XPATH_TYPE, new Object[]{Integer.toString(type)})); //"Can not deal with XPath type: " + type);
// System.out.println("----------------");
Expression expr = compiler.compile(0);
// System.out.println("expr: "+expr);
this.setExpression(expr);
if((null != locator) && locator instanceof ExpressionNode)
{
expr.exprSetParent((ExpressionNode)locator);
}
| public XPath(String exprString, SourceLocator locator, PrefixResolver prefixResolver, int type)Construct an XPath object.
(Needs review -sc) This method initializes an XPathParser/
Compiler and compiles the expression.
this(exprString, locator, prefixResolver, type, null);
| public XPath(Expression expr)Construct an XPath object.
this.setExpression(expr);
|
Methods Summary |
---|
public void | assertion(boolean b, java.lang.String msg)Tell the user of an assertion error, and probably throw an
exception.
if (!b)
{
String fMsg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_INCORRECT_PROGRAMMER_ASSERTION,
new Object[]{ msg });
throw new RuntimeException(fMsg);
}
| public boolean | bool(com.sun.org.apache.xpath.internal.XPathContext xctxt, int contextNode, com.sun.org.apache.xml.internal.utils.PrefixResolver namespaceContext)Given an expression and a context, evaluate the XPath
and return the result.
xctxt.pushNamespaceContext(namespaceContext);
xctxt.pushCurrentNodeAndExpression(contextNode, contextNode);
try
{
return m_mainExp.bool(xctxt);
}
catch (TransformerException te)
{
te.setLocator(this.getLocator());
ErrorListener el = xctxt.getErrorListener();
if(null != el) // defensive, should never happen.
{
el.error(te);
}
else
throw te;
}
catch (Exception e)
{
while (e instanceof com.sun.org.apache.xml.internal.utils.WrappedRuntimeException)
{
e = ((com.sun.org.apache.xml.internal.utils.WrappedRuntimeException) e).getException();
}
// e.printStackTrace();
String msg = e.getMessage();
if (msg == null || msg.length() == 0) {
msg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_XPATH_ERROR, null);
}
TransformerException te = new TransformerException(msg,
getLocator(), e);
ErrorListener el = xctxt.getErrorListener();
// te.printStackTrace();
if(null != el) // defensive, should never happen.
{
el.fatalError(te);
}
else
throw te;
}
finally
{
xctxt.popNamespaceContext();
xctxt.popCurrentNodeAndExpression();
}
return false;
| public void | callVisitors(com.sun.org.apache.xpath.internal.ExpressionOwner owner, com.sun.org.apache.xpath.internal.XPathVisitor visitor)This will traverse the heararchy, calling the visitor for
each member. If the called visitor method returns
false, the subtree should not be called.
m_mainExp.callVisitors(this, visitor);
| public void | error(com.sun.org.apache.xpath.internal.XPathContext xctxt, int sourceNode, java.lang.String msg, java.lang.Object[] args)Tell the user of an error, and probably throw an
exception.
String fmsg = XSLMessages.createXPATHMessage(msg, args);
ErrorListener ehandler = xctxt.getErrorListener();
if (null != ehandler)
{
ehandler.fatalError(new TransformerException(fmsg,
(SAXSourceLocator)xctxt.getSAXLocator()));
}
else
{
SourceLocator slocator = xctxt.getSAXLocator();
System.out.println(fmsg + "; file " + slocator.getSystemId()
+ "; line " + slocator.getLineNumber() + "; column "
+ slocator.getColumnNumber());
}
| public com.sun.org.apache.xpath.internal.objects.XObject | execute(com.sun.org.apache.xpath.internal.XPathContext xctxt, int contextNode, com.sun.org.apache.xml.internal.utils.PrefixResolver namespaceContext)Given an expression and a context, evaluate the XPath
and return the result.
xctxt.pushNamespaceContext(namespaceContext);
xctxt.pushCurrentNodeAndExpression(contextNode, contextNode);
XObject xobj = null;
try
{
xobj = m_mainExp.execute(xctxt);
}
catch (TransformerException te)
{
te.setLocator(this.getLocator());
ErrorListener el = xctxt.getErrorListener();
if(null != el) // defensive, should never happen.
{
el.error(te);
}
else
throw te;
}
catch (Exception e)
{
while (e instanceof com.sun.org.apache.xml.internal.utils.WrappedRuntimeException)
{
e = ((com.sun.org.apache.xml.internal.utils.WrappedRuntimeException) e).getException();
}
// e.printStackTrace();
String msg = e.getMessage();
if (msg == null || msg.length() == 0) {
msg = XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_XPATH_ERROR, null);
}
TransformerException te = new TransformerException(msg,
getLocator(), e);
ErrorListener el = xctxt.getErrorListener();
// te.printStackTrace();
if(null != el) // defensive, should never happen.
{
el.fatalError(te);
}
else
throw te;
}
finally
{
xctxt.popNamespaceContext();
xctxt.popCurrentNodeAndExpression();
}
return xobj;
| public com.sun.org.apache.xpath.internal.objects.XObject | execute(com.sun.org.apache.xpath.internal.XPathContext xctxt, org.w3c.dom.Node contextNode, com.sun.org.apache.xml.internal.utils.PrefixResolver namespaceContext)Given an expression and a context, evaluate the XPath
and return the result.
return execute(
xctxt, xctxt.getDTMHandleFromNode(contextNode),
namespaceContext);
| public void | fixupVariables(java.util.Vector vars, int globalsSize)This function is used to fixup variables from QNames to stack frame
indexes at stylesheet build time.
m_mainExp.fixupVariables(vars, globalsSize);
| public com.sun.org.apache.xpath.internal.Expression | getExpression()Get the raw Expression object that this class wraps.
return m_mainExp;
| public javax.xml.transform.SourceLocator | getLocator()Get the SourceLocator on the expression object.
return m_mainExp;
| public double | getMatchScore(com.sun.org.apache.xpath.internal.XPathContext xctxt, int context)Get the match score of the given node.
xctxt.pushCurrentNode(context);
xctxt.pushCurrentExpressionNode(context);
try
{
XObject score = m_mainExp.execute(xctxt);
if (DEBUG_MATCHES)
{
DTM dtm = xctxt.getDTM(context);
System.out.println("score: " + score.num() + " for "
+ dtm.getNodeName(context) + " for xpath "
+ this.getPatternString());
}
return score.num();
}
finally
{
xctxt.popCurrentNode();
xctxt.popCurrentExpressionNode();
}
// return XPath.MATCH_SCORE_NONE;
| public java.lang.String | getPatternString()Return the XPath string associated with this object.
return m_patternString;
| public void | installFunction(java.lang.String name, int funcIndex, com.sun.org.apache.xpath.internal.functions.Function func)Install a built-in function.
FunctionTable.installFunction(func, funcIndex);
| public void | setExpression(com.sun.org.apache.xpath.internal.Expression exp)Set the raw expression object for this object.
if(null != m_mainExp)
exp.exprSetParent(m_mainExp.exprGetParent()); // a bit bogus
m_mainExp = exp;
| public void | warn(com.sun.org.apache.xpath.internal.XPathContext xctxt, int sourceNode, java.lang.String msg, java.lang.Object[] args)Warn the user of an problem.
String fmsg = XSLMessages.createXPATHWarning(msg, args);
ErrorListener ehandler = xctxt.getErrorListener();
if (null != ehandler)
{
// TO DO: Need to get stylesheet Locator from here.
ehandler.warning(new TransformerException(fmsg, (SAXSourceLocator)xctxt.getSAXLocator()));
}
|
|