elharo commented on code in PR #207: URL: https://github.com/apache/maven-dependency-plugin/pull/207#discussion_r1140734121
########## src/main/java/org/apache/maven/plugins/dependency/tree/JSONDependencyNodeVisitor.java: ########## @@ -0,0 +1,125 @@ +package org.apache.maven.plugins.dependency.tree; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.commons.lang3.StringUtils; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.shared.dependency.graph.DependencyNode; +import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor; + +import java.io.Writer; +import java.util.List; + +/** + * A dependency node visitor that serializes visited nodes to a writer using the + * JSON format. + * + * @author <a href="mailto:[email protected]">Zhenxu Ke</a> + * @since 3.3.1 + */ +public class JSONDependencyNodeVisitor + extends AbstractSerializingVisitor + implements DependencyNodeVisitor +{ + + /** + * Constructor. + * + * @param writer the writer to write to. Review Comment: nit: no period ########## src/main/java/org/apache/maven/plugins/dependency/tree/JSONDependencyNodeVisitor.java: ########## @@ -0,0 +1,125 @@ +package org.apache.maven.plugins.dependency.tree; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.commons.lang3.StringUtils; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.shared.dependency.graph.DependencyNode; +import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor; + +import java.io.Writer; +import java.util.List; + +/** + * A dependency node visitor that serializes visited nodes to a writer using the + * JSON format. + * + * @author <a href="mailto:[email protected]">Zhenxu Ke</a> + * @since 3.3.1 + */ +public class JSONDependencyNodeVisitor + extends AbstractSerializingVisitor + implements DependencyNodeVisitor +{ + + /** + * Constructor. + * + * @param writer the writer to write to. + */ + public JSONDependencyNodeVisitor( Writer writer ) + { + super( writer ); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean endVisit( DependencyNode node ) + { + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean visit( DependencyNode node ) + { + if ( node.getParent() == null || node.getParent() == node ) + { + writeNode( 0, node, true ); + } + + return true; + } + + private void writeNode( int indent, DependencyNode node, boolean root ) + { + Artifact artifact = node.getArtifact(); + + writer.println( indentations( indent ) + "{" ); Review Comment: We shouldn't be using a PrintWriter here. They swallow IOExceptions. Even if the superclass already does this, let's not make the same mistake again. ########## src/main/java/org/apache/maven/plugins/dependency/tree/JSONDependencyNodeVisitor.java: ########## @@ -0,0 +1,125 @@ +package org.apache.maven.plugins.dependency.tree; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.commons.lang3.StringUtils; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.shared.dependency.graph.DependencyNode; +import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor; + +import java.io.Writer; +import java.util.List; + +/** + * A dependency node visitor that serializes visited nodes to a writer using the + * JSON format. + * + * @author <a href="mailto:[email protected]">Zhenxu Ke</a> + * @since 3.3.1 + */ +public class JSONDependencyNodeVisitor + extends AbstractSerializingVisitor + implements DependencyNodeVisitor +{ + + /** + * Constructor. + * + * @param writer the writer to write to. + */ + public JSONDependencyNodeVisitor( Writer writer ) + { + super( writer ); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean endVisit( DependencyNode node ) + { + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean visit( DependencyNode node ) + { + if ( node.getParent() == null || node.getParent() == node ) + { + writeNode( 0, node, true ); + } + + return true; + } + + private void writeNode( int indent, DependencyNode node, boolean root ) + { + Artifact artifact = node.getArtifact(); + + writer.println( indentations( indent ) + "{" ); + indent++; + String groupId = indentations( indent ) + "\"groupId\": \"" + artifact.getGroupId() + "\""; Review Comment: I think manually constructing the JSON is preferable to adding another library. (Some JSON libraries due have security issues.) ########## src/main/java/org/apache/maven/plugins/dependency/tree/JSONDependencyNodeVisitor.java: ########## @@ -0,0 +1,125 @@ +package org.apache.maven.plugins.dependency.tree; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.commons.lang3.StringUtils; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.shared.dependency.graph.DependencyNode; +import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor; + +import java.io.Writer; +import java.util.List; + +/** + * A dependency node visitor that serializes visited nodes to a writer using the + * JSON format. + * + * @author <a href="mailto:[email protected]">Zhenxu Ke</a> + * @since 3.3.1 + */ +public class JSONDependencyNodeVisitor + extends AbstractSerializingVisitor + implements DependencyNodeVisitor +{ + + /** + * Constructor. + * + * @param writer the writer to write to. + */ + public JSONDependencyNodeVisitor( Writer writer ) + { + super( writer ); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean endVisit( DependencyNode node ) + { + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean visit( DependencyNode node ) + { + if ( node.getParent() == null || node.getParent() == node ) + { + writeNode( 0, node, true ); + } + + return true; + } + + private void writeNode( int indent, DependencyNode node, boolean root ) + { + Artifact artifact = node.getArtifact(); + + writer.println( indentations( indent ) + "{" ); + indent++; + String groupId = indentations( indent ) + "\"groupId\": \"" + artifact.getGroupId() + "\""; + String artifactId = indentations( indent ) + "\"artifactId\": \"" + artifact.getArtifactId() + "\""; + String version = indentations( indent ) + "\"version\": \"" + artifact.getVersion() + "\""; + String type = indentations( indent ) + "\"type\": \"" + artifact.getType() + "\""; + String scope = indentations( indent ) + "\"scope\": \"" + artifact.getScope() + "\""; + String[] elements = root ? new String[] { groupId, artifactId, version, type } + : new String[] { groupId, artifactId, version, type, scope }; + + writer.print( StringUtils.join( "," + System.lineSeparator(), elements ) ); Review Comment: Please don't system dependent output like System.lineSeparator(). Specify the character being output. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
