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/*
19 * $Id: Arg.java 468655 2006-10-28 07:12:06Z minchau $
20 */
21package org.apache.xpath;
22
23import org.apache.xml.utils.QName;
24import org.apache.xpath.objects.XObject;
25
26/**
27 * This class holds an instance of an argument on
28 * the stack. The value of the argument can be either an
29 * XObject or a String containing an expression.
30 * @xsl.usage internal
31 */
32public class Arg
33{
34
35  /** Field m_qname: The name of this argument, expressed as a QName
36   * (Qualified Name) object.
37   * @see getQName
38   * @see setQName
39   *  */
40  private QName m_qname;
41
42  /**
43   * Get the qualified name for this argument.
44   *
45   * @return QName object containing the qualified name
46   */
47  public final QName getQName()
48  {
49    return m_qname;
50  }
51
52  /**
53   * Set the qualified name for this argument.
54   *
55   * @param name QName object representing the new Qualified Name.
56   */
57  public final void setQName(QName name)
58  {
59    m_qname = name;
60  }
61
62  /** Field m_val: Stored XObject value of this argument
63   * @see #getVal()
64   * @see #setVal()
65   */
66  private XObject m_val;
67
68  /**
69   * Get the value for this argument.
70   *
71   * @return the argument's stored XObject value.
72   * @see #setVal(XObject)
73   */
74  public final XObject getVal()
75  {
76    return m_val;
77  }
78
79  /**
80   * Set the value of this argument.
81   *
82   * @param val an XObject representing the arguments's value.
83   * @see #getVal()
84   */
85  public final void setVal(XObject val)
86  {
87    m_val = val;
88  }
89
90  /**
91   * Have the object release it's resources.
92   * Call only when the variable or argument is going out of scope.
93   */
94  public void detach()
95  {
96    if(null != m_val)
97    {
98      m_val.allowDetachToRelease(true);
99      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}
259