⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xpathexpressionimpl.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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: XPathExpressionImpl.java,v 1.3 2005/09/27 09:40:43 sunithareddy Exp $package com.sun.org.apache.xpath.internal.jaxp;import com.sun.org.apache.xpath.internal.*;import javax.xml.transform.TransformerException;import com.sun.org.apache.xpath.internal.objects.XObject;import com.sun.org.apache.xml.internal.dtm.DTM;import com.sun.org.apache.xml.internal.utils.PrefixResolver;import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;import com.sun.org.apache.xalan.internal.res.XSLMessages;import javax.xml.namespace.NamespaceContext;import javax.xml.namespace.QName;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathFunctionResolver;import javax.xml.xpath.XPathVariableResolver;import javax.xml.xpath.XPathConstants;import org.w3c.dom.Node;import org.w3c.dom.Document;import org.w3c.dom.DOMImplementation;import org.w3c.dom.traversal.NodeIterator;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.DocumentBuilder;import org.xml.sax.InputSource;/** * The XPathExpression interface encapsulates a (compiled) XPath expression. * * @version $Revision: 1.3 $ * @author  Ramesh Mandava */public class XPathExpressionImpl  implements javax.xml.xpath.XPathExpression{    private XPathFunctionResolver functionResolver;    private XPathVariableResolver variableResolver;    private JAXPPrefixResolver prefixResolver;    private com.sun.org.apache.xpath.internal.XPath xpath;    // By default Extension Functions are allowed in XPath Expressions. If    // Secure Processing Feature is set on XPathFactory then the invocation of    // extensions function need to throw XPathFunctionException    private boolean featureSecureProcessing = false;    /** Protected constructor to prevent direct instantiation; use compile()     * from the context.     */    protected XPathExpressionImpl() { };    protected XPathExpressionImpl(com.sun.org.apache.xpath.internal.XPath xpath,             JAXPPrefixResolver prefixResolver,             XPathFunctionResolver functionResolver,            XPathVariableResolver variableResolver ) {         this.xpath = xpath;        this.prefixResolver = prefixResolver;        this.functionResolver = functionResolver;        this.variableResolver = variableResolver;        this.featureSecureProcessing = false;    };    protected XPathExpressionImpl(com.sun.org.apache.xpath.internal.XPath xpath,            JAXPPrefixResolver prefixResolver,            XPathFunctionResolver functionResolver,            XPathVariableResolver variableResolver,            boolean featureSecureProcessing ) {         this.xpath = xpath;        this.prefixResolver = prefixResolver;        this.functionResolver = functionResolver;        this.variableResolver = variableResolver;        this.featureSecureProcessing = featureSecureProcessing;    };    public void setXPath (com.sun.org.apache.xpath.internal.XPath xpath ) {        this.xpath = xpath;    }      public Object eval(Object item, QName returnType)            throws javax.xml.transform.TransformerException {        XObject resultObject = eval ( item );        return getResultAsType( resultObject, returnType );    }        private XObject eval ( Object contextItem )            throws javax.xml.transform.TransformerException {        com.sun.org.apache.xpath.internal.XPathContext xpathSupport = null;        if ( functionResolver != null ) {            JAXPExtensionsProvider jep = new JAXPExtensionsProvider(                    functionResolver, featureSecureProcessing );            xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext( jep );        } else {            xpathSupport = new com.sun.org.apache.xpath.internal.XPathContext();        }        xpathSupport.setVarStack(new JAXPVariableStack(variableResolver));        XObject xobj = null;                  Node contextNode = (Node)contextItem;        // We always need to have a ContextNode with Xalan XPath implementation        // To allow simple expression evaluation like 1+1 we are setting         // dummy Document as Context Node                if ( contextNode == null )             xobj = xpath.execute(xpathSupport, DTM.NULL, prefixResolver);        else            xobj = xpath.execute(xpathSupport, contextNode, prefixResolver);                 return xobj;    }    /**     * <p>Evaluate the compiled XPath expression in the specified context and     *  return the result as the specified type.</p>     *     * <p>See "Evaluation of XPath Expressions" section of JAXP 1.3 spec     * for context item evaluation,     * variable, function and QName resolution and return type conversion.</p>     *     * <p>If <code>returnType</code> is not one of the types defined      * in {@link XPathConstants},     * then an <code>IllegalArgumentException</code> is thrown.</p>     *     * <p>If a <code>null</code> value is provided for     * <code>item</code>, an empty document will be used for the     * context.     * If <code>returnType</code> is <code>null</code>, then a      * <code>NullPointerException</code> is thrown.</p>     *     * @param item The starting context (node or node list, for example).     * @param returnType The desired return type.     *     * @return The <code>Object</code> that is the result of evaluating the     * expression and converting the result to     *   <code>returnType</code>.     *     * @throws XPathExpressionException If the expression cannot be evaluated.     * @throws IllegalArgumentException If <code>returnType</code> is not one     * of the types defined in {@link XPathConstants}.     * @throws NullPointerException If  <code>returnType</code> is     * <code>null</code>.     */    public Object evaluate(Object item, QName returnType)        throws XPathExpressionException {        //Validating parameters to enforce constraints defined by JAXP spec        if ( returnType == null ) {           //Throwing NullPointerException as defined in spec            String fmsg = XSLMessages.createXPATHMessage(                     XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,                    new Object[] {"returnType"} );            throw new NullPointerException( fmsg );        }        // Checking if requested returnType is supported. returnType need to be        // defined in XPathConstants         if ( !isSupported ( returnType ) ) {            String fmsg = XSLMessages.createXPATHMessage(                     XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,                    new Object[] { returnType.toString() } );            throw new IllegalArgumentException ( fmsg );        }        try {             return eval( item, returnType);        } catch ( java.lang.NullPointerException npe ) {            // If VariableResolver returns null Or if we get             // NullPointerException at this stage for some other reason            // then we have to reurn XPathException            throw new XPathExpressionException ( npe );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -