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:com.medvision360.medrecord.basex.AbstractXmlConverter.java

License:Creative Commons License

protected <T> String xpath(org.jdom2.Document d, String p, Filter<T> filter) {
    XPathExpression<T> xpath = XPathFactory.instance().compile(p, filter);
    T value = xpath.evaluateFirst(d);/*  www. j  a v a  2s.  c o m*/
    if (value == null) {
        return null;
    }
    if (value instanceof Element) {
        return ((Element) value).getValue();
    }
    if (value instanceof Attribute) {
        return ((Attribute) value).getValue();
    }
    return value.toString();
}

From source file:com.medvision360.medrecord.basex.MockLocatableParser.java

License:Creative Commons License

private <T> String xpath(Document d, String p, Filter<T> filter) {
    XPathExpression<T> xpath = XPathFactory.instance().compile(p, filter);
    T value = xpath.evaluateFirst(d);//from w w w .j  av a 2s. co  m
    if (value == null) {
        return null;
    }
    if (value instanceof Element) {
        return ((Element) value).getValue();
    }
    if (value instanceof Attribute) {
        return ((Attribute) value).getValue();
    }
    return value.toString();
}

From source file:com.mycompany.hr.ws.HolidayEndpoint.java

License:Apache License

@Autowired
public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException {
    this.humanResourceService = humanResourceService;
    Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
    XPathFactory xPathFactory = XPathFactory.instance();
    startDateExpression = xPathFactory.compile("//hr:StartDate", Filters.element(), null, namespace);
    endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), null, namespace);
    firstNameExpression = xPathFactory.compile("//hr:FirstName", Filters.element(), null, namespace);
    lastNameExpression = xPathFactory.compile("//hr:LastName", Filters.element(), null, namespace);
}

From source file:com.ohnosequences.xml.model.uniprot.ProteinXML.java

License:Open Source License

public void clasifyGoTermsByAspect() throws JDOMException {
    initComponentTag();/*w ww  .ja  v  a2  s  .co  m*/
    initFunctionTag();
    initProcessTag();

    if (doc == null) {
        doc = root.getDocument();
    }

    XPathExpression<Element> xpProcess = XPathFactory.instance()
            .compile(
                    "//protein[id/text()='" + getId() + "']//" + GoTermXML.TAG_NAME + "["
                            + GoTermXML.ASPECT_TAG_NAME + "/text()='" + GoTermXML.ASPECT_PROCESS + "']",
                    Filters.element());
    List<Element> processGoTerms = xpProcess.evaluate(doc);

    XPathExpression<Element> xpFunction = XPathFactory.instance()
            .compile(
                    "//protein[id/text()='" + getId() + "']//" + GoTermXML.TAG_NAME + "["
                            + GoTermXML.ASPECT_TAG_NAME + "/text()='" + GoTermXML.ASPECT_FUNCTION + "']",
                    Filters.element());
    List<Element> functionGoTerms = xpFunction.evaluate(doc);

    XPathExpression<Element> xpComponent = XPathFactory.instance()
            .compile(
                    "//protein[id/text()='" + getId() + "']//" + GoTermXML.TAG_NAME + "["
                            + GoTermXML.ASPECT_TAG_NAME + "/text()='" + GoTermXML.ASPECT_COMPONENT + "']",
                    Filters.element());
    List<Element> componentGoTerms = xpComponent.evaluate(doc);

    for (Element processGo : processGoTerms) {
        processGo.detach();
        this.addGoTerm(new GoTermXML(processGo), true);
    }
    for (Element componentGo : componentGoTerms) {
        componentGo.detach();
        this.addGoTerm(new GoTermXML(componentGo), true);
    }
    for (Element functionGo : functionGoTerms) {
        functionGo.detach();
        this.addGoTerm(new GoTermXML(functionGo), true);
    }

}

From source file:com.thoughtworks.go.config.ConfigCipherUpdater.java

License:Apache License

