org.apache.xpath.objects.XBoolean Java Examples

The following examples show how to use org.apache.xpath.objects.XBoolean. 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: 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 #2
Source File: FuncContains.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
{

  String s1 = m_arg0.execute(xctxt).str();
  String s2 = m_arg1.execute(xctxt).str();

  // Add this check for JDK consistency for empty strings.
  if (s1.length() == 0 && s2.length() == 0)
    return XBoolean.S_TRUE;

  int index = s1.indexOf(s2);

  return (index > -1) ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #3
Source File: FuncLang.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
{

  String lang = m_arg0.execute(xctxt).str();
  int parent = xctxt.getCurrentNode();
  boolean isLang = false;
  DTM dtm = xctxt.getDTM(parent);

  while (DTM.NULL != parent)
  {
    if (DTM.ELEMENT_NODE == dtm.getNodeType(parent))
    {
      int langAttr = dtm.getAttributeNode(parent, "http://www.w3.org/XML/1998/namespace", "lang");

      if (DTM.NULL != langAttr)
      {
        String langVal = dtm.getNodeValue(langAttr);
        // %OPT%
        if (langVal.toLowerCase().startsWith(lang.toLowerCase()))
        {
          int valLen = lang.length();

          if ((langVal.length() == valLen)
                  || (langVal.charAt(valLen) == '-'))
          {
            isLang = true;
          }
        }

        break;
      }
    }

    parent = dtm.getParent(parent);
  }

  return isLang ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #4
Source File: And.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * AND 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_FALSE;
}
 
Example #5
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 #6
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 #7
Source File: FuncExtElementAvailable.java    From j2objc with Apache License 2.0 4 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
{

  String prefix;
  String namespace;
  String methName;

  String fullName = m_arg0.execute(xctxt).str();
  int indexOfNSSep = fullName.indexOf(':');

  if (indexOfNSSep < 0)
  {
    prefix = "";
    namespace = Constants.S_XSLNAMESPACEURL;
    methName = fullName;
  }
  else
  {
    prefix = fullName.substring(0, indexOfNSSep);
    namespace = xctxt.getNamespaceContext().getNamespaceForPrefix(prefix);
    if (null == namespace)
      return XBoolean.S_FALSE;
    methName= fullName.substring(indexOfNSSep + 1);
  }

  if (namespace.equals(Constants.S_XSLNAMESPACEURL)
  ||  namespace.equals(Constants.S_BUILTIN_EXTENSIONS_URL))
  {
    try
    {
      TransformerImpl transformer = (TransformerImpl) xctxt.getOwnerObject();
      return transformer.getStylesheet().getAvailableElements().containsKey(
                                                          new QName(namespace, methName))
             ? XBoolean.S_TRUE : XBoolean.S_FALSE;
    }
    catch (Exception e)
    {
      return XBoolean.S_FALSE;
    }
  }
  else
  {
    //dml
    ExtensionsProvider extProvider = (ExtensionsProvider)xctxt.getOwnerObject();
    return extProvider.elementAvailable(namespace, methName)
           ? XBoolean.S_TRUE : XBoolean.S_FALSE;
  }
}
 
Example #8
Source File: Bool.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_BOOLEAN == right.getType())
    return right;
  else
    return right.bool() ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #9
Source File: Gte.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 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.greaterThanOrEqual(right)
         ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #10
Source File: Lt.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 left.lessThan(right) ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #11
Source File: Gt.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 left.greaterThan(right) ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #12
Source File: Lte.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 left.lessThanOrEqual(right) ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #13
Source File: Equals.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 left.equals(right) ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #14
Source File: NotEquals.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 (left.notEquals(right)) ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #15
Source File: FuncTrue.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 XBoolean.S_TRUE;
}
 
Example #16
Source File: FuncStartsWith.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 m_arg0.execute(xctxt).xstr().startsWith(m_arg1.execute(xctxt).xstr())
         ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #17
Source File: FuncFalse.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 XBoolean.S_FALSE;
}
 
Example #18
Source File: FuncBoolean.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 m_arg0.execute(xctxt).bool() ? XBoolean.S_TRUE : XBoolean.S_FALSE;
}
 
Example #19
Source File: FuncNot.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 m_arg0.execute(xctxt).bool() ? XBoolean.S_FALSE : XBoolean.S_TRUE;
}