/*=============================================================================*
* 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.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.i18n.Messages;
import org.apache.xalan.Version;
import org.apache.xpath.XPathAPI;
import org.apache.xpath.objects.XNodeSet;
import org.apache.xpath.objects.XObject;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.rpc.JAXRPCException;
import javax.xml.transform.TransformerException;
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 XalanXPathExpressionEvaluator
extends AbstractXPathExpressionEvaluator
{
private static final Log LOG = LogFactory.getLog( XalanXPathExpressionEvaluator.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.XALAN_VERSION,Version.getVersion( ) + ")" ).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 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,
resourcePropSet.getMetaData().getName().toString()));
}
Element propsDocElem = resourcePropSet.toElement( );
Element nsContextElem = xpathExpr.getNamespaceContext( );
if ( nsContextElem == null )
{
nsContextElem = propsDocElem;
}
XObject xResult;
try
{
xResult = XPathAPI.eval( propsDocElem,
xpathExpr.getValue( ),
nsContextElem );
}
catch ( Exception e )
{
throw new QueryEvaluationErrorFaultException( MSG.getMessage( Keys.XPATH_FAILED, xpathExpr.getValue( ),
e.getLocalizedMessage( )));
}
return normalizeResult( xResult );
}
private Object normalizeResult( XObject xResult )
{
Object result;
if ( xResult instanceof XNodeSet )
{
NodeList nodeList = null;
try
{
nodeList = ( (XNodeSet) xResult ).nodelist( );
}
catch ( TransformerException te )
{
throw new JAXRPCException( te );
}
Node[] nodes = new Node[nodeList.getLength( )];
for ( int i = 0; i < nodeList.getLength( ); i++ )
{
nodes[i] = nodeList.item( i );
}
result = nodes;
}
else
{
result = xResult;
}
return result;
}
}