Example usage for javax.xml.xpath XPath compile

List of usage examples for javax.xml.xpath XPath compile

Introduction

In this page you can find the example usage for javax.xml.xpath XPath compile.

Prototype

public XPathExpression compile(String expression) throws XPathExpressionException;

Source Link

Document

Compile an XPath expression for later evaluation.

Usage

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.PanelSettings.java

private static void readGeosearchConfiguration(Document xmlConfiguration, XPath xpath)
        throws XPathExpressionException {
    /**************************
     * MASTER/*  w  w w  .j  a  v a  2 s  . c  o  m*/
     **************************/
    Node masterNode = (Node) xpath.compile("/configuration/general/geosearch/master").evaluate(xmlConfiguration,
            XPathConstants.NODE);
    geosearchMaster = readGeosearchNode(masterNode);

    if (geosearchMaster != null) {
        geosearchMaster.setMaster(true);
        // sets files
        GeosearchFilesVO geosearchFilesVO = readGeosearchMasterFiles(xmlConfiguration, xpath);
        geosearchMaster.setFiles(geosearchFilesVO);
    }

    /*************************
     * SLAVES
     *************************/
    geosearchSlaves = new LinkedList<GeosearchInstanceVO>();

    NodeList slavesNodes = (NodeList) xpath.compile("/configuration/general/geosearch/slaves/slave")
            .evaluate(xmlConfiguration, XPathConstants.NODESET);
    for (int i = 0; i < slavesNodes.getLength(); i++) {
        Node slaveNode = slavesNodes.item(i);
        GeosearchInstanceVO slave = readGeosearchNode(slaveNode);
        if (slave != null) {
            slave.setMaster(false);
            geosearchSlaves.add(slave);
        }
    }
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.PanelSettings.java

/**
 * TODO//w w w. ja  v  a  2 s .  co  m
 *
 * @return
 * @throws XPathExpressionException 
 */
private static GeosearchFilesVO readGeosearchMasterFiles(Document xmlConfiguration, XPath xpath)
        throws XPathExpressionException {
    GeosearchFilesVO geosearchFilesVO = new GeosearchFilesVO();

    // DATAIMPORT
    String dataImportName = (String) xpath
            .compile("/configuration/general/geosearch/master/files/dataImport/@name")
            .evaluate(xmlConfiguration, XPathConstants.STRING);
    String dataImportJavaClass = (String) xpath
            .compile("/configuration/general/geosearch/master/files/dataImport/@class")
            .evaluate(xmlConfiguration, XPathConstants.STRING);
    String dataImportFileName = (String) xpath
            .compile("/configuration/general/geosearch/master/files/dataImport/@fileName")
            .evaluate(xmlConfiguration, XPathConstants.STRING);
    String dataImportType = (String) xpath
            .compile("/configuration/general/geosearch/master/files/dataImport/@type")
            .evaluate(xmlConfiguration, XPathConstants.STRING);
    String dataImportDriver = (String) xpath
            .compile("/configuration/general/geosearch/master/files/dataImport/@driver")
            .evaluate(xmlConfiguration, XPathConstants.STRING);
    String dataImportTransformer = (String) xpath
            .compile("/configuration/general/geosearch/master/files/dataImport/@transformer")
            .evaluate(xmlConfiguration, XPathConstants.STRING);

    GeosearchDataImportFileVO dataImportFileVO = new GeosearchDataImportFileVO();
    dataImportFileVO.setName(dataImportName);
    dataImportFileVO.setJavaClass(dataImportJavaClass);
    dataImportFileVO.setFileName(dataImportFileName);
    dataImportFileVO.setType(dataImportType);
    dataImportFileVO.setDriver(dataImportDriver);
    dataImportFileVO.setTransformer(dataImportTransformer);

    // CONFIG  
    String configFileName = (String) xpath
            .compile("/configuration/general/geosearch/master/files/config/@fileName")
            .evaluate(xmlConfiguration, XPathConstants.STRING);
    String searchHandler = (String) xpath
            .compile("/configuration/general/geosearch/master/files/config/@searchHandler")
            .evaluate(xmlConfiguration, XPathConstants.STRING);
    GeosearchConfigFileVO configFileVO = new GeosearchConfigFileVO();
    configFileVO.setFileName(configFileName);
    configFileVO.setSearchHandler(searchHandler);

    // SCHEMA
    String schemaFileName = (String) xpath
            .compile("/configuration/general/geosearch/master/files/schema/@fileName")
            .evaluate(xmlConfiguration, XPathConstants.STRING);
    String defaultSearchField = (String) xpath
            .compile("/configuration/general/geosearch/master/files/schema/@defaultSearchField")
            .evaluate(xmlConfiguration, XPathConstants.STRING);
    GeosearchSchemaFileVO schemaFileVO = new GeosearchSchemaFileVO();
    schemaFileVO.setFileName(schemaFileName);
    schemaFileVO.setDefaultSearchField(defaultSearchField);

    geosearchFilesVO.setDataImport(dataImportFileVO);
    geosearchFilesVO.setConfig(configFileVO);
    geosearchFilesVO.setSchema(schemaFileVO);

    return geosearchFilesVO;
}

From source file:Main.java

public static String getProcessIdFromBpmn(final String bpmn) {
    String processId = null;//from   ww w.  jav a  2s  .  c om
    try {
        final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        final Document doc = domFactory.newDocumentBuilder()
                .parse(new ByteArrayInputStream(bpmn.getBytes("UTF-8")));
        final XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(new NamespaceContext() {
            @Override
            public Iterator<?> getPrefixes(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getPrefix(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getNamespaceURI(final String prefix) {
                // Only require the URI for the bpmn2 NS.
                return BPMN2_NAMESPACE_URI;
            }
        });
        final XPathExpression expr = xpath.compile(BPMN_PROCESS_ID_XPATH_EXPR);

        final Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        processId = node.getAttributes().getNamedItem(BPMN_PROCESS_ID_ATTR).getNodeValue();
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return processId;
}

From source file:Main.java

public static final String[] executeXPathExpression(InputSource inputSource, String xpathExpression,
        String namespace) throws XPathExpressionException, SAXException, IOException,
        ParserConfigurationException, TransformerException {

    // optional namespace spec: xmlns:prefix:URI
    String nsPrefix = null;/*from  ww  w  . ja  va 2  s. com*/
    String nsUri = null;
    if (namespace != null && namespace.startsWith("xmlns:")) {
        String[] nsDef = namespace.substring("xmlns:".length()).split("=");
        if (nsDef.length == 2) {
            nsPrefix = nsDef[0];
            nsUri = nsDef[1];
        }
    }

    // Parse XML to DOM
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    Document doc = dbFactory.newDocumentBuilder().parse(inputSource);

    // Find nodes by XPATH
    XPathFactory xpFactory = XPathFactory.newInstance();
    XPath xpath = xpFactory.newXPath();

    // namespace?
    if (nsPrefix != null) {
        final String myPrefix = nsPrefix;
        final String myUri = nsUri;
        xpath.setNamespaceContext(new NamespaceContext() {
            public String getNamespaceURI(String prefix) {
                return myPrefix.equals(prefix) ? myUri : null;
            }

            public String getPrefix(String namespaceURI) {
                return null; // we are not using this.
            }

            public Iterator<?> getPrefixes(String namespaceURI) {
                return null; // we are not using this.
            }
        });
    }

    XPathExpression expr = xpath.compile(xpathExpression);
    NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

    List<String> lines = new ArrayList<>();

    for (int i = 0; i < nodes.getLength(); i++) {
        lines.add((indenting(nodes.item(i))));
    }

    return lines.toArray(new String[lines.size()]);

}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.PanelSettings.java

private static void readContentTypesConfiguration(Document xmlConfiguration, XPath xpath)
        throws XPathExpressionException {
    csvContentTypes = new HashSet<String>();
    shapeContentTypes = new HashSet<String>();
    compressedContentTypes = new HashSet<String>();

    // CSV/*from  w  w  w .j  ava2s. co  m*/
    NodeList csvContentTypeNodes = (NodeList) xpath.compile("/configuration/files[@type='csv']/content-type")
            .evaluate(xmlConfiguration, XPathConstants.NODESET);
    for (int i = 0; i < csvContentTypeNodes.getLength(); i++) {
        Node csvContentTypeNode = csvContentTypeNodes.item(i);
        String csvContentType = csvContentTypeNode.getTextContent().toLowerCase();
        csvContentTypes.add(csvContentType);
    }

    // SHAPE
    NodeList shapeContentTypeNodes = (NodeList) xpath
            .compile("/configuration/files[@type='shape']/content-type")
            .evaluate(xmlConfiguration, XPathConstants.NODESET);
    for (int i = 0; i < shapeContentTypeNodes.getLength(); i++) {
        Node shapeContentTypeNode = shapeContentTypeNodes.item(i);
        String shapeContentType = shapeContentTypeNode.getTextContent().toLowerCase();
        shapeContentTypes.add(shapeContentType);
    }

    // COMPRESSED
    NodeList compressedContentTypeNodes = (NodeList) xpath
            .compile("/configuration/files[@type='compressed']/content-type")
            .evaluate(xmlConfiguration, XPathConstants.NODESET);
    for (int i = 0; i < compressedContentTypeNodes.getLength(); i++) {
        Node compressedContentTypeNode = compressedContentTypeNodes.item(i);
        String compressedContentType = compressedContentTypeNode.getTextContent().toLowerCase();
        compressedContentTypes.add(compressedContentType);
    }
}

From source file:com.odoko.solrcli.actions.CrawlPostAction.java

/**
 * Gets all nodes matching an XPath//from w ww . j a  v a 2  s.  co m
 */
public static NodeList getNodesFromXP(Node n, String xpath) throws XPathExpressionException {
  XPathFactory factory = XPathFactory.newInstance();
  XPath xp = factory.newXPath();
  XPathExpression expr = xp.compile(xpath);
  return (NodeList) expr.evaluate(n, XPathConstants.NODESET);
}

From source file:edu.virginia.speclab.juxta.author.model.JuxtaXMLParser.java

static public String getIndexBasedXPathForGeneralXPath(String xpathString, String xml) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {/*from www. j  a v  a2  s. c o  m*/
        factory.setNamespaceAware(false); // ignore the horrible issues of namespacing
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xml)));
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        Node root = doc.getFirstChild();
        XPathExpression expr = xpath.compile(xpathString);
        Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        return nodeToSimpleXPath(node, root);
    } catch (SAXException ex) {
    } catch (IOException ex) {
    } catch (XPathExpressionException ex) {
    } catch (ParserConfigurationException ex) {
    }
    return null;
}

