org.apache.xpath.objects.XNodeSet Java Examples

The following examples show how to use org.apache.xpath.objects.XNodeSet. 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: Expression.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Given an select expression and a context, evaluate the XPath
 * and return the resulting iterator, but do not clone.
 *
 * @param xctxt The execution context.
 * @param contextNode The node that "." expresses.
 *
 *
 * @return A valid DTMIterator.
 * @throws TransformerException thrown if the active ProblemListener decides
 * the error condition is severe enough to halt processing.
 *
 * @throws javax.xml.transform.TransformerException
 * @xsl.usage experimental
 */
public DTMIterator asIteratorRaw(XPathContext xctxt, int contextNode)
        throws javax.xml.transform.TransformerException
{

  try
  {
    xctxt.pushCurrentNodeAndExpression(contextNode, contextNode);

    XNodeSet nodeset = (XNodeSet)execute(xctxt);
    return nodeset.iterRaw();
  }
  finally
  {
    xctxt.popCurrentNodeAndExpression();
  }
}
 
Example #2
Source File: FuncCurrent.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
{
 
  SubContextList subContextList = xctxt.getCurrentNodeList();
  int currentNode = DTM.NULL;

  if (null != subContextList) {
      if (subContextList instanceof PredicatedNodeTest) {
          LocPathIterator iter = ((PredicatedNodeTest)subContextList)
                                                        .getLocPathIterator();
          currentNode = iter.getCurrentContextNode();
       } else if(subContextList instanceof StepPattern) {
         throw new RuntimeException(XSLMessages.createMessage(
            XSLTErrorResources.ER_PROCESSOR_ERROR,null));
       }
  } else {
      // not predicate => ContextNode == CurrentNode
      currentNode = xctxt.getContextNode();
  }
  return new XNodeSet(currentNode, xctxt.getDTMManager());
}
 
Example #3
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 #4
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 #5
Source File: MCRIncludeHandler.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public XNodeSet resolve(ExpressionContext context, String uri, String sStatic)
    throws TransformerException, JDOMException {
    LOGGER.debug("including xml " + uri);

    Element xml = resolve(uri, sStatic);
    Node node = jdom2dom(xml);

    try {
        return asNodeSet(context, node);
    } catch (Exception ex) {
        LOGGER.error(ex);
        throw ex;
    }
}
 
Example #6
Source File: KeyTable.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Build a keys table.
 * @param doc The owner document key.
 * @param nscontext The stylesheet's namespace context.
 * @param name The key name
 * @param keyDeclarations The stylesheet's xsl:key declarations.
 *
 * @throws javax.xml.transform.TransformerException
 */
public KeyTable(
        int doc, PrefixResolver nscontext, QName name, Vector keyDeclarations, XPathContext xctxt)
          throws javax.xml.transform.TransformerException
{
  m_docKey = doc;
  m_keyDeclarations = keyDeclarations;
  KeyIterator ki = new KeyIterator(name, keyDeclarations);

  m_keyNodes = new XNodeSet(ki);
  m_keyNodes.allowDetachToRelease(false);
  m_keyNodes.setRoot(doc, xctxt);
}
 
Example #7
Source File: MCRIncludeHandler.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
public XNodeSet resolve(ExpressionContext context, String ref) throws JDOMException, TransformerException {
    LOGGER.debug("including component " + ref);
    Map<String, Element> cache = chooseCacheLevel(ref, Boolean.FALSE.toString());
    Element resolved = cache.get(ref);
    return (resolved == null ? null : asNodeSet(context, jdom2dom(resolved)));
}
 
Example #8
Source File: MCRIncludeHandler.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
private XNodeSet asNodeSet(ExpressionContext context, Node node) throws TransformerException, JDOMException {
    NodeSet nodeSet = new NodeSet();
    nodeSet.addNode(node);
    XPathContext xpc = context.getXPathContext();
    return new XNodeSetForDOM((NodeList) nodeSet, xpc);
}
 
Example #9
Source File: NodeSorter.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor NodeCompareElem
 *
 *
 * @param node Current node
 *
 * @throws javax.xml.transform.TransformerException
 */
