Example usage for org.jdom2.xpath XPathFactory instance

List of usage examples for org.jdom2.xpath XPathFactory instance

Introduction

In this page you can find the example usage for org.jdom2.xpath XPathFactory instance.

Prototype

public static final XPathFactory instance() 

Source Link

Document

Obtain an instance of an XPathFactory using the default mechanisms to determine what XPathFactory implementation to use.

Usage

From source file:Hibernate.Util.HibernateUtil.java

private static org.w3c.dom.Document Configuracion(String dataBaseName, String userName, String password)
        throws FileNotFoundException, JDOMException, IOException {

    Document documentJDOM = new SAXBuilder().build(xmlFileURL);
    XPathExpression<Element> xPathExpression = XPathFactory.instance()
            .compile("/hibernate-configuration/session-factory/property", Filters.element());
    List<Element> elementList = xPathExpression.evaluate(documentJDOM);
    //Esto es relativo a en que posicin aparecen las lineas en el hibernate.cfg.xml
    elementList.get(2).setText(dataBaseName);
    elementList.get(3).setText(userName);
    elementList.get(4).setText(password);
    DOMOutputter domOutputter = new DOMOutputter();
    return domOutputter.output(documentJDOM);
}

From source file:io.macgyver.plugin.ci.jenkins.decorators.GitHubDecorator.java

License:Apache License

@Override
public void call(NodeInfo t1) {
    final XPathExpression<Text> githubProjectXPath = XPathFactory.instance().compile(
            "//com.coravy.hudson.plugins.github.GithubProjectProperty/projectUrl/text()", Filters.text());

    JenkinsScanner c = ((JenkinsScanner) t1.getUserData());

    Document d = c.getServiceClient().getJobConfig(t1.getNode().get("name").asText());

    Optional<String> cloneUrl = extractText(d, "//hudson.plugins.git.UserRemoteConfig/url/text()");

    Optional<String> projectUrl = extractText(d,
            "//com.coravy.hudson.plugins.github.GithubProjectProperty/projectUrl/text()");

    List<String> tmp = Lists.newArrayList();

    ObjectNode params = new ObjectMapper().createObjectNode();
    if (projectUrl.isPresent()) {
        tmp.add("j.githubProjectUrl={projectUrl}");
        params.put("projectUrl", stripTrailingSlash(projectUrl.get()));
    }//ww w .  j  a va2  s. co  m
    if (cloneUrl.isPresent()) {
        tmp.add("j.githubCloneUrl={cloneUrl}");
        params.put("cloneUrl", stripTrailingSlash(cloneUrl.get()));
    }

    if (!tmp.isEmpty()) {

        String clause = Joiner.on(", ").join(tmp);

        String cypher = "match (j:CIJob) where ID(j)={nodeId} SET " + clause + " return j";
        params.put("nodeId", t1.getNodeId());
        t1.getNeoRxClient().execCypher(cypher, params);

    }

    if (projectUrl.isPresent()) {
        String cypher = "match (j:CIJob) where ID(j)={nodeId} MERGE (s:SCMRepo {url:{projectUrl}, type:'github'}) MERGE (j)-[r:BUILDS]->(s)"
                + " ON CREATE set r.createTs=timestamp(), s.createTs=timestamp(), r.updateTs=timestamp(), s.updateTs=timestamp() "
                + " ON MATCH  set r.updateTs=timestamp(), s.updateTs=timestamp()";
        t1.getNeoRxClient().execCypher(cypher, params);
    } else if (cloneUrl.isPresent()) {
        String url = cloneUrl.get();
    }

}

From source file:io.macgyver.plugin.ci.jenkins.decorators.GitHubDecorator.java

License:Apache License

public Optional<String> extractText(Document d, String xpath) {
    XPathExpression<Text> expression = XPathFactory.instance().compile(xpath, Filters.text());
    return getText(expression.evaluate(d));
}

From source file:it.intecs.pisa.openCatalogue.solr.ingester.BaseIngester.java

