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;
}
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;
}
Aggregations