Search in sources :

Example 1 with SimpleNamespaceContext

use of org.jaxen.SimpleNamespaceContext in project webservices-axiom by apache.

the class XPathEvaluator method evaluateXpath.

public List evaluateXpath(String xpathExpression, Object element, String nsURI) throws Exception {
    AXIOMXPath xpath = new AXIOMXPath(xpathExpression);
    if (nsURI != null) {
        SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
        nsContext.addNamespace(null, nsURI);
        xpath.setNamespaceContext(nsContext);
    }
    return xpath.selectNodes(element);
}
Also used : SimpleNamespaceContext(org.jaxen.SimpleNamespaceContext) AXIOMXPath(org.apache.axiom.om.xpath.AXIOMXPath)

Example 2 with SimpleNamespaceContext

use of org.jaxen.SimpleNamespaceContext in project tdi-studio-se by Talend.

the class SoapExecutor method createXPath.

/**
     * Constructs XPath query over the SOAP message
     * @param query XPath query
     * @param response SOAP message
     * @return XPath query
     * @throws SOAPException in case of SOAP issue
     * @throws JaxenException XPath problem
     */
public XPath createXPath(String query, SOAPMessage response) throws SOAPException, JaxenException {
    //Uses DOM to XPath mapping
    XPath xpath = new DOMXPath(query);
    //Define a namespaces used in response
    SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
    SOAPPart sp = response.getSOAPPart();
    SOAPEnvelope env = sp.getEnvelope();
    SOAPBody bdy = env.getBody();
    //Add namespaces from SOAP envelope
    addNamespaces(nsContext, env);
    //Add namespaces of top body element
    Iterator bodyElements = bdy.getChildElements();
    while (bodyElements.hasNext()) {
        SOAPElement element = (SOAPElement) bodyElements.next();
        addNamespaces(nsContext, element);
    }
    xpath.setNamespaceContext(nsContext);
    return xpath;
}
Also used : XPath(org.jaxen.XPath) DOMXPath(org.jaxen.dom.DOMXPath) DOMXPath(org.jaxen.dom.DOMXPath) Iterator(java.util.Iterator) SimpleNamespaceContext(org.jaxen.SimpleNamespaceContext)

Example 3 with SimpleNamespaceContext

use of org.jaxen.SimpleNamespaceContext in project vcell by virtualcell.

the class SEDMLUtils method parseXPath.

/**
 * Parses xPath string specified as arg1 in XML file specified as arg2 (using XOM and Jaxen)
 * @param xPathExpr
 * @param xmlFile
 * @return
 */
private static String parseXPath(String xPathExpr, File xmlFile) {
    try {
        Builder builder = new Builder();
        nu.xom.Document document = builder.build(xmlFile);
        nu.xom.Element rootElement = document.getRootElement();
        String defaultNamespaceURI = rootElement.getNamespaceURI();
        String defaultNamespacePrefix = rootElement.getNamespacePrefix();
        if ("".equals(defaultNamespacePrefix)) {
            ToolSupport.nameSpaces_PrefixesHashMap.put("sbml", defaultNamespaceURI);
        } else {
            ToolSupport.nameSpaces_PrefixesHashMap.put(defaultNamespacePrefix, defaultNamespaceURI);
        }
        org.jaxen.XPath xpath = new XOMXPath(xPathExpr);
        xpath.setNamespaceContext(new SimpleNamespaceContext(ToolSupport.nameSpaces_PrefixesHashMap));
        Object node = xpath.selectSingleNode(document);
        String elementId = ((nu.xom.Element) node).getAttributeValue("id");
        log.debug("Returned node id : " + elementId);
        return elementId;
    } catch (JaxenException e) {
        // An error occurred parsing or executing the XPath
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    } catch (IOException e) {
        // An error occurred opening the document
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    } catch (ParsingException e) {
        // An error occurred parsing the document
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    }
}
Also used : Builder(nu.xom.Builder) SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) IOException(java.io.IOException) XOMXPath(org.jaxen.xom.XOMXPath) JaxenException(org.jaxen.JaxenException) ParsingException(nu.xom.ParsingException) SimpleNamespaceContext(org.jaxen.SimpleNamespaceContext)