private FileFilesystem storeMetadata(org.jdom2.Document metadata, boolean isValid) throws IOException {
    if (metadataRepository != null) {
        XPathExpression<Element> xpath = XPathFactory.instance().compile(idXPath, Filters.element());

        String key = xpath.evaluateFirst(metadata.getRootElement()).getTextTrim().replace(":", "_").replace(".",
                "_");
        FileFilesystem fs = null;//from   w ww .  j  a  va  2  s . c o m
        if (isValid) {
            fs = new FileFilesystem(metadataRepository.getAbsolutePath() + "/" + key + ".xml");
        } else {
            fs = new FileFilesystem(metadataRepository.getAbsolutePath() + "/" + key + ".notValid.xml");
        }
        XMLOutputter outputter = new XMLOutputter();
        String metadataString = outputter.outputString(metadata);
        byte[] b = metadataString.getBytes();
        fs.getOutputStream().write(b);
        return fs;
    } else {
        return null;
    }
}

From source file:it.intecs.pisa.openCatalogue.solr.ingester.BaseIngester.java

protected String getItemId(Document doc) {
    XPathExpression<Element> xpath = XPathFactory.instance().compile(idXPath, Filters.element());
    String key = xpath.evaluateFirst(doc.getRootElement()).getTextTrim();
    return key;/*from   w w w .  j  ava2s  . co  m*/
}

From source file:lu.list.itis.dkd.aig.resolution.Template.java

License:Apache License

/**
 * Method used to extract and build all processes from the template.
 *
 * @throws TemplateParseException/*from w  ww  . ja  va 2  s . c  o  m*/
 *         Thrown when either the extraction or the instantiation of a process failed.
 *
 */
@SuppressWarnings("null")
private void extractProcesses(final Document document) throws TemplateParseException {
    List<Element> processElements;

    try {
        final XPathExpression<Element> xpath = XPathFactory.instance().compile(Externalization.PROCESS_XPATH,
                Filters.element());
        processElements = xpath.evaluate(document);
        if ((null == processElements) || (processElements.isEmpty())) {
            // throw new TemplateParseException("No processes could be extracted from the
            // template. Check your node \"processes\" and the subsequent \"process\" nodes for
            // correct nesting and spelling!"); //$NON-NLS-1$
            Template.logger.log(Level.WARNING, "No processes could be extracted from the template."); //$NON-NLS-1$
        }
    } catch (final NullPointerException | IllegalArgumentException | IllegalStateException e) {
        throw new TemplateParseException(
                "No processes could be extracted from the template. Check your node \"processes\" and the subsequent \"process\" nodes for correct nesting and spelling!", //$NON-NLS-1$
                e);
    }

    for (final Element processElement : processElements) {
        final ResolutionProcess process = ResolutionProcessBuilder.buildProcessFrom(processElement);

        if (process instanceof OptimizationProcess) {
            optimizationProcesses.put(process.getIdentifier(), (OptimizationProcess) process);
        } else { // Both are exclusive.
            initializationProcesses.put(process.getIdentifier(), (InitializationProcess) process);
        }
    }
}

From source file:lu.list.itis.dkd.aig.resolution.Template.java

License:Apache License

/**
 * Method used to extract all dependency statements from the template document.
 *
 * @throws TemplateParseException/* w  ww  . j  a  v  a 2s. c o m*/
 *         Thrown whenever the extraction of dependencies failed or when the retrieved
 *         dependency element did not contain all the correct building blocks to instantiate a
 *         new instance of a dependency.
 */
@SuppressWarnings("null")
private void extractDependencies(final Document document) throws TemplateParseException {
    List<Element> dependencyElements;
    try {
        final XPathExpression<Element> xpath = XPathFactory.instance().compile(Externalization.DEPENDENCY_XPATH,
                Filters.element());
        // The new list is required to be able to remove elements.
        dependencyElements = Lists.newArrayList(xpath.evaluate(document));

        if ((null == dependencyElements) || (dependencyElements.isEmpty())) {
            Template.logger.log(Level.WARNING, "No dependencies could be extracted from the template."); //$NON-NLS-1$
        }

        dependencyElements.removeIf(Objects::isNull);
    } catch (final NullPointerException | IllegalArgumentException | IllegalStateException e) {
        throw new TemplateParseException(
                "No dependencies could be extracted from the template. Check your node \"variableDependencies\" and the subsequent \"depencency\" nodes for correct nesting and spelling!", //$NON-NLS-1$
                e);
    }

    for (final Element dependencyElement : dependencyElements) {
        final Dependency dependency = new Dependency(dependencyElement);
        dependencies.add(dependency);
        dependencyByHead.put(dependency.getHead(), dependency);
    }
}

