Skip to content

Commit

Permalink
Better indentation when adding GCP libraries to pom.xml GoogleCloudPl…
Browse files Browse the repository at this point in the history
…atform#3381

Change-Id: I5d4a22b089f1516845e8597cf11c5198772dc4dd
  • Loading branch information
lak-proddev committed Jun 27, 2019
1 parent f8c6895 commit 5d028af
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void updateDependencies(Collection<Library> selectedLibraries,
dependency.appendChild(artifactIdElement);

if (testComment == null) {
dependencies.appendChild(dependency);
addChild(dependencies, dependency);
} else {
dependencies.insertBefore(dependency, testComment);
}
Expand Down Expand Up @@ -279,7 +279,7 @@ private void handleDependencyManaged(LibraryFile artifact, Element dependency) {
}
} else {
if (versionNode != null) {
dependency.removeChild(versionNode);
removeChild(dependency, versionNode);
}
}
}
Expand All @@ -305,8 +305,8 @@ private void createBOMIfNeeded(XPath xpath) throws CoreException {
dependencyManagement = document.createElementNS("http://maven.apache.org/POM/4.0.0",
"dependencyManagement");
}
dependencyManagement.appendChild(dependencies);
document.getDocumentElement().appendChild(dependencyManagement);
addChild(dependencyManagement, dependencies);
addChild(document.getDocumentElement(), dependencyManagement);
}

Element dependency =
Expand All @@ -321,7 +321,7 @@ private void createBOMIfNeeded(XPath xpath) throws CoreException {
if (bom != null) {
boms.add(bom);
}
dependencies.appendChild(dependency);
addChild(dependencies, dependency);
}
} catch (XPathExpressionException ex) {
IStatus status = StatusUtil.error(Pom.class, ex.getMessage(), ex);
Expand Down Expand Up @@ -444,7 +444,7 @@ static void removeUnusedDependencies(Element dependencies,
}

for (Node node : nodesToRemove) {
dependencies.removeChild(node);
removeChild(dependencies, node);
}
}

Expand Down Expand Up @@ -482,10 +482,29 @@ private static String getValue(Element dependency, String childName) {
private void writeDocument() throws CoreException, TransformerException {
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2"); //$NON-NLS-1$ //$NON-NLS-2$
ByteArrayOutputStream out = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(out));
InputStream in = new ByteArrayInputStream(out.toByteArray());

pomFile.setContents(in, true, true, null);
}

private void addChild(Node parent, Node newChild) {
parent.appendChild(newChild);
removeWhiteSpaceBefore(newChild);// for child indentation
}

private static void removeChild(Node parent, Node oldChild) {
removeWhiteSpaceBefore(oldChild);
parent.removeChild(oldChild);
}

private static void removeWhiteSpaceBefore(Node node) {
Node prevElement = node.getPreviousSibling();
if (prevElement != null && prevElement.getNodeType() == Node.TEXT_NODE
&& prevElement.getNodeValue().trim().length() == 0) {
node.getParentNode().removeChild(prevElement);
}
}
}

0 comments on commit 5d028af

Please sign in to comment.