📄 xpath.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: XPath.java,v 1.2.4.1 2005/09/15 01:41:57 jeffsuttor Exp $ */package com.sun.org.apache.xpath.internal;import java.io.Serializable;import javax.xml.transform.ErrorListener;import javax.xml.transform.SourceLocator;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.utils.PrefixResolver;import com.sun.org.apache.xml.internal.utils.SAXSourceLocator;import com.sun.org.apache.xpath.internal.compiler.Compiler;import com.sun.org.apache.xpath.internal.compiler.FunctionTable;import com.sun.org.apache.xpath.internal.compiler.XPathParser;import com.sun.org.apache.xpath.internal.functions.Function;import com.sun.org.apache.xpath.internal.objects.XObject;import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;/** * The XPath class wraps an expression object and provides general services * for execution of that expression. * @xsl.usage advanced */public class XPath implements Serializable, ExpressionOwner{ static final long serialVersionUID = 3976493477939110553L; /** The top of the expression tree. * @serial */ private Expression m_mainExp; /** * The function table for xpath build-in functions */ private transient FunctionTable m_funcTable = null; /** * initial the function table */ private void initFunctionTable(){ m_funcTable = new FunctionTable(); } /** * Get the raw Expression object that this class wraps. * * * @return the raw Expression object, which should not normally be null. */ public Expression getExpression() { return m_mainExp; } /** * This function is used to fixup variables from QNames to stack frame * indexes at stylesheet build time. * @param vars List of QNames that correspond to variables. This list * should be searched backwards for the first qualified name that * corresponds to the variable reference qname. The position of the * QName in the vector from the start of the vector will be its position * in the stack frame (but variables above the globalsTop value will need * to be offset to the current stack frame). */ public void fixupVariables(java.util.Vector vars, int globalsSize) { m_mainExp.fixupVariables(vars, globalsSize); } /** * Set the raw expression object for this object. * * * @param exp the raw Expression object, which should not normally be null. */ public void setExpression(Expression exp) { if(null != m_mainExp) exp.exprSetParent(m_mainExp.exprGetParent()); // a bit bogus m_mainExp = exp; } /** * Get the SourceLocator on the expression object. * * * @return the SourceLocator on the expression object, which may be null. */ public SourceLocator getLocator() { return m_mainExp; }// /**// * Set the SourceLocator on the expression object.// *// *// * @param l the SourceLocator on the expression object, which may be null.// */// public void setLocator(SourceLocator l)// {// // Note potential hazards -- l may not be serializable, or may be changed// // after being assigned here.// m_mainExp.setSourceLocator(l);// } /** The pattern string, mainly kept around for diagnostic purposes. * @serial */ String m_patternString; /** * Return the XPath string associated with this object. * * * @return the XPath string associated with this object. */ public String getPatternString() { return m_patternString; } /** Represents a select type expression. */ public static final int SELECT = 0; /** Represents a match type expression. */ public static final int MATCH = 1; /** * Construct an XPath object. * * (Needs review -sc) This method initializes an XPathParser/ * Compiler and compiles the expression. * @param exprString The XPath expression. * @param locator The location of the expression, may be null. * @param prefixResolver A prefix resolver to use to resolve prefixes to * namespace URIs. * @param type one of {@link #SELECT} or {@link #MATCH}. * @param errorListener The error listener, or null if default should be used. * * @throws javax.xml.transform.TransformerException if syntax or other error. */ public XPath( String exprString, SourceLocator locator, PrefixResolver prefixResolver, int type, ErrorListener errorListener) throws javax.xml.transform.TransformerException { initFunctionTable(); 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, m_funcTable); 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); } } /** * Construct an XPath object. * * (Needs review -sc) This method initializes an XPathParser/ * Compiler and compiles the expression. * @param exprString The XPath expression. * @param locator The location of the expression, may be null. * @param prefixResolver A prefix resolver to use to resolve prefixes to * namespace URIs. * @param type one of {@link #SELECT} or {@link #MATCH}. * @param errorListener The error listener, or null if default should be used. * * @throws javax.xml.transform.TransformerException if syntax or other error. */ public XPath( String exprString, SourceLocator locator, PrefixResolver prefixResolver, int type, ErrorListener errorListener, FunctionTable aTable) throws javax.xml.transform.TransformerException { m_funcTable = aTable; 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, m_funcTable); 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); } } /** * Construct an XPath object. * * (Needs review -sc) This method initializes an XPathParser/ * Compiler and compiles the expression. * @param exprString The XPath expression. * @param locator The location of the expression, may be null. * @param prefixResolver A prefix resolver to use to resolve prefixes to * namespace URIs. * @param type one of {@link #SELECT} or {@link #MATCH}. * * @throws javax.xml.transform.TransformerException if syntax or other error. */ public XPath( String exprString, SourceLocator locator, PrefixResolver prefixResolver, int type) throws javax.xml.transform.TransformerException { this(exprString, locator, prefixResolver, type, null); } /** * Construct an XPath object. * * @param expr The Expression object. * * @throws javax.xml.transform.TransformerException if syntax or other error. */ public XPath(Expression expr) { this.setExpression(expr); initFunctionTable(); } /** * Given an expression and a context, evaluate the XPath * and return the result. * * @param xctxt The execution context. * @param contextNode The node that "." expresses. * @param namespaceContext The context in which namespaces in the * XPath are supposed to be expanded. * * @return The result of the XPath or null if callbacks are used. * @throws TransformerException thrown if * the error condition is severe enough to halt processing. * * @throws javax.xml.transform.TransformerException * @xsl.usage experimental */ public XObject execute( XPathContext xctxt, org.w3c.dom.Node contextNode, PrefixResolver namespaceContext) throws javax.xml.transform.TransformerException { return execute( xctxt, xctxt.getDTMHandleFromNode(contextNode), namespaceContext); } /** * Given an expression and a context, evaluate the XPath * and return the result. * * @param xctxt The execution context. * @param contextNode The node that "." expresses. * @param namespaceContext The context in which namespaces in the * XPath are supposed to be expanded. * * @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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -