View Javadoc
1   package org.apache.maven.plugins.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
29   * <a href="https://en.wikipedia.org/wiki/GraphML">graphml format</a>.
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> "
43          + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" "
44          + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:y=\"http://www.yworks.com/xml/graphml\" "
45          + "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns "
46          + "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">" + System.lineSeparator()
47          + "  <key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/> " + System.lineSeparator()
48          + "  <key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/> " + System.lineSeparator()
49          + "<graph id=\"dependencies\" edgedefault=\"directed\">" + System.lineSeparator();
50  
51      /**
52       * Graphml xml file footer.
53       */
54      private static final String GRAPHML_FOOTER = "</graph></graphml>";
55  
56      /**
57       * Constructor.
58       *
59       * @param writer the writer to write to.
60       */
61      public GraphmlDependencyNodeVisitor( Writer writer )
62      {
63          super( writer );
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public boolean endVisit( DependencyNode node )
71      {
72          if ( node.getParent() == null || node.getParent() == node )
73          {
74              writer.write( GRAPHML_FOOTER );
75          }
76          else
77          {
78              DependencyNode p = node.getParent();
79              writer.print( "<edge source=\"" + generateId( p ) + "\" target=\"" + generateId( node ) + "\">" );
80              if ( node.getArtifact().getScope() != null )
81              {
82                  // add Edge label
83                  writer.print( "<data key=\"d1\"><y:PolyLineEdge><y:EdgeLabel>" + node.getArtifact().getScope()
84                      + "</y:EdgeLabel></y:PolyLineEdge></data>" );
85              }
86              writer.println( "</edge>" );
87          }
88          return true;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
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 }