NodeCompareElem(int node) throws javax.xml.transform.TransformerException
{
  m_node = node;

  if (!m_keys.isEmpty())
  {
    NodeSortKey k1 = (NodeSortKey) m_keys.elementAt(0);
    XObject r = k1.m_selectPat.execute(m_execContext, node,
                                       k1.m_namespaceContext);

    double d;

    if (k1.m_treatAsNumbers)
    {
      d = r.num();

      // Can't use NaN for compare. They are never equal. Use zero instead.  
      m_key1Value = new Double(d);
    }
    else
    {
      m_key1Value = k1.m_col.getCollationKey(r.str());
    }

    if (r.getType() == XObject.CLASS_NODESET)
    {
      // %REVIEW%
      DTMIterator ni = ((XNodeSet)r).iterRaw();
      int current = ni.getCurrentNode();
      if(DTM.NULL == current)
        current = ni.nextNode();

      // if (ni instanceof ContextNodeList) // %REVIEW%
      // tryNextKey = (DTM.NULL != current);

      // else abdicate... should never happen, but... -sb
    }

    if (m_keys.size() > 1)
    {
      NodeSortKey k2 = (NodeSortKey) m_keys.elementAt(1);

      XObject r2 = k2.m_selectPat.execute(m_execContext, node,
                                          k2.m_namespaceContext);

      if (k2.m_treatAsNumbers) {
        d = r2.num();
        m_key2Value = new Double(d);
      } else {
        m_key2Value = k2.m_col.getCollationKey(r2.str());
      }
    }

    /* Leave this in case we decide to use an array later
    while (kIndex <= m_keys.size() && kIndex < maxkey)
    {
      NodeSortKey k = (NodeSortKey)m_keys.elementAt(kIndex);
      XObject r = k.m_selectPat.execute(m_execContext, node, k.m_namespaceContext);
      if(k.m_treatAsNumbers)
        m_KeyValue[kIndex] = r.num();
      else
        m_KeyValue[kIndex] = r.str();
    } */
  }  // end if not empty    
}
 
Example #10
Source File: FilterExprIteratorSimple.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Execute the expression.  Meant for reuse by other FilterExpr iterators 
 * that are not derived from this object.
 */
public static XNodeSet executeFilterExpr(int context, XPathContext xctxt, 
												PrefixResolver prefixResolver,
												boolean isTopLevel,
												int stackFrame,
												Expression expr )
  throws org.apache.xml.utils.WrappedRuntimeException
{
  PrefixResolver savedResolver = xctxt.getNamespaceContext();
  XNodeSet result = null;

  try
  {
    xctxt.pushCurrentNode(context);
    xctxt.setNamespaceContext(prefixResolver);

    // The setRoot operation can take place with a reset operation, 
    // and so we may not be in the context of LocPathIterator#nextNode, 
    // so we have to set up the variable context, execute the expression, 
    // and then restore the variable context.

    if (isTopLevel)
    {
      // System.out.println("calling m_expr.execute(getXPathContext())");
      VariableStack vars = xctxt.getVarStack();

      // These three statements need to be combined into one operation.
      int savedStart = vars.getStackFrame();
      vars.setStackFrame(stackFrame);

      result = (org.apache.xpath.objects.XNodeSet) expr.execute(xctxt);
      result.setShouldCacheNodes(true);

      // These two statements need to be combined into one operation.
      vars.setStackFrame(savedStart);
    }
    else
        result = (org.apache.xpath.objects.XNodeSet) expr.execute(xctxt);

  }
  catch (javax.xml.transform.TransformerException se)
  {

    // TODO: Fix...
    throw new org.apache.xml.utils.WrappedRuntimeException(se);
  }
  finally
  {
    xctxt.popCurrentNode();
    xctxt.setNamespaceContext(savedResolver);
  }
  return result;
}
 
Example #11
Source File: LocPathIterator.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Execute this iterator, meaning create a clone that can
 * store state, and initialize it for fast execution from
 * the current runtime state.  When this is called, no actual
 * query from the current context node is performed.
 *
 * @param xctxt The XPath execution context.
 *
 * @return An XNodeSet reference that holds this iterator.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt)
        throws javax.xml.transform.TransformerException
{

  XNodeSet iter = new XNodeSet((LocPathIterator)m_clones.getInstance());

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

  return iter;
}
 
Example #12
Source File: LocPathIterator.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Given an select expression and a context, evaluate the XPath
 * and return the resulting iterator.
 * 
 * @param xctxt The execution context.
 * @param contextNode The node that "." expresses.
 * @throws TransformerException thrown if the active ProblemListener decides
 * the error condition is severe enough to halt processing.
 *
 * @throws javax.xml.transform.TransformerException
 * @xsl.usage experimental
 */
public DTMIterator asIterator(
        XPathContext xctxt, int contextNode)
          throws javax.xml.transform.TransformerException
{
  XNodeSet iter = new XNodeSet((LocPathIterator)m_clones.getInstance());

  iter.setRoot(contextNode, xctxt);

  return iter;
}