View Javadoc

1   package org.apache.maven.plugin.dependency.treeSerializers ;
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.tree.DependencyNode;
25  import org.apache.maven.shared.dependency.tree.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 extends AbstractSerializingVisitor
35                                            implements DependencyNodeVisitor
36  {
37  
38      /**
39       * Graphml xml file header. Define Schema and root element. We also define 2 key as meta data.
40       */
41      private static final String GRAPHML_HEADER =
42          "<?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\" "
45              + "xmlns:y=\"http://www.yworks.com/xml/graphml\" "
46              + "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns "
47              + "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n"
48              + "  <key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/> \n"
49              + "  <key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/> \n"
50              + "<graph id=\"dependencies\" edgedefault=\"directed\">\n";
51  
52      /**
53       * Graphml xml file footer.
54       */
55      private static final String GRAPHML_FOOTER = "</graph></graphml>";
56  
57      /**
58       * Constructor.
59       *
60       * @param writer the writer to write to.
61       */
62      public GraphmlDependencyNodeVisitor( Writer writer )
63      {
64          super( writer );
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
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      public boolean visit( DependencyNode node )
95      {
96          if ( node.getParent() == null || node.getParent() == node )
97          {
98              writer.write( GRAPHML_HEADER );
99          }
100         // write node
101         writer.print( "<node id=\"" + generateId( node ) + "\">" );
102         // add node label
103         writer.print( "<data key=\"d0\"><y:ShapeNode><y:NodeLabel>" + node.toNodeString()
104             + "</y:NodeLabel></y:ShapeNode></data>" );
105         writer.println( "</node>" );
106         return true;
107     }
108 
109     /**
110      * Generate a unique id from a DependencyNode.
111      * <p>
112      * Current implementation is rather simple and uses hashcode.
113      * </p>
114      *
115      * @param node the DependencyNode to use.
116      * @return the unique id.
117      */
118     private static String generateId( DependencyNode node )
119     {
120         return String.valueOf( node.hashCode() );
121     }
122 }