Search in sources :

Example 6 with PrefixResolverDefault

use of org.apache.xml.utils.PrefixResolverDefault in project j2objc by google.

the class CachedXPathAPI method eval.

/**
 *  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);
}
Also used : PrefixResolverDefault(org.apache.xml.utils.PrefixResolverDefault) Document(org.w3c.dom.Document)

Example 7 with PrefixResolverDefault

use of org.apache.xml.utils.PrefixResolverDefault in project intellij-community by JetBrains.

the class XalanStyleFrame method eval.

public Value eval(String expr) throws Debugger.EvaluationException {
    assert isValid();
    try {
        final DTMIterator context = myTransformer.getContextNodeList();
        final int ctx;
        final DTM dtm = context.getDTM(myCurrentNode);
        if (dtm.getDocumentRoot(myCurrentNode) == myCurrentNode) {
            ctx = dtm.getFirstChild(myCurrentNode);
        } else {
            ctx = myCurrentNode;
        }
        final DTMNodeProxy c = new DTMNodeProxy(dtm, ctx);
        final PrefixResolver prefixResolver = new PrefixResolverDefault(c) {

            public String getNamespaceForPrefix(String prefix, Node context) {
                if (context instanceof DTMNodeProxy) {
                    final DTMNodeProxy proxy = (DTMNodeProxy) context;
                    final DTM dtm = proxy.getDTM();
                    int p = proxy.getDTMNodeNumber();
                    while (p != DTM.NULL) {
                        int nsNode = dtm.getFirstNamespaceNode(p, true);
                        while (nsNode != DTM.NULL) {
                            final String s = dtm.getLocalName(nsNode);
                            if (s.equals(prefix)) {
                                return dtm.getNodeValue(nsNode);
                            }
                            nsNode = dtm.getNextNamespaceNode(p, nsNode, true);
                        }
                        p = dtm.getParent(p);
                    }
                }
                return super.getNamespaceForPrefix(prefix, context);
            }
        };
        final XPath xPath = new XPath(expr, myCurrentElement, prefixResolver, XPath.SELECT, myTransformer.getErrorListener());
        return new XObjectValue(xPath.execute(myContext, myCurrentNode, myCurrentElement));
    } catch (Exception e) {
        debug(e);
        final String message = e.getMessage();
        throw new Debugger.EvaluationException(message != null ? message : e.getClass().getSimpleName());
    }
}
Also used : XPath(org.apache.xpath.XPath) Debugger(org.intellij.plugins.xsltDebugger.rt.engine.Debugger) Node(org.w3c.dom.Node) PrefixResolver(org.apache.xml.utils.PrefixResolver) TransformerException(javax.xml.transform.TransformerException) DTMIterator(org.apache.xml.dtm.DTMIterator) DTM(org.apache.xml.dtm.DTM) DTMNodeProxy(org.apache.xml.dtm.ref.DTMNodeProxy) PrefixResolverDefault(org.apache.xml.utils.PrefixResolverDefault)

Example 8 with PrefixResolverDefault

use of org.apache.xml.utils.PrefixResolverDefault in project robovm by robovm.

the class CachedXPathAPI method eval.

/**
   *  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);
}
Also used : PrefixResolverDefault(org.apache.xml.utils.PrefixResolverDefault) Document(org.w3c.dom.Document)

Aggregations

PrefixResolverDefault (org.apache.xml.utils.PrefixResolverDefault)8 Document (org.w3c.dom.Document)5 XPath (org.apache.xpath.XPath)2 XPathContext (org.apache.xpath.XPathContext)2 Node (org.w3c.dom.Node)2 QName (javax.xml.namespace.QName)1 TransformerException (javax.xml.transform.TransformerException)1 DTM (org.apache.xml.dtm.DTM)1 DTMIterator (org.apache.xml.dtm.DTMIterator)1 DTMNodeProxy (org.apache.xml.dtm.ref.DTMNodeProxy)1 PrefixResolver (org.apache.xml.utils.PrefixResolver)1 Debugger (org.intellij.plugins.xsltDebugger.rt.engine.Debugger)1