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