1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.plugins.dependency.tree;
20
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.List;
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/Trivial_Graph_Format">TGF format</a>.
30 *
31 * @author <a href="mailto:jerome.creignou@gmail.com">Jerome Creignou</a>
32 * @since 2.1
33 */
34 public class TGFDependencyNodeVisitor extends AbstractSerializingVisitor implements DependencyNodeVisitor {
35
36 /**
37 * Utiity class to write an Edge.
38 *
39 * @author <a href="mailto:jerome.creignou@gmail.com">Jerome Creignou</a>
40 */
41 static final class EdgeAppender {
42 /**
43 * Edge start.
44 */
45 private final DependencyNode from;
46
47 /**
48 * Edge end.
49 */
50 private final DependencyNode to;
51
52 /**
53 * Edge label. (optional)
54 */
55 private final String label;
56
57 /**
58 * Build a new EdgeAppender.
59 *
60 * @param from edge start.
61 * @param to edge end
62 * @param label optional label.
63 */
64 EdgeAppender(DependencyNode from, DependencyNode to, String label) {
65 super();
66 this.from = from;
67 this.to = to;
68 this.label = label;
69 }
70
71 /**
72 * build a string representing the edge.
73 */
74 @Override
75 public String toString() {
76 StringBuilder result = new StringBuilder(generateId(from));
77 result.append(' ').append(generateId(to));
78 if (label != null) {
79 result.append(' ').append(label);
80 }
81 return result.toString();
82 }
83 }
84
85 /**
86 * List of edges.
87 */
88 private final List<EdgeAppender> edges = new ArrayList<>();
89
90 /**
91 * Constructor.
92 *
93 * @param writer the writer to write to.
94 */
95 public TGFDependencyNodeVisitor(Writer writer) {
96 super(writer);
97 }
98
99 /**
100 * {@inheritDoc}
101 */
102 @Override
103 public boolean endVisit(DependencyNode node) {
104 if (node.getParent() == null || node.getParent() == node) {
105 // dump edges on last node endVisit
106 writer.println("#");
107 for (EdgeAppender edge : edges) {
108 writer.println(edge.toString());
109 }
110 } else {
111 DependencyNode p = node.getParent();
112 // using scope as edge label.
113 edges.add(new EdgeAppender(p, node, node.getArtifact().getScope()));
114 }
115 return true;
116 }
117
118 /**
119 * {@inheritDoc}
120 */
121 @Override
122 public boolean visit(DependencyNode node) {
123 // write node
124 writer.write(generateId(node));
125 writer.write(" ");
126 writer.println(node.toNodeString());
127 return true;
128 }
129
130 /**
131 * Generate a unique id from a DependencyNode.
132 * <p>
133 * Current implementation is rather simple and uses hashcode.
134 * </p>
135 *
136 * @param node the DependencyNode to use.
137 * @return the unique id.
138 */
139 private static String generateId(DependencyNode node) {
140 return String.valueOf(node.hashCode());
141 }
142 }