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    /*
019     * $Id: Arg.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xpath;
022    
023    import org.apache.xml.utils.QName;
024    import org.apache.xpath.objects.XObject;
025    
026    /**
027     * This class holds an instance of an argument on
028     * the stack. The value of the argument can be either an
029     * XObject or a String containing an expression.
030     * @xsl.usage internal
031     */
032    public class Arg
033    {
034    
035      /** Field m_qname: The name of this argument, expressed as a QName
036       * (Qualified Name) object.
037       * @see getQName
038       * @see setQName
039       *  */
040      private QName m_qname;
041    
042      /**
043       * Get the qualified name for this argument.
044       *
045       * @return QName object containing the qualified name
046       */
047      public final QName getQName()
048      {
049        return m_qname;
050      }
051    
052      /**
053       * Set the qualified name for this argument.
054       *
055       * @param name QName object representing the new Qualified Name.
056       */
057      public final void setQName(QName name)
058      {
059        m_qname = name;
060      }
061    
062      /** Field m_val: Stored XObject value of this argument
063       * @see #getVal()
064       * @see #setVal()
065       */
066      private XObject m_val;
067    
068      /**
069       * Get the value for this argument.
070       *
071       * @return the argument's stored XObject value.
072       * @see #setVal(XObject)
073       */
074      public final XObject getVal()
075      {
076        return m_val;
077      }
078    
079      /**
080       * Set the value of this argument.
081       *
082       * @param val an XObject representing the arguments's value.
083       * @see #getVal()
084       */
085      public final void setVal(XObject val)
086      {
087        m_val = val;
088      }
089      
090      /**
091       * Have the object release it's resources.
092       * Call only when the variable or argument is going out of scope.
093       */
094      public void detach()
095      {
096        if(null != m_val)
097        {
098          m_val.allowDetachToRelease(true);
099          m_val.detach();
100        }
101      }
102    
103    
104      /** Field m_expression: Stored expression value of this argument.
105       * @see #setExpression
106       * @see #getExpression
107       * */
108      private String m_expression;
109    
110      /**
111       * Get the value expression for this argument.
112       *
113       * @return String containing the expression previously stored into this
114       * argument
115       * @see #setExpression
116       */
117      public String getExpression()
118      {
119        return m_expression;
120      }
121    
122      /**
123       * Set the value expression for this argument.
124       *
125       * @param expr String containing the expression to be stored as this
126       * argument's value.
127       * @see #getExpression
128       */
129      public void setExpression(String expr)
130      {
131        m_expression = expr;
132      }
133    
134      /** 
135       * True if this variable was added with an xsl:with-param or
136       * is added via setParameter.
137       */
138      private boolean m_isFromWithParam;
139    
140      /**
141       * Tell if this variable is a parameter passed with a with-param or as 
142       * a top-level parameter.
143       */
144       public boolean isFromWithParam()
145       {
146        return m_isFromWithParam;
147       }
148    
149      /** 
150       * True if this variable is currently visible.  To be visible,
151       * a variable needs to come either from xsl:variable or be 
152       * a "received" parameter, ie one for which an xsl:param has
153       * been encountered.
154       * Set at the time the object is constructed and updated as needed.
155       */
156      private boolean m_isVisible;
157    
158      /**
159       * Tell if this variable is currently visible.
160       */
161       public boolean isVisible()
162       {
163        return m_isVisible;
164       }
165       
166      /**
167       * Update visibility status of this variable.
168       */
169       public void setIsVisible(boolean b)
170       {
171        m_isVisible = b;
172       }
173    
174      /**
175       * Construct a dummy parameter argument, with no QName and no
176       * value (either expression string or value XObject). isVisible
177       * defaults to true.
178       */
179      public Arg()
180      {
181    
182        m_qname = new QName("");
183        ;  // so that string compares can be done.
184        m_val = null;
185        m_expression = null;
186        m_isVisible = true;
187        m_isFromWithParam = false;
188      }
189    
190      /**
191       * Construct a parameter argument that contains an expression.
192       *
193       * @param qname Name of the argument, expressed as a QName object.
194       * @param expression String to be stored as this argument's value expression.
195       * @param isFromWithParam True if this is a parameter variable.
196       */
197      public Arg(QName qname, String expression, boolean isFromWithParam)
198      {
199    
200        m_qname = qname;
201        m_val = null;
202        m_expression = expression;
203        m_isFromWithParam = isFromWithParam;
204        m_isVisible = !isFromWithParam;
205      }
206    
207      /**
208       * Construct a parameter argument which has an XObject value.
209       * isVisible defaults to true.
210       *
211       * @param qname Name of the argument, expressed as a QName object.
212       * @param val Value of the argument, expressed as an XObject
213       */
214      public Arg(QName qname, XObject val)
215      {
216    
217        m_qname = qname;
218        m_val = val;
219        m_isVisible = true;
220        m_isFromWithParam = false;
221        m_expression = null;
222      }
223      
224      /**
225       * Equality function specialized for the variable name.  If the argument 
226       * is not a qname, it will deligate to the super class.
227       * 
228       * @param   obj   the reference object with which to compare.
229       * @return  <code>true</code> if this object is the same as the obj
230       *          argument; <code>false</code> otherwise.
231       */
232      public boolean equals(Object obj) 
233      {
234        if(obj instanceof QName)
235        {
236          return m_qname.equals(obj);
237        }
238        else
239          return super.equals(obj);
240      }
241    
242      /**
243       * Construct a parameter argument.
244       *
245       * @param qname Name of the argument, expressed as a QName object.
246       * @param val Value of the argument, expressed as an XObject
247       * @param isFromWithParam True if this is a parameter variable.
248       */
249      public Arg(QName qname, XObject val, boolean isFromWithParam)
250      {
251    
252        m_qname = qname;
253        m_val = val;
254        m_isFromWithParam = isFromWithParam;
255        m_isVisible = !isFromWithParam;
256        m_expression = null;
257      }
258    }