/*=============================================================================*
* 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.ws.resource.properties.ResourcePropertySet;
import org.apache.ws.resource.properties.faults.InvalidQueryExpressionFaultException;
import org.apache.ws.resource.properties.faults.QueryEvaluationErrorFaultException;
import org.apache.ws.resource.properties.impl.XmlBeansResourcePropertySet;
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.resource.i18n.MessagesImpl;
import org.apache.ws.resource.i18n.Keys;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.ws.util.i18n.Messages;
import org.apache.xmlbeans.XmlObject;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.rpc.JAXRPCException;
import java.net.URI;
/**
* LOG-DONE
* Evaluator supporting XPath queries using Xalan-J XPathAPI. The implementation creates resource property document on
* the fly and runs the query against it.
*/
public class XmlBeansXPathExpressionEvaluator
extends AbstractXPathExpressionEvaluator
{
private static final Log LOG = LogFactory.getLog( XmlBeansXPathExpressionEvaluator.class );
public static final Messages MSG = MessagesImpl.getInstance();
private static final URI[] SUPPORTED_DIALECTS = new URI[]
{
QueryConstants.DIALECT_URI__XPATH1_0
};
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public URI[] getSupportedDialects( )
{
return SUPPORTED_DIALECTS;
}
/**
* DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public String toString( )
{
return MSG.getMessage( Keys.XMLBEANS_XPATH_EVAL).intern( );
}
/**
* DOCUMENT_ME
*
* @param xpathExpr DOCUMENT_ME
* @param resourcePropSet DOCUMENT_ME
*
* @return DOCUMENT_ME
*
* @throws UnknownQueryExpressionDialectException
* DOCUMENT_ME
* @throws QueryEvaluationErrorException DOCUMENT_ME
* @throws InvalidQueryExpressionException
* DOCUMENT_ME
* @throws IllegalArgumentException DOCUMENT_ME
* @throws InvalidQueryExpressionFaultException
* DOCUMENT_ME
* @throws JAXRPCException DOCUMENT_ME
* @throws QueryEvaluationErrorFaultException
* DOCUMENT_ME
*/
protected Object evaluate( XPathExpression xpathExpr,
ResourcePropertySet resourcePropSet )
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_RPSET,xpathExpr.toString(),
resourcePropSet.getMetaData().getName().toString()));
}
XmlBeansResourcePropertySet xBeanPropSet;
try
{
xBeanPropSet = (XmlBeansResourcePropertySet) resourcePropSet;
}
catch ( ClassCastException cce )
{
throw new JAXRPCException( MSG.getMessage( Keys.ERROR_XPATH_EXPR_REQ_XMLBEANRPSET,
cce ));
}
XmlObject propsDocXBean = XmlBeanUtils.getRootElement( xBeanPropSet.toXmlObject( ) );
Element nsContextElem = xpathExpr.getNamespaceContext( );
if ( nsContextElem == null )
{
nsContextElem = ( (Document) propsDocXBean.newDomNode( ) ).getDocumentElement( );
}
try
{
return propsDocXBean.selectPath( xpathExpr.getValue( ) );
}
catch ( RuntimeException re )
{
throw new QueryEvaluationErrorFaultException( MSG.getMessage( Keys.QUERY_FAILED, xpathExpr.getValue( ),re.getLocalizedMessage( ) ));
}
}
}