Example usage for org.dom4j XPath selectSingleNode

List of usage examples for org.dom4j XPath selectSingleNode

Introduction

In this page you can find the example usage for org.dom4j XPath selectSingleNode.

Prototype

Node selectSingleNode(Object context);

Source Link

Document

selectSingleNode evaluates this XPath expression on the given Node or List of Node s and returns the result as a single Node instance.

Usage

From source file:com.zimbra.common.soap.SoapTestHarness.java

License:Open Source License

private void doSelect(Element context, Element parent)
        throws SoapFaultException, IOException, HarnessException {

    String path = parent.getAttribute(A_PATH, null);
    String attr = parent.getAttribute(A_ATTR, null);
    String match = parent.getAttribute(A_MATCH, null);

    Element se;//from  w  ww  .  j a  va 2  s.  c  om
    if (path != null) {
        // FIXME: hacky!
        org.dom4j.Element d4context = context.toXML();
        org.dom4j.XPath xpath = d4context.createXPath(path);
        xpath.setNamespaceURIs(getURIs(mResponseProto));
        org.dom4j.Node node = xpath.selectSingleNode(d4context);
        //System.out.println("path = " + path + " node = " + node);
        if (!(node instanceof org.dom4j.Element)) {
            mCurrent.check(false, "select failed: " + path);
            return;
        } else {
            se = Element.convertDOM((org.dom4j.Element) node);
            mCurrent.check(true, "select ok: " + path);
        }
    } else {
        se = context;
    }

    String value;
    if (attr != null) {
        value = se.getAttribute(attr, null);
    } else {
        value = se.getText();
    }

    if (match != null) {
        boolean ok = Pattern.matches(match, value);
        mCurrent.check(ok, "match " + (ok ? "ok" : "failed") + " (" + match + ")" + " (" + value + ")");
    }

    //System.out.println(se.getText());
    String property = parent.getAttribute(A_SET, null);
    if (property != null) {
        //System.out.println(property+" "+value);
        setProperty(property, value);
    }

    for (Element e : parent.listElements()) {
        if (e.getQName().equals(E_SELECT)) {
            doSelect(se, e);
        } else {
            checkGlobals(e);
        }
    }
}

From source file:cz.fi.muni.xkremser.editor.server.fedora.utils.FoxmlUtils.java

License:Open Source License

private static org.dom4j.Element findRelsExtDescriptionElement(org.dom4j.Document relsExt) {
    XPath descriptionXPath = Dom4jUtils.createXPath("/rdf:RDF/rdf:Description");
    return (org.dom4j.Element) descriptionXPath.selectSingleNode(relsExt);
}

From source file:cz.fi.muni.xkremser.editor.server.fedora.utils.FoxmlUtils.java

License:Open Source License

public static org.dom4j.Element findDescriptionElement(org.dom4j.Document relsExt) {
    XPath descriptionXPath = Dom4jUtils.createXPath("//rdf:Description");
    return (org.dom4j.Element) descriptionXPath.selectSingleNode(relsExt);
}

From source file:cz.mzk.editor.server.handler.FindMetadataHandler.java

License:Open Source License

public String findSysno(String barcode) {

    String alephUrl = EditorConfiguration.ServerConstants.Z3950_DEFAULT_HOSTS[z39Client.getProfileIndex()];

    if (alephUrl == null || "".equals(alephUrl))
        return null;

    if (!alephUrl.startsWith("http")) {
        alephUrl = "http://" + alephUrl;
    }//from   w w w .j ava 2 s .  com

    String urlToSetNum = alephUrl + "/X?op=find&code=BAR&request=%s&base=";
    String urlToSysno = alephUrl + "/X?op=present&set_entry=1&set_number=";

    String completeUrlToSetNum = String.format(urlToSetNum, barcode);
    String[] oaiBases = configuration.getOaiBases();
    String sysno = null;

    for (String base : oaiBases) {
        try {
            InputStream inputStream = RESTHelper.get(completeUrlToSetNum + base, null, null, false);
            Document records = Dom4jUtils.loadDocument(inputStream, true);

            XPath xpath = DocumentHelper.createXPath("/find/set_number");
            Node resultNode = xpath.selectSingleNode(records);

            if (resultNode != null) {
                String setNumber = resultNode.getText();
                InputStream sysnoStream = RESTHelper.get(urlToSysno + setNumber, null, null, false);
                Document sysnoDoc = Dom4jUtils.loadDocument(sysnoStream, true);
                xpath = DocumentHelper.createXPath("/present/record/doc_number");
                Node sysnoNode = xpath.selectSingleNode(sysnoDoc);
                if (sysnoNode != null)
                    sysno = sysnoNode.getText();
                break;
            }

        } catch (IOException e) {
            LOGGER.error(e.getMessage());
            e.printStackTrace();
        } catch (DocumentException e) {
            LOGGER.error(e.getMessage());
            e.printStackTrace();
        }

    }

    return sysno;
}

From source file:dkpro.similarity.algorithms.sound.dict.PLS.java

License:Apache License

/**
 *
 * @param dictionaryFilename//from  w w  w . j  av a  2s.  c o m
 *            The filename of the PLS dictionary to read in.
 * @throws DocumentException
 */