Example 4 with SimpleNamespaceContext

use of org.jaxen.SimpleNamespaceContext in project application by collectionspace.

the class XTmplTmpl method compileTemplate.

@SuppressWarnings("unchecked")
private void compileTemplate(Document template) throws InvalidXTmplException {
    try {
        Map<String, String> map = new HashMap<String, String>();
        map.put("xtmpl", XTMPL_URI);
        Dom4jXPath xpath = new Dom4jXPath("//.[@xtmpl:point]");
        xpath.setNamespaceContext(new SimpleNamespaceContext(map));
        List<Node> paths = xpath.selectNodes(template);
        QName attr_qname = new QName("point", new Namespace("xtmpl", XTMPL_URI));
        for (Node n : paths) {
            if (!(n instanceof Element))
                continue;
            Element e = (Element) n;
            Attribute attr = e.attribute(attr_qname);
            String key = attr.getText();
            String path = e.getPath();
            e.remove(attr);
            attach.put(key, path);
        }
        removeNamespaces(template.getDocument().getRootElement());
        document = template;
    } catch (JaxenException e) {
        throw new InvalidXTmplException("Cannot parse template file", e);
    }
}
Also used : HashMap(java.util.HashMap) Attribute(org.dom4j.Attribute) QName(org.dom4j.QName) Node(org.dom4j.Node) Element(org.dom4j.Element) Namespace(org.dom4j.Namespace) Dom4jXPath(org.jaxen.dom4j.Dom4jXPath) JaxenException(org.jaxen.JaxenException) SimpleNamespaceContext(org.jaxen.SimpleNamespaceContext)

Example 5 with SimpleNamespaceContext

use of org.jaxen.SimpleNamespaceContext in project pentaho-platform by pentaho.

the class PentahoObjectsConfig method getObjectScope.

public String getObjectScope(String objectId) {
    try {
        HashMap<String, String> map = new HashMap<String, String>();
        // $NON-NLS-1$
        map.put("default", DEFAULT_NAMESPACE);
        Dom4jXPath xpath = new Dom4jXPath(BEAN_ID_XPATH);
        xpath.setNamespaceContext(new SimpleNamespaceContext(map));
        Element element = (Element) xpath.selectSingleNode(document);
        return element.attributeValue(SCOPE_ATTRIBUTE);
    } catch (JaxenException jex) {
        return null;
    }
}
Also used : Dom4jXPath(org.jaxen.dom4j.Dom4jXPath) HashMap(java.util.HashMap) Element(org.dom4j.Element) JaxenException(org.jaxen.JaxenException) SimpleNamespaceContext(org.jaxen.SimpleNamespaceContext)

Aggregations

SimpleNamespaceContext (org.jaxen.SimpleNamespaceContext)7 JaxenException (org.jaxen.JaxenException)5 HashMap (java.util.HashMap)4 Element (org.dom4j.Element)4 Dom4jXPath (org.jaxen.dom4j.Dom4jXPath)4 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 Builder (nu.xom.Builder)1 ParsingException (nu.xom.ParsingException)1 AXIOMXPath (org.apache.axiom.om.xpath.AXIOMXPath)1 Attribute (org.dom4j.Attribute)1 Namespace (org.dom4j.Namespace)1 Node (org.dom4j.Node)1 QName (org.dom4j.QName)1 XPath (org.jaxen.XPath)1 DOMXPath (org.jaxen.dom.DOMXPath)1 XOMXPath (org.jaxen.xom.XOMXPath)1 Element (org.jdom.Element)1 SAXBuilder (org.jdom.input.SAXBuilder)1