Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  *
  *
  * This file incorporates work covered by the following copyright and
  * permission notice:
  *
  * Copyright 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.
  */
 
 package org.apache.taglibs.standard.tag.common.xml;
 
 
 
 import  org.apache.xalan.res.XSLMessages;
 import  org.apache.xml.dtm.DTM;
 import  org.apache.xpath.jaxp.JAXPVariableStack;
 import  org.apache.xpath.objects.XNodeSet;
 import  org.apache.xpath.objects.XObject;
 import  org.apache.xpath.res.XPATHErrorResources;
 
 
 
The JSTLXPathImpl class provides implementation for the methods defined in javax.xml.xpath.XPath interface. This provide simple access to the results of an XPath expression. This class provides our own implementation of XPath, so that we can support a generic Object type in returnType arguement for XPath's evaluate instance method. Most of the implementation is exactly similar to what is already provided in org.apache.xpath.jaxp.XPathImpl.java
 
public class JSTLXPathImpl implements javax.xml.xpath.XPath {
    // Private variables
    private NamespaceContext namespaceContext=null;
    private org.apache.xpath.jaxp.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
        this. = this. = vr;
        this. = this. = fr;
    }
            boolean featureSecureProcessing ) {
        this. = this. = vr;
        this. = this. = fr;
        this. = featureSecureProcessing;
    }

    

Establishes a variable resolver.

Parameters:
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 NullPointerExceptionfmsg );
        }
        this. = resolver;
    }

    

Returns the current variable resolver.

Returns:
Current variable resolver
        return ;
    }

    

Establishes a function resolver.

Parameters:
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 NullPointerExceptionfmsg );
        }
        this. = resolver;
    }

    

Returns the current function resolver.

Returns:
Current function resolver
        return ;
    }

    

Establishes a namespace context.

Parameters:
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 NullPointerExceptionfmsg ); 
        }
        this. = nsContext;
        this. = new org.apache.xpath.jaxp.JAXPPrefixResolver ( nsContext );
    }

    

Returns the current namespace context.

Returns:
Current Namespace context
        return ;
    }
    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.setNamespaceAwaretrue );
            dbf.setValidatingfalse );
            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(==null) {
            DOMImplementation dim = getParser().getDOMImplementation();
             = dim.createDocument("http://java.sun.com/jaxp/xpath",
                "dummyroot"null);
        }
        return ;
    }
    
    private XObject eval(String expressionObject contextItem)
        throws javax.xml.transform.TransformerException {
        org.apache.xpath.XPath xpath = new org.apache.xpath.XPath( expression,
            null, org.apache.xpath.XPath.SELECT ); 
        org.apache.xpath.XPathContext xpathSupport = null;
        if (  != null ) {
            org.apache.xpath.jaxp.JAXPExtensionsProvider jep = 
                    new org.apache.xpath.jaxp.JAXPExtensionsProvider(
                     );
            xpathSupport = new org.apache.xpath.XPathContext( jep );
        } else { 
            xpathSupport = new org.apache.xpath.XPathContext();
        }
        XObject xobj = null;
        
        xpathSupport.setVarStack(new JAXPVariableStack());
        
        // If item is null, then we will create a a Dummy contextNode
        if ( contextItem instanceof Node ) {
            xobj = xpath.execute (xpathSupport, (Node)contextItem,
                     );
        } else {
            xobj = xpath.execute ( xpathSupport, DTM.NULL,  );
        }
 
        return xobj;
    }
        
    

Evaluate an XPath expression in the specified context and return the result as the specified type.

See "Evaluation of XPath Expressions" section of JAXP 1.3 spec for context item evaluation, variable, function and QName resolution and return type conversion.

If returnType is not one of the types defined in XPathConstants ( NUMBER, STRING, BOOLEAN, NODE or NODESET) then an IllegalArgumentException is thrown.

If a null value is provided for item, an empty document will be used for the context. If expression or returnType is null, then a NullPointerException is thrown.

