1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  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 package com.sun.org.apache.xpath.internal.operations;
  22 
  23 import com.sun.org.apache.xml.internal.utils.QName;
  24 import com.sun.org.apache.xpath.internal.Expression;
  25 import com.sun.org.apache.xpath.internal.ExpressionOwner;
  26 import com.sun.org.apache.xpath.internal.XPathContext;
  27 import com.sun.org.apache.xpath.internal.XPathVisitor;
  28 import com.sun.org.apache.xpath.internal.objects.XObject;
  29 import java.util.List;
  30 
  31 /**
  32  * The unary operation base class.
  33  *
  34  * @LastModified: Oct 2017
  35  */
  36 public abstract class UnaryOperation extends Expression implements ExpressionOwner
  37 {
  38     static final long serialVersionUID = 6536083808424286166L;
  39 
  40   /** The operand for the operation.
  41    *  @serial */
  42   protected Expression m_right;
  43 
  44   /**
  45    * This function is used to fixup variables from QNames to stack frame
  46    * indexes at stylesheet build time.
  47    * @param vars List of QNames that correspond to variables.  This list
  48    * should be searched backwards for the first qualified name that
  49    * corresponds to the variable reference qname.  The position of the
  50    * QName in the vector from the start of the vector will be its position
  51    * in the stack frame (but variables above the globalsTop value will need
  52    * to be offset to the current stack frame).
  53    */
  54   public void fixupVariables(List<QName> vars, int globalsSize)
  55   {
  56     m_right.fixupVariables(vars, globalsSize);
  57   }
  58 
  59   /**
  60    * Tell if this expression or it's subexpressions can traverse outside
  61    * the current subtree.
  62    *
  63    * @return true if traversal outside the context node's subtree can occur.
  64    */
  65   public boolean canTraverseOutsideSubtree()
  66   {
  67 
  68     if (null != m_right && m_right.canTraverseOutsideSubtree())
  69       return true;
  70 
  71     return false;
  72   }
  73 
  74   /**
  75    * Set the expression operand for the operation.
  76    *
  77    *
  78    * @param r The expression operand to which the unary operation will be
  79    *          applied.
  80    */
  81   public void setRight(Expression r)
  82   {
  83     m_right = r;
  84     r.exprSetParent(this);
  85   }
  86 
  87   /**
  88    * Execute the operand and apply the unary operation to the result.
  89    *
  90    *
  91    * @param xctxt The runtime execution context.
  92    *
  93    * @return An XObject that represents the result of applying the unary
  94    *         operation to the evaluated operand.
  95    *
  96    * @throws javax.xml.transform.TransformerException
  97    */
  98   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  99   {
 100 
 101     return operate(m_right.execute(xctxt));
 102   }
 103 
 104   /**
 105    * Apply the operation to two operands, and return the result.
 106    *
 107    *
 108    * @param right non-null reference to the evaluated right operand.
 109    *
 110    * @return non-null reference to the XObject that represents the result of the operation.
 111    *
 112    * @throws javax.xml.transform.TransformerException
 113    */
 114   public abstract XObject operate(XObject right)
 115     throws javax.xml.transform.TransformerException;
 116 
 117   /** @return the operand of unary operation, as an Expression.
 118    */
 119   public Expression getOperand(){
 120     return m_right;
 121   }
 122 
 123   /**
 124    * @see com.sun.org.apache.xpath.internal.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
 125    */
 126   public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
 127   {
 128         if(visitor.visitUnaryOperation(owner, this))
 129         {
 130                 m_right.callVisitors(this, visitor);
 131         }
 132   }
 133 
 134 
 135   /**
 136    * @see ExpressionOwner#getExpression()
 137    */
 138   public Expression getExpression()
 139   {
 140     return m_right;
 141   }
 142 
 143   /**
 144    * @see ExpressionOwner#setExpression(Expression)
 145    */
 146   public void setExpression(Expression exp)
 147   {
 148         exp.exprSetParent(this);
 149         m_right = exp;
 150   }
 151 
 152   /**
 153    * @see Expression#deepEquals(Expression)
 154    */
 155   public boolean deepEquals(Expression expr)
 156   {
 157         if(!isSameClass(expr))
 158                 return false;
 159 
 160         if(!m_right.deepEquals(((UnaryOperation)expr).m_right))
 161                 return false;
 162 
 163         return true;
 164   }
 165 
 166 
 167 }