org.apache.xpath.objects.XObject Java Examples

The following examples show how to use org.apache.xpath.objects.XObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XPathHelper.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluates an XPath expression to an XObject.
 * @param contextNode the node to start searching from
 * @param str a valid XPath string
 * @param a prefix resolver to use for resolving namespace prefixes, or null
 * @return an XObject, which can be used to obtain a string, number, nodelist, etc (should never be {@code null})
 * @throws TransformerException if a syntax or other error occurs
 */
private static XObject evaluateXPath(final DomNode contextNode,
        final String str, final PrefixResolver prefixResolver) throws TransformerException {
    final XPathContext xpathSupport = new XPathContext();
    final Node xpathExpressionContext;
    if (contextNode.getNodeType() == Node.DOCUMENT_NODE) {
        xpathExpressionContext = ((Document) contextNode).getDocumentElement();
    }
    else {
        xpathExpressionContext = contextNode;
    }

    PrefixResolver resolver = prefixResolver;
    if (resolver == null) {
        resolver = new HtmlUnitPrefixResolver(xpathExpressionContext);
    }

    final boolean caseSensitive = contextNode.getPage().hasCaseSensitiveTagNames();

    final XPathAdapter xpath = new XPathAdapter(str, null, resolver, null, caseSensitive);
    final int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
    return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}
 
Example #2
Source File: VariableStack.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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, boolean destructiveOK)
        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 destructiveOK ? val : val.getFresh();
}
 
Example #3
Source File: TransformerImpl.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Internal -- push the global variables from the Stylesheet onto
 * the context's runtime variable stack.
 * <p>If we encounter a variable
 * that is already defined in the variable stack, we ignore it.  This
 * is because the second variable definition will be at a lower import
 * precedence.  Presumably, global"variables at the same import precedence
 * with the same name will have been caught during the recompose process.
 * <p>However, if we encounter a parameter that is already defined in the
 * variable stack, we need to see if this is a parameter whose value was
 * supplied by a setParameter call.  If so, we need to "receive" the one
 * already in the stack, ignoring this one.  If it is just an earlier
 * xsl:param or xsl:variable definition, we ignore it using the same
 * reasoning as explained above for the variable.
 *
 * @param contextNode The root of the source tree, can't be null.
 *
 * @throws TransformerException
 */
protected void pushGlobalVars(int contextNode) throws TransformerException
{

  XPathContext xctxt = m_xcontext;
  VariableStack vs = xctxt.getVarStack();
  StylesheetRoot sr = getStylesheet();
  Vector vars = sr.getVariablesAndParamsComposed();
  
  int i = vars.size();
  vs.link(i);

  while (--i >= 0)
  {
    ElemVariable v = (ElemVariable) vars.elementAt(i);

    // XObject xobj = v.getValue(this, contextNode);
    XObject xobj = new XUnresolvedVariable(v, contextNode, this,
                                   vs.getStackFrame(), 0, true);
    
    if(null == vs.elementAt(i))                               
      vs.setGlobalVariable(i, xobj);
  }

}
 
Example #4
Source File: FuncSum.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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
{

  DTMIterator nodes = m_arg0.asIterator(xctxt, xctxt.getCurrentNode());
  double sum = 0.0;
  int pos;

  while (DTM.NULL != (pos = nodes.nextNode()))
  {
    DTM dtm = nodes.getDTM(pos);
    XMLString s = dtm.getStringValue(pos);

    if (null != s)
      sum += s.toDouble();
  }
  nodes.detach();

  return new XNumber(sum);
}
 
Example #5
Source File: ElemExsltFuncResult.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Generate the EXSLT function return value, and assign it to the variable
 * index slot assigned for it in ElemExsltFunction compose().
 * 
 */
