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

📄 xpathimpl.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: XPathImpl.java,v 1.2 2005/08/16 22:41:08 jeffsuttor Exp $package com.sun.org.apache.xpath.internal.jaxp;import javax.xml.namespace.QName;import javax.xml.namespace.NamespaceContext;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathFunctionResolver;import javax.xml.xpath.XPathVariableResolver;import javax.xml.xpath.XPathExpression;import com.sun.org.apache.xml.internal.dtm.DTM;import com.sun.org.apache.xpath.internal.*;import com.sun.org.apache.xpath.internal.objects.XObject;import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;import com.sun.org.apache.xalan.internal.res.XSLMessages;import org.w3c.dom.Node;import org.w3c.dom.DOMImplementation;import org.w3c.dom.Document;import org.w3c.dom.traversal.NodeIterator;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import javax.xml.parsers.*;import java.io.IOException;/** * The XPathImpl class provides implementation for the methods defined  in * javax.xml.xpath.XPath interface. This provide simple access to the results * of an XPath expression. * * * @version $Revision: 1.2 $ * @author  Ramesh Mandava */public class XPathImpl implements javax.xml.xpath.XPath {    // Private variables    private XPathVariableResolver variableResolver;    private XPathFunctionResolver functionResolver;    private XPathVariableResolver origVariableResolver;    private XPathFunctionResolver origFunctionResolver;    private NamespaceContext namespaceContext=null;    private JAXPPrefixResolver prefixResolver;    // 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;     XPathImpl( XPathVariableResolver vr, XPathFunctionResolver fr ) {        this.origVariableResolver = this.variableResolver = vr;        this.origFunctionResolver = this.functionResolver = fr;    }    XPathImpl( XPathVariableResolver vr, XPathFunctionResolver fr,             boolean featureSecureProcessing ) {        this.origVariableResolver = this.variableResolver = vr;        this.origFunctionResolver = this.functionResolver = fr;        this.featureSecureProcessing = featureSecureProcessing;    }    /**     * <p>Establishes a variable resolver.</p>     *     * @param resolver Variable Resolver     */    public void setXPathVariableResolver(XPathVariableResolver resolver) {        if ( resolver == null ) {            String fmsg = XSLMessages.createXPATHMessage(                     XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,                    new Object[] {"XPathVariableResolver"} );            throw new NullPointerException( fmsg );        }        this.variableResolver = resolver;    }    /**     * <p>Returns the current variable resolver.</p>     *     * @return Current variable resolver     */    public XPathVariableResolver getXPathVariableResolver() {        return variableResolver;    }    /**     * <p>Establishes a function resolver.</p>     *     * @param resolver XPath function resolver     */    public void setXPathFunctionResolver(XPathFunctionResolver resolver) {        if ( resolver == null ) {            String fmsg = XSLMessages.createXPATHMessage(                     XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,                    new Object[] {"XPathFunctionResolver"} );            throw new NullPointerException( fmsg );        }        this.functionResolver = resolver;    }    /**     * <p>Returns the current function resolver.</p>     *     * @return Current function resolver     */    public XPathFunctionResolver getXPathFunctionResolver() {        return functionResolver;    }    /**     * <p>Establishes a namespace context.</p>     *     * @param nsContext Namespace context to use     */    public void setNamespaceContext(NamespaceContext nsContext) {        if ( nsContext == null ) {            String fmsg = XSLMessages.createXPATHMessage(                     XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,                    new Object[] {"NamespaceContext"} );            throw new NullPointerException( fmsg );         }        this.namespaceContext = nsContext;        this.prefixResolver = new JAXPPrefixResolver ( nsContext );    }    /**     * <p>Returns the current namespace context.</p>     *     * @return Current Namespace context     */    public NamespaceContext getNamespaceContext() {        return namespaceContext;    }    private static Document d = null;        private static DocumentBuilder getParser() {        try {            // we'd really like to cache those DocumentBuilders, but we can't because:            // 1. thread safety. parsers are not thread-safe, so at least            //    we need one instance per a thread.            // 2. parsers are non-reentrant, so now we are looking at having a            // pool of parsers.            // 3. then the class loading issue. The look-up procedure of            //    DocumentBuilderFactory.newInstance() depends on context class loader            //    and system properties, which may change during the execution of JVM.            //            // so we really have to create a fresh DocumentBuilder every time we need one            // - KK            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();            dbf.setNamespaceAware( true );            dbf.setValidating( false );            return dbf.newDocumentBuilder();        } catch (ParserConfigurationException e) {            // this should never happen with a well-behaving JAXP implementation.             throw new Error(e);        }    }    private static Document getDummyDocument( ) {        // we don't need synchronization here; even if two threads        // enter this code at the same time, we just waste a little time        if(d==null) {            DOMImplementation dim = getParser().getDOMImplementation();            d = dim.createDocument("http://java.sun.com/jaxp/xpath",                "dummyroot", null);        }        return d;    }        private XObject eval(String expression, Object contextItem)        throws javax.xml.transform.TransformerException {        com.sun.org.apache.xpath.internal.XPath xpath = new com.sun.org.apache.xpath.internal.XPath( expression,            null, prefixResolver, com.sun.org.apache.xpath.internal.XPath.SELECT );         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();        }        XObject xobj = null;                xpathSupport.setVarStack(new JAXPVariableStack(variableResolver));                // If item is null, then we will create a a Dummy contextNode        if ( contextItem instanceof Node ) {            xobj = xpath.execute (xpathSupport, (Node)contextItem,                    prefixResolver );        } else {            xobj = xpath.execute ( xpathSupport, DTM.NULL, prefixResolver );        }         return xobj;    }            /**     * <p>Evaluate an <code>XPath</code> 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 <code>QName</code> resolution and return type conversion.</p>     *     * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants} (     * {@link XPathConstants#NUMBER NUMBER},     * {@link XPathConstants#STRING STRING},     * {@link XPathConstants#BOOLEAN BOOLEAN},     * {@link XPathConstants#NODE NODE} or     * {@link XPathConstants#NODESET NODESET})     * 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>expression</code> or <code>returnType</code> is <code>null</code>, then a     * <code>NullPointerException</code> is thrown.</p>     *     * @param expression The XPath expression.     * @param item The starting context (node or node list, for example).     * @param returnType The desired return type.     *     * @return Result of evaluating an XPath expression as an <code>Object</code> of <code>returnType</code>.     *     * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.     * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.     * @throws NullPointerException If <code>expression</code> or <code>returnType</code> is <code>null</code>.     */    public Object evaluate(String expression, Object item, QName returnType)            throws XPathExpressionException {        if ( expression == null ) {            String fmsg = XSLMessages.createXPATHMessage(                     XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,                    new Object[] {"XPath expression"} );            throw new NullPointerException ( fmsg );        }        if ( returnType == null ) {            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 );

⌨️ 快捷键说明

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