Search in sources :

Example 1 with EolDetectingInputStream

use of org.jfrog.build.extractor.EolDetectingInputStream in project build-info by JFrogDev.

the class PomTransformer method transform.

/**
 * Performs the transformation.
 *
 * @return True if the file was modified.
 */
public Boolean transform(File pomFile) throws IOException {
    this.pomFile = pomFile;
    if (!pomFile.exists()) {
        throw new IllegalArgumentException("Couldn't find pom file: " + pomFile);
    }
    SAXBuilder saxBuilder = createSaxBuilder();
    Document document;
    EolDetectingInputStream eolDetectingStream = null;
    InputStreamReader inputStreamReader = null;
    try {
        eolDetectingStream = new EolDetectingInputStream(new FileInputStream(pomFile));
        inputStreamReader = new InputStreamReader(eolDetectingStream, "UTF-8");
        document = saxBuilder.build(inputStreamReader);
    } catch (JDOMException e) {
        throw new IOException("Failed to parse pom: " + pomFile.getAbsolutePath(), e);
    } finally {
        IOUtils.closeQuietly(inputStreamReader);
        IOUtils.closeQuietly(eolDetectingStream);
    }
    Element rootElement = document.getRootElement();
    Namespace ns = rootElement.getNamespace();
    getProperties(rootElement, ns);
    changeParentVersion(rootElement, ns);
    changeCurrentModuleVersion(rootElement, ns);
    // changePropertiesVersion(rootElement, ns);
    changeDependencyManagementVersions(rootElement, ns);
    changeDependencyVersions(rootElement, ns);
    if (scmUrl != null) {
        changeScm(rootElement, ns);
    }
    if (modified && !dryRun) {
        FileOutputStream fileOutputStream = new FileOutputStream(pomFile);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
        try {
            XMLOutputter outputter = new XMLOutputter();
            String eol = eolDetectingStream.getEol();
            if (!"".equals(eol)) {
                Format format = outputter.getFormat();
                format.setLineSeparator(eol);
                format.setTextMode(Format.TextMode.PRESERVE);
                outputter.setFormat(format);
            }
            outputter.output(document, outputStreamWriter);
        } finally {
            IOUtils.closeQuietly(outputStreamWriter);
            IOUtils.closeQuietly(fileOutputStream);
        }
    }
    return modified;
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) Namespace(org.jdom.Namespace) Format(org.jdom.output.Format) EolDetectingInputStream(org.jfrog.build.extractor.EolDetectingInputStream)

Example 2 with EolDetectingInputStream

use of org.jfrog.build.extractor.EolDetectingInputStream in project build-info by JFrogDev.

the class PropertiesTransformer method transform.

/**
 * {@inheritDoc}
 *
 * @return True in case the properties file was modified during the transformation. False otherwise.
 */
public Boolean transform() throws IOException, InterruptedException {
    if (!propertiesFile.exists()) {
        throw new IllegalArgumentException("Couldn't find properties file: " + propertiesFile.getAbsolutePath());
    }
    Properties properties = new Properties();
    EolDetectingInputStream eolDetectingInputStream = null;
    try {
        eolDetectingInputStream = new EolDetectingInputStream(new FileInputStream(propertiesFile));
        properties.load(eolDetectingInputStream);
    } finally {
        IOUtils.closeQuietly(eolDetectingInputStream);
    }
    String eol = eolDetectingInputStream.getEol();
    boolean hasEol = !"".equals(eol);
    StringBuilder resultBuilder = new StringBuilder();
    boolean modified = false;
    Enumeration<?> propertyNames = properties.propertyNames();
    while (propertyNames.hasMoreElements()) {
        String propertyName = (String) propertyNames.nextElement();
        String propertyValue = properties.getProperty(propertyName);
        StringBuilder lineBuilder = new StringBuilder(propertyName).append("=");
        String newPropertyValue = versionsByName.get(propertyName);
        if ((newPropertyValue != null) && !newPropertyValue.equals(propertyValue)) {
            if (!modified) {
                modified = true;
            }
            lineBuilder.append(newPropertyValue);
        } else {
            lineBuilder.append(propertyValue);
        }
        resultBuilder.append(lineBuilder.toString());
        if (hasEol) {
            resultBuilder.append(eol);
        }
    }
    if (modified) {
        propertiesFile.delete();
        String toWrite = resultBuilder.toString();
        Files.write(toWrite, propertiesFile, Charsets.UTF_8);
    }
    return modified;
}
Also used : EolDetectingInputStream(org.jfrog.build.extractor.EolDetectingInputStream) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Aggregations

EolDetectingInputStream (org.jfrog.build.extractor.EolDetectingInputStream)2 FileInputStream (java.io.FileInputStream)1 Properties (java.util.Properties)1 Document (org.jdom.Document)1 Element (org.jdom.Element)1 JDOMException (org.jdom.JDOMException)1 Namespace (org.jdom.Namespace)1 SAXBuilder (org.jdom.input.SAXBuilder)1 Format (org.jdom.output.Format)1 XMLOutputter (org.jdom.output.XMLOutputter)1