You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by mk...@apache.org on 2005/05/17 19:24:26 UTC

cvs commit: xml-xalan/java/src/org/apache/xpath/jaxp JAXPExtensionsProvider.java JAXPPrefixResolver.java JAXPVariableStack.java XPathExpressionImpl.java XPathFactoryImpl.java XPathImpl.java

mkwan       2005/05/17 10:24:26

  Added:       java/src/org/apache/xpath/jaxp JAXPExtensionsProvider.java
                        JAXPPrefixResolver.java JAXPVariableStack.java
                        XPathExpressionImpl.java XPathFactoryImpl.java
                        XPathImpl.java
  Log:
  JAXP 1.3 integration
  Commit the XPath API implementation under org.apache.xpath.jaxp
  
  Revision  Changes    Path
  1.1                  xml-xalan/java/src/org/apache/xpath/jaxp/JAXPExtensionsProvider.java
  
  Index: JAXPExtensionsProvider.java
  ===================================================================
  /*
   * Copyright 1999-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.
   */
  
  // $Id: JAXPExtensionsProvider.java,v 1.1 2005/05/17 17:24:26 mkwan Exp $
  
  package org.apache.xpath.jaxp;
  
  import javax.xml.transform.TransformerException;
  import javax.xml.xpath.XPathFunctionResolver;
  import javax.xml.xpath.XPathFunction;
  import javax.xml.xpath.XPathFunctionException;
  
  import org.apache.xpath.ExtensionsProvider;
  import org.apache.xpath.XPathContext;
  import org.apache.xpath.objects.XObject;
  import org.apache.xpath.objects.XNodeSet;
  import org.apache.xpath.res.XPATHErrorResources;
  import org.apache.xalan.res.XSLMessages;
  
  import org.apache.xpath.functions.FuncExtFunction;
  import java.util.Vector;
  import java.util.ArrayList;
  import javax.xml.namespace.QName;
  
  /**
   * 
   * @author Ramesh Mandava ( ramesh.mandava@sun.com )
   */
  public class JAXPExtensionsProvider implements ExtensionsProvider {
      	
      private final XPathFunctionResolver resolver;
      private boolean extensionInvocationDisabled = false;
  	
      public JAXPExtensionsProvider(XPathFunctionResolver resolver) {
          this.resolver = resolver;
          this.extensionInvocationDisabled = false;
      }
  
      public JAXPExtensionsProvider(XPathFunctionResolver resolver, 
          boolean featureSecureProcessing ) {
          this.resolver = resolver;
          this.extensionInvocationDisabled = featureSecureProcessing;
      }
  
      /**
       * Is the extension function available?
       */
  
      public boolean functionAvailable(String ns, String funcName)
            throws javax.xml.transform.TransformerException {
        try {
          if ( funcName == null ) {
              String fmsg = XSLMessages.createXPATHMessage( 
                  XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
                  new Object[] {"Function Name"} );
              throw new NullPointerException ( fmsg ); 
          }
          //Find the XPathFunction corresponding to namespace and funcName
          javax.xml.namespace.QName myQName = new QName( ns, funcName );
          javax.xml.xpath.XPathFunction xpathFunction = 
              resolver.resolveFunction ( myQName, 0 );
          if (  xpathFunction == null ) {
              return false;
          }
          return true;
        } catch ( Exception e ) {
          return false;
        }
         
  
      }
          
  
      /**
       * Is the extension element available?
       */
      public boolean elementAvailable(String ns, String elemName)
            throws javax.xml.transform.TransformerException {
          return false;
      }
  
      /**
       * Execute the extension function.
       */
      public Object extFunction(String ns, String funcName, Vector argVec,
          Object methodKey) throws javax.xml.transform.TransformerException {
          try {
  
              if ( funcName == null ) {
                  String fmsg = XSLMessages.createXPATHMessage(
                      XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
                      new Object[] {"Function Name"} );
                  throw new NullPointerException ( fmsg ); 
              }
              //Find the XPathFunction corresponding to namespace and funcName
              javax.xml.namespace.QName myQName = new QName( ns, funcName );
  
              // JAXP 1.3 spec says When XMLConstants.FEATURE_SECURE_PROCESSING 
              // feature is set then invocation of extension functions need to
              // throw XPathFunctionException
              if ( extensionInvocationDisabled ) {
                  String fmsg = XSLMessages.createXPATHMessage(
                      XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED,
                      new Object[] { myQName.toString() } );
                  throw new XPathFunctionException ( fmsg );
              }
  
              // Assuming user is passing all the needed parameters ( including
              // default values )
              int arity = argVec.size();
  
              javax.xml.xpath.XPathFunction xpathFunction = 
                  resolver.resolveFunction ( myQName, arity );
  
              // not using methodKey
              ArrayList argList = new ArrayList( arity);
              for ( int i=0; i<arity; i++ ) {
                  Object argument = argVec.elementAt( i );
                  // XNodeSet object() returns NodeVector and not NodeList
                  // Explicitly getting NodeList by using nodelist()
                  if ( argument instanceof XNodeSet ) {
                      argList.add ( i, ((XNodeSet)argument).nodelist() );
                  } else if ( argument instanceof XObject ) {
                      Object passedArgument = ((XObject)argument).object();
                      argList.add ( i, passedArgument );
                  } else {
                      argList.add ( i, argument );
                  }
              }
  
              return ( xpathFunction.evaluate ( argList ));
          } catch ( XPathFunctionException xfe ) {
              // If we get XPathFunctionException then we want to terminate
              // further execution by throwing WrappedRuntimeException 
              throw new org.apache.xml.utils.WrappedRuntimeException ( xfe );
          } catch ( Exception e ) {
              throw new javax.xml.transform.TransformerException ( e );
          }
      
      }
  
      /**
       * Execute the extension function.
       */
      public Object extFunction(FuncExtFunction extFunction,
                                Vector argVec)
          throws javax.xml.transform.TransformerException {
          try {
              String namespace = extFunction.getNamespace();
              String functionName = extFunction.getFunctionName();
              int arity = extFunction.getArgCount();
              javax.xml.namespace.QName myQName = 
                  new javax.xml.namespace.QName( namespace, functionName );
  
              // JAXP 1.3 spec says  When XMLConstants.FEATURE_SECURE_PROCESSING
              // feature is set then invocation of extension functions need to
              // throw XPathFunctionException
              if ( extensionInvocationDisabled ) {
                  String fmsg = XSLMessages.createXPATHMessage(
                      XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED,                    new Object[] { myQName.toString() } );
                  throw new XPathFunctionException ( fmsg );
              }
  
              XPathFunction xpathFunction = 
                  resolver.resolveFunction( myQName, arity );
  
              ArrayList argList = new ArrayList( arity);
              for ( int i=0; i<arity; i++ ) {
                  Object argument = argVec.elementAt( i );
                  // XNodeSet object() returns NodeVector and not NodeList
                  // Explicitly getting NodeList by using nodelist()
                  if ( argument instanceof XNodeSet ) {
                      argList.add ( i, ((XNodeSet)argument).nodelist() );
                  } else if ( argument instanceof XObject ) {
                      Object passedArgument = ((XObject)argument).object();
                      argList.add ( i, passedArgument );
                  } else {
                      argList.add ( i, argument );
                  }
              }
         
              return ( xpathFunction.evaluate ( argList ));
  
          } catch ( XPathFunctionException xfe ) {
              // If we get XPathFunctionException then we want to terminate 
              // further execution by throwing WrappedRuntimeException
              throw new org.apache.xml.utils.WrappedRuntimeException ( xfe );
          } catch ( Exception e ) {
              throw new javax.xml.transform.TransformerException ( e );
          }
      }
  
  }
  
  
  
  1.1                  xml-xalan/java/src/org/apache/xpath/jaxp/JAXPPrefixResolver.java
  
  Index: JAXPPrefixResolver.java
  ===================================================================
  /*
   * Copyright 1999-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.
   */
  // $Id: JAXPPrefixResolver.java,v 1.1 2005/05/17 17:24:26 mkwan Exp $
  
  package org.apache.xpath.jaxp;
  
  import org.w3c.dom.Node;
  import org.w3c.dom.NamedNodeMap;
  import org.apache.xml.utils.PrefixResolver;
  
  import javax.xml.namespace.NamespaceContext;
  
  /**
   * <meta name="usage" content="general"/>
   * This class implements a Default PrefixResolver which
   * can be used to perform prefix-to-namespace lookup
   * for the XPath object.
   * This class delegates the resolution to the passed NamespaceContext
   */
  public class JAXPPrefixResolver implements PrefixResolver
  {
  
      private NamespaceContext namespaceContext;
      
  
      public JAXPPrefixResolver ( NamespaceContext nsContext ) {
          this.namespaceContext = nsContext;
      } 
  
  
      public String getNamespaceForPrefix( String prefix ) {
          return namespaceContext.getNamespaceURI( prefix );
      }
  
      /**
       * Return the base identifier.
       *
       * @return null
       */
      public String getBaseIdentifier() {
          return null;
      }
  
      /**
       * @see PrefixResolver#handlesNullPrefixes() 
       */
      public boolean handlesNullPrefixes() {
          return false;
      }
  
  
      /**
       * The URI for the XML namespace.
       * (Duplicate of that found in org.apache.xpath.XPathContext). 
       */
       
      public static final String S_XMLNAMESPACEURI =
          "http://www.w3.org/XML/1998/namespace";
  
  
      /**
       * Given a prefix and a Context Node, get the corresponding namespace.
       * Warning: This will not work correctly if namespaceContext
       * is an attribute node.
       * @param prefix Prefix to resolve.
       * @param namespaceContext Node from which to start searching for a
       * xmlns attribute that binds a prefix to a namespace.
       * @return Namespace that prefix resolves to, or null if prefix
       * is not bound.
       */
      public String getNamespaceForPrefix(String prefix,
                                        org.w3c.dom.Node namespaceContext) {
          Node parent = namespaceContext;
          String namespace = null;
  
          if (prefix.equals("xml")) {
              namespace = S_XMLNAMESPACEURI;
          } else {
              int type;
  
              while ((null != parent) && (null == namespace)
                  && (((type = parent.getNodeType()) == Node.ELEMENT_NODE)
                      || (type == Node.ENTITY_REFERENCE_NODE))) {
  
                  if (type == Node.ELEMENT_NODE) {
                      NamedNodeMap nnm = parent.getAttributes();
  
                      for (int i = 0; i < nnm.getLength(); i++) {
                          Node attr = nnm.item(i);
                          String aname = attr.getNodeName();
                          boolean isPrefix = aname.startsWith("xmlns:");
  
                          if (isPrefix || aname.equals("xmlns")) {
                              int index = aname.indexOf(':');
                              String p =isPrefix ?aname.substring(index + 1) :"";
  
                              if (p.equals(prefix)) {
                                  namespace = attr.getNodeValue();
                                  break;
                              }
                          }
                      }
                  }
  
                  parent = parent.getParentNode();
              }
          }
          return namespace;
      }
  
  }
  
  
  
  
  1.1                  xml-xalan/java/src/org/apache/xpath/jaxp/JAXPVariableStack.java
  
  Index: JAXPVariableStack.java
  ===================================================================
  /*
   * Copyright 1999-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.
   */
  // $Id: JAXPVariableStack.java,v 1.1 2005/05/17 17:24:26 mkwan Exp $
  
  package org.apache.xpath.jaxp;
  
  import javax.xml.transform.TransformerException;
  import javax.xml.xpath.XPathVariableResolver;
  
  import org.apache.xml.utils.QName;
  import org.apache.xpath.VariableStack;
  import org.apache.xpath.XPathContext;
  import org.apache.xpath.objects.XObject;
  
  import org.apache.xpath.res.XPATHErrorResources;
  import org.apache.xalan.res.XSLMessages;
  
  
  /**
   * Overrides {@link VariableStack} and delegates the call to
   * {@link javax.xml.xpath.XPathVariableResolver}.
   * 
   * @author Ramesh Mandava ( ramesh.mandava@sun.com )
   */
  public class JAXPVariableStack extends VariableStack {
      	
      private final XPathVariableResolver resolver;
  	
      public JAXPVariableStack(XPathVariableResolver resolver) {
          this.resolver = resolver;
      }
  	
      public XObject getVariableOrParam(XPathContext xctxt, QName qname)
          throws TransformerException,IllegalArgumentException {
          if ( qname == null ) {
              //JAXP 1.3 spec says that if variable name is null then 
              // we need to through IllegalArgumentException
              String fmsg = XSLMessages.createXPATHMessage( 
                  XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
                  new Object[] {"Variable qname"} );
              throw new IllegalArgumentException( fmsg );
          }
  	javax.xml.namespace.QName name =
  	    new javax.xml.namespace.QName(
                  qname.getNamespace(),
                  qname.getLocalPart());
          Object varValue = resolver.resolveVariable( name );		
          if ( varValue == null ) {
              String fmsg = XSLMessages.createXPATHMessage( 
                  XPATHErrorResources.ER_RESOLVE_VARIABLE_RETURNS_NULL,
                  new Object[] { name.toString()} );
              throw new TransformerException( fmsg );
          } 
          return XObject.create( varValue, xctxt );
      }
  
  }
  
  
  
  1.1                  xml-xalan/java/src/org/apache/xpath/jaxp/XPathExpressionImpl.java
  
  Index: XPathExpressionImpl.java
  ===================================================================
  /*
   * Copyright 1999-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.
   */
  // $Id: XPathExpressionImpl.java,v 1.1 2005/05/17 17:24:26 mkwan Exp $
  
  package org.apache.xpath.jaxp;
  
  import org.apache.xpath.*;
  import javax.xml.transform.TransformerException;
  
  import org.apache.xpath.objects.XObject;
  import org.apache.xml.utils.PrefixResolver;
  import org.apache.xpath.res.XPATHErrorResources;
  import org.apache.xalan.res.XSLMessages;
  
  import javax.xml.namespace.NamespaceContext;
  import javax.xml.namespace.QName;
  import javax.xml.xpath.XPathExpressionException;
  import javax.xml.xpath.XPathConstants;
  import javax.xml.xpath.XPathFunctionResolver;
  import javax.xml.xpath.XPathVariableResolver;
  import javax.xml.xpath.XPathConstants;
  
  import org.w3c.dom.Node;
  import org.w3c.dom.Document;
  import org.w3c.dom.DOMImplementation;
  import org.w3c.dom.traversal.NodeIterator;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.DocumentBuilder;
  
  import org.xml.sax.InputSource;
  
  /**
   * The XPathExpression interface encapsulates a (compiled) XPath expression.
   *
   * @version $Revision: 1.1 $
   * @author  Ramesh Mandava
   */
  public class XPathExpressionImpl  implements javax.xml.xpath.XPathExpression{
  
      private XPathFunctionResolver functionResolver;
      private XPathVariableResolver variableResolver;
      private JAXPPrefixResolver prefixResolver;
      private org.apache.xpath.XPath xpath;
  
      // 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;
  
      /** Protected constructor to prevent direct instantiation; use compile()
       * from the context.
       */
      protected XPathExpressionImpl() { };
  
      protected XPathExpressionImpl(org.apache.xpath.XPath xpath, 
              JAXPPrefixResolver prefixResolver, 
              XPathFunctionResolver functionResolver,
              XPathVariableResolver variableResolver ) { 
          this.xpath = xpath;
          this.prefixResolver = prefixResolver;
          this.functionResolver = functionResolver;
          this.variableResolver = variableResolver;
          this.featureSecureProcessing = false;
      };
  
      protected XPathExpressionImpl(org.apache.xpath.XPath xpath,
              JAXPPrefixResolver prefixResolver,
              XPathFunctionResolver functionResolver,
              XPathVariableResolver variableResolver,
              boolean featureSecureProcessing ) { 
          this.xpath = xpath;
          this.prefixResolver = prefixResolver;
          this.functionResolver = functionResolver;
          this.variableResolver = variableResolver;
          this.featureSecureProcessing = featureSecureProcessing;
      };
  
      public void setXPath (org.apache.xpath.XPath xpath ) {
          this.xpath = xpath;
      }  
  
      public Object eval(Object item, QName returnType)
              throws javax.xml.transform.TransformerException {
          XObject resultObject = eval ( item );
          return getResultAsType( resultObject, returnType );
      }
      
      private XObject eval ( Object contextItem )
              throws javax.xml.transform.TransformerException {
          org.apache.xpath.XPathContext xpathSupport = null;
          if ( functionResolver != null ) {
              JAXPExtensionsProvider jep = new JAXPExtensionsProvider(
                      functionResolver, featureSecureProcessing );
              xpathSupport = new org.apache.xpath.XPathContext( jep );
          } else {
              xpathSupport = new org.apache.xpath.XPathContext();
          }
  
          xpathSupport.setVarStack(new JAXPVariableStack(variableResolver));
          XObject xobj = null;
            
          Node contextNode = (Node)contextItem;
          // We always need to have a ContextNode with Xalan XPath implementation
          // To allow simple expression evaluation like 1+1 we are setting 
          // dummy Document as Context Node
          if ( contextNode == null ) {
                contextNode = getDummyDocument();
          } 
  
          xobj = xpath.execute(xpathSupport, contextNode, prefixResolver );
          return xobj;
      }
  
  
      /**
       * <p>Evaluate the compiled XPath expression in the specified context and
       *  return the result as the specified type.</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 a <code>null</code> value is provided for
       * <code>item</code>, an empty document will be used for the
       * context.
       * If <code>returnType</code> is <code>null</code>, then a 
       * <code>NullPointerException</code> is thrown.</p>
       *
       * @param item The starting context (node or node list, for example).
       * @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>returnType</code> is
       * <code>null</code>.
       */
      public Object evaluate(Object item, QName returnType)
          throws XPathExpressionException {
          //Validating parameters to enforce constraints defined by JAXP spec
          if ( returnType == null ) {
             //Throwing NullPointerException as defined in spec
              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 { 
              return eval( item, returnType);
          } 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);
              }
          }
  
      }
      
      /**
       * <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 );
      }
  
  
      private static Document getDummyDocument( ) {
          try {
              if ( dbf == null ) {
                  dbf = DocumentBuilderFactory.newInstance();
                  dbf.setNamespaceAware( true );
                  dbf.setValidating( false );
              }
              db = dbf.newDocumentBuilder();
  
              DOMImplementation dim = db.getDOMImplementation();
              d = dim.createDocument("http://java.sun.com/jaxp/xpath",
                  "dummyroot", null);
              return d;
          } catch ( Exception e ) {
              e.printStackTrace();
          }
          return null;
      }
  
  
  
  
  }
  
  
  
  1.1                  xml-xalan/java/src/org/apache/xpath/jaxp/XPathFactoryImpl.java
  
  Index: XPathFactoryImpl.java
  ===================================================================
  /*
   * Copyright 1999-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.
   */
  // $Id: XPathFactoryImpl.java,v 1.1 2005/05/17 17:24:26 mkwan Exp $
  
  package org.apache.xpath.jaxp;
  
  import org.apache.xpath.res.XPATHErrorResources;
  import org.apache.xalan.res.XSLMessages;
  
  import javax.xml.XMLConstants;
  import javax.xml.xpath.XPathFactory;
  import javax.xml.xpath.XPathFactoryConfigurationException;
  import javax.xml.xpath.XPathFunctionResolver;
  import javax.xml.xpath.XPathVariableResolver;
  
  /**
   * The XPathFactory builds XPaths.
   *
   * @version $Revision: 1.1 $
   * @author  Ramesh Mandava
   */
  public  class XPathFactoryImpl extends XPathFactory {
  	
  	/**
  	 * <p>Name of class as a constant to use for debugging.</p>
  	 */
  	private static final String CLASS_NAME = "XPathFactoryImpl";
  	
  	/**
  	 *<p>XPathFunctionResolver for this XPathFactory and created XPaths.</p>
  	 */
  	private XPathFunctionResolver xPathFunctionResolver = null;
  	
  	/**
  	 * <p>XPathVariableResolver for this XPathFactory and created XPaths</p>
  	 */
  	private XPathVariableResolver xPathVariableResolver = null;
  
  	/**
  	 * <p>State of secure processing feature.</p>
  	 */
  	private boolean featureSecureProcessing = false;
  		
  	/**
  	 * <p>Is specified object model supported by this 
           * <code>XPathFactory</code>?</p>
  	 * 
  	 * @param objectModel Specifies the object model which the returned
           * <code>XPathFactory</code> will understand.
  	 *  
  	 * @return <code>true</code> if <code>XPathFactory</code> supports 
           * <code>objectModel</code>, else <code>false</code>.
  	 * 
  	 * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
  	 * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
  	 */
  	public boolean isObjectModelSupported(String objectModel) {
  		
              if (objectModel == null) {
                  String fmsg = XSLMessages.createXPATHMessage(
                          XPATHErrorResources.ER_OBJECT_MODEL_NULL,
                          new Object[] { this.getClass().getName() } );
  
                  throw new NullPointerException( fmsg );
              }
  		
              if (objectModel.length() == 0) {
                  String fmsg = XSLMessages.createXPATHMessage(
                          XPATHErrorResources.ER_OBJECT_MODEL_EMPTY,
                          new Object[] { this.getClass().getName() } );
                  throw new IllegalArgumentException( fmsg );
              }
  		
  	    // know how to support default object model, W3C DOM
              if (objectModel.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) {
                  return true;
              }
  		
              // don't know how to support anything else
              return false;
  	}
  
          /**
           * <p>Returns a new <code>XPath</code> object using the underlying
           * object model determined when the factory was instantiated.</p>
  	 * 
  	 * @return New <code>XPath</code>
  	 */
  	public javax.xml.xpath.XPath newXPath() {
  	    return new org.apache.xpath.jaxp.XPathImpl(
                      xPathVariableResolver, xPathFunctionResolver,
                      featureSecureProcessing );
  	}
  	    
  	/**
  	 * <p>Set a feature for this <code>XPathFactory</code> and 
           * <code>XPath</code>s created by this factory.</p>
  	 * 
  	 * <p>
  	 * Feature names are fully qualified {@link java.net.URI}s.
  	 * Implementations may define their own features.
  	 * An {@link XPathFactoryConfigurationException} is thrown if this
           * <code>XPathFactory</code> or the <code>XPath</code>s
  	 *  it creates cannot support the feature.
  	 * It is possible for an <code>XPathFactory</code> to expose a feature
           * value but be unable to change its state.
  	 * </p>
  	 * 
  	 * <p>See {@link javax.xml.xpath.XPathFactory} for full documentation
           * of specific features.</p>
  	 * 
  	 * @param name Feature name.
  	 * @param value Is feature state <code>true</code> or <code>false</code>.
  	 *  
  	 * @throws XPathFactoryConfigurationException if this 
           * <code>XPathFactory</code> or the <code>XPath</code>s
  	 *   it creates cannot support this feature.
           * @throws NullPointerException if <code>name</code> is 
           * <code>null</code>.
  	 */
  	public void setFeature(String name, boolean value)
  		throws XPathFactoryConfigurationException {
  			
              // feature name cannot be null
              if (name == null) {
                  String fmsg = XSLMessages.createXPATHMessage(
                          XPATHErrorResources.ER_FEATURE_NAME_NULL,
                          new Object[] { CLASS_NAME, new Boolean( value) } );
                  throw new NullPointerException( fmsg );
               }
  		
              // secure processing?
              if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
  
                  featureSecureProcessing = value;
  						
                  // all done processing feature
                  return;
              }
  		
              // unknown feature
              String fmsg = XSLMessages.createXPATHMessage(
                      XPATHErrorResources.ER_FEATURE_UNKNOWN,
                      new Object[] { name, CLASS_NAME, new Boolean(value) } );
              throw new XPathFactoryConfigurationException( fmsg );
  	}
  
  	/**
  	 * <p>Get the state of the named feature.</p>
  	 * 
  	 * <p>
  	 * Feature names are fully qualified {@link java.net.URI}s.
  	 * Implementations may define their own features.
  	 * An {@link XPathFactoryConfigurationException} is thrown if this
           * <code>XPathFactory</code> or the <code>XPath</code>s
  	 * it creates cannot support the feature.
  	 * It is possible for an <code>XPathFactory</code> to expose a feature 
           * value but be unable to change its state.
  	 * </p>
  	 * 
  	 * @param name Feature name.
  	 * 
  	 * @return State of the named feature.
  	 * 
  	 * @throws XPathFactoryConfigurationException if this 
           * <code>XPathFactory</code> or the <code>XPath</code>s
  	 *   it creates cannot support this feature.
           * @throws NullPointerException if <code>name</code> is 
           * <code>null</code>.
  	 */
  	public boolean getFeature(String name)
  		throws XPathFactoryConfigurationException {
  
              // feature name cannot be null
              if (name == null) {
                  String fmsg = XSLMessages.createXPATHMessage(
                          XPATHErrorResources.ER_GETTING_NULL_FEATURE,
                          new Object[] { CLASS_NAME } );
                  throw new NullPointerException( fmsg );
              }
  		
              // secure processing?
              if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
                  return featureSecureProcessing;
              }
  		
              // unknown feature
              String fmsg = XSLMessages.createXPATHMessage(
                      XPATHErrorResources.ER_GETTING_UNKNOWN_FEATURE,
                      new Object[] { name, CLASS_NAME } );
  
              throw new XPathFactoryConfigurationException( fmsg );
          }
  		
  	/**
           * <p>Establish a default function resolver.</p>
           * 
  	 * <p>Any <code>XPath</code> objects constructed from this factory will use
  	 * the specified resolver by default.</p>
  	 *
  	 * <p>A <code>NullPointerException</code> is thrown if 
           * <code>resolver</code> is <code>null</code>.</p>
           * 
  	 * @param resolver XPath function resolver.
  	 * 
  	 * @throws NullPointerException If <code>resolver</code> is 
           * <code>null</code>.
  	 */
          public void setXPathFunctionResolver(XPathFunctionResolver resolver) {
  			
              // resolver cannot be null
              if (resolver == null) {
                  String fmsg = XSLMessages.createXPATHMessage(
                          XPATHErrorResources.ER_NULL_XPATH_FUNCTION_RESOLVER,
                          new Object[] {  CLASS_NAME } );
                  throw new NullPointerException( fmsg );
              }
  			
              xPathFunctionResolver = resolver;
          }
  		
  	/**
  	 * <p>Establish a default variable resolver.</p>
  	 *
  	 * <p>Any <code>XPath</code> objects constructed from this factory will use
  	 * the specified resolver by default.</p>
  	 * 
  	 * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p>
  	 * 
  	 * @param resolver Variable resolver.
  	 * 
  	 *  @throws NullPointerException If <code>resolver</code> is 
           * <code>null</code>.
  	 */
  	public void setXPathVariableResolver(XPathVariableResolver resolver) {
  
  		// resolver cannot be null
  		if (resolver == null) {
                      String fmsg = XSLMessages.createXPATHMessage(
                              XPATHErrorResources.ER_NULL_XPATH_VARIABLE_RESOLVER,
                              new Object[] {  CLASS_NAME } );
  		    throw new NullPointerException( fmsg );
  		}
  			
  		xPathVariableResolver = resolver;
  	}
  }
  
  
  
  
  
  
  1.1                  xml-xalan/java/src/org/apache/xpath/jaxp/XPathImpl.java
  
  Index: XPathImpl.java
  ===================================================================
  /*
   * Copyright 1999-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.
   */
  // $Id: XPathImpl.java,v 1.1 2005/05/17 17:24:26 mkwan Exp $
  
  package org.apache.xpath.jaxp;
  
  import javax.xml.namespace.QName;
  import javax.xml.namespace.NamespaceContext;
  import javax.xml.xpath.XPathExpressionException;
  import javax.xml.xpath.XPathConstants;
  import javax.xml.xpath.XPathFunctionResolver;
  import javax.xml.xpath.XPathVariableResolver;
  import javax.xml.xpath.XPathExpression;
  
  import org.apache.xml.dtm.DTM;
  import org.apache.xpath.*;
  import org.apache.xpath.objects.XObject;
  import org.apache.xpath.res.XPATHErrorResources;
  import org.apache.xalan.res.XSLMessages;
  
  import org.w3c.dom.Node;
  import org.w3c.dom.DOMImplementation;
  import org.w3c.dom.Document;
  import org.w3c.dom.traversal.NodeIterator;
  
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  import javax.xml.parsers.*;
  
  import java.io.IOException;
  
  /**
   * The XPathImpl class provides implementation for the methods defined  in
   * javax.xml.xpath.XPath interface. This provide simple access to the results
   * of an XPath expression.
   *
   *
   * @version $Revision: 1.1 $
   * @author  Ramesh Mandava
   */
  public class XPathImpl implements javax.xml.xpath.XPath {
  
      // Private variables
      private XPathVariableResolver variableResolver;
      private XPathFunctionResolver functionResolver;
      private XPathVariableResolver origVariableResolver;
      private XPathFunctionResolver origFunctionResolver;
      private NamespaceContext namespaceContext=null;
      private 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; 
  
      XPathImpl( XPathVariableResolver vr, XPathFunctionResolver fr ) {
          this.origVariableResolver = this.variableResolver = vr;
          this.origFunctionResolver = this.functionResolver = fr;
      }
  
      XPathImpl( XPathVariableResolver vr, XPathFunctionResolver fr, 
              boolean featureSecureProcessing ) {
          this.origVariableResolver = this.variableResolver = vr;
          this.origFunctionResolver = this.functionResolver = fr;
          this.featureSecureProcessing = featureSecureProcessing;
      }
  
      /**
       * <p>Establishes a variable resolver.</p>
       *
       * @param 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 NullPointerException( fmsg );
          }
          this.variableResolver = resolver;
      }
  
      /**
       * <p>Returns the current variable resolver.</p>
       *
       * @return Current variable resolver
       */
      public XPathVariableResolver getXPathVariableResolver() {
          return variableResolver;
      }
  
      /**
       * <p>Establishes a function resolver.</p>
       *
       * @param 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 NullPointerException( fmsg );
          }
          this.functionResolver = resolver;
      }
  
      /**
       * <p>Returns the current function resolver.</p>
       *
       * @return Current function resolver
       */
      public XPathFunctionResolver getXPathFunctionResolver() {
          return functionResolver;
      }
  
      /**
       * <p>Establishes a namespace context.</p>
       *
       * @param 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 NullPointerException( fmsg ); 
          }
          this.namespaceContext = nsContext;
          this.prefixResolver = new JAXPPrefixResolver ( nsContext );
      }
  
      /**
       * <p>Returns the current namespace context.</p>
       *
       * @return Current Namespace context
       */
      public NamespaceContext getNamespaceContext() {
          return namespaceContext;
      }
  
      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.setNamespaceAware( true );
              dbf.setValidating( false );
              return dbf.newDocumentBuilder();
          } catch (ParserConfigurationException e) {
              // this should never happen with a well-behaving JAXP implementation. 
              throw new Error(e.toString());
          }
      }
  
      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(d==null) {
              DOMImplementation dim = getParser().getDOMImplementation();
              d = dim.createDocument("http://java.sun.com/jaxp/xpath",
                  "dummyroot", null);
          }
          return d;
      }
  
      
      private XObject eval(String expression, Object contextItem)
          throws javax.xml.transform.TransformerException {
          org.apache.xpath.XPath xpath = new org.apache.xpath.XPath( expression,
              null, prefixResolver, org.apache.xpath.XPath.SELECT ); 
          org.apache.xpath.XPathContext xpathSupport = null;
          if ( functionResolver != null ) {
              JAXPExtensionsProvider jep = new JAXPExtensionsProvider(
                      functionResolver, featureSecureProcessing );
              xpathSupport = new org.apache.xpath.XPathContext( jep );
          } else { 
              xpathSupport = new org.apache.xpath.XPathContext();
          }
  
          XObject xobj = null;
          
          xpathSupport.setVarStack(new JAXPVariableStack(variableResolver));
          
          // If item is null, then we will create a a Dummy contextNode
          if ( contextItem instanceof Node ) {
              xobj = xpath.execute (xpathSupport, (Node)contextItem,
                      prefixResolver );
          } else {
              xobj = xpath.execute ( xpathSupport, DTM.NULL, prefixResolver );
          }
   
          return xobj;
      }
          
      /**
       * <p>Evaluate an <code>XPath</code> expression in the specified context and return the result as the specified type.</p>
       *
       * <p>See "Evaluation of XPath Expressions" section of JAXP 1.3 spec
       * for context item evaluation,
       * variable, function and <code>QName</code> resolution and return type conversion.</p>
       *
       * <p>If <code>returnType</code> is not one of the types defined in {@link XPathConstants} (
       * {@link XPathConstants#NUMBER NUMBER},
       * {@link XPathConstants#STRING STRING},
       * {@link XPathConstants#BOOLEAN BOOLEAN},
       * {@link XPathConstants#NODE NODE} or
       * {@link XPathConstants#NODESET NODESET})
       * then an <code>IllegalArgumentException</code> is thrown.</p>
       *
       * <p>If a <code>null</code> value is provided for
       * <code>item</code>, an empty document will be used for the
       * context.
       * If <code>expression</code> or <code>returnType</code> is <code>null</code>, then a
       * <code>NullPointerException</code> is thrown.</p>
       *
       * @param expression The XPath expression.
       * @param item The starting context (node or node list, for example).
       * @param returnType The desired return type.
       *
       * @return Result of evaluating an XPath expression as an <code>Object</code> of <code>returnType</code>.
       *
       * @throws XPathExpressionException If <code>expression</code> cannot be evaluated.
       * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
       * @throws NullPointerException If <code>expression</code> or <code>returnType</code> is <code>null</code>.
       */
      public Object evaluate(String expression, Object item, QName 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
          if ( !isSupported ( returnType ) ) {
              String fmsg = XSLMessages.createXPATHMessage(
                      XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
                      new Object[] { returnType.toString() } );
              throw new IllegalArgumentException ( fmsg );
          }
  
          try {
   
              XObject resultObject = eval( expression, item );
              return getResultAsType( resultObject, returnType );
          } 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 isSupported( QName returnType ) {
          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();
          }
          String fmsg = XSLMessages.createXPATHMessage(
                  XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
                  new Object[] { returnType.toString()});
          throw new IllegalArgumentException( fmsg );
      }
           
              
          
      /**
       * <p>Evaluate an XPath expression in the specified context and return the result as a <code>String</code>.</p>
       *
       * <p>This method calls {@link #evaluate(String expression, Object item, QName returnType)} with a <code>returnType</code> of
       * {@link XPathConstants#STRING}.</p>
       *
       * <p>See "Evaluation of XPath Expressions" 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.
       * If <code>expression</code> is <code>null</code>, then a <code>NullPointerException</code> is thrown.</p>
       *
       * @param expression The XPath expression.
       * @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 <code>expression</code> cannot be evaluated.
       * @throws NullPointerException If <code>expression</code> is <code>null</code>.
       */
      public String evaluate(String expression, Object item)
          throws XPathExpressionException {
          return (String)this.evaluate( expression, item, XPathConstants.STRING );
      }
  
      /**
       * <p>Compile an XPath expression for later evaluation.</p>
       *
       * <p>If <code>expression</code> contains any {@link XPathFunction}s,
       * they must be available via the {@link XPathFunctionResolver}.
       * An {@link XPathExpressionException} will be thrown if the <code>XPathFunction</code>
       * cannot be resovled with the <code>XPathFunctionResolver</code>.</p>
       * 
       * <p>If <code>expression</code> is <code>null</code>, a <code>NullPointerException</code> is thrown.</p>
       *
       * @param expression The XPath expression.
       *
       * @return Compiled XPath expression.
  
       * @throws XPathExpressionException If <code>expression</code> cannot be compiled.
       * @throws NullPointerException If <code>expression</code> is <code>null</code>.
       */
      public XPathExpression compile(String expression)
          throws XPathExpressionException {
          if ( expression == null ) {
              String fmsg = XSLMessages.createXPATHMessage( 
                      XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
                      new Object[] {"XPath expression"} );
              throw new NullPointerException ( fmsg );
          }
          try {
              org.apache.xpath.XPath xpath = new XPath (expression, null,
                      prefixResolver, org.apache.xpath.XPath.SELECT );
              // Can have errorListener
              XPathExpressionImpl ximpl = new XPathExpressionImpl (xpath,
                      prefixResolver, functionResolver, variableResolver,
                      featureSecureProcessing );
              return ximpl;
          } catch ( javax.xml.transform.TransformerException te ) {
              throw new XPathExpressionException ( te ) ;
          }
      }
  
  
      /**
       * <p>Evaluate an 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(String expression, 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>expression</code>, <code>source</code> or <code>returnType</code> is <code>null</code>,
       * then a <code>NullPointerException</code> is thrown.</p>
       *
       * @param expression The XPath expression.
       * @param source The input source of the document to evaluate over.
       * @param returnType The desired return type.
       *
       * @return The <code>Object</code> that encapsulates the result of evaluating the expression.
       *
       * @throws XPathExpressionException If expression cannot be evaluated.
       * @throws IllegalArgumentException If <code>returnType</code> is not one of the types defined in {@link XPathConstants}.
       * @throws NullPointerException If <code>expression</code>, <code>source</code> or <code>returnType</code>
       *   is <code>null</code>.
       */
      public Object evaluate(String expression, InputSource source, 
              QName returnType) throws XPathExpressionException {
          // Checking validity of different parameters
          if( source== 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().parse( source );
  
              XObject resultObject = eval( expression, document );
              return getResultAsType( resultObject, returnType );
          } catch ( SAXException e ) {
              throw new XPathExpressionException ( e );
          } catch( IOException 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 );
              }
          }
  
      } 
   
  
  
  
      /**
       * <p>Evaluate an 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(String expression, 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>expression</code> or <code>source</code> is <code>null</code>,
       * then a <code>NullPointerException</code> is thrown.</p>
       *
       * @param expression The XPath expression.
       * @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 expression cannot be evaluated.
       * @throws NullPointerException If <code>expression</code> or <code>source</code> is <code>null</code>.
       */
      public String evaluate(String expression, InputSource source)
          throws XPathExpressionException {
          return (String)this.evaluate( expression, source, XPathConstants.STRING );
      }
  
      /**
       * <p>Reset this <code>XPath</code> to its original configuration.</p>
       *
       * <p><code>XPath</code> is reset to the same state as when it was created with
       * {@link XPathFactory#newXPath()}.
       * <code>reset()</code> is designed to allow the reuse of existing <code>XPath</code>s
       * thus saving resources associated with the creation of new <code>XPath</code>s.</p>
       *
       * <p>The reset <code>XPath</code> is not guaranteed to have the same
       * {@link XPathFunctionResolver}, {@link XPathVariableResolver}
       * or {@link NamespaceContext} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.
       * It is guaranteed to have a functionally equal <code>XPathFunctionResolver</code>,
       * <code>XPathVariableResolver</code>
       * and <code>NamespaceContext</code>.</p>
       */
      public void reset() {
          this.variableResolver = this.origVariableResolver;
          this.functionResolver = this.origFunctionResolver;
          this.namespaceContext = null;
      }
   
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org