public void execute(TransformerImpl transformer) throws TransformerException
{    
  XPathContext context = transformer.getXPathContext();

  // Verify that result has not already been set by another result
  // element. Recursion is allowed: intermediate results are cleared 
  // in the owner ElemExsltFunction execute().
  if (transformer.currentFuncResultSeen()) {
      throw new TransformerException("An EXSLT function cannot set more than one result!");
  }

  int sourceNode = context.getCurrentNode();

  // Set the return value;
  XObject var = getValue(transformer, sourceNode);
  transformer.popCurrentFuncResult();
  transformer.pushCurrentFuncResult(var);
}
 
Example #6
Source File: FuncQname.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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
{

  int context = getArg0AsNode(xctxt);
  XObject val;

  if (DTM.NULL != context)
  {
    DTM dtm = xctxt.getDTM(context);
    String qname = dtm.getNodeNameX(context);
    val = (null == qname) ? XString.EMPTYSTRING : new XString(qname);
  }
  else
  {
    val = XString.EMPTYSTRING;
  }

  return val;
}
 
Example #7
Source File: FuncNormalizeSpace.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Execute an expression in the XPath runtime context, and return the 
 * result of the expression.
 *
 *
 * @param xctxt The XPath runtime context.
 *
 * @return The result of the expression in the form of a <code>XObject</code>.
 *
 * @throws javax.xml.transform.TransformerException if a runtime exception 
 *         occurs.
 */
public void executeCharsToContentHandler(XPathContext xctxt, 
                                            ContentHandler handler)
  throws javax.xml.transform.TransformerException,
         org.xml.sax.SAXException
{
  if(Arg0IsNodesetExpr())
  {
    int node = getArg0AsNode(xctxt);
    if(DTM.NULL != node)
    {
      DTM dtm = xctxt.getDTM(node);
      dtm.dispatchCharactersEvents(node, handler, true);
    }
  }
  else
  {
    XObject obj = execute(xctxt);
    obj.dispatchCharactersEvents(handler);
  }
}
 
Example #8
Source File: CachedXPathAPI.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 *   Evaluate XPath string to an XObject.
 *   XPath namespace prefixes are resolved from the namespaceNode.
 *   The implementation of this is a little slow, since it creates
 *   a number of objects each time it is called.  This could be optimized
 *   to keep the same objects around, but then thread-safety issues would arise.
 *
 *   @param contextNode The node to start searching from.
 *   @param str A valid XPath string.
 *   @param prefixResolver Will be called if the parser encounters namespace
 *                         prefixes, to resolve the prefixes to URLs.
 *   @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
 *   @see org.apache.xpath.objects.XObject
 *   @see org.apache.xpath.objects.XNull
 *   @see org.apache.xpath.objects.XBoolean
 *   @see org.apache.xpath.objects.XNumber
 *   @see org.apache.xpath.objects.XString
 *   @see org.apache.xpath.objects.XRTreeFrag
 *
 * @throws TransformerException
 */
public  XObject eval(
        Node contextNode, String str, PrefixResolver prefixResolver)
          throws TransformerException
{

  // Since we don't have a XML Parser involved here, install some default support
  // for things like namespaces, etc.
  // (Changed from: XPathContext xpathSupport = new XPathContext();
  //    because XPathContext is weak in a number of areas... perhaps
  //    XPathContext should be done away with.)
  // Create the XPath object.
  XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);

  // Create an XPathContext that doesn't support pushing and popping of
  // variable resolution scopes.  Sufficient for simple XPath 1.0 expressions.
  XPathContext xpathSupport = new XPathContext(false);

  // Execute the XPath, and have it return the result
  int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);

  return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}
 
Example #9
Source File: StepPattern.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Execute this pattern step, including predicates.
 *
 *
 * @param xctxt XPath runtime context.
 * @param currentNode The current node 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, int currentNode)
        throws javax.xml.transform.TransformerException
{

  DTM dtm = xctxt.getDTM(currentNode);

  if (dtm != null)
  {
    int expType = dtm.getExpandedTypeID(currentNode);

    return execute(xctxt, currentNode, dtm, expType);
  }

  return NodeTest.SCORE_NONE;
}
 
