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  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   *
32   * http://en.wikipedia.org/wiki/Trivial_Graph_Format
33   *
34   * @author <a href="mailto:jerome.creignou@gmail.com">Jerome Creignou</a>
35   * @since 2.1
36   */
37  public class TGFDependencyNodeVisitor
38      extends AbstractSerializingVisitor
39      implements DependencyNodeVisitor
40  {
41  
42      /**
43       * Utiity class to write an Edge.
44       *
45       * @author <a href="mailto:jerome.creignou@gmail.com">Jerome Creignou</a>
46       */
47      static final class EdgeAppender
48      {
49          /**
50           * Edge start.
51           */
52          private DependencyNode from;
53  
54          /**
55           * Edge end.
56           */
57          private DependencyNode to;
58  
59          /**
60           * Edge label. (optional)
61           */
62          private String label;
63  
64          /**
65           * Build a new EdgeAppender.
66           *
67           * @param from edge start.
68           * @param to edge end
69           * @param label optional label.
70           */
71          public EdgeAppender( DependencyNode from, DependencyNode to, String label )
72          {
73              super();
74              this.from = from;
75              this.to = to;
76              this.label = label;
77          }
78  
79          /**
80           * build a string representing the edge.
81           */
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     public boolean endVisit( DependencyNode node )
114     {
115         if ( node.getParent() == null || node.getParent() == node )
116         {
117             // dump edges on last node endVisit
118             writer.println( "#" );
119             for ( EdgeAppender edge : edges )
120             {
121                 writer.println( edge.toString() );
122             }
123         }
124         else
125         {
126             DependencyNode p = node.getParent();
127             // using scope as edge label.
128             edges.add( new EdgeAppender( p, node, node.getArtifact().getScope() ) );
129         }
130         return true;
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     public boolean visit( DependencyNode node )
137     {
138         // write node
139         writer.write( generateId( node ) );
140         writer.write( " " );
141         writer.println( node.toNodeString() );
142         return true;
143     }
144 
145     /**
146      * Generate a unique id from a DependencyNode.
147      * <p>
148      * Current implementation is rather simple and uses hashcode.
149      * </p>
150      *
151      * @param node the DependencyNode to use.
152      * @return the unique id.
153      */
154     private static String generateId( DependencyNode node )
155     {
156         return String.valueOf( node.hashCode() );
157     }
158 }