Example usage for org.jdom2.input SAXBuilder SAXBuilder

List of usage examples for org.jdom2.input SAXBuilder SAXBuilder

Introduction

In this page you can find the example usage for org.jdom2.input SAXBuilder SAXBuilder.

Prototype

public SAXBuilder() 

Source Link

Document

Creates a new JAXP-based SAXBuilder.

Usage

From source file:com.tactfactory.harmony.utils.XMLUtils.java

License:Open Source License

/**
 * Open an XML file.//from   w w  w . java  2  s  . co  m
 * @param fileName The name of the file.
 * @return The openened Document object. Or null if nothing found.
 */
public static Document openXML(final String fileName) {
    Document doc = null;

    try {
        // Make engine
        final SAXBuilder builder = new SAXBuilder();
        final File xmlFile = new File(fileName);

        if (!xmlFile.exists()) {
            doc = new Document();
        } else {
            // Load XML File
            doc = builder.build(xmlFile);
        }

    } catch (JDOMException e) {
        ConsoleUtils.displayError(e);
    } catch (IOException e) {
        ConsoleUtils.displayError(e);
    }

    return doc;
}

From source file:com.tactfactory.harmony.utils.XMLUtils.java

License:Open Source License

/**
 * Open a remote XML file./*from  w  w  w .ja va  2  s .c  o m*/
 * @param url The url of the xml file.
 * @return The Element corresponding to the XML.
 */
public static Document getRemoteXML(final String url) {
    Document result = null;
    try {
        SAXBuilder builder = new SAXBuilder();
        result = builder.build(new URL(url));
    } catch (MalformedURLException e) {
        ConsoleUtils.displayError(e);
    } catch (JDOMException e) {
        ConsoleUtils.displayError(e);
    } catch (IOException e) {
        ConsoleUtils.displayError(e);
    }

    return result;
}

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 va2  s .  c om*/
        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.GoConfigMigration.java

License:Apache License

private int getCurrentSchemaVersion(String content) {
    try {//w w w .  java  2s  .  c  o  m
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(new ByteArrayInputStream(content.getBytes()));
        Element root = document.getRootElement();

        String currentVersion = root.getAttributeValue(schemaVersion) == null ? "0"
                : root.getAttributeValue(schemaVersion);
        return Integer.parseInt(currentVersion);
    } catch (Exception e) {
        throw bomb(e);
    }
}

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

License:Apache License

public <T> T fromXmlPartial(InputStream inputStream, Class<T> o) throws Exception {
    Document document = new SAXBuilder().build(inputStream);
    Element element = document.getRootElement();
    return classParser(element, o, configCache, new GoCipher(), registry, new ConfigReferenceElements())
            .parse();/*w  ww .  jav  a 2s. com*/
}

From source file:com.thoughtworks.go.domain.materials.mercurial.HgModificationSplitter.java

License:Apache License

public List<Modification> modifications() {
    try {//from  w  w w . ja  va 2  s . co  m
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(new StringReader(output));
        return parseDOMTree(document);
    } catch (Exception e) {
        throw ExceptionUtils.bomb("Unable to parse hg log output: " + result.replaceSecretInfo(output),
                result.smudgedException(e));
    }
}

From source file:com.thoughtworks.go.domain.materials.svn.SvnCommand.java

License:Apache License

public List<SvnExternal> getAllExternalURLs() {
    CommandLine svnExternalCommand = svn(true).withArgs("propget", "--non-interactive", "svn:externals", "-R")
            .withArg(repositoryUrl);/*from  w  ww  . j  a  va 2  s . c  om*/
    ConsoleResult result = executeCommand(svnExternalCommand);
    String svnExternalConsoleOut = result.outputAsString();
    SvnInfo remoteInfo = remoteInfo(new SAXBuilder());
    String repoUrl = remoteInfo.getUrl();
    String repoRoot = remoteInfo.getRoot();
    List<SvnExternal> svnExternalList = null;
    try {
        svnExternalList = new SvnExternalParser().parse(svnExternalConsoleOut, repoUrl, repoRoot);
    } catch (RuntimeException e) {
        throw (RuntimeException) result.smudgedException(e);
    }
    return svnExternalList;
}

From source file:com.thoughtworks.go.domain.materials.svn.SvnCommand.java

License:Apache License

private SAXBuilder getBuilder() {
    SAXBuilder saxBuilder = saxBuilderThreadLocal.get();
    if (saxBuilder == null) {
        saxBuilder = new SAXBuilder();
        saxBuilderThreadLocal.set(saxBuilder);
    }//www .j a  v  a  2 s  .  c  o  m
    return saxBuilder;
}

From source file:com.thoughtworks.xstream.io.xml.JDom2Driver.java

License:Open Source License

public HierarchicalStreamReader createReader(Reader reader) {
    try {//  w w w  . ja va2s . c  o m
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(reader);
        return new JDom2Reader(document, getNameCoder());
    } catch (IOException e) {
        throw new StreamException(e);
    } catch (JDOMException e) {
        throw new StreamException(e);
    }
}

From source file:com.thoughtworks.xstream.io.xml.JDom2Driver.java

License:Open Source License

public HierarchicalStreamReader createReader(InputStream in) {
    try {//from w  w w.  j  ava2 s. c  om
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(in);
        return new JDom2Reader(document, getNameCoder());
    } catch (IOException e) {
        throw new StreamException(e);
    } catch (JDOMException e) {
        throw new StreamException(e);
    }
}