Exemple #1
0
 /**
  * Execute the function. The function must return a valid object.
  *
  * @param xctxt The current execution context.
  * @return A valid XObject.
  * @throws javax.xml.transform.TransformerException
  */
 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
   final XObject obj = m_arg0.execute(xctxt);
   final double val = obj.num();
   if (val >= -0.5 && val < 0) return new XNumber(-0.0);
   if (val == 0.0) return new XNumber(val);
   return new XNumber(java.lang.Math.floor(val + 0.5));
 }
  /**
   * Use an XPath string to select a nodelist. XPath namespace prefixes are resolved from the
   * namespaceNode.
   *
   * @param contextNode The node to start searching from.
   * @param xpathnode
   * @param str
   * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
   * @return A NodeIterator, should never be null.
   * @throws TransformerException
   */
  public NodeList selectNodeList(Node contextNode, Node xpathnode, String str, Node namespaceNode)
      throws TransformerException {

    // Execute the XPath, and have it return the result
    XObject list = eval(contextNode, xpathnode, str, namespaceNode);

    // Return a NodeList.
    return list.nodelist();
  }
  /**
   * Use an XPath string to select a nodelist. XPath namespace prefixes are resolved from the
   * namespaceNode.
   *
   * @param contextNode The node to start searching from.
   * @param xpathnode
   * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
   * @return A NodeIterator, should never be null.
   * @throws TransformerException
   * @deprecated
   */
  public NodeIterator selectNodeIterator(Node contextNode, Node xpathnode, Node namespaceNode)
      throws TransformerException {

    // Execute the XPath, and have it return the result
    XObject list = eval(contextNode, xpathnode, getStrFromNode(xpathnode), namespaceNode);

    // Have the XObject return its result as a NodeSetDTM.
    return list.nodeset();
  }
Exemple #4
0
  /**
   * Get a global variable or parameter from the global stack frame.
   *
   * @param xctxt The XPath context, which must be passed in order to lazy evaluate variables.
   * @param index Global variable index relative to the global stack frame bottom.
   * @return The value of the variable.
   * @throws TransformerException
   */
  public XObject getGlobalVariable(XPathContext xctxt, final int index, boolean destructiveOK)
      throws TransformerException {

    XObject val = _stackFrames[index];

    // Lazy execution of variables.
    if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE)
      return (_stackFrames[index] = val.execute(xctxt));

    return destructiveOK ? val : val.getFresh();
  }
  /**
   * Get the XObject representation of the variable.
   *
   * @param transformer non-null reference to the the current transform-time state.
   * @param sourceNode non-null reference to the <a
   *     href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
   * @return the XObject representation of the variable.
   * @throws javax.xml.transform.TransformerException
   */
  public XObject getValue(TransformerImpl transformer, int sourceNode) throws TransformerException {

    XObject var;
    XPathContext xctxt = transformer.getXPathContext();

    xctxt.pushCurrentNode(sourceNode);

    try {
      if (null != m_selectPattern) {
        var = m_selectPattern.execute(xctxt, sourceNode, this);

        var.allowDetachToRelease(false);

        if (transformer.getDebug())
          transformer
              .getTraceManager()
              .fireSelectedEvent(sourceNode, this, "select", m_selectPattern, var);
      } else if (null == getFirstChildElem()) {
        var = XString.EMPTYSTRING;
      } else {

        // Use result tree fragment.
        // Global variables may be deferred (see XUnresolvedVariable) and hence
        // need to be assigned to a different set of DTMs than local variables
        // so they aren't popped off the stack on return from a template.
        int df;

        // Bugzilla 7118: A variable set via an RTF may create local
        // variables during that computation. To keep them from overwriting
        // variables at this level, push a new variable stack.
        ////// PROBLEM: This is provoking a variable-used-before-set
        ////// problem in parameters. Needs more study.
        try {
          ////////// xctxt.getVarStack().link(0);
          if (m_parentNode instanceof Stylesheet) // Global variable
          df = transformer.transformToGlobalRTF(this);
          else df = transformer.transformToRTF(this);
        } finally {
          ////////////// xctxt.getVarStack().unlink();
        }

        var = new XRTreeFrag(df, xctxt, this);
      }
    } finally {
      xctxt.popCurrentNode();
    }

    return var;
  }
