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.IOException;
22 import java.io.UncheckedIOException;
23 import java.io.Writer;
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 href="https://en.wikipedia.org/wiki/DOT_language">DOT
31 * format</a>
32 *
33 * @author <a href="mailto:pi.songs@gmail.com">Pi Song</a>
34 * @since 2.1
35 */
36 public class DOTDependencyNodeVisitor extends AbstractSerializingVisitor implements DependencyNodeVisitor {
37
38 /**
39 * Constructor.
40 *
41 * @param writer the writer to write to
42 */
43 public DOTDependencyNodeVisitor(Writer writer) {
44 super(writer);
45 }
46
47 /**
48 * {@inheritDoc}
49 */
50 @Override
51 public boolean visit(DependencyNode node) {
52 try {
53 if (node.getParent() == null || node.getParent() == node) {
54 writer.write("digraph \"" + node.toNodeString() + "\" { " + System.lineSeparator());
55 writer.flush();
56 }
57
58 // Generate "currentNode -> Child" lines
59
60 List<DependencyNode> children = node.getChildren();
61
62 for (DependencyNode child : children) {
63 writer.write("\t\"" + node.toNodeString() + "\" -> \"" + child.toNodeString() + "\" ; "
64 + System.lineSeparator());
65 }
66 writer.flush();
67 } catch (IOException e) {
68 throw new UncheckedIOException("Failed to write DOT format output", e);
69 }
70
71 return true;
72 }
73
74 /**
75 * {@inheritDoc}
76 */
77 @Override
78 public boolean endVisit(DependencyNode node) {
79 try {
80 if (node.getParent() == null || node.getParent() == node) {
81 writer.write(" } " + System.lineSeparator());
82 writer.flush();
83 }
84 } catch (IOException e) {
85 throw new UncheckedIOException("Failed to write DOT format output", e);
86 }
87 return true;
88 }
89 }