public void migrate() {
    File cipherFile = systemEnvironment.getDESCipherFile();
    String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(timeProvider.currentTime());

    File backupCipherFile = new File(systemEnvironment.getConfigDir(), "cipher.original." + timestamp);
    File configFile = new File(systemEnvironment.getCruiseConfigFile());
    File backupConfigFile = new File(configFile.getParentFile(),
            configFile.getName() + ".original." + timestamp);
    try {/*from   ww w  . j a  v  a2  s. co m*/
        if (!cipherFile.exists() || !FileUtils.readFileToString(cipherFile, UTF_8).equals(FLAWED_VALUE)) {
            return;
        }
        LOGGER.info("Found unsafe cipher {} on server, Go will make an attempt to rekey", FLAWED_VALUE);
        FileUtils.copyFile(cipherFile, backupCipherFile);
        LOGGER.info("Old cipher was successfully backed up to {}", backupCipherFile.getAbsoluteFile());
        FileUtils.copyFile(configFile, backupConfigFile);
        LOGGER.info("Old config was successfully backed up to {}", backupConfigFile.getAbsoluteFile());

        String oldCipher = FileUtils.readFileToString(backupCipherFile, UTF_8);
        new DESCipherProvider(systemEnvironment).resetCipher();

        String newCipher = FileUtils.readFileToString(cipherFile, UTF_8);

        if (newCipher.equals(oldCipher)) {
            LOGGER.warn("Unable to generate a new safe cipher. Your cipher is unsafe.");
            FileUtils.deleteQuietly(backupCipherFile);
            FileUtils.deleteQuietly(backupConfigFile);
            return;
        }
        Document document = new SAXBuilder().build(configFile);
        List<String> encryptedAttributes = Arrays.asList("encryptedPassword", "encryptedManagerPassword");
        List<String> encryptedNodes = Arrays.asList("encryptedValue");
        XPathFactory xPathFactory = XPathFactory.instance();
        for (String attributeName : encryptedAttributes) {
            XPathExpression<Element> xpathExpression = xPathFactory
                    .compile(String.format("//*[@%s]", attributeName), Filters.element());
            List<Element> encryptedPasswordElements = xpathExpression.evaluate(document);
            for (Element element : encryptedPasswordElements) {
                Attribute encryptedPassword = element.getAttribute(attributeName);
                encryptedPassword.setValue(reEncryptUsingNewKey(decodeHex(oldCipher), decodeHex(newCipher),
                        encryptedPassword.getValue()));
                LOGGER.debug("Replaced encrypted value at {}", element.toString());
            }
        }
        for (String nodeName : encryptedNodes) {
            XPathExpression<Element> xpathExpression = xPathFactory.compile(String.format("//%s", nodeName),
                    Filters.element());
            List<Element> encryptedNode = xpathExpression.evaluate(document);
            for (Element element : encryptedNode) {
                element.setText(
                        reEncryptUsingNewKey(decodeHex(oldCipher), decodeHex(newCipher), element.getValue()));
                LOGGER.debug("Replaced encrypted value at {}", element.toString());
            }
        }
        try (FileOutputStream fileOutputStream = new FileOutputStream(configFile)) {
            XmlUtils.writeXml(document, fileOutputStream);
        }
        LOGGER.info("Successfully re-encrypted config");
    } catch (Exception e) {
        LOGGER.error("Re-keying of cipher failed with error: [{}]", e.getMessage(), e);
        if (backupCipherFile.exists()) {
            try {
                FileUtils.copyFile(backupCipherFile, cipherFile);
            } catch (IOException e1) {
                LOGGER.error(
                        "Could not replace the cipher file [{}] with original one [{}], please do so manually. Error: [{}]",
                        cipherFile.getAbsolutePath(), backupCipherFile.getAbsolutePath(), e.getMessage(), e);
                bomb(e1);
            }
        }
    }
}

From source file:com.thoughtworks.go.config.validation.UniqueOnCancelValidator.java

License:Apache License