Exemple #6
0
  /**
   * Get a local variable or parameter in the current stack frame.
   *
   * @param xctxt The XPath context, which must be passed in order to lazy evaluate variables.
   * @param index Local variable index relative to the current stack frame bottom.
   * @return The value of the variable.
   * @throws TransformerException
   */
  public XObject getLocalVariable(XPathContext xctxt, int index) throws TransformerException {

    index += _currentFrameBottom;

    XObject val = _stackFrames[index];

    if (null == val)
      throw new TransformerException(
          XSLMessages.createXPATHMessage(
              XPATHErrorResources.ER_VARIABLE_ACCESSED_BEFORE_BIND, null),
          xctxt.getSAXLocator());
    // "Variable accessed before it is bound!", xctxt.getSAXLocator());

    // Lazy execution of variables.
    if (val.getType() == XObject.CLASS_UNRESOLVEDVARIABLE)
      return (_stackFrames[index] = val.execute(xctxt));

    return val;
  }
Exemple #7
0
  /**
   * Test a node to see if it matches any of the patterns in the union.
   *
   * @param xctxt XPath runtime context.
   * @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST}, {@link
   *     org.apache.xpath.patterns.NodeTest#SCORE_NONE}, {@link
   *     org.apache.xpath.patterns.NodeTest#SCORE_NSWILD}, {@link
   *     org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or {@link
   *     org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
   * @throws javax.xml.transform.TransformerException
   */
  public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {

    XObject bestScore = null;
    int n = m_patterns.length;

    for (int i = 0; i < n; i++) {
      XObject score = m_patterns[i].execute(xctxt);

      if (score != NodeTest.SCORE_NONE) {
        if (null == bestScore) bestScore = score;
        else if (score.num() > bestScore.num()) bestScore = score;
      }
    }

    if (null == bestScore) {
      bestScore = NodeTest.SCORE_NONE;
    }

    return bestScore;
  }
 /**
  * Return the Java value corresponding to an XPath result.
  *
  * @param xo the XPath type
  * @return the corresponding Java value per the JSTL mapping rules
  * @throws TransformerException if there was a problem converting the type
  */
 static Object coerceToJava(XObject xo) throws TransformerException {
   if (xo instanceof XBoolean) {
     return xo.bool();
   } else if (xo instanceof XNumber) {
     return xo.num();
   } else if (xo instanceof XString) {
     return xo.str();
   } else if (xo instanceof XNodeSet) {
     NodeList nodes = xo.nodelist();
     // if there is only one node in the nodeset return it rather than the list
     if (nodes.getLength() == 1) {
       return nodes.item(0);
     } else {
       return nodes;
     }
   } else {
     // unexpected result type
     throw new AssertionError();
   }
 }
Exemple #9
0
 /**
  * Apply the operation to two operands, and return the result.
  *
  * @param left non-null reference to the evaluated left operand.
  * @param right non-null reference to the evaluated right operand.
  * @return non-null reference to the XObject that represents the result of the operation.
  * @throws javax.xml.transform.TransformerException
  */
 public XObject operate(XObject left, XObject right)
     throws javax.xml.transform.TransformerException {
   return left.lessThan(right) ? XBoolean.S_TRUE : XBoolean.S_FALSE;
 }
