View Javadoc
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  
23  import org.apache.maven.shared.dependency.graph.DependencyNode;
24  import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
25  
26  /**
27   * A dependency node visitor that serializes visited nodes to a writer using the
28   * <a href="https://en.wikipedia.org/wiki/GraphML">graphml format</a>.
29   *
30   * @author <a href="mailto:jerome.creignou@gmail.com">Jerome Creignou</a>
31   * @since 2.1
32   */
33  public class GraphmlDependencyNodeVisitor extends AbstractSerializingVisitor implements DependencyNodeVisitor {
34  
35      /**
36       * Graphml xml file header. Define Schema and root element. We also define 2 key as meta data.
37       */
38      private static final String GRAPHML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> "
39              + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" "
40              + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
41              + "xmlns:y=\"http://www.yworks.com/xml/graphml\" "
42              + "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns "
43              + "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">" + System.lineSeparator()
44              + "  <key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/> " + System.lineSeparator()
45              + "  <key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/> " + System.lineSeparator()
46              + "<graph id=\"dependencies\" edgedefault=\"directed\">" + System.lineSeparator();
47  
48      /**
49       * Graphml xml file footer.
50       */
51      private static final String GRAPHML_FOOTER = "</graph></graphml>";
52  
53      /**
54       * Constructor.
55       *
56       * @param writer the writer to write to.
57       */
58      public GraphmlDependencyNodeVisitor(Writer writer) {
59          super(writer);
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public boolean endVisit(DependencyNode node) {
67          if (node.getParent() == null || node.getParent() == node) {
68              writer.write(GRAPHML_FOOTER);
69          } else {
70              DependencyNode p = node.getParent();
71              writer.print("<edge source=\"" + generateId(p) + "\" target=\"" + generateId(node) + "\">");
72              if (node.getArtifact().getScope() != null) {
73                  // add Edge label
74                  writer.print("<data key=\"d1\"><y:PolyLineEdge><y:EdgeLabel>"
75                          + node.getArtifact().getScope() + "</y:EdgeLabel></y:PolyLineEdge></data>");
76              }
77              writer.println("</edge>");
78          }
79          return true;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public boolean visit(DependencyNode node) {
87          if (node.getParent() == null || node.getParent() == node) {
88              writer.write(GRAPHML_HEADER);
89          }
90          // write node
91          writer.print("<node id=\"" + generateId(node) + "\">");
92          // add node label
93          writer.print("<data key=\"d0\"><y:ShapeNode><y:NodeLabel>" + node.toNodeString()
94                  + "</y:NodeLabel></y:ShapeNode></data>");
95          writer.println("</node>");
96          return true;
97      }
98  
99      /**
100      * Generate a unique id from a DependencyNode.
101      * <p>
102      * Current implementation is rather simple and uses hashcode.
103      * </p>
104      *
105      * @param node the DependencyNode to use.
106      * @return the unique id.
107      */
108     private static String generateId(DependencyNode node) {
109         return String.valueOf(node.hashCode());
110     }
111 }