Example usage for org.jdom2.xpath XPathFactory compile

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

Introduction

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

Prototype

public <T> XPathExpression<T> compile(String expression, Filter<T> filter) 

Source Link

Document

Create a XPathExpression<T> instance from this factory.

Usage

From source file:AIR.Common.xml.XmlElement.java

License:Open Source License

public List<Element> selectNodes(String xpath) {
    XPathFactory factory = XPathFactory.instance();
    XPathExpression<Element> expr = factory.compile(xpath, Filters.element());
    return expr.evaluate(_element);
}

From source file:AIR.Common.xml.XmlElement.java

License:Open Source License

public Element selectSingleNode(String xpath) {
    XPathFactory factory = XPathFactory.instance();
    XPathExpression<Element> expr = factory.compile(xpath, Filters.element());
    return expr.evaluateFirst(_element);
}

From source file:com.googlesource.gerrit.plugins.manifest.ManifestUpdater.java

License:Apache License

public List<Project> getProjects() {
    Document doc = manifestXml.getDocument();
    XPathFactory xFactory = XPathFactory.instance();
    String xPathStr = "/manifest/project";
    XPathExpression<Element> prjXPathExpr = xFactory.compile(xPathStr, Filters.element());

    List<Project> projects = new ArrayList<>();
    for (Element element : prjXPathExpr.evaluate(doc)) {
        projects.add(new Project(element));
    }//from  w  ww. ja  v a2  s  .c  om
    return projects;
}

From source file:com.kingmed.dp.ndp.impl.NDPServeResponseHandler.java

protected List<Element> checkStatus(String responseBody, String expression) throws JDOMException, IOException {
    List<Element> items = null;
    Reader reader = new StringReader(responseBody);
    SAXBuilder builder = new SAXBuilder();
    Document jdomDoc = null;//from w  w  w.  j  a v  a 2  s  .  c  o  m
    jdomDoc = builder.build(reader);
    XPathFactory xFactory = XPathFactory.instance();
    XPathExpression<Element> expr = xFactory.compile(expression, Filters.element());
    items = expr.evaluate(jdomDoc);
    return items;
}

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 w  w  w . j  a v a  2 s. c  o  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");
            }/*w  ww  . ja v  a  2s  . co  m*/
        }
    }
}

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.
 *///  ww w .  j  a  va2 s. co  m
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  w  w w. j  a va2s.co m*/
        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   www  .j a v a2 s .c o 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 . ja  v  a2 s .c o  m
    return domainXml;
}