Permalink
Browse files

git-svn-id: https://svn.apache.org/repos/asf/maven/sandbox/branches/M…

…DEP-145@744852 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent cbb9471 commit 511ff59d60a57f0ad0485f539490b2f73e65828c Piyawoot Songsiritat committed Feb 16, 2009
@@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
+import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@@ -42,6 +43,9 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.dependency.utils.DependencyUtil;
+import org.apache.maven.plugin.dependency.treeSerializers.GraphmlDependencyNodeVisitor;
+import org.apache.maven.plugin.dependency.treeSerializers.TGFDependencyNodeVisitor;
+import org.apache.maven.plugin.dependency.treeSerializers.DOTDependencyNodeVisitor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;
import org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter;
@@ -142,6 +146,18 @@
* @since 2.0-alpha-5
*/
private File outputFile;
+
+ /**
+ * If specified, this parameter will cause the dependency tree to be written using the specified format. Currently
+ * supported format are text, dot, graphml and tgf.
+ *
+ * These formats can be plotted to image files. An example of how to plot a dot file using
+ * pygraphviz can be found <a href="http://networkx.lanl.gov/pygraphviz/tutorial.html#layout-and-drawing">here</a>
+ *
+ * @parameter expression="${outputType}" default-value="text"
+ * @since 2.1
+ */
+ private String outputType;
/**
* The scope to filter by when resolving the dependency tree, or <code>null</code> to include dependencies from
@@ -333,7 +349,7 @@ private String serialiseDependencyTree( DependencyNode rootNode )
StringWriter writer = new StringWriter();
TreeTokens treeTokens = toTreeTokens( tokens );
- DependencyNodeVisitor visitor = new SerializingDependencyNodeVisitor( writer, treeTokens );
+ DependencyNodeVisitor visitor = getSerializingDependencyNodeVisitor( writer );
// TODO: remove the need for this when the serializer can calculate last nodes from visitor calls only
visitor = new BuildingDependencyNodeVisitor( visitor );
@@ -355,6 +371,26 @@ private String serialiseDependencyTree( DependencyNode rootNode )
return writer.toString();
}
+ public DependencyNodeVisitor getSerializingDependencyNodeVisitor( Writer writer )
+ {
+ if ( "graphml".equals( outputType ) )
+ {
+ return new GraphmlDependencyNodeVisitor( writer );
+ }
+ else if ( "tgf".equals( outputType ) )
+ {
+ return new TGFDependencyNodeVisitor( writer );
+ }
+ else if ( "dot".equals( outputType ) )
+ {
+ return new DOTDependencyNodeVisitor( writer ) ;
+ }
+ else
+ {
+ return new SerializingDependencyNodeVisitor( writer, toTreeTokens( tokens ) );
+ }
+ }
+
/**
* Gets the tree tokens instance for the specified name.
*
@@ -0,0 +1,57 @@
+package org.apache.maven.plugin.dependency.treeSerializers ;
+
+/*
+ * 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 java.io.PrintWriter;
+import java.io.Writer;
+
+/**
+ * Abstract class for visitors performing serialization.
+ *
+ * @author <a href="mailto:jerome.creignou@gmail.com">Jerome Creignou</a>
+ */
+public abstract class AbstractSerializingVisitor
+{
+
+ /**
+ * The writer to serialize to.
+ */
+ protected final PrintWriter writer;
+
+ /**
+ * Constructor.
+ * <p>
+ * Build a new AbstractSerializingDependencyNodeVisitor with the writer to serialize to.
+ * </p>
+ *
+ * @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 );
+ }
+ }
+}
@@ -0,0 +1,91 @@
+package org.apache.maven.plugin.dependency.treeSerializers;
+
+/*
+ * 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.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
+import org.apache.maven.shared.dependency.tree.DependencyNode;
+import org.apache.maven.project.MavenProject;
+
+import java.io.Writer;
+import java.util.List;
+import java.util.Iterator;
+
+/**
+ * A dependency node visitor that serializes visited nodes to DOT format
+ * http://en.wikipedia.org/wiki/DOT_language
+ *
+ * @author <a href="mailto:pi.songs@gmail.com">Pi Song</a>
+ * @since 2.1
+ */
+public class DOTDependencyNodeVisitor extends AbstractSerializingVisitor
+ implements DependencyNodeVisitor {
+
+ /**
+ * Constructor.
+ *
+ * @param writer the writer to write to.
+ */
+ public DOTDependencyNodeVisitor( Writer writer)
+ {
+ super( writer );
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean visit( DependencyNode node )
+ {
+ if ( node.getParent() == null || node.getParent() == node )
+ {
+ writer.write( "digraph \"" + node.toNodeString() + "\" { \n" );
+ }
+
+ // Generate "currentNode -> Child" lines
+
+ List children = node.getChildren() ;
+
+ for ( Iterator it = children.iterator () ; it.hasNext (); )
+ {
+ StringBuffer sb = new StringBuffer() ;
+ sb.append( "\t\"" ) ;
+ sb.append(node.toNodeString()) ;
+ sb.append( "\" -> \"" ) ;
+ sb.append( ((DependencyNode)it.next()).toNodeString() ) ;
+ sb.append( "\" ; " ) ;
+ writer.println( sb.toString() ) ;
+ }
+
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean endVisit( DependencyNode node )
+ {
+ if ( node.getParent() == null || node.getParent() == node )
+ {
+ writer.write( " } " );
+ }
+ return true;
+ }
+
+}
@@ -0,0 +1,122 @@
+package org.apache.maven.plugin.dependency.treeSerializers ;
+
+/*
+ * 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 java.io.Writer;
+
+import org.apache.maven.shared.dependency.tree.DependencyNode;
+import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
+import org.apache.maven.shared.dependency.tree.traversal.SerializingDependencyNodeVisitor;
+
+/**
+ * A dependency node visitor that serializes visited nodes to a writer using the graphml format.
+ * {@link http://graphml.graphdrawing.org/}
+ *
+ * @author <a href="mailto:jerome.creignou@gmail.com">Jerome Creignou</a>
+ * @since 2.1
+ */
+public class GraphmlDependencyNodeVisitor extends AbstractSerializingVisitor
+ implements DependencyNodeVisitor
+{
+
+ /**
+ * Graphml xml file header. Define Schema and root element. We also define 2 key as meta data.
+ */
+ private static final String GRAPHML_HEADER =
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" "
+ + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ + "xmlns:y=\"http://www.yworks.com/xml/graphml\" "
+ + "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns "
+ + "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n"
+ + " <key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/> \n"
+ + " <key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/> \n"
+ + "<graph id=\"dependencies\" edgedefault=\"directed\">\n";
+
+ /**
+ * Graphml xml file footer.
+ */
+ private static final String GRAPHML_FOOTER = "</graph></graphml>";
+
+ /**
+ * Constructor.
+ *
+ * @param writer the writer to write to.
+ */
+ public GraphmlDependencyNodeVisitor( Writer writer )
+ {
+ super( writer );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ 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>" );
+ }
+ writer.println( "</edge>" );
+ }
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean visit( DependencyNode node )
+ {
+ if ( node.getParent() == null || node.getParent() == node )
+ {
+ writer.write( GRAPHML_HEADER );
+ }
+ // 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;
+ }
+
+ /**
+ * Generate a unique id from a DependencyNode.
+ * <p>
+ * Current implementation is rather simple and uses hashcode.
+ * </p>
+ *
+ * @param node the DependencyNode to use.
+ * @return the unique id.
+ */
+ private static String generateId( DependencyNode node )
+ {
+ return String.valueOf( node.hashCode() );
+ }
+}
Oops, something went wrong.

0 comments on commit 511ff59

Please sign in to comment.