1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 1999-2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 /*
  21  * $Id: Arg.java,v 1.1.2.1 2005/08/01 01:30:11 jeffsuttor Exp $
  22  */
  23 package com.sun.org.apache.xpath.internal;
  24 
  25 import com.sun.org.apache.xml.internal.utils.QName;
  26 import com.sun.org.apache.xpath.internal.objects.XObject;
  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    */
  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    */
  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    */
  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    */
  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    */
  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    */
 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    */
 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    */
 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    */
 163    public boolean isVisible()
 164    {
 165     return m_isVisible;
 166    }
 167 
 168   /**
 169    * Update visibility status of this variable.
 170    */
 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    */
 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    */
 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    */
 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   /**
 227    * Equality function specialized for the variable name.  If the argument
 228    * is not a qname, it will deligate to the super class.
 229    *
 230    * @param   obj   the reference object with which to compare.
 231    * @return  <code>true</code> if this object is the same as the obj
 232    *          argument; <code>false</code> otherwise.
 233    */
 234   public boolean equals(Object obj)
 235   {
 236     if(obj instanceof QName)
 237     {
 238       return m_qname.equals(obj);
 239     }
 240     else
 241       return super.equals(obj);
 242   }
 243 
 244   /**
 245    * Construct a parameter argument.
 246    *
 247    * @param qname Name of the argument, expressed as a QName object.
 248    * @param val Value of the argument, expressed as an XObject
 249    * @param isFromWithParam True if this is a parameter variable.
 250    */
 251   public Arg(QName qname, XObject val, boolean isFromWithParam)
 252   {
 253 
 254     m_qname = qname;
 255     m_val = val;
 256     m_isFromWithParam = isFromWithParam;
 257     m_isVisible = !isFromWithParam;
 258     m_expression = null;
 259   }
 260 }