Package org.apache.ws.resource.properties.query.xpath.impl

Source Code of org.apache.ws.resource.properties.query.xpath.impl.JaxenXPathExpressionEvaluator$JaxenNamespaceContext

/*=============================================================================*
*  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.ws.resource.properties.query.xpath.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.resource.i18n.Keys;
import org.apache.ws.resource.i18n.MessagesImpl;
import org.apache.ws.resource.properties.ResourcePropertySet;
import org.apache.ws.resource.properties.query.InvalidQueryExpressionException;
import org.apache.ws.resource.properties.query.QueryConstants;
import org.apache.ws.resource.properties.query.QueryEvaluationErrorException;
import org.apache.ws.resource.properties.query.UnknownQueryExpressionDialectException;
import org.apache.ws.resource.properties.query.xpath.AbstractXPathExpressionEvaluator;
import org.apache.ws.resource.properties.query.xpath.XPathExpression;
import org.apache.ws.util.i18n.Messages;
import org.apache.ws.util.xml.NamespaceContext;
import org.apache.xmlbeans.XmlObject;
import org.jaxen.JaxenException;
import org.jaxen.XPath;
import org.jaxen.dom.DOMXPath;
import org.w3c.dom.Node;

import java.net.URI;

/**
* An XPath expression evaluator that utilizes the Jaxen XPath evaluation engine.
*
* LOG-DONE
*/
public class JaxenXPathExpressionEvaluator
        extends AbstractXPathExpressionEvaluator
{
    private static final Log LOG = LogFactory.getLog( JaxenXPathExpressionEvaluator.class );
    private static final Messages MSG = MessagesImpl.getInstance();
    private static final URI[] SUPPORTED_DIALECTS = new URI[]
    {
        QueryConstants.DIALECT_URI__XPATH1_0
    };

    protected Object evaluate( XPathExpression expr, Object evalContext )
            throws UnknownQueryExpressionDialectException, QueryEvaluationErrorException,
            InvalidQueryExpressionException
    {
        Node evalContextNode;
        if ( evalContext instanceof XmlObject )
        {
            evalContextNode = ( (XmlObject) evalContext ).newDomNode();
        }
        else if ( evalContext instanceof Node )
        {
            evalContextNode = (Node) evalContext;
        }
        else
        {
            throw new IllegalArgumentException(
                    "This evaluator requires the evaluation context to be either an XMLBeans XmlObject or a DOM Node." );
        }
        return evaluate( expr, evalContextNode );
    }

    /**
     * Creates a DOM representation of the specified resource property set and
     * evaluates the specified expression against it.
     *
     * @param xpathExpr       DOCUMENT_ME
     * @param resourcePropSet DOCUMENT_ME
     *
     * @return DOCUMENT_ME
     *
     * @throws org.apache.ws.resource.properties.query.UnknownQueryExpressionDialectException
     *                                  DOCUMENT_ME
     * @throws org.apache.ws.resource.properties.query.QueryEvaluationErrorException
     *                                  DOCUMENT_ME
     * @throws org.apache.ws.resource.properties.query.InvalidQueryExpressionException
     *                                  DOCUMENT_ME
     * @throws IllegalArgumentException DOCUMENT_ME
     * @throws org.apache.ws.resource.properties.faults.InvalidQueryExpressionFaultException
     *                                  DOCUMENT_ME
     * @throws org.apache.ws.resource.properties.faults.QueryEvaluationErrorFaultException
     *                                  DOCUMENT_ME
     */
    protected Object evaluate( XPathExpression xpathExpr,
                               ResourcePropertySet resourcePropSet )
            throws UnknownQueryExpressionDialectException,
            QueryEvaluationErrorException,
            InvalidQueryExpressionException
    {
        // TODO: for better performance, we could use the Jaxen Navigator that
        //       comes with XMLBeans v1 to resolve the expression directly
        //       against the resource property set XmlObject; this is low
        //       priority, since XMLBeans v2 no longer includes a Jaxen
        //       integration (it instead includes Saxon)
        return evaluate( xpathExpr, resourcePropSet.toElement() );
    }

    protected Object evaluate( XPathExpression xpathExpr, Node evalContextNode )
            throws UnknownQueryExpressionDialectException, QueryEvaluationErrorException,
            InvalidQueryExpressionException
    {
        if ( xpathExpr == null )
        {
            throw new IllegalArgumentException( MSG.getMessage( Keys.NULL_XPATH ) );
        }
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( MSG.getMessage( Keys.EVAL_XPATH, xpathExpr,
                    evalContextNode.getNodeName() ) );
        }
        if ( xpathExpr.getNamespaceContext() == null || !( xpathExpr.getNamespaceContext() instanceof Node ) )
        {
            throw new QueryEvaluationErrorException( MSG.getMessage( Keys.BAD_XPATH_NAMESPACE_CONTEXT ) );
        }
        try
        {
            XPath xpath = new DOMXPath( xpathExpr.getValue() );
            xpath.setNamespaceContext( new JaxenNamespaceContext( xpathExpr.getNamespaceContext() ) );
            return xpath.evaluate( evalContextNode );
        }
        catch ( JaxenException je )
        {
            throw new QueryEvaluationErrorException( MSG.getMessage( Keys.XPATH_FAILED, xpathExpr.getValue(),
                    je ) );
        }
    }

    /**
     * DOCUMENT_ME
     *
     * @return DOCUMENT_ME
     */
    public URI[] getSupportedDialects()
    {
        return SUPPORTED_DIALECTS;
    }

    /**
     * DOCUMENT_ME
     *
     * @return DOCUMENT_ME
     */
    public String toString()
    {
        return ( MSG.getMessage( Keys.JAXEN_EVALUATOR ) );
    }

    protected class JaxenNamespaceContext implements org.jaxen.NamespaceContext
    {
        private NamespaceContext m_nsContext;

        public JaxenNamespaceContext( NamespaceContext domNsContext )
        {
           m_nsContext = domNsContext;
        }

        public String translateNamespacePrefixToUri( String prefix )
        {
           return m_nsContext.getPrefix( prefix );
        }
    }

}
TOP

Related Classes of org.apache.ws.resource.properties.query.xpath.impl.JaxenXPathExpressionEvaluator$JaxenNamespaceContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.