public void validate(Element element, ConfigElementImplementationRegistry registry) throws Exception {
    XPathFactory xPathFactory = XPathFactory.instance();
    List<String> tasks = ConfigUtil.allTasks(registry);
    for (String task : tasks) {
        List<Element> taskNodes = xPathFactory.compile("//" + task, Filters.element()).evaluate(element);
        for (Element taskNode : taskNodes) {
            List<Element> list = xPathFactory.compile("oncancel", Filters.element()).evaluate(taskNode);
            if (list.size() > 1) {
                throw new Exception("Task [" + task + "] should not contain more than 1 oncancel task");
            }//from w  w  w . j a  v a2 s. com
        }
    }
}

From source file:com.xebialabs.overcast.support.libvirt.jdom.DiskXml.java

License:Apache License

/**
 * update the disks in the domain XML. It is assumed that the the size of the volumes is the same as the number of
 * disk elements and that the order is the same.
 *//*from   ww w .  ja  v  a2  s .  c om*/
public static void updateDisks(Document domainXml, List<StorageVol> volumes) throws LibvirtException {
    XPathFactory xpf = XPathFactory.instance();
    XPathExpression<Element> diskExpr = xpf.compile(XPATH_DISK, Filters.element());
    XPathExpression<Attribute> fileExpr = xpf.compile(XPATH_DISK_FILE, Filters.attribute());
    List<Element> disks = diskExpr.evaluate(domainXml);
    Iterator<StorageVol> cloneDiskIter = volumes.iterator();
    for (Element disk : disks) {
        Attribute file = fileExpr.evaluateFirst(disk);
        StorageVol cloneDisk = cloneDiskIter.next();
        file.setValue(cloneDisk.getPath());
    }
}

From source file:com.xebialabs.overcast.support.libvirt.jdom.DiskXml.java

License:Apache License

/** Get the disks connected to the domain. */
public static List<Disk> getDisks(Connect connect, Document domainXml) {
    try {//from   www.j  a va  2  s  .  com
        List<Disk> ret = Lists.newArrayList();
        XPathFactory xpf = XPathFactory.instance();
        XPathExpression<Element> diskExpr = xpf.compile(XPATH_DISK, Filters.element());
        XPathExpression<Attribute> typeExpr = xpf.compile(XPATH_DISK_TYPE, Filters.attribute());
        XPathExpression<Attribute> fileExpr = xpf.compile(XPATH_DISK_FILE, Filters.attribute());
        XPathExpression<Attribute> devExpr = xpf.compile(XPATH_DISK_DEV, Filters.attribute());
        List<Element> disks = diskExpr.evaluate(domainXml);
        for (Element disk : disks) {
            Attribute type = typeExpr.evaluateFirst(disk);
            Attribute file = fileExpr.evaluateFirst(disk);
            Attribute dev = devExpr.evaluateFirst(disk);

            StorageVol volume = LibvirtUtil.findVolume(connect, file.getValue());

            ret.add(new Disk(dev.getValue(), file.getValue(), volume, type.getValue()));
        }
        return ret;
    } catch (LibvirtException e) {
        throw new LibvirtRuntimeException(e);
    }
}

From source file:com.xebialabs.overcast.support.libvirt.jdom.DomainXml.java

License:Apache License

public static Document setDomainName(Document domainXml, String name) {
    XPathFactory xpf = XPathFactory.instance();

    XPathExpression<Element> nameExpr = xpf.compile("/domain/name", Filters.element());
    Element nameElement = nameExpr.evaluateFirst(domainXml);
    nameElement.setText(name);/*from w w w .j  a  va  2 s.  co m*/

    return domainXml;
}

From source file:com.xebialabs.overcast.support.libvirt.jdom.DomainXml.java

License:Apache License

/** remove elements that need to be unique per clone. */
public static Document prepareForCloning(Document domainXml) {
    XPathFactory xpf = XPathFactory.instance();

    // remove uuid so it will be generated
    domainXml.getRootElement().removeChild("uuid");

    // remove mac address, so it will be generated
    XPathExpression<Element> macExpr = xpf.compile("/domain/devices/interface/mac", Filters.element());
    for (Element mac : macExpr.evaluate(domainXml)) {
        mac.getParentElement().removeChild("mac");
    }/*from ww  w.  j  a v a  2 s  . co  m*/
    return domainXml;
}