1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the  "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18// $Id: JAXPVariableStack.java 524815 2007-04-02 15:52:15Z zongaro $
19
20package org.apache.xpath.jaxp;
21
22import javax.xml.transform.TransformerException;
23import javax.xml.xpath.XPathVariableResolver;
24
25import org.apache.xml.utils.QName;
26import org.apache.xpath.VariableStack;
27import org.apache.xpath.XPathContext;
28import org.apache.xpath.objects.XObject;
29
30import org.apache.xpath.res.XPATHErrorResources;
31import org.apache.xalan.res.XSLMessages;
32
33
34/**
35 * Overrides {@link VariableStack} and delegates the call to
36 * {@link javax.xml.xpath.XPathVariableResolver}.
37 *
38 * @author Ramesh Mandava ( ramesh.mandava@sun.com )
39 */
40public class JAXPVariableStack extends VariableStack {
41
42    private final XPathVariableResolver resolver;
43
44    public JAXPVariableStack(XPathVariableResolver resolver) {
45        super(2);
46        this.resolver = resolver;
47    }
48
49    public XObject getVariableOrParam(XPathContext xctxt, QName qname)
50        throws TransformerException,IllegalArgumentException {
51        if ( qname == null ) {
52            //JAXP 1.3 spec says that if variable name is null then
53            // we need to through IllegalArgumentException
54            String fmsg = XSLMessages.createXPATHMessage(
55                XPATHErrorResources.ER_ARG_CANNOT_BE_NULL,
56                new Object[] {"Variable qname"} );
57            throw new IllegalArgumentException( fmsg );
58        }
59	javax.xml.namespace.QName name =
60	    new javax.xml.namespace.QName(
61                qname.getNamespace(),
62                qname.getLocalPart());
63        Object varValue = resolver.resolveVariable( name );
64        if ( varValue == null ) {
65            String fmsg = XSLMessages.createXPATHMessage(
66                XPATHErrorResources.ER_RESOLVE_VARIABLE_RETURNS_NULL,
67                new Object[] { name.toString()} );
68            throw new TransformerException( fmsg );
69        }
70        return XObject.create( varValue, xctxt );
71    }
72
73}
74