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