@SuppressWarnings("unchecked")
public PLS(String dictionaryFilename) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(dictionaryFilename);
    dict = new MultiValueMap();

    // Get dictionary alphabet and language
    Element lexicon = document.getRootElement();
    alphabetId = lexicon.attributeValue("alphabet");
    dictionaryLanguage = lexicon.attributeValue("lang");

    // Extract dictionary name from metadata
    Map<String, String> namespaceMap = new HashMap<String, String>();
    namespaceMap.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    namespaceMap.put("dc", "http://purl.org/dc/elements/1.1/");
    XPath xpath = document.createXPath("//rdf:Description");
    xpath.setNamespaceURIs(namespaceMap);
    Node node = xpath.selectSingleNode(lexicon);
    if (node != null) {
        dictionaryId = node.valueOf("@dc:title");
    } else {
        dictionaryId = "";
    }

    for (Iterator<Element> lexemeIterator = lexicon.elementIterator("lexeme"); lexemeIterator.hasNext();) {
        Element lexeme = lexemeIterator.next();
        List<Element> graphemes = lexeme.selectNodes("grapheme");
        List<Element> phonemes = lexeme.selectNodes("phoneme");
        for (Element grapheme : graphemes) {
            for (Element phoneme : phonemes) {
                dict.put(grapheme.getText(), phoneme.getText());
            }
        }
    }
}

From source file:eu.scape_project.planning.services.taverna.parser.T2FlowParser.java

License:Apache License

/**
 * Reads the ID annotation of the t2flow.
 * //from ww  w  .j ava  2s . c  om
 * @return the id the workflow adheres to
 * @throws TavernaParserException
 */
public String getId() throws TavernaParserException {

    log.debug("Extracting profile ID");

    XPath xpath = DocumentHelper.createXPath("/t2f:workflow/t2f:dataflow[@role='top']/@id");

    xpath.setNamespaceURIs(T2FLOW_NAMESPACE_MAP);
    Node node = xpath.selectSingleNode(doc);
    if (node == null) {
        return null;
    }
    return node.getText();
}

From source file:eu.scape_project.planning.services.taverna.parser.T2FlowParser.java

License:Apache License

/**
 * Reads the name annotation of the t2flow.
 * /*from   w w  w. j ava2 s.com*/
 * @return the name of the workflow
 * @throws TavernaParserException
 */
public String getName() throws TavernaParserException {

    log.debug("Extracting workflow name");

    XPath xpath = DocumentHelper.createXPath(
            "/t2f:workflow/t2f:dataflow[@role='top']/t2f:annotations/*/*/*/*/annotationBean[@class='net.sf.taverna.t2.annotation.annotationbeans.DescriptiveTitle']/text");

    xpath.setNamespaceURIs(T2FLOW_NAMESPACE_MAP);
    Node node = xpath.selectSingleNode(doc);
    if (node == null) {
        return null;
    }
    return node.getText();
}

From source file:eu.scape_project.planning.services.taverna.parser.T2FlowParser.java

License:Apache License

/**
 * Reads the description annotation of the t2flow.
 * //  w w  w.  ja v a 2 s.c om
 * @return the description of the workflow
 * @throws TavernaParserException
 */
public String getDescription() throws TavernaParserException {

    log.debug("Extracting workflow description");

    XPath xpath = DocumentHelper.createXPath(
            "/t2f:workflow/t2f:dataflow[@role='top']/t2f:annotations/*/*/*/*/annotationBean[@class='net.sf.taverna.t2.annotation.annotationbeans.FreeTextDescription']/text");

    xpath.setNamespaceURIs(T2FLOW_NAMESPACE_MAP);
    Node node = xpath.selectSingleNode(doc);
    if (node == null) {
        return null;
    }
    return node.getText();
}

From source file:eu.scape_project.planning.services.taverna.parser.T2FlowParser.java

License:Apache License

/**
 * Reads the author annotation of the t2flow.
 * /*from   w ww  .  j a  v  a 2 s. co m*/
 * @return the author of the workflow
 * @throws TavernaParserException
 */
public String getAuthor() throws TavernaParserException {

    log.debug("Extracting workflow author");

    XPath xpath = DocumentHelper.createXPath(
            "/t2f:workflow/t2f:dataflow[@role='top']/t2f:annotations/*/*/*/*/annotationBean[@class='net.sf.taverna.t2.annotation.annotationbeans.Author']/text");

    xpath.setNamespaceURIs(T2FLOW_NAMESPACE_MAP);
    Node node = xpath.selectSingleNode(doc);
    if (node == null) {
        return null;
    }
    return node.getText();
}

From source file:gov.abrs.etms.tag.ProcessImageTag.java

License:Open Source License

private int[] extractBoxConstraint(Element root) {
    int[] result = new int[4];
    try {//from   w  ww. j a  v  a2 s . co  m
        String nodeName = currentToken.getNode().getName();
        XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
        Element node = (Element) xPath.selectSingleNode(root);
        result[0] = Integer.valueOf(node.attribute("x").getValue()).intValue();
        result[1] = Integer.valueOf(node.attribute("y").getValue()).intValue();
        result[2] = Integer.valueOf(node.attribute("width").getValue()).intValue();
        result[3] = Integer.valueOf(node.attribute("height").getValue()).intValue();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}