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