Parameters:
expression The XPath expression.
item The starting context (node or node list, for example).
returnType The desired return type.
Returns:
Result of evaluating an XPath expression as an Object of returnType.
Throws:
XPathExpressionException If expression cannot be evaluated.
IllegalArgumentException If returnType is not one of the types defined in XPathConstants.
NullPointerException If expression or returnType is null.
    public Object evaluate(String expressionObject itemQName 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 or JSTLXPathConstants
        if ( !isSupported ( returnType ) ) {
            String fmsg = XSLMessages.createXPATHMessage(
                    XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
                    new Object[] { returnType.toString() } );
            throw new IllegalArgumentException ( fmsg );
        }
        try {
 
            XObject resultObject = evalexpressionitem );
            return getResultAsTyperesultObjectreturnType );
        } 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 );
        } catch ( javax.xml.transform.TransformerException te ) {
            Throwable nestedException = te.getException();
            if ( nestedException instanceof javax.xml.xpath.XPathFunctionException ) {
                throw (javax.xml.xpath.XPathFunctionException)nestedException;
            } else {
                // For any other exceptions we need to throw 
                // XPathExpressionException ( as per spec )
                throw new XPathExpressionException ( te );
            }
        } 
        
    }
    private boolean isSupportedQName returnType ) {
        if ( ( returnType.equals. ) ) ||
             ( returnType.equals. ) ) ||
             ( returnType.equals. ) ) ||
             ( returnType.equals. ) ) ||
             ( returnType.equals. ) ) ||
             ( returnType.equals. ) )   ) {
  
            return true;
        }
        return false;
     }
    private Object getResultAsType( XObject resultObjectQName returnType )
        throws javax.xml.transform.TransformerException {
        // XPathConstants.STRING
        if ( returnType.equals. ) ) { 
            return resultObject.str();
        }
        // XPathConstants.NUMBER
        if ( returnType.equals. ) ) { 
            return new Double ( resultObject.num());
        }
        // XPathConstants.BOOLEAN
        if ( returnType.equals. ) ) { 
            return Boolean.valueOfresultObject.bool());
        }
        // XPathConstants.NODESET ---ORdered, UNOrdered???
        if ( returnType.equals. ) ) { 
            return resultObject.nodelist();
        }
        // XPathConstants.NODE
        if ( returnType.equals. ) ) { 
            NodeIterator ni = resultObject.nodeset(); 
            //Return the first node, or null
            return ni.nextNode();
        }
        // JSTLXPathConstants.OBJECT
        if ( returnType.equals. ) ) {
            if (resultObject instanceof XNodeSet)
                return resultObject.nodelist();
            else 
                return resultObject.object();
        }
        String fmsg = XSLMessages.createXPATHMessage(
                XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
                new Object[] { returnType.toString()});
        throw new IllegalArgumentExceptionfmsg );
    }
         
            
        
    

Evaluate an XPath expression in the specified context and return the result as a String.

This method calls evaluate(String expression, Object item, QName returnType) with a returnType of XPathConstants.STRING.

See "Evaluation of XPath Expressions" of JAXP 1.3 spec for context item evaluation, variable, function and QName resolution and return type conversion.

If a null value is provided for item, an empty document will be used for the context. If expression is null, then a NullPointerException is thrown.

Parameters:
expression The XPath expression.
item The starting context (node or node list, for example).
Returns:
The String that is the result of evaluating the expression and converting the result to a String.
Throws:
XPathExpressionException If expression cannot be evaluated.
NullPointerException If expression is null.
    public String evaluate(String expressionObject item)
        throws XPathExpressionException {
        return (String)this.evaluateexpressionitem. );
    }

    

Compile an XPath expression for later evaluation.

If expression contains any javax.xml.xpath.XPathFunctions, they must be available via the XPathFunctionResolver. An XPathExpressionException will be thrown if the XPathFunction cannot be resovled with the XPathFunctionResolver.

If expression is null, a NullPointerException is thrown.

