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  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.shared.dependency.graph.DependencyNode;
27  import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
28  
29  /**
30   * A dependency node visitor that serializes visited nodes to a writer using the TGF format.
31   * http://en.wikipedia.org/wiki/Trivial_Graph_Format
32   *
33   * @author <a href="mailto:jerome.creignou@gmail.com">Jerome Creignou</a>
34   * @since 2.1
35   */
36  public class TGFDependencyNodeVisitor
37      extends AbstractSerializingVisitor
38      implements DependencyNodeVisitor
39  {
40  
41      /**
42       * Utiity class to write an Edge.
43       *
44       * @author <a href="mailto:jerome.creignou@gmail.com">Jerome Creignou</a>
45       */
46      static final class EdgeAppender
47      {
48          /**
49           * Edge start.
50           */
51          private DependencyNode from;
52  
53          /**
54           * Edge end.
55           */
56          private DependencyNode to;
57  
58          /**
59           * Edge label. (optional)
60           */
61          private String label;
62  
63          /**
64           * Build a new EdgeAppender.
65           *
66           * @param from edge start.
67           * @param to edge end
68           * @param label optional label.
69           */
70          public EdgeAppender( DependencyNode from, DependencyNode to, String label )
71          {
72              super();
73              this.from = from;
74              this.to = to;
75              this.label = label;
76          }
77  
78          /**
79           * build a string representing the edge.
80           */
81          @Override
82          public String toString()
83          {
84              StringBuilder result = new StringBuilder( generateId( from ) );
85              result.append( ' ' ).append( generateId( to ) );
86              if ( label != null )
87              {
88                  result.append( ' ' ).append( label );
89              }
90              return result.toString();
91          }
92  
93      }
94  
95      /**
96       * List of edges.
97       */
98      private List<EdgeAppender> edges = new ArrayList<EdgeAppender>();
99  
100     /**
101      * Constructor.
102      *
103      * @param writer the writer to write to.
104      */
105     public TGFDependencyNodeVisitor( Writer writer )
106     {
107         super( writer );
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public boolean endVisit( DependencyNode node )
115     {
116         if ( node.getParent() == null || node.getParent() == node )
117         {
118             // dump edges on last node endVisit
119             writer.println( "#" );
120             for ( EdgeAppender edge : edges )
121             {
122                 writer.println( edge.toString() );
123             }
124         }
125         else
126         {
127             DependencyNode p = node.getParent();
128             // using scope as edge label.
129             edges.add( new EdgeAppender( p, node, node.getArtifact().getScope() ) );
130         }
131         return true;
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public boolean visit( DependencyNode node )
139     {
140         // write node
141         writer.write( generateId( node ) );
142         writer.write( " " );
143         writer.println( node.toNodeString() );
144         return true;
145     }
146 
147     /**
148      * Generate a unique id from a DependencyNode.
149      * <p>
150      * Current implementation is rather simple and uses hashcode.
151      * </p>
152      *
153      * @param node the DependencyNode to use.
154      * @return the unique id.
155      */
156     private static String generateId( DependencyNode node )
157     {
158         return String.valueOf( node.hashCode() );
159     }
160 }