📄 expression.java
字号:
/* * Copyright 1999-2004 The Apache Software Foundation. * * 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. *//* * $Id: Expression.java,v 1.2.4.2 2005/09/14 19:50:20 jeffsuttor Exp $ */package com.sun.org.apache.xpath.internal;import javax.xml.transform.ErrorListener;import javax.xml.transform.TransformerException;import com.sun.org.apache.xalan.internal.res.XSLMessages;import com.sun.org.apache.xml.internal.dtm.DTM;import com.sun.org.apache.xml.internal.dtm.DTMIterator;import com.sun.org.apache.xml.internal.utils.XMLString;import com.sun.org.apache.xpath.internal.objects.XNodeSet;import com.sun.org.apache.xpath.internal.objects.XObject;import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;import org.xml.sax.ContentHandler;/** * This abstract class serves as the base for all expression objects. An * Expression can be executed to return a {@link com.sun.org.apache.xpath.internal.objects.XObject}, * normally has a location within a document or DOM, can send error and warning * events, and normally do not hold state and are meant to be immutable once * construction has completed. An exception to the immutibility rule is iterators * and walkers, which must be cloned in order to be used -- the original must * still be immutable. */public abstract class Expression implements java.io.Serializable, ExpressionNode, XPathVisitable{ static final long serialVersionUID = 565665869777906902L; /** * The location where this expression was built from. Need for diagnostic * messages. May be null. * @serial */ private ExpressionNode m_parent; /** * Tell if this expression or it's subexpressions can traverse outside * the current subtree. * * @return true if traversal outside the context node's subtree can occur. */ public boolean canTraverseOutsideSubtree() { return false; } // /**// * Set the location where this expression was built from.// *// *// * @param locator the location where this expression was built from, may be// * null.// */// public void setSourceLocator(SourceLocator locator)// {// m_slocator = locator;// } /** * 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. * * @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) throws javax.xml.transform.TransformerException { // For now, the current node is already pushed. return execute(xctxt); } /** * 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 { // For now, the current node is already pushed. return execute(xctxt); } /** * 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 abstract XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException; /** * Execute an expression in the XPath runtime context, and return the * result of the expression, but tell that a "safe" object doesn't have * to be returned. The default implementation just calls execute(xctxt). * * * @param xctxt The XPath runtime context. * @param destructiveOK true if a "safe" object doesn't need to be returned. * * @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, boolean destructiveOK) throws javax.xml.transform.TransformerException { return execute(xctxt); } /** * Evaluate expression to a number. * * * @param xctxt The XPath runtime context. * @return The expression evaluated as a double. * * @throws javax.xml.transform.TransformerException */ public double num(XPathContext xctxt) throws javax.xml.transform.TransformerException { return execute(xctxt).num(); } /** * Evaluate expression to a boolean. * * * @param xctxt The XPath runtime context. * @return false * * @throws javax.xml.transform.TransformerException */ public boolean bool(XPathContext xctxt) throws javax.xml.transform.TransformerException { return execute(xctxt).bool(); } /** * Cast result object to a string. * * * @param xctxt The XPath runtime context. * @return The string this wraps or the empty string if null * * @throws javax.xml.transform.TransformerException */ public XMLString xstr(XPathContext xctxt) throws javax.xml.transform.TransformerException { return execute(xctxt).xstr(); } /** * Tell if the expression is a nodeset expression. In other words, tell * if you can execute {@link #asNode(XPathContext) asNode} without an exception. * @return true if the expression can be represented as a nodeset. */ public boolean isNodesetExpr() { return false; } /** * Return the first node out of the nodeset, if this expression is * a nodeset expression. * @param xctxt The XPath runtime context. * @return the first node out of the nodeset, or DTM.NULL. * * @throws javax.xml.transform.TransformerException */ public int asNode(XPathContext xctxt) throws javax.xml.transform.TransformerException { DTMIterator iter = execute(xctxt).iter(); return iter.nextNode(); } /** * Given an select expression and a context, evaluate the XPath * and return the resulting iterator. * * @param xctxt The execution context. * @param contextNode The node that "." expresses. * * * @return A valid DTMIterator. * @throws TransformerException thrown if the active ProblemListener decides * the error condition is severe enough to halt processing. * * @throws javax.xml.transform.TransformerException * @xsl.usage experimental */ public DTMIterator asIterator(XPathContext xctxt, int contextNode) throws javax.xml.transform.TransformerException { try { xctxt.pushCurrentNodeAndExpression(contextNode, contextNode); return execute(xctxt).iter(); } finally { xctxt.popCurrentNodeAndExpression(); } } /** * Given an select expression and a context, evaluate the XPath * and return the resulting iterator, but do not clone. * * @param xctxt The execution context. * @param contextNode The node that "." expresses. * * * @return A valid DTMIterator. * @throws TransformerException thrown if the active ProblemListener decides * the error condition is severe enough to halt processing. * * @throws javax.xml.transform.TransformerException * @xsl.usage experimental */ public DTMIterator asIteratorRaw(XPathContext xctxt, int contextNode) throws javax.xml.transform.TransformerException { try { xctxt.pushCurrentNodeAndExpression(contextNode, contextNode); XNodeSet nodeset = (XNodeSet)execute(xctxt); return nodeset.iterRaw(); } finally { xctxt.popCurrentNodeAndExpression(); } } /** * Execute an expression in the XPath runtime context, and return the * result of the expression. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -