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