org.apache.xpath.objects.XNumber Java Examples

The following examples show how to use org.apache.xpath.objects.XNumber. 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: 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 #2
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 #3
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 #4
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, int context)
        throws javax.xml.transform.TransformerException
{

  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();
  }
  nl.detach();

  return score;
}
 
Example #5
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, int context, 
                       DTM dtm, int expType)
        throws javax.xml.transform.TransformerException
{

  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 #6
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 #7
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 #8
Source File: XPathParser.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 *
 * Number ::= [0-9]+('.'[0-9]+)? | '.'[0-9]+
 *
 *
 * @throws javax.xml.transform.TransformerException
 */
protected void Number() throws javax.xml.transform.TransformerException
{

  if (null != m_token)
  {

    // Mutate the token to remove the quotes and have the XNumber object
    // already made.
    double num;

    try
    {
    	// XPath 1.0 does not support number in exp notation
    	if ((m_token.indexOf('e') > -1)||(m_token.indexOf('E') > -1))
    		throw new NumberFormatException();
      num = Double.valueOf(m_token).doubleValue();
    }
    catch (NumberFormatException nfe)
    {
      num = 0.0;  // to shut up compiler.

      error(XPATHErrorResources.ER_COULDNOT_BE_FORMATTED_TO_NUMBER,
            new Object[]{ m_token });  //m_token+" could not be formatted to a number!");
    }

    m_ops.m_tokenQueue.setElementAt(new XNumber(num),m_queueMark - 1);
    m_ops.setOp(m_ops.getOp(OpMap.MAPINDEX_LENGTH), m_queueMark - 1);
    m_ops.setOp(OpMap.MAPINDEX_LENGTH, m_ops.getOp(OpMap.MAPINDEX_LENGTH) + 1);

    nextToken();
  }
}
 
Example #9
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 #10
Source File: HasPositionalPredChecker.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Visit a predicate within a location path.  Note that there isn't a 
 * proper unique component for predicates, and that the expression will 
 * be called also for whatever type Expression is.
 * 
 * @param owner The owner of the expression, to which the expression can 
 *              be reset if rewriting takes place.
 * @param pred The predicate object.
 * @return true if the sub expressions should be traversed.
 */
public boolean visitPredicate(ExpressionOwner owner, Expression pred)
{
  m_predDepth++;

  if(m_predDepth == 1)
  {
    if((pred instanceof Variable) || 
       (pred instanceof XNumber) ||
       (pred instanceof Div) ||
       (pred instanceof Plus) ||
       (pred instanceof Minus) ||
       (pred instanceof Mod) ||
       (pred instanceof Quo) ||
       (pred instanceof Mult) ||
       (pred instanceof org.apache.xpath.operations.Number) ||
       (pred instanceof Function))
        m_hasPositionalPred = true;
    else
    	pred.callVisitors(owner, this);
  }

  m_predDepth--;

  // Don't go have the caller go any further down the subtree.
  return false;
}
 
Example #11
Source File: NodeTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Get the static score for this node test.
 * @return Should be one of the SCORE_XXX constants.
 */
public XNumber getStaticScore()
{
  return m_score;
}
 
Example #12
Source File: NodeTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Set the static score for this node test.
 * @param score Should be one of the SCORE_XXX constants.
 */
public void setStaticScore(XNumber score)
{
  m_score = score;
}
 
Example #13
Source File: Compiler.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Compile a literal number value.
 * 
 * @param opPos The current position in the m_opMap array.
 *
 * @return reference to {@link org.apache.xpath.objects.XNumber} instance.
 *
 * @throws TransformerException if a error occurs creating the Expression.
 */
protected Expression numberlit(int opPos)
{

  opPos = getFirstChildPos(opPos);

  return (XNumber) getTokenQueue().elementAt(getOp(opPos));
}
 
Example #14
Source File: FuncPosition.java    From j2objc with Apache License 2.0 3 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
{
  double pos = (double) getPositionInContextNodeList(xctxt);
  
  return new XNumber(pos);
}
 
Example #15
Source File: FuncLast.java    From j2objc with Apache License 2.0 3 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
{
  XNumber xnum = new XNumber((double) getCountOfContextNodeList(xctxt));
  // System.out.println("last: "+xnum.num());
  return xnum;
}
 
Example #16
Source File: Number.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * 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_NUMBER == right.getType())
    return right;
  else
    return new XNumber(right.num());
}
 
Example #17
Source File: XPathVisitor.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * Visit a number literal.
 * @param owner The owner of the expression, to which the expression can 
 *              be reset if rewriting takes place.
 * @param num The number literal object.
 * @return true if the sub expressions should be traversed.
 */
public boolean visitNumberLiteral(ExpressionOwner owner, XNumber num)
{
	return true;
}
 
Example #18
Source File: Mult.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * 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(left.num() * right.num());
}
 
Example #19
Source File: Neg.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * 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
{
  return new XNumber(-right.num());
}
 
Example #20
Source File: Minus.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * 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(left.num() - right.num());
}
 
Example #21
Source File: FuncCeiling.java    From j2objc with Apache License 2.0 2 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
{
  return new XNumber(Math.ceil(m_arg0.execute(xctxt).num()));
}
 
Example #22
Source File: Div.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * 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(left.num() / right.num());
}
 
Example #23
Source File: FuncNumber.java    From j2objc with Apache License 2.0 2 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
{
  return new XNumber(getArg0AsNumber(xctxt));
}
 
Example #24
Source File: FuncStringLength.java    From j2objc with Apache License 2.0 2 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
{
  return new XNumber(getArg0AsString(xctxt).length());
}
 
Example #25
Source File: Mod.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * 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(left.num() % right.num());
}
 
Example #26
Source File: FuncFloor.java    From j2objc with Apache License 2.0 2 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
{
  return new XNumber(java.lang.Math.floor(m_arg0.execute(xctxt).num()));
}
 
Example #27
Source File: Quo.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * 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()));
}
 
Example #28
Source File: Plus.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * 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(left.num() + right.num());
}