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

📄 xpathexpressionimpl.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        } 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);            }        }    }        /**     * <p>Evaluate the compiled XPath expression in the specified context and     * return the result as a <code>String</code>.</p>     *     * <p>This method calls {@link #evaluate(Object item, QName returnType)}     * with a <code>returnType</code> of     * {@link XPathConstants#STRING}.</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 a <code>null</code> value is provided for     * <code>item</code>, an empty document will be used for the     * context.     *     * @param item The starting context (node or node list, for example).     *     * @return The <code>String</code> that is the result of evaluating the     * expression and converting the result to a     *   <code>String</code>.     *     * @throws XPathExpressionException If the expression cannot be evaluated.     */    public String evaluate(Object item)         throws XPathExpressionException {        return (String)this.evaluate( item, XPathConstants.STRING );    }    static DocumentBuilderFactory dbf = null;    static DocumentBuilder db = null;    static Document d = null;    /**     * <p>Evaluate the compiled XPath expression in the context of the      * specified <code>InputSource</code> and return the result as the     *  specified type.</p>     *     * <p>This method builds a data model for the {@link InputSource} and calls     * {@link #evaluate(Object item, QName returnType)} on the resulting      * document object.</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 <code>source</code> or <code>returnType</code> is <code>null</code>,     * then a <code>NullPointerException</code> is thrown.</p>     *     * @param source The <code>InputSource</code> of the document to evaluate     * over.     * @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>source</code> or      * <code>returnType</code> is <code>null</code>.     */    public Object evaluate(InputSource source, QName returnType)        throws XPathExpressionException {        if ( ( source == null ) || ( returnType == null ) ) {            String fmsg = XSLMessages.createXPATHMessage(                     XPATHErrorResources.ER_SOURCE_RETURN_TYPE_CANNOT_BE_NULL,                    null );            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 {            if ( dbf == null ) {                dbf = DocumentBuilderFactory.newInstance();                dbf.setNamespaceAware( true );                dbf.setValidating( false );            }            db = dbf.newDocumentBuilder();            Document document = db.parse( source );            return eval(  document, returnType );        } catch ( Exception e ) {            throw new XPathExpressionException ( e );        }    }    /**     * <p>Evaluate the compiled XPath expression in the context of the specified <code>InputSource</code> and return the result as a     * <code>String</code>.</p>     *     * <p>This method calls {@link #evaluate(InputSource source, QName returnType)} with a <code>returnType</code> of     * {@link XPathConstants#STRING}.</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>source</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>     *     * @param source The <code>InputSource</code> of the document to evaluate over.     *     * @return The <code>String</code> that is the result of evaluating the expression and converting the result to a     *   <code>String</code>.     *     * @throws XPathExpressionException If the expression cannot be evaluated.     * @throws NullPointerException If  <code>source</code> is <code>null</code>.     */    public String evaluate(InputSource source)        throws XPathExpressionException {        return (String)this.evaluate( source, XPathConstants.STRING );    }    private boolean isSupported( QName returnType ) {        // XPathConstants.STRING        if ( ( returnType.equals( XPathConstants.STRING ) ) ||             ( returnType.equals( XPathConstants.NUMBER ) ) ||             ( returnType.equals( XPathConstants.BOOLEAN ) ) ||             ( returnType.equals( XPathConstants.NODE ) ) ||             ( returnType.equals( XPathConstants.NODESET ) )  ) {                return true;        }        return false;     }     private Object getResultAsType( XObject resultObject, QName returnType )        throws javax.xml.transform.TransformerException {        // XPathConstants.STRING        if ( returnType.equals( XPathConstants.STRING ) ) {            return resultObject.str();        }        // XPathConstants.NUMBER        if ( returnType.equals( XPathConstants.NUMBER ) ) {            return new Double ( resultObject.num());        }        // XPathConstants.BOOLEAN        if ( returnType.equals( XPathConstants.BOOLEAN ) ) {            return new Boolean( resultObject.bool());        }        // XPathConstants.NODESET ---ORdered, UNOrdered???        if ( returnType.equals( XPathConstants.NODESET ) ) {            return resultObject.nodelist();        }        // XPathConstants.NODE        if ( returnType.equals( XPathConstants.NODE ) ) {            NodeIterator ni = resultObject.nodeset();            //Return the first node, or null            return ni.nextNode();        }        // If isSupported check is already done then the execution path         // shouldn't come here. Being defensive        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 + -