001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    // $Id: JAXPVariableStack.java 524815 2007-04-02 15:52:15Z zongaro $
019    
020    package org.apache.xpath.jaxp;
021    
022    import javax.xml.transform.TransformerException;
023    import javax.xml.xpath.XPathVariableResolver;
024    
025    import org.apache.xml.utils.QName;
026    import org.apache.xpath.VariableStack;
027    import org.apache.xpath.XPathContext;
028    import org.apache.xpath.objects.XObject;
029    
030    import org.apache.xpath.res.XPATHErrorResources;
031    import org.apache.xalan.res.XSLMessages;
032    
033    
034    /**
035     * Overrides {@link VariableStack} and delegates the call to
036     * {@link javax.xml.xpath.XPathVariableResolver}.
037     * 
038     * @author Ramesh Mandava ( ramesh.mandava@sun.com )
039     */
040    public class JAXPVariableStack extends VariableStack {
041            
042        private final XPathVariableResolver resolver;
043            
044        public JAXPVariableStack(XPathVariableResolver resolver) {
045            super(2);
046            this.resolver = resolver;
047        }
048            
049        public XObject getVariableOrParam(XPathContext xctxt, QName qname)
050            throws TransformerException,IllegalArgumentException {
051            if ( qname == null ) {
052                //JAXP 1.3 spec says that if variable name is null then 
053                // we need to through IllegalArgumentException
054                String fmsg = XSLMessages.createXPATHMessage( 
055                    XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
056                    new Object[] {"Variable qname"} );
057                throw new IllegalArgumentException( fmsg );
058            }
059            javax.xml.namespace.QName name =
060                new javax.xml.namespace.QName(
061                    qname.getNamespace(),
062                    qname.getLocalPart());
063            Object varValue = resolver.resolveVariable( name );             
064            if ( varValue == null ) {
065                String fmsg = XSLMessages.createXPATHMessage( 
066                    XPATHErrorResources.ER_RESOLVE_VARIABLE_RETURNS_NULL,
067                    new Object[] { name.toString()} );
068                throw new TransformerException( fmsg );
069            } 
070            return XObject.create( varValue, xctxt );
071        }
072    
073    }