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.objects;
  23 
  24 import com.sun.org.apache.xalan.internal.res.XSLMessages;
  25 import com.sun.org.apache.xml.internal.dtm.DTMIterator;
  26 import com.sun.org.apache.xml.internal.utils.QName;
  27 import com.sun.org.apache.xml.internal.utils.XMLString;
  28 import com.sun.org.apache.xpath.internal.Expression;
  29 import com.sun.org.apache.xpath.internal.XPathContext;
  30 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  31 import java.util.List;
  32 
  33 /**
  34  * This class makes an select statement act like an result tree fragment.
  35  */
  36 public class XRTreeFragSelectWrapper extends XRTreeFrag implements Cloneable
  37 {
  38     static final long serialVersionUID = -6526177905590461251L;
  39   public XRTreeFragSelectWrapper(Expression expr)
  40   {
  41     super(expr);
  42   }
  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     ((Expression)m_obj).fixupVariables(vars, globalsSize);
  57   }
  58 
  59   /**
  60    * For support of literal objects in xpaths.
  61    *
  62    * @param xctxt The XPath execution context.
  63    *
  64    * @return the result of executing the select expression
  65    *
  66    * @throws javax.xml.transform.TransformerException
  67    */
  68   public XObject execute(XPathContext xctxt)
  69           throws javax.xml.transform.TransformerException
  70   {
  71          XObject m_selected;
  72      m_selected = ((Expression)m_obj).execute(xctxt);
  73      m_selected.allowDetachToRelease(m_allowRelease);
  74      if (m_selected.getType() == CLASS_STRING)
  75        return m_selected;
  76      else
  77        return new XString(m_selected.str());
  78   }
  79 
  80   /**
  81    * Detaches the <code>DTMIterator</code> from the set which it iterated
  82    * over, releasing any computational resources and placing the iterator
  83    * in the INVALID state. After <code>detach</code> has been invoked,
  84    * calls to <code>nextNode</code> or <code>previousNode</code> will
  85    * raise a runtime exception.
  86    *
  87    * In general, detach should only be called once on the object.
  88    */
  89   public void detach()
  90   {
  91         throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_DETACH_NOT_SUPPORTED_XRTREEFRAGSELECTWRAPPER, null)); //"detach() not supported by XRTreeFragSelectWrapper!");
  92   }
  93 
  94   /**
  95    * Cast result object to a number.
  96    *
  97    * @return The result tree fragment as a number or NaN
  98    */
  99   public double num()
 100     throws javax.xml.transform.TransformerException
 101   {
 102 
 103         throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NUM_NOT_SUPPORTED_XRTREEFRAGSELECTWRAPPER, null)); //"num() not supported by XRTreeFragSelectWrapper!");
 104   }
 105 
 106 
 107   /**
 108    * Cast result object to an XMLString.
 109    *
 110    * @return The document fragment node data or the empty string.
 111    */
 112   public XMLString xstr()
 113   {
 114         throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_XSTR_NOT_SUPPORTED_XRTREEFRAGSELECTWRAPPER, null)); //"xstr() not supported by XRTreeFragSelectWrapper!");
 115   }
 116 
 117   /**
 118    * Cast result object to a string.
 119    *
 120    * @return The document fragment node data or the empty string.
 121    */
 122   public String str()
 123   {
 124         throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_STR_NOT_SUPPORTED_XRTREEFRAGSELECTWRAPPER, null)); //"str() not supported by XRTreeFragSelectWrapper!");
 125   }
 126 
 127   /**
 128    * Tell what kind of class this is.
 129    *
 130    * @return the string type
 131    */
 132   public int getType()
 133   {
 134     return CLASS_STRING;
 135   }
 136 
 137   /**
 138    * Cast result object to a result tree fragment.
 139    *
 140    * @return The document fragment this wraps
 141    */
 142   public int rtf()
 143   {
 144     throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_RTF_NOT_SUPPORTED_XRTREEFRAGSELECTWRAPPER, null)); //"rtf() not supported by XRTreeFragSelectWrapper!");
 145   }
 146 
 147   /**
 148    * Cast result object to a DTMIterator.
 149    *
 150    * @return The document fragment as a DTMIterator
 151    */
 152   public DTMIterator asNodeIterator()
 153   {
 154     throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_RTF_NOT_SUPPORTED_XRTREEFRAGSELECTWRAPPER, null)); //"asNodeIterator() not supported by XRTreeFragSelectWrapper!");
 155   }
 156 
 157 }