From source file:cz.incad.kramerius.virtualcollections.VirtualCollectionsManager.java

public static VirtualCollection doVC(String pid, FedoraAccess fedoraAccess, ArrayList<String> languages) {
    try {//from w w w. j  a  v a 2 s.  c o m
        String xPathStr;
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr;

        ArrayList<String> langs = new ArrayList<String>();
        if (languages == null || languages.isEmpty()) {
            String[] ls = KConfiguration.getInstance().getPropertyList("interface.languages");
            for (int i = 0; i < ls.length; i++) {
                String lang = ls[++i];
                langs.add(lang);
            }
        } else {
            langs = new ArrayList<String>(languages);
        }
        String name = "";
        boolean canLeave = true;
        fedoraAccess.getDC(pid);
        Document doc = fedoraAccess.getDC(pid);
        xPathStr = "//dc:title/text()";
        expr = xpath.compile(xPathStr);
        Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        if (node != null) {
            name = StringEscapeUtils.escapeXml(node.getNodeValue());
        }

        xPathStr = "//dc:type/text()";
        expr = xpath.compile(xPathStr);
        node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        if (node != null) {
            canLeave = Boolean.parseBoolean(StringEscapeUtils.escapeXml(node.getNodeValue()));
        }
        VirtualCollection vc = new VirtualCollection(name, pid, canLeave);

        for (String lang : langs) {
            String dsName = TEXT_DS_PREFIX + lang;
            String value = IOUtils.readAsString(fedoraAccess.getDataStream(pid, dsName),
                    Charset.forName("UTF8"), true);
            vc.addDescription(lang, value);
        }
        return vc;
    } catch (Exception vcex) {
        logger.log(Level.WARNING, "Could not get virtual collection for  " + pid + ": " + vcex.toString());
        return null;
    }
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Find first page pid./*  ww  w . ja v a 2 s.  c o m*/
 * 
 * @param pid
 *        the pid
 * @return the string
 */
public static String findFirstPagePid(String pid) {

    ArrayList<String> pids = new ArrayList<String>();
    try {
        String command = configuration.getFedoraHost() + "/get/" + pid + "/RELS-EXT";
        InputStream is = RESTHelper.get(command, configuration.getFedoraLogin(),
                configuration.getFedoraPassword(), true);
        Document contentDom = XMLUtils.parseDocument(is);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("/RDF/Description/*");
        NodeList nodes = (NodeList) expr.evaluate(contentDom, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childnode = nodes.item(i);
            String nodeName = childnode.getNodeName();
            if (nodeName.contains(FedoraRelationship.hasPage.getStringRepresentation())
                    || nodeName.contains(FedoraRelationship.isOnPage.getStringRepresentation())) {
                return childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("uuid:")[1];
            } else if (!nodeName.contains("hasModel") && childnode.hasAttributes()
                    && childnode.getAttributes().getNamedItem("rdf:resource") != null) {
                pids.add(childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("/")[1]);
            }
        }
        for (String relpid : pids) {
            return FedoraUtils.findFirstPagePid(relpid);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Gets the rdf pids.//from   w ww.  j  a  v  a 2  s  . c om
 * 
 * @param pid
 *        the pid
 * @param relation
 *        the relation
 * @return the rdf pids
 */
public static ArrayList<String> getRdfPids(String pid, String relation) {
    ArrayList<String> pids = new ArrayList<String>();
    try {

        String command = configuration.getFedoraHost() + "/get/" + pid + "/" + RELS_EXT_STREAM;
        InputStream is = RESTHelper.get(command, configuration.getFedoraLogin(),
                configuration.getFedoraPassword(), true);
        Document contentDom = XMLUtils.parseDocument(is);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        String xPathStr = "/RDF/Description/" + relation;
        XPathExpression expr = xpath.compile(xPathStr);
        NodeList nodes = (NodeList) expr.evaluate(contentDom, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childnode = nodes.item(i);
            if (!childnode.getNodeName().contains("hasModel")) {
                pids.add(childnode.getNodeName() + " "
                        + childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("/")[1]);
            }
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return pids;
}