View Javadoc

1   package org.apache.maven.plugin.dependency.tree ;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Writer;
23  
24  import org.apache.maven.shared.dependency.graph.DependencyNode;
25  import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
26  
27  /**
28   * A dependency node visitor that serializes visited nodes to a writer using the graphml format.
29   * {@link http://graphml.graphdrawing.org/}
30   *
31   * @author <a href="mailto:jerome.creignou@gmail.com">Jerome Creignou</a>
32   * @since 2.1
33   */
34  public class GraphmlDependencyNodeVisitor
35      extends AbstractSerializingVisitor
36      implements DependencyNodeVisitor
37  {
38  
39      /**
40       * Graphml xml file header. Define Schema and root element. We also define 2 key as meta data.
41       */
42      private static final String GRAPHML_HEADER =
43          "<?xml version=\"1.0\" encoding=\"UTF-8\"?> "
44              + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" "
45              + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
46              + "xmlns:y=\"http://www.yworks.com/xml/graphml\" "
47              + "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns "
48              + "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n"
49              + "  <key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/> \n"
50              + "  <key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/> \n"
51              + "<graph id=\"dependencies\" edgedefault=\"directed\">\n";
52  
53      /**
54       * Graphml xml file footer.
55       */
56      private static final String GRAPHML_FOOTER = "</graph></graphml>";
57  
58      /**
59       * Constructor.
60       *
61       * @param writer the writer to write to.
62       */
63      public GraphmlDependencyNodeVisitor( Writer writer )
64      {
65          super( writer );
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      public boolean endVisit( DependencyNode node )
72      {
73          if ( node.getParent() == null || node.getParent() == node )
74          {
75              writer.write( GRAPHML_FOOTER );
76          }
77          else
78          {
79              DependencyNode p = node.getParent();
80              writer.print( "<edge source=\"" + generateId( p ) + "\" target=\"" + generateId( node ) + "\">" );
81              if ( node.getArtifact().getScope() != null )
82              {
83                  // add Edge label
84                  writer.print( "<data key=\"d1\"><y:PolyLineEdge><y:EdgeLabel>" + node.getArtifact().getScope()
85                      + "</y:EdgeLabel></y:PolyLineEdge></data>" );
86              }
87              writer.println( "</edge>" );
88          }
89          return true;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      public boolean visit( DependencyNode node )
96      {
97          if ( node.getParent() == null || node.getParent() == node )
98          {
99              writer.write( GRAPHML_HEADER );
100         }
101         // write node
102         writer.print( "<node id=\"" + generateId( node ) + "\">" );
103         // add node label
104         writer.print( "<data key=\"d0\"><y:ShapeNode><y:NodeLabel>" + node.toNodeString()
105             + "</y:NodeLabel></y:ShapeNode></data>" );
106         writer.println( "</node>" );
107         return true;
108     }
109 
110     /**
111      * Generate a unique id from a DependencyNode.
112      * <p>
113      * Current implementation is rather simple and uses hashcode.
114      * </p>
115      *
116      * @param node the DependencyNode to use.
117      * @return the unique id.
118      */
119     private static String generateId( DependencyNode node )
120     {
121         return String.valueOf( node.hashCode() );
122     }
123 }