Exemple #10
0
  /**
   * Execute the function. The function must return a valid object.
   *
   * @param xctxt The current execution context.
   * @return A valid XObject.
   * @throws javax.xml.transform.TransformerException
   */
  public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {

    // TransformerImpl transformer = (TransformerImpl)xctxt;
    TransformerImpl transformer = (TransformerImpl) xctxt.getOwnerObject();
    XNodeSet nodes = null;
    int context = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(context);
    int docContext = dtm.getDocumentRoot(context);

    if (DTM.NULL == docContext) {

      // path.error(context, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC); //"context does not
      // have an owner document!");
    }

    String xkeyname = getArg0().execute(xctxt).str();
    QName keyname = new QName(xkeyname, xctxt.getNamespaceContext());
    XObject arg = getArg1().execute(xctxt);
    boolean argIsNodeSetDTM = (XObject.CLASS_NODESET == arg.getType());
    KeyManager kmgr = transformer.getKeyManager();

    // Don't bother with nodeset logic if the thing is only one node.
    if (argIsNodeSetDTM) {
      XNodeSet ns = (XNodeSet) arg;
      ns.setShouldCacheNodes(true);
      int len = ns.getLength();
      if (len <= 1) argIsNodeSetDTM = false;
    }

    if (argIsNodeSetDTM) {
      Hashtable usedrefs = null;
      DTMIterator ni = arg.iter();
      int pos;
      UnionPathIterator upi = new UnionPathIterator();
      upi.exprSetParent(this);

      while (DTM.NULL != (pos = ni.nextNode())) {
        dtm = xctxt.getDTM(pos);
        XMLString ref = dtm.getStringValue(pos);

        if (null == ref) continue;

        if (null == usedrefs) usedrefs = new Hashtable();

        if (usedrefs.get(ref) != null) {
          continue; // We already have 'em.
        } else {

          // ISTRUE being used as a dummy value.
          usedrefs.put(ref, ISTRUE);
        }

        XNodeSet nl =
            kmgr.getNodeSetDTMByKey(xctxt, docContext, keyname, ref, xctxt.getNamespaceContext());

        nl.setRoot(xctxt.getCurrentNode(), xctxt);

        //        try
        //        {
        upi.addIterator(nl);
        //        }
        //        catch(CloneNotSupportedException cnse)
        //        {
        //          // will never happen.
        //        }
        // mnodeset.addNodesInDocOrder(nl, xctxt); needed??
      }

      int current = xctxt.getCurrentNode();
      upi.setRoot(current, xctxt);

      nodes = new XNodeSet(upi);
    } else {
      XMLString ref = arg.xstr();
      nodes = kmgr.getNodeSetDTMByKey(xctxt, docContext, keyname, ref, xctxt.getNamespaceContext());
      nodes.setRoot(xctxt.getCurrentNode(), xctxt);
    }

    return nodes;
  }
Exemple #11
0
  /**
   * Apply the operation to two operands, and return the result.
   *
   * @param right non-null reference to the evaluated right operand.
   * @return non-null reference to the XObject that represents the result of the operation.
   * @throws javax.xml.transform.TransformerException
   */
  public XObject operate(XObject right) throws javax.xml.transform.TransformerException {

    if (XObject.CLASS_BOOLEAN == right.getType()) return right;
    else return right.bool() ? XBoolean.S_TRUE : XBoolean.S_FALSE;
  }
Exemple #12
0
 /**
  * Apply the operation to two operands, and return the result.
  *
  * @param left non-null reference to the evaluated left operand.
  * @param right non-null reference to the evaluated right operand.
  * @return non-null reference to the XObject that represents the result of the operation.
  * @throws javax.xml.transform.TransformerException
  */
 public XObject operate(XObject left, XObject right)
     throws javax.xml.transform.TransformerException {
   return new XNumber((int) (left.num() / right.num()));
 }
 /**
  * For support of literal objects in xpaths.
  *
  * @param xctxt The XPath execution context.
  * @return This object.
  * @throws javax.xml.transform.TransformerException
  */
 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
   Expression expr = ((ElemVariable) m_obj).getSelect().getExpression();
   XObject xobj = expr.execute(xctxt);
   xobj.allowDetachToRelease(false);
   return xobj;
 }