Skip to content

Fix [MDEP-931] Replace PrintWriter with Writer in AbstractSerializing Visitor and subclasses #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ private void createMessage(
Set<String> duplicateDependencies, StringBuilder sb, String messageDuplicateDepInDependencies) {
if (!duplicateDependencies.isEmpty()) {
if (sb.length() > 0) {
sb.append("\n");
sb.append(System.lineSeparator());
}
sb.append(messageDuplicateDepInDependencies);
for (Iterator<String> it = duplicateDependencies.iterator(); it.hasNext(); ) {
String dup = it.next();

sb.append("\to ").append(dup);
if (it.hasNext()) {
sb.append("\n");
sb.append(System.lineSeparator());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.maven.plugins.dependency.tree;

import java.io.PrintWriter;
import java.io.Writer;

/**
Expand All @@ -31,7 +30,7 @@ public abstract class AbstractSerializingVisitor {
/**
* The writer to serialize to.
*/
protected final PrintWriter writer;
protected final Writer writer;

/**
* Constructor.
Expand All @@ -42,10 +41,6 @@ public abstract class AbstractSerializingVisitor {
* @param writer the writer to serialize to.
*/
public AbstractSerializingVisitor(Writer writer) {
if (writer instanceof PrintWriter) {
this.writer = (PrintWriter) writer;
} else {
this.writer = new PrintWriter(writer, true);
}
this.writer = writer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.maven.plugins.dependency.tree;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.util.List;

Expand Down Expand Up @@ -47,16 +49,23 @@ public DOTDependencyNodeVisitor(Writer writer) {
*/
@Override
public boolean visit(DependencyNode node) {
if (node.getParent() == null || node.getParent() == node) {
writer.write("digraph \"" + node.toNodeString() + "\" { " + System.lineSeparator());
}
try {
if (node.getParent() == null || node.getParent() == node) {
writer.write("digraph \"" + node.toNodeString() + "\" { " + System.lineSeparator());
writer.flush();
}

// Generate "currentNode -> Child" lines
// Generate "currentNode -> Child" lines

List<DependencyNode> children = node.getChildren();
List<DependencyNode> children = node.getChildren();

for (DependencyNode child : children) {
writer.println("\t\"" + node.toNodeString() + "\" -> \"" + child.toNodeString() + "\" ; ");
for (DependencyNode child : children) {
writer.write("\t\"" + node.toNodeString() + "\" -> \"" + child.toNodeString() + "\" ; "
+ System.lineSeparator());
}
writer.flush();
} catch (IOException e) {
throw new UncheckedIOException("Failed to write DOT format output", e);
}

return true;
Expand All @@ -67,8 +76,13 @@ public boolean visit(DependencyNode node) {
*/
@Override
public boolean endVisit(DependencyNode node) {
if (node.getParent() == null || node.getParent() == node) {
writer.write(" } ");
try {
if (node.getParent() == null || node.getParent() == node) {
writer.write(" } " + System.lineSeparator());
writer.flush();
}
} catch (IOException e) {
throw new UncheckedIOException("Failed to write DOT format output", e);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.maven.plugins.dependency.tree;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;

import org.apache.maven.shared.dependency.graph.DependencyNode;
Expand Down Expand Up @@ -64,36 +66,47 @@ public GraphmlDependencyNodeVisitor(Writer writer) {
*/
@Override
public boolean endVisit(DependencyNode node) {
if (node.getParent() == null || node.getParent() == node) {
writer.write(GRAPHML_FOOTER);
} else {
DependencyNode p = node.getParent();
writer.print("<edge source=\"" + generateId(p) + "\" target=\"" + generateId(node) + "\">");
if (node.getArtifact().getScope() != null) {
// add Edge label
writer.print("<data key=\"d1\"><y:PolyLineEdge><y:EdgeLabel>"
+ node.getArtifact().getScope() + "</y:EdgeLabel></y:PolyLineEdge></data>");
try {
if (node.getParent() == null || node.getParent() == node) {
writer.write(GRAPHML_FOOTER);
} else {
DependencyNode p = node.getParent();
writer.write("<edge source=\"" + generateId(p) + "\" target=\"" + generateId(node) + "\">");
if (node.getArtifact().getScope() != null) {
// add Edge label
writer.write("<data key=\"d1\"><y:PolyLineEdge><y:EdgeLabel>"
+ node.getArtifact().getScope() + "</y:EdgeLabel></y:PolyLineEdge></data>");
}
writer.write("</edge>" + System.lineSeparator());
}
writer.println("</edge>");
writer.flush();
return true;
} catch (IOException e) {
throw new UncheckedIOException("Failed to write GraphML format output", e);
}
return true;
}

/**
* {@inheritDoc}
*/
@Override
public boolean visit(DependencyNode node) {
if (node.getParent() == null || node.getParent() == node) {
writer.write(GRAPHML_HEADER);
try {
if (node.getParent() == null || node.getParent() == node) {
writer.write(GRAPHML_HEADER);
writer.flush();
}
// write node
writer.write("<node id=\"" + generateId(node) + "\">");
// add node label
writer.write("<data key=\"d0\"><y:ShapeNode><y:NodeLabel>" + node.toNodeString()
+ "</y:NodeLabel></y:ShapeNode></data>");
writer.write("</node>" + System.lineSeparator());
writer.flush();
return true;
} catch (IOException e) {
throw new UncheckedIOException("Failed to write GraphML format output", e);
}
// write node
writer.print("<node id=\"" + generateId(node) + "\">");
// add node label
writer.print("<data key=\"d0\"><y:ShapeNode><y:NodeLabel>" + node.toNodeString()
+ "</y:NodeLabel></y:ShapeNode></data>");
writer.println("</node>");
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.maven.plugins.dependency.tree;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -44,25 +46,31 @@ public JsonDependencyNodeVisitor(Writer writer) {

@Override
public boolean visit(DependencyNode node) {
if (node.getParent() == null || node.getParent() == node) {
writeRootNode(node);
try {
if (node.getParent() == null || node.getParent() == node) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird. Nodes can be their own parent?

writeRootNode(node);
}
return true;
} catch (IOException e) {
throw new UncheckedIOException("Failed to write JSON format output", e);
}
return true;
}

/**
* Writes the node to the writer. This method is recursive and will write all children nodes.
*
* @param node the node to write
* @throws IOException if an I/O error occurs while writing
*/
private void writeRootNode(DependencyNode node) {
private void writeRootNode(DependencyNode node) throws IOException {
Set<DependencyNode> visited = new HashSet<>();
int indent = 2;
StringBuilder sb = new StringBuilder();
sb.append("{").append("\n");
sb.append("{").append(System.lineSeparator());
writeNode(indent, node, sb, visited);
sb.append("}").append("\n");
sb.append("}").append(System.lineSeparator());
writer.write(sb.toString());
writer.flush();
}
/**
* Appends the node and its children to the string builder.
Expand Down Expand Up @@ -91,21 +99,21 @@ private void writeNode(int indent, DependencyNode node, StringBuilder sb, Set<De
* @param sb the string builder to append to
*/
private void writeChildren(int indent, DependencyNode node, StringBuilder sb, Set<DependencyNode> visited) {
sb.append(indent(indent)).append("\"children\": [").append("\n");
sb.append(indent(indent)).append("\"children\": [").append(System.lineSeparator());
indent += 2;
for (int i = 0; i < node.getChildren().size(); i++) {
DependencyNode child = node.getChildren().get(i);
sb.append(indent(indent));
sb.append("{").append("\n");
sb.append("{").append(System.lineSeparator());
writeNode(indent + 2, child, sb, visited);
sb.append(indent(indent)).append("}");
// we skip the comma for the last child
if (i != node.getChildren().size() - 1) {
sb.append(",");
}
sb.append("\n");
sb.append(System.lineSeparator());
}
sb.append(indent(indent)).append("]").append("\n");
sb.append(indent(indent)).append("]").append(System.lineSeparator());
}

@Override
Expand Down Expand Up @@ -156,7 +164,7 @@ private void appendKeyValue(StringBuilder sb, int indent, String key, String val
.append(value)
.append("\"")
.append(",")
.append("\n");
.append(System.lineSeparator());
}
/**
* Appends a key value pair to the string builder without a comma at the end. This is used for the last children of a node.
Expand All @@ -180,7 +188,7 @@ private void appendKeyWithoutComma(StringBuilder sb, int indent, String key, Str
.append("\"")
.append(value)
.append("\"")
.append("\n");
.append(System.lineSeparator());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.maven.plugins.dependency.tree;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -35,7 +37,7 @@
public class TGFDependencyNodeVisitor extends AbstractSerializingVisitor implements DependencyNodeVisitor {

/**
* Utiity class to write an Edge.
* Utility class to write an Edge.
*
* @author <a href="mailto:[email protected]">Jerome Creignou</a>
*/
Expand Down Expand Up @@ -102,30 +104,41 @@ public TGFDependencyNodeVisitor(Writer writer) {
*/
@Override
public boolean endVisit(DependencyNode node) {
if (node.getParent() == null || node.getParent() == node) {
// dump edges on last node endVisit
writer.println("#");
for (EdgeAppender edge : edges) {
writer.println(edge.toString());
try {
if (node.getParent() == null || node.getParent() == node) {
// dump edges on last node endVisit
writer.write("#" + System.lineSeparator());
for (EdgeAppender edge : edges) {
writer.write(edge.toString() + System.lineSeparator());
}
writer.flush();
} else {
DependencyNode parent = node.getParent();
// using scope as edge label.
edges.add(new EdgeAppender(parent, node, node.getArtifact().getScope()));
}
} else {
DependencyNode p = node.getParent();
// using scope as edge label.
edges.add(new EdgeAppender(p, node, node.getArtifact().getScope()));
return true;
} catch (IOException e) {
throw new UncheckedIOException("Failed to write TGF format output", e);
}
return true;
}

/**
* {@inheritDoc}
*/
@Override
public boolean visit(DependencyNode node) {
// write node
writer.write(generateId(node));
writer.write(" ");
writer.println(node.toNodeString());
return true;
try {
// Write node
writer.write(generateId(node));
writer.write(" ");
writer.write(node.toNodeString());
writer.write(System.lineSeparator());
writer.flush();
return true;
} catch (IOException e) {
throw new UncheckedIOException("Failed to write TGF format output", e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void testTreeJsonParsing() throws Exception {
List<String> contents = runTreeMojo("tree2.json", "json");

System.setProperty("jakarta.json.provider", "org.glassfish.json.JsonProviderImpl");
try (JsonReader reader = Json.createReader(new StringReader(String.join("\n", contents)))) {
try (JsonReader reader = Json.createReader(new StringReader(String.join(System.lineSeparator(), contents)))) {
JsonObject root = reader.readObject();

assertEquals(root.getString("groupId"), "testGroupId");
Expand Down