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 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.shared.dependency.tree.DependencyNode;
27 import org.apache.maven.shared.dependency.tree.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 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 final static 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 public String toString()
82 {
83 StringBuffer result = new StringBuffer( generateId( from ) );
84 result.append( ' ' ).append( generateId( to ) );
85 if ( label != null )
86 {
87 result.append( ' ' ).append( label );
88 }
89 return result.toString();
90 }
91
92 }
93
94 /**
95 * List of edges.
96 */
97 private List<EdgeAppender> edges = new ArrayList<EdgeAppender>();
98
99 /**
100 * Constructor.
101 *
102 * @param writer the writer to write to.
103 */
104 public TGFDependencyNodeVisitor( Writer writer )
105 {
106 super( writer );
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 public boolean endVisit( DependencyNode node )
113 {
114 if ( node.getParent() == null || node.getParent() == node )
115 {
116 // dump edges on last node endVisit
117 writer.println( "#" );
118 for ( EdgeAppender edge : edges )
119 {
120 writer.println( edge.toString() );
121 }
122 }
123 else
124 {
125 DependencyNode p = node.getParent();
126 // using scope as edge label.
127 edges.add( new EdgeAppender( p, node, node.getArtifact().getScope() ) );
128 }
129 return true;
130 }
131
132 /**
133 * {@inheritDoc}
134 */
135 public boolean visit( DependencyNode node )
136 {
137 // write node
138 writer.write( generateId( node ) );
139 writer.write( " " );
140 writer.println( node.toNodeString() );
141 return true;
142 }
143
144 /**
145 * Generate a unique id from a DependencyNode.
146 * <p>
147 * Current implementation is rather simple and uses hashcode.
148 * </p>
149 *
150 * @param node the DependencyNode to use.
151 * @return the unique id.
152 */
153 private static String generateId( DependencyNode node )
154 {
155 return String.valueOf( node.hashCode() );
156 }
157 }