Example #10
Source File: FuncCount.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
   * 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
  {

//    DTMIterator nl = m_arg0.asIterator(xctxt, xctxt.getCurrentNode());

//    // We should probably make a function on the iterator for this, 
//    // as a given implementation could optimize.
//    int i = 0;
//
//    while (DTM.NULL != nl.nextNode())
//    {
//      i++;
//    }
//    nl.detach();
	DTMIterator nl = m_arg0.asIterator(xctxt, xctxt.getCurrentNode());
	int i = nl.getLength();	
	nl.detach();

    return new XNumber((double) i);
  }
 
Example #11
Source File: XalanXPathEvaluator.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
protected boolean evaluate(InputSource inputSource) {
   try {
      DocumentBuilder dbuilder = createDocumentBuilder();
      Document doc = dbuilder.parse(inputSource);

      //An XPath expression could return a true or false value instead of a node.
      //eval() is a better way to determine the boolean value of the exp.
      //For compliance with legacy behavior where selecting an empty node returns true,
      //selectNodeIterator is attempted in case of a failure.

      CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
      XObject result = cachedXPathAPI.eval(doc, xpath);
      if (result.bool())
         return true;
      else {
         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(doc, xpath);
         return (iterator.nextNode() != null);
      }
   } catch (Throwable e) {
      return false;
   }
}
 
Example #12
Source File: XPathUtils.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluates an XPath expression to an XObject.
 * @param contextNode the node to start searching from
 * @param str a valid XPath string
 * @param a prefix resolver to use for resolving namespace prefixes, or null
 * @return an XObject, which can be used to obtain a string, number, nodelist, etc (should never be {@code null})
 * @throws TransformerException if a syntax or other error occurs
 */
private static XObject evaluateXPath(final DomNode contextNode,
        final String str, final PrefixResolver prefixResolver) throws TransformerException {
    final XPathContext xpathSupport = new XPathContext();
    final Node xpathExpressionContext;
    if (contextNode.getNodeType() == Node.DOCUMENT_NODE) {
        xpathExpressionContext = ((Document) contextNode).getDocumentElement();
    }
    else {
        xpathExpressionContext = contextNode;
    }

    PrefixResolver resolver = prefixResolver;
    if (resolver == null) {
        resolver = new HtmlUnitPrefixResolver(xpathExpressionContext);
    }

    final boolean caseSensitive = contextNode.getPage().hasCaseSensitiveTagNames();
    final boolean attributeCaseSensitive = caseSensitive
                                                || contextNode.getPage().getWebClient()
                                                    .getBrowserVersion().hasFeature(XPATH_ATTRIBUTE_CASE_SENSITIVE);
    final XPathAdapter xpath = new XPathAdapter(str, null, resolver, null, caseSensitive, attributeCaseSensitive);
    final int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
    return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}
 
Example #13
Source File: VariableStack.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #14
Source File: CachedXPathAPI.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 *  Evaluate XPath string to an XObject. 
 *  XPath namespace prefixes are resolved from the namespaceNode.
 *  The implementation of this is a little slow, since it creates
 *  a number of objects each time it is called.  This could be optimized
 *  to keep the same objects around, but then thread-safety issues would arise.
 *
 *  @param contextNode The node to start searching from.
 *  @param str A valid XPath string.
 *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
 *  @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
 *  @see org.apache.xpath.objects.XObject
 *  @see org.apache.xpath.objects.XNull
 *  @see org.apache.xpath.objects.XBoolean
 *  @see org.apache.xpath.objects.XNumber
 *  @see org.apache.xpath.objects.XString
 *  @see org.apache.xpath.objects.XRTreeFrag
 *
 * @throws TransformerException
 */
