-
Notifications
You must be signed in to change notification settings - Fork 174
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
[MDEP-435] Added xml outputType to dependency tree #24
base: master
Are you sure you want to change the base?
Changes from all commits
a84888d
c780355
fc74901
88b36d5
c7ca99e
642b299
8150710
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,8 +107,14 @@ public class TreeMojo | |
|
||
/** | ||
* If specified, this parameter will cause the dependency tree to be written using the specified format. Currently | ||
* supported format are: <code>text</code> (default), <code>dot</code>, <code>graphml</code> and <code>tgf</code>. | ||
* These additional formats can be plotted to image files. | ||
* supported format are: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. format --> formats |
||
* <ul> | ||
* <li>text (default)</li> | ||
* <li>dot</li> | ||
* <li>graphml</li> | ||
* <li>tgf</li> | ||
* <li>xml</li> | ||
* </ul> | ||
* | ||
* @since 2.2 | ||
*/ | ||
|
@@ -384,6 +390,10 @@ else if ( "dot".equals( outputType ) ) | |
{ | ||
return new DOTDependencyNodeVisitor( writer ); | ||
} | ||
else if ( "xml".equals( outputType ) ) | ||
{ | ||
return new XMLDependencyNodeVisitor( writer ); | ||
} | ||
else | ||
{ | ||
return new SerializingDependencyNodeVisitor( writer, toGraphTokens( tokens ) ); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
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 javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import javax.xml.transform.OutputKeys; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.TransformerException; | ||
import javax.xml.transform.TransformerFactory; | ||
import javax.xml.transform.dom.DOMSource; | ||
import javax.xml.transform.stream.StreamResult; | ||
|
||
import org.apache.maven.artifact.Artifact; | ||
import org.apache.maven.shared.dependency.graph.DependencyNode; | ||
import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
import java.io.Writer; | ||
import java.util.List; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A dependency node visitor that serializes visited nodes to | ||
* <a href="https://en.wikipedia.org/wiki/XML">XML format</a> | ||
* | ||
* @author <a href="mailto:[email protected]">Bogdan Sikora</a> | ||
* @since 3.1.2 | ||
*/ | ||
public class XMLDependencyNodeVisitor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this class have to be public? |
||
extends AbstractSerializingVisitor | ||
implements DependencyNodeVisitor | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* @param writer the writer to write to. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: no period at end of doc comment, per sun Javadoc style |
||
*/ | ||
public XMLDependencyNodeVisitor( Writer writer ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Writers can't be guaranteed to handle encoding properly when the XML is serialized. This should be an output stream. |
||
{ | ||
super( writer ); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean visit( DependencyNode node ) | ||
{ | ||
try | ||
{ | ||
if ( node.getParent() == null || node.getParent() == node ) | ||
{ | ||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually using DocumentBuilder and Transformer look like quite some overhead for something simple like this. I think there are faster implementations that can directly write proper xml. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not code in java, so google is my main source and i haven't found any other way. As for the XXE. It is not parsing an XML byt creating it, so it should be ok right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with @rfscholte that this is quite a heavy approach. The @S4n60w3n, if I created a pull request on your MDEP-435 branch, could you verify that the PrettyPrintXMLWriter-approach gives the same outcome as your DOM-based one? Alternatively, could you tell me how I can verify that your implementation and mine have the same outcome? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mthmulders I will try to make time for that, but basically if you run my implementation and yours on this repo and output it to the file. Then you can make diff. It should be the same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is too heavyweight, but you should set namespace aware to true on the factory. |
||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | ||
Document doc = dBuilder.newDocument(); | ||
Element rootElement = getNode( doc, node, true ); | ||
doc.appendChild( rootElement ); | ||
|
||
List<DependencyNode> children = node.getChildren(); | ||
for ( DependencyNode child : children ) | ||
{ | ||
handleChild( doc, child, rootElement ); | ||
} | ||
|
||
TransformerFactory transformerFactory = TransformerFactory.newInstance(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The serialization could be split out into a separate method. |
||
Transformer transformer = transformerFactory.newTransformer(); | ||
transformer.setOutputProperty( OutputKeys.INDENT, "yes" ); | ||
transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2" ); | ||
DOMSource source = new DOMSource( doc ); | ||
|
||
StreamResult console = new StreamResult( writer ); | ||
|
||
transformer.transform( source, console ); | ||
} | ||
} | ||
catch ( ParserConfigurationException | TransformerException e ) | ||
{ | ||
LoggerFactory.getLogger(XMLDependencyNodeVisitor.class).error(e.getMessage()); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean endVisit( DependencyNode node ) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Render child with its children recursively | ||
* | ||
* @param doc Docuemnt to use | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. document |
||
* @param node child node to handle | ||
* @param depth depth of the child | ||
*/ | ||
private void handleChild( Document doc, DependencyNode node, Element parent ) | ||
{ | ||
Element element = getNode( doc, node, false ); | ||
Node dependencies = parent.getElementsByTagName( "dependencies" ).item( 0 ); | ||
dependencies.appendChild( element ); | ||
|
||
List<DependencyNode> children = node.getChildren(); | ||
for ( DependencyNode child : children ) | ||
{ | ||
handleChild( doc, child, element ); | ||
} | ||
} | ||
|
||
/** | ||
* Get element from node | ||
* | ||
* @param doc Docuemnt to use | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sp: document |
||
* @param node Node to get data from | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Node --> node (per sun javadoc conventions) |
||
*/ | ||
private Element getNode( Document doc, DependencyNode node, boolean root ) | ||
{ | ||
Artifact artifact = node.getArtifact(); | ||
Element element = null; | ||
|
||
if ( root ) | ||
{ | ||
element = doc.createElement( "project" ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A separate method for the root rather than an if statement would reduce cyclomatic complexity There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's weird that the syntax is sort of like a pom but not fully a POM. E.g. the namespaces are different. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, seems like it should have an XSD as well. |
||
} | ||
else | ||
{ | ||
element = doc.createElement( "dependency" ); | ||
} | ||
|
||
Element groupId = doc.createElement( "groupId" ); | ||
groupId.setTextContent( artifact.getGroupId() ); | ||
element.appendChild( groupId ); | ||
|
||
Element artifactId = doc.createElement( "artifactId" ); | ||
artifactId.setTextContent( artifact.getArtifactId() ); | ||
element.appendChild( artifactId ); | ||
|
||
Element version = doc.createElement( "version" ); | ||
version.setTextContent( artifact.getVersion() ); | ||
element.appendChild( version ); | ||
|
||
if ( !root ) | ||
{ | ||
Element scope = doc.createElement( "scope" ); | ||
scope.setTextContent( artifact.getScope() ); | ||
element.appendChild( scope ); | ||
|
||
Element type = doc.createElement( "type" ); | ||
type.setTextContent( artifact.getType() ); | ||
element.appendChild( type ); | ||
} | ||
else | ||
{ | ||
Element packaging = doc.createElement( "packaging" ); | ||
packaging.setTextContent( artifact.getType() ); | ||
element.appendChild( packaging ); | ||
} | ||
|
||
Element dependencies = doc.createElement( "dependencies" ); | ||
element.appendChild( dependencies ); | ||
|
||
return element; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will cause --> causes (tech writing lives in the eternal present)