Parameters:
expression The XPath expression.
Returns:
Compiled XPath expression.
Throws:
XPathExpressionException If expression cannot be compiled.
NullPointerException If expression is null.
    public XPathExpression compile(String expression)
        throws XPathExpressionException {
        // This is never used in JSTL
        if ( expression == null ) {
            String fmsg = XSLMessages.createXPATHMessage( 
                    XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
                    new Object[] {"XPath expression"} );
            throw new NullPointerException ( fmsg );
        }
        return null;
        /*
        try {
            org.apache.xpath.XPath xpath = new XPath (expression, null,
                    prefixResolver, org.apache.xpath.XPath.SELECT );
            // Can have errorListener
            org.apache.xpath.jaxp.XPathExpressionImpl ximpl = 
                    new org.apache.xpath.jaxp.XPathExpressionImpl (xpath,
                    prefixResolver, functionResolver, variableResolver,
                    featureSecureProcessing );
            return ximpl;
        } catch ( javax.xml.transform.TransformerException te ) {
            throw new XPathExpressionException ( te ) ;
        }
         **/
    }


    

Evaluate an XPath expression in the context of the specified InputSource and return the result as the specified type.

This method builds a data model for the InputSource and calls evaluate(String expression, Object item, QName returnType) on the resulting document object.

See "Evaluation of XPath Expressions" section of JAXP 1.3 spec for context item evaluation, variable, function and QName resolution and return type conversion.

If returnType is not one of the types defined in XPathConstants, then an IllegalArgumentException is thrown.

If expression, source or returnType is null, then a NullPointerException is thrown.

Parameters:
expression The XPath expression.
source The input source of the document to evaluate over.
returnType The desired return type.
Returns:
The Object that encapsulates the result of evaluating the expression.
Throws:
XPathExpressionException If expression cannot be evaluated.
IllegalArgumentException If returnType is not one of the types defined in XPathConstants.
NullPointerException If expression, source or returnType is null.
    public Object evaluate(String expressionInputSource source
            QName returnTypethrows XPathExpressionException {
        // Checking validity of different parameters
        ifsource== null ) {
            String fmsg = XSLMessages.createXPATHMessage( 
                    XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
                    new Object[] {"source"} );
            throw new NullPointerException ( fmsg );
        }
        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 );
        }
        
        try {
            Document document = getParser().parsesource );
            XObject resultObject = evalexpressiondocument );
            return getResultAsTyperesultObjectreturnType );
        } catch ( SAXException e ) {
            throw new XPathExpressionException ( e );
        } catchIOException e ) {
            throw new XPathExpressionException ( e );            
        } catch ( javax.xml.transform.TransformerException te ) {
            Throwable nestedException = te.getException();
            if ( nestedException instanceof javax.xml.xpath.XPathFunctionException ) {
                throw (javax.xml.xpath.XPathFunctionException)nestedException;
            } else {
                throw new XPathExpressionException ( te );
            }
        }
    } 
 



    

Evaluate an XPath expression in the context of the specified InputSource and return the result as a String.

This method calls evaluate(String expression, InputSource source, QName returnType) with a returnType of XPathConstants.STRING.

See "Evaluation of XPath Expressions" section of JAXP 1.3 spec for context item evaluation, variable, function and QName resolution and return type conversion.

If expression or source is null, then a NullPointerException is thrown.

Parameters:
expression The XPath expression.
source The InputSource of the document to evaluate over.
Returns:
The String that is the result of evaluating the expression and converting the result to a String.
Throws:
XPathExpressionException If expression cannot be evaluated.
NullPointerException If expression or source is null.
    public String evaluate(String expressionInputSource source)
        throws XPathExpressionException {
        return (String)this.evaluateexpressionsource. );
    }

    

Reset this XPath to its original configuration.

XPath is reset to the same state as when it was created with javax.xml.xpath.XPathFactory.newXPath(). reset() is designed to allow the reuse of existing XPaths thus saving resources associated with the creation of new XPaths.

The reset XPath is not guaranteed to have the same XPathFunctionResolver, XPathVariableResolver or NamespaceContext Objects, e.g. Object.equals(Object obj). It is guaranteed to have a functionally equal XPathFunctionResolver, XPathVariableResolver and NamespaceContext.

    public void reset() {
        this. = this.;
        this. = this.;
        this. = null;
    }
 
New to GrepCode? Check out our FAQ X