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