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;
23 
24 import com.sun.org.apache.xml.internal.utils.QName;
25 import com.sun.org.apache.xpath.internal.objects.XObject;
26 import java.util.Objects;
27 
28 /**
29  * This class holds an instance of an argument on
30  * the stack. The value of the argument can be either an
31  * XObject or a String containing an expression.
32  * @xsl.usage internal
33  */
34 public class Arg
35 {
36 
37   /** Field m_qname: The name of this argument, expressed as a QName
38    * (Qualified Name) object.
39    * @see getQName
40    * @see setQName
41    *  */
42   private QName m_qname;
43 
44   /**
45    * Get the qualified name for this argument.
46    *
47    * @return QName object containing the qualified name
48    */
getQName()49   public final QName getQName()
50   {
51     return m_qname;
52   }
53 
54   /**
55    * Set the qualified name for this argument.
56    *
57    * @param name QName object representing the new Qualified Name.
58    */
setQName(QName name)59   public final void setQName(QName name)
60   {
61     m_qname = name;
62   }
63 
64   /** Field m_val: Stored XObject value of this argument
65    * @see #getVal()
66    * @see #setVal()
67    */
68   private XObject m_val;
69 
70   /**
71    * Get the value for this argument.
72    *
73    * @return the argument's stored XObject value.
74    * @see #setVal(XObject)
75    */
getVal()76   public final XObject getVal()
77   {
78     return m_val;
79   }
80 
81   /**
82    * Set the value of this argument.
83    *
84    * @param val an XObject representing the arguments's value.
85    * @see #getVal()
86    */
setVal(XObject val)87   public final void setVal(XObject val)
88   {
89     m_val = val;
90   }
91 
92   /**
93    * Have the object release it's resources.
94    * Call only when the variable or argument is going out of scope.
95    */
detach()96   public void detach()
97   {
98     if(null != m_val)
99     {
100       m_val.allowDetachToRelease(true);
101       m_val.detach();
102     }
103   }
104 
105 
106   /** Field m_expression: Stored expression value of this argument.
107    * @see #setExpression
108    * @see #getExpression
109    * */
110   private String m_expression;
111 
112   /**
113    * Get the value expression for this argument.
114    *
115    * @return String containing the expression previously stored into this
116    * argument
117    * @see #setExpression
118    */
getExpression()119   public String getExpression()
120   {
121     return m_expression;
122   }
123 
124   /**
125    * Set the value expression for this argument.
126    *
127    * @param expr String containing the expression to be stored as this
128    * argument's value.
129    * @see #getExpression
130    */
setExpression(String expr)131   public void setExpression(String expr)
132   {
133     m_expression = expr;
134   }
135 
136   /**
137    * True if this variable was added with an xsl:with-param or
138    * is added via setParameter.
139    */
140   private boolean m_isFromWithParam;
141 
142   /**
143    * Tell if this variable is a parameter passed with a with-param or as
144    * a top-level parameter.
145    */
isFromWithParam()146    public boolean isFromWithParam()
147    {
148     return m_isFromWithParam;
149    }
150 
151   /**
152    * True if this variable is currently visible.  To be visible,
153    * a variable needs to come either from xsl:variable or be
154    * a "received" parameter, ie one for which an xsl:param has
155    * been encountered.
156    * Set at the time the object is constructed and updated as needed.
157    */
158   private boolean m_isVisible;
159 
160   /**
161    * Tell if this variable is currently visible.
162    */
isVisible()163    public boolean isVisible()
164    {
165     return m_isVisible;
166    }
167 
168   /**
169    * Update visibility status of this variable.
170    */
setIsVisible(boolean b)171    public void setIsVisible(boolean b)
172    {
173     m_isVisible = b;
174    }
175 
176   /**
177    * Construct a dummy parameter argument, with no QName and no
178    * value (either expression string or value XObject). isVisible
179    * defaults to true.
180    */
Arg()181   public Arg()
182   {
183 
184     m_qname = new QName("");
185        // so that string compares can be done.
186     m_val = null;
187     m_expression = null;
188     m_isVisible = true;
189     m_isFromWithParam = false;
190   }
191 
192   /**
193    * Construct a parameter argument that contains an expression.
194    *
195    * @param qname Name of the argument, expressed as a QName object.
196    * @param expression String to be stored as this argument's value expression.
197    * @param isFromWithParam True if this is a parameter variable.
198    */
Arg(QName qname, String expression, boolean isFromWithParam)199   public Arg(QName qname, String expression, boolean isFromWithParam)
200   {
201 
202     m_qname = qname;
203     m_val = null;
204     m_expression = expression;
205     m_isFromWithParam = isFromWithParam;
206     m_isVisible = !isFromWithParam;
207   }
208 
209   /**
210    * Construct a parameter argument which has an XObject value.
211    * isVisible defaults to true.
212    *
213    * @param qname Name of the argument, expressed as a QName object.
214    * @param val Value of the argument, expressed as an XObject
215    */
Arg(QName qname, XObject val)216   public Arg(QName qname, XObject val)
217   {
218 
219     m_qname = qname;
220     m_val = val;
221     m_isVisible = true;
222     m_isFromWithParam = false;
223     m_expression = null;
224   }
225 
226     @Override
hashCode()227     public int hashCode() {
228         return Objects.hashCode(this.m_qname);
229     }
230 
231   /**
232    * Equality function specialized for the variable name.  If the argument
233    * is not a qname, it will deligate to the super class.
234    *
235    * @param   obj   the reference object with which to compare.
236    * @return  <code>true</code> if this object is the same as the obj
237    *          argument; <code>false</code> otherwise.
238    */
239   @Override
equals(Object obj)240   public boolean equals(Object obj)
241   {
242     if(obj instanceof QName)
243     {
244       return m_qname.equals(obj);
245     }
246     else
247       return super.equals(obj);
248   }
249 
250   /**
251    * Construct a parameter argument.
252    *
253    * @param qname Name of the argument, expressed as a QName object.
254    * @param val Value of the argument, expressed as an XObject
255    * @param isFromWithParam True if this is a parameter variable.
256    */
Arg(QName qname, XObject val, boolean isFromWithParam)257   public Arg(QName qname, XObject val, boolean isFromWithParam)
258   {
259 
260     m_qname = qname;
261     m_val = val;
262     m_isFromWithParam = isFromWithParam;
263     m_isVisible = !isFromWithParam;
264     m_expression = null;
265   }
266 }
267