1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: Arg.java,v 1.15 2004/02/17 04:30:02 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal;
  20. import com.sun.org.apache.xml.internal.utils.QName;
  21. import com.sun.org.apache.xpath.internal.objects.XObject;
  22. /**
  23. * This class holds an instance of an argument on
  24. * the stack. The value of the argument can be either an
  25. * XObject or a String containing an expression.
  26. * @xsl.usage internal
  27. */
  28. public class Arg
  29. {
  30. /** Field m_qname: The name of this argument, expressed as a QName
  31. * (Qualified Name) object.
  32. * @see getQName
  33. * @see setQName
  34. * */
  35. private QName m_qname;
  36. /**
  37. * Get the qualified name for this argument.
  38. *
  39. * @return QName object containing the qualified name
  40. */
  41. public final QName getQName()
  42. {
  43. return m_qname;
  44. }
  45. /**
  46. * Set the qualified name for this argument.
  47. *
  48. * @param name QName object representing the new Qualified Name.
  49. */
  50. public final void setQName(QName name)
  51. {
  52. m_qname = name;
  53. }
  54. /** Field m_val: Stored XObject value of this argument
  55. * @see #getVal()
  56. * @see #setVal()
  57. */
  58. private XObject m_val;
  59. /**
  60. * Get the value for this argument.
  61. *
  62. * @return the argument's stored XObject value.
  63. * @see #setVal(XObject)
  64. */
  65. public final XObject getVal()
  66. {
  67. return m_val;
  68. }
  69. /**
  70. * Set the value of this argument.
  71. *
  72. * @param val an XObject representing the arguments's value.
  73. * @see #getVal()
  74. */
  75. public final void setVal(XObject val)
  76. {
  77. m_val = val;
  78. }
  79. /**
  80. * Have the object release it's resources.
  81. * Call only when the variable or argument is going out of scope.
  82. */
  83. public void detach()
  84. {
  85. if(null != m_val)
  86. {
  87. m_val.allowDetachToRelease(true);
  88. m_val.detach();
  89. }
  90. }
  91. /** Field m_expression: Stored expression value of this argument.
  92. * @see #setExpression
  93. * @see #getExpression
  94. * */
  95. private String m_expression;
  96. /**
  97. * Get the value expression for this argument.
  98. *
  99. * @return String containing the expression previously stored into this
  100. * argument
  101. * @see #setExpression
  102. */
  103. public String getExpression()
  104. {
  105. return m_expression;
  106. }
  107. /**
  108. * Set the value expression for this argument.
  109. *
  110. * @param expr String containing the expression to be stored as this
  111. * argument's value.
  112. * @see #getExpression
  113. */
  114. public void setExpression(String expr)
  115. {
  116. m_expression = expr;
  117. }
  118. /**
  119. * True if this variable was added with an xsl:with-param or
  120. * is added via setParameter.
  121. */
  122. private boolean m_isFromWithParam;
  123. /**
  124. * Tell if this variable is a parameter passed with a with-param or as
  125. * a top-level parameter.
  126. */
  127. public boolean isFromWithParam()
  128. {
  129. return m_isFromWithParam;
  130. }
  131. /**
  132. * True if this variable is currently visible. To be visible,
  133. * a variable needs to come either from xsl:variable or be
  134. * a "received" parameter, ie one for which an xsl:param has
  135. * been encountered.
  136. * Set at the time the object is constructed and updated as needed.
  137. */
  138. private boolean m_isVisible;
  139. /**
  140. * Tell if this variable is currently visible.
  141. */
  142. public boolean isVisible()
  143. {
  144. return m_isVisible;
  145. }
  146. /**
  147. * Update visibility status of this variable.
  148. */
  149. public void setIsVisible(boolean b)
  150. {
  151. m_isVisible = b;
  152. }
  153. /**
  154. * Construct a dummy parameter argument, with no QName and no
  155. * value (either expression string or value XObject). isVisible
  156. * defaults to true.
  157. */
  158. public Arg()
  159. {
  160. m_qname = new QName("");
  161. ; // so that string compares can be done.
  162. m_val = null;
  163. m_expression = null;
  164. m_isVisible = true;
  165. m_isFromWithParam = false;
  166. }
  167. /**
  168. * Construct a parameter argument that contains an expression.
  169. *
  170. * @param qname Name of the argument, expressed as a QName object.
  171. * @param expression String to be stored as this argument's value expression.
  172. * @param isFromWithParam True if this is a parameter variable.
  173. */
  174. public Arg(QName qname, String expression, boolean isFromWithParam)
  175. {
  176. m_qname = qname;
  177. m_val = null;
  178. m_expression = expression;
  179. m_isFromWithParam = isFromWithParam;
  180. m_isVisible = !isFromWithParam;
  181. }
  182. /**
  183. * Construct a parameter argument which has an XObject value.
  184. * isVisible defaults to true.
  185. *
  186. * @param qname Name of the argument, expressed as a QName object.
  187. * @param val Value of the argument, expressed as an XObject
  188. */
  189. public Arg(QName qname, XObject val)
  190. {
  191. m_qname = qname;
  192. m_val = val;
  193. m_isVisible = true;
  194. m_isFromWithParam = false;
  195. m_expression = null;
  196. }
  197. /**
  198. * Equality function specialized for the variable name. If the argument
  199. * is not a qname, it will deligate to the super class.
  200. *
  201. * @param obj the reference object with which to compare.
  202. * @return <code>true</code> if this object is the same as the obj
  203. * argument; <code>false</code> otherwise.
  204. */
  205. public boolean equals(Object obj)
  206. {
  207. if(obj instanceof QName)
  208. {
  209. return m_qname.equals(obj);
  210. }
  211. else
  212. return super.equals(obj);
  213. }
  214. /**
  215. * Construct a parameter argument.
  216. *
  217. * @param qname Name of the argument, expressed as a QName object.
  218. * @param val Value of the argument, expressed as an XObject
  219. * @param isFromWithParam True if this is a parameter variable.
  220. */
  221. public Arg(QName qname, XObject val, boolean isFromWithParam)
  222. {
  223. m_qname = qname;
  224. m_val = val;
  225. m_isFromWithParam = isFromWithParam;
  226. m_isVisible = !isFromWithParam;
  227. m_expression = null;
  228. }
  229. }