From source file:lu.list.itis.dkd.aig.resolution.Template.java

License:Apache License

/**
 * Helper method used to extract all variable's keys from the template document and register the
 * blueprint to build the variable on demand by its key with the {@link VariableBuilder}.
 *
 * @throws TemplateParseException//from   ww w.  j  a va 2  s  . co  m
 *         Thrown when the provided variable identifier was not a valid URI or if no variables
 *         could be extracted.
 */
@SuppressWarnings("null")
private void extractVariableKeys(final Document document) throws TemplateParseException {
    List<Element> variableElements;
    try {
        final XPathExpression<Element> xpath = XPathFactory.instance().compile(Externalization.VARIABLE_XPATH,
                Filters.element());
        variableElements = xpath.evaluate(document);
        if ((null == variableElements) || (variableElements.isEmpty())) {
            // throw new TemplateParseException("No variables could be extracted from the
            // template. Check your node \"variableDefinitions\" and the subsequent \"variable\"
            // nodes for correct nesting and spelling!"); //$NON-NLS-1$
            Template.logger.log(Level.WARNING, "No variables could be extracted from the template."); //$NON-NLS-1$
        }
    } catch (final NullPointerException | IllegalArgumentException | IllegalStateException e) {
        throw new TemplateParseException(
                "No variables could be extracted from the template. Check your node \"variableDefinitions\" and the subsequent \"variable\" nodes for correct nesting and spelling!"); //$NON-NLS-1$
    }

    for (final Element variableElement : variableElements) {
        variableKeys.add(VariableBuilder.registerBlueprint(variableElement));
    }
}

From source file:lu.list.itis.dkd.aig.resolution.Template.java

License:Apache License

/**
 * Method used to extract the metadata node from the provided document template.
 *
 * @param document//from w w w.j  a  va2s.  c o m
 *        The document to extract the metadata from.
 * @throws TemplateParseException
 *         Thrown when the metadata could not be extracted from the document.
 */
private void populateTemplateMetadataValues(final Document document) throws TemplateParseException {
    Element metadataRoot;
    try {
        final XPathExpression<Element> xpath = XPathFactory.instance()
                .compile(Externalization.TEMPLATE_METADATA_XPATH, Filters.element());
        metadataRoot = xpath.evaluateFirst(document);
    } catch (final NullPointerException | IllegalArgumentException | IllegalStateException e) {
        throw new TemplateParseException(
                "Compiling the XPath expression to resolve the template metadata root node failed!", e); //$NON-NLS-1$
    }

    for (final Element metadataElement : metadataRoot.getChildren()) {
        templateMetadata.put(metadataElement.getName(), metadataElement.getText());
    }
}

From source file:lu.list.itis.dkd.aig.resolution.Template.java

License:Apache License

/**
 * Method used for extracting the QTI item husk from an item template.
 *
 * @throws TemplateParseException/*from   w w  w  .  jav a2  s . c  om*/
 *         TODO
 */
private void extractItem(final Document document) throws TemplateParseException {
    Element qtiRoot;

    try {
        final XPathExpression<Element> xpath = XPathFactory.instance().compile(Externalization.QTI_XPATH,
                Filters.element());
        qtiRoot = xpath.evaluateFirst(document);
    } catch (final NullPointerException | IllegalArgumentException | IllegalStateException e) {
        throw new TemplateParseException("Compiling the Xpath expression to resolve the QTI root node failed!", //$NON-NLS-1$
                e);
    }

    itemLayer = DocumentConverter.convertDocumentToString(new Document(qtiRoot.detach()));
    itemLayer = itemLayer.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""); //$NON-NLS-1$ //$NON-NLS-2$
}