public  XObject eval(Node contextNode, String str, Node namespaceNode)
        throws TransformerException
{

  // Since we don't have a XML Parser involved here, install some default support
  // for things like namespaces, etc.
  // (Changed from: XPathContext xpathSupport = new XPathContext();
  //    because XPathContext is weak in a number of areas... perhaps
  //    XPathContext should be done away with.)

  // Create an object to resolve namespace prefixes.
  // XPath namespaces are resolved from the input context node's document element
  // if it is a root node, or else the current context node (for lack of a better
  // resolution space, given the simplicity of this sample code).
  PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
    (namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
    ? ((Document) namespaceNode).getDocumentElement() : namespaceNode);

  // Create the XPath object.
  XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);

  // Execute the XPath, and have it return the result
  // return xpath.execute(xpathSupport, contextNode, prefixResolver);
  int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);

  return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
}
 
Example #15
Source File: XalanXPathExecuter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Object selectObject(Node contextNode, String expression) throws JRException {
	try {
		Object value;
		XObject object = xpathAPI.eval(contextNode, expression);
		switch (object.getType()) {
			case XObject.CLASS_NODESET:
				value = object.nodeset().nextNode();
				break;
			case XObject.CLASS_BOOLEAN:
				value = object.bool();
				break;
			case XObject.CLASS_NUMBER:
				value = object.num();
				break;
			default:
				value = object.str();
				break;
		}
		return value;
	} catch (TransformerException e) {
		throw 
			new JRException(
				EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
				new Object[]{expression},
				e);
	}
}
 
Example #16
Source File: FuncLocalPart.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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
{

  int context = getArg0AsNode(xctxt);
  if(DTM.NULL == context)
    return XString.EMPTYSTRING;
  DTM dtm = xctxt.getDTM(context);
  String s = (context != DTM.NULL) ? dtm.getLocalName(context) : "";
  if(s.startsWith("#") || s.equals("xmlns"))
    return XString.EMPTYSTRING;

  return new XString(s);
}
 
Example #17
Source File: XPathAPI.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 *  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 str A valid XPath string.
 *  @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 static NodeIterator selectNodeIterator(
        Node contextNode, String str, Node namespaceNode)
          throws TransformerException
{

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

  // Have the XObject return its result as a NodeSetDTM.                
  return list.nodeset();
}
 
Example #18
Source File: XPath.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Get the match score of the given node.
 *
 * @param xctxt XPath runtime context.
 * @param context The current source tree context node.
 * 
 * @return score, one of {@link #MATCH_SCORE_NODETEST},
 * {@link #MATCH_SCORE_NONE}, {@link #MATCH_SCORE_OTHER}, 
 * or {@link #MATCH_SCORE_QNAME}.
 *
 * @throws javax.xml.transform.TransformerException
 */
public double getMatchScore(XPathContext xctxt, int context)
        throws javax.xml.transform.TransformerException
{

  xctxt.pushCurrentNode(context);
  xctxt.pushCurrentExpressionNode(context);

  try
  {
    XObject score = m_mainExp.execute(xctxt);

    if (DEBUG_MATCHES)
    {
      DTM dtm = xctxt.getDTM(context);
      System.out.println("score: " + score.num() + " for "
                         + dtm.getNodeName(context) + " for xpath "
                         + this.getPatternString());
    }

    return score.num();
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.popCurrentExpressionNode();
  }

  // return XPath.MATCH_SCORE_NONE;
}
 
Example #19
Source File: FuncSubstringAfter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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
{

  XMLString s1 = m_arg0.execute(xctxt).xstr();
  XMLString s2 = m_arg1.execute(xctxt).xstr();
  int index = s1.indexOf(s2);

  return (-1 == index)
         ? XString.EMPTYSTRING
         : (XString)s1.substring(index + s2.length());
}
 
Example #20
Source File: XPathUtils.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates an XPath expression from the specified node, returning the resultant nodes.
 *
 * @param <T> the type class
 * @param node the node to start searching from
 * @param xpathExpr the XPath expression
 * @param resolver the prefix resolver to use for resolving namespace prefixes, or null
 * @return the list of objects found
 */
@SuppressWarnings("unchecked")
public static <T> List<T> getByXPath(final DomNode node, final String xpathExpr, final PrefixResolver resolver) {
    if (xpathExpr == null) {
        throw new NullPointerException("Null is not a valid XPath expression");
    }

    PROCESS_XPATH_.set(Boolean.TRUE);
    final List<T> list = new ArrayList<>();
    try {
        final XObject result = evaluateXPath(node, xpathExpr, resolver);

        if (result instanceof XNodeSet) {
            final NodeList nodelist = ((XNodeSet) result).nodelist();
            for (int i = 0; i < nodelist.getLength(); i++) {
                list.add((T) nodelist.item(i));
            }
        }
        else if (result instanceof XNumber) {
            list.add((T) Double.valueOf(result.num()));
        }
        else if (result instanceof XBoolean) {
            list.add((T) Boolean.valueOf(result.bool()));
        }
        else if (result instanceof XString) {
            list.add((T) result.str());
        }
        else {
            throw new RuntimeException("Unproccessed " + result.getClass().getName());
        }
    }
    catch (final Exception e) {
        throw new RuntimeException("Could not retrieve XPath >" + xpathExpr + "< on " + node, e);
    }
    finally {
        PROCESS_XPATH_.set(Boolean.FALSE);
    }
    return list;
}
 
Example #21
Source File: UnionPattern.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #22
Source File: Operation.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Execute a binary operation by calling execute on each of the operands,
 * and then calling the operate method on the derived class.
 *
 *
 * @param xctxt The runtime execution context.
 *
 * @return The XObject result of the operation.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt)
        throws javax.xml.transform.TransformerException
{

  XObject left = m_left.execute(xctxt, true);
  XObject right = m_right.execute(xctxt, true);

  XObject result = operate(left, right);
  left.detach();
  right.detach();
  return result;
}
 
Example #23
Source File: FuncRound.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example #24
Source File: StepPattern.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Execute an expression in the XPath runtime context, and return the
 * result of the expression.
 *
 *
 * @param xctxt The XPath runtime context.
 * @param currentNode The currentNode.
 * @param dtm The DTM of the current node.
 * @param expType The expanded type ID of the current node.
 *
 * @return The result of the expression in the form of a <code>XObject</code>.
 *
 * @throws javax.xml.transform.TransformerException if a runtime exception
 *         occurs.
 */
public XObject execute(
        XPathContext xctxt, int currentNode, DTM dtm, int expType)
          throws javax.xml.transform.TransformerException
{

  if (m_whatToShow == NodeTest.SHOW_BYFUNCTION)
  {
    if (null != m_relativePathPattern)
    {
      return m_relativePathPattern.execute(xctxt);
    }
    else
      return NodeTest.SCORE_NONE;
  }

  XObject score;

  score = super.execute(xctxt, currentNode, dtm, expType);

  if (score == NodeTest.SCORE_NONE)
    return NodeTest.SCORE_NONE;

  if (getPredicateCount() != 0)
  {
    if (!executePredicates(xctxt, dtm, currentNode))
      return NodeTest.SCORE_NONE;
  }

  if (null != m_relativePathPattern)
    return m_relativePathPattern.executeRelativePathPattern(xctxt, dtm,
            currentNode);

  return score;
}
 
Example #25
Source File: XPathHelper.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates an XPath expression from the specified node, returning the resultant nodes.
 *
 * @param <T> the type class
 * @param node the node to start searching from
 * @param xpathExpr the XPath expression
 * @param resolver the prefix resolver to use for resolving namespace prefixes, or null
 * @return the list of objects found
 */
@SuppressWarnings("unchecked")
public static <T> List<T> getByXPath(final DomNode node, final String xpathExpr,
        final PrefixResolver resolver) {
    if (xpathExpr == null) {
        throw new IllegalArgumentException("Null is not a valid XPath expression");
    }

    PROCESS_XPATH_.set(Boolean.TRUE);
    final List<T> list = new ArrayList<>();
    try {
        final XObject result = evaluateXPath(node, xpathExpr, resolver);

        if (result instanceof XNodeSet) {
            final NodeList nodelist = ((XNodeSet) result).nodelist();
            for (int i = 0; i < nodelist.getLength(); i++) {
                list.add((T) nodelist.item(i));
            }
        }
        else if (result instanceof XNumber) {
            list.add((T) Double.valueOf(result.num()));
        }
        else if (result instanceof XBoolean) {
            list.add((T) Boolean.valueOf(result.bool()));
        }
        else if (result instanceof XString) {
            list.add((T) result.str());
        }
        else {
            throw new RuntimeException("Unproccessed " + result.getClass().getName());
        }
    }
    catch (final Exception e) {
        throw new RuntimeException("Could not retrieve XPath >" + xpathExpr + "< on " + node, e);
    }
    finally {
        PROCESS_XPATH_.set(Boolean.FALSE);
    }
    return list;
}
 
Example #26
Source File: FunctionPattern.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test a node to see if it matches the given node test.
 *
 * @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
{

  int context = xctxt.getCurrentNode();
  DTMIterator nl = m_functionExpr.asIterator(xctxt, context);
  XNumber score = SCORE_NONE;

  if (null != nl)
  {
    int n;

    while (DTM.NULL != (n = nl.nextNode()))
    {
      score = (n == context) ? SCORE_OTHER : SCORE_NONE;

      if (score == SCORE_OTHER)
      {
        context = n;

        break;
      }
    }

    nl.detach();
  }

  return score;
}
 
Example #27
Source File: FuncDoclocation.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
   * 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
  {

    int whereNode = getArg0AsNode(xctxt);
    String fileLocation = null;

    if (DTM.NULL != whereNode)
    {
      DTM dtm = xctxt.getDTM(whereNode);
      
      // %REVIEW%
      if (DTM.DOCUMENT_FRAGMENT_NODE ==  dtm.getNodeType(whereNode))
      {
        whereNode = dtm.getFirstChild(whereNode);
      }

      if (DTM.NULL != whereNode)
      {        
        fileLocation = dtm.getDocumentBaseURI();
//        int owner = dtm.getDocument();
//        fileLocation = xctxt.getSourceTreeManager().findURIFromDoc(owner);
      }
    }

    return new XString((null != fileLocation) ? fileLocation : "");
  }
 
Example #28
Source File: Or.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * OR two expressions and return the boolean result. Override
 * superclass method for optimization purposes.
 *
 * @param xctxt The runtime execution context.
 *
 * @return {@link org.apache.xpath.objects.XBoolean#S_TRUE} or 
 * {@link org.apache.xpath.objects.XBoolean#S_FALSE}.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{

  XObject expr1 = m_left.execute(xctxt);

  if (!expr1.bool())
  {
    XObject expr2 = m_right.execute(xctxt);

    return expr2.bool() ? XBoolean.S_TRUE : XBoolean.S_FALSE;
  }
  else
    return XBoolean.S_TRUE;
}
 
Example #29
Source File: Arg.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a parameter argument which has an XObject value.
 * isVisible defaults to true.
 *
 * @param qname Name of the argument, expressed as a QName object.
 * @param val Value of the argument, expressed as an XObject
 */
public Arg(QName qname, XObject val)
{

  m_qname = qname;
  m_val = val;
  m_isVisible = true;
  m_isFromWithParam = false;
  m_expression = null;
}
 
Example #30
Source File: CachedXPathAPI.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 *  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 str A valid XPath string.
 *  @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  NodeIterator selectNodeIterator(
        Node contextNode, String str, Node namespaceNode)
          throws TransformerException
{

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

  // Have the XObject return its result as a NodeSetDTM.                
  return list.nodeset();
}