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 1 commit 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 @@ -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,7 @@
*/
package org.apache.maven.plugins.dependency.tree;

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

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

// Generate "currentNode -> Child" lines
try {
if (node.getParent() == null || node.getParent() == node) {
writer.write("digraph \"" + node.toNodeString() + "\" { " + System.lineSeparator());
}

List<DependencyNode> children = node.getChildren();
// Generate "currentNode -> Child" lines

for (DependencyNode child : children) {
writer.println("\t\"" + node.toNodeString() + "\" -> \"" + child.toNodeString() + "\" ; ");
List<DependencyNode> children = node.getChildren();
for (DependencyNode child : children) {
writer.write("\t\"" + node.toNodeString() + "\" -> \"" + child.toNodeString() + "\" ; "
+ System.lineSeparator());
Copy link
Contributor

Choose a reason for hiding this comment

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

Output should be platform independent. That is, use \n

}
writer.flush();
return true;
} catch (IOException e) {
throw new RuntimeException("Failed to write to DOT output", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

UncheckedIOException but really we need to figure out how to not thrown an exception at all here, or perhaps change the method signature. Possibly we need to put everything in a StringWriter first and then write the string later.

}

return true;
}

/**
* {@inheritDoc}
*/
@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(" } ");
writer.flush();
}
return true;
} catch (IOException e) {
throw new RuntimeException("Failed to write to DOT output", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

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

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

import org.apache.maven.shared.dependency.graph.DependencyNode;
Expand Down Expand Up @@ -63,37 +64,48 @@ public GraphmlDependencyNodeVisitor(Writer writer) {
* {@inheritDoc}
*/
@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>");
public boolean visit(DependencyNode node) {
try {
if (node.getParent() == null || node.getParent() == node) {
writer.write(GRAPHML_HEADER);
}
writer.println("</edge>");
// 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 RuntimeException("Failed to write GraphML node", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

}
return true;
}

/**
* {@inheritDoc}
*/
@Override
public boolean visit(DependencyNode node) {
if (node.getParent() == null || node.getParent() == node) {
writer.write(GRAPHML_HEADER);
public boolean endVisit(DependencyNode node) {
try {
if (node.getParent() == null || node.getParent() == node) {
writer.write(GRAPHML_FOOTER);
writer.flush();
} 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.flush();
}
return true;
} catch (IOException e) {
throw new RuntimeException("Failed to write GraphML edge or footer", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

}
// 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,7 @@
*/
package org.apache.maven.plugins.dependency.tree;

import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -56,13 +57,18 @@ public boolean visit(DependencyNode node) {
* @param node the node to write
*/
private void writeRootNode(DependencyNode node) {
Set<DependencyNode> visited = new HashSet<>();
int indent = 2;
StringBuilder sb = new StringBuilder();
sb.append("{").append("\n");
writeNode(indent, node, sb, visited);
sb.append("}").append("\n");
writer.write(sb.toString());
try {
Set<DependencyNode> visited = new HashSet<>();
int indent = 2;
StringBuilder sb = new StringBuilder();
sb.append("{").append("\n");
writeNode(indent, node, sb, visited);
sb.append("}").append("\n");
writer.write(sb.toString());
writer.flush();
} catch (IOException e) {
throw new RuntimeException("Failed to write JSON output", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

}
}
/**
* Appends the node and its children to the string builder.
Expand Down Expand Up @@ -110,7 +116,14 @@ private void writeChildren(int indent, DependencyNode node, StringBuilder sb, Se

@Override
public boolean endVisit(DependencyNode node) {
return true;
try {
if (node.getParent() == null || node.getParent() == node) {
writer.flush();
}
return true;
} catch (IOException e) {
throw new RuntimeException("Failed to flush JSON output", e);
}
}
/**
* Appends the artifact values to the string builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.maven.plugins.dependency.tree;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -101,31 +102,43 @@ public TGFDependencyNodeVisitor(Writer writer) {
* {@inheritDoc}
*/
@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());
}
} else {
DependencyNode p = node.getParent();
// using scope as edge label.
edges.add(new EdgeAppender(p, node, node.getArtifact().getScope()));
public boolean visit(DependencyNode node) {
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 RuntimeException("Failed to write TGF node", e);
}
return true;
}

/**
* {@inheritDoc}
*/
@Override
public boolean visit(DependencyNode node) {
// write node
writer.write(generateId(node));
writer.write(" ");
writer.println(node.toNodeString());
return true;
public boolean endVisit(DependencyNode node) {
try {
if (node.getParent() == null || node.getParent() == node) {
// dump edges on last node endVisit
writer.write("#" + System.lineSeparator());
Copy link
Contributor

Choose a reason for hiding this comment

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

\n

for (EdgeAppender edge : edges) {
writer.write(edge.toString());
writer.write(System.lineSeparator());
Copy link
Contributor

Choose a reason for hiding this comment

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

\n

}
writer.flush();
} else {
DependencyNode p = node.getParent();
Copy link
Contributor

Choose a reason for hiding this comment

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

p --> parent

// using scope as edge label.
edges.add(new EdgeAppender(p, node, node.getArtifact().getScope()));
}
return true;
} catch (IOException e) {
throw new RuntimeException("Failed to write TGF edges or footer", e);
}
}

/**
Expand Down