View Javadoc
1   package org.apache.maven.plugin.dependency.analyze;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Iterator;
23  import java.util.ResourceBundle;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.doxia.sink.Sink;
27  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
28  
29  /**
30   * This is the view part of the analyze-report mojo. It generates the HTML report for the project website. The HTML
31   * output is same as the CLI output.
32   */
33  public class AnalyzeReportView
34  {
35      /**
36       * Generates the HTML report.
37       */
38      public void generateReport( ProjectDependencyAnalysis analysis, Sink sink, ResourceBundle bundle )
39      {
40          sink.head();
41          sink.title();
42          sink.text( bundle.getString( "analyze.report.header" ) );
43          sink.title_();
44          sink.head_();
45          sink.body();
46  
47          // Generate title
48          sink.section1();
49          sink.sectionTitle1();
50          sink.text( bundle.getString( "analyze.report.mainTitle" ) );
51          sink.sectionTitle1_();
52  
53          // Generate Used Declared dependencies:
54          sink.section2();
55          sink.sectionTitle2();
56          sink.text( bundle.getString( "analyze.report.UsedDeclaredDependencies" ) );
57          sink.sectionTitle2_();
58          if ( analysis.getUsedDeclaredArtifacts().isEmpty() )
59          {
60              sink.paragraph();
61              sink.text( bundle.getString( "analyze.report.noDependency" ) );
62              sink.paragraph_();
63              sink.horizontalRule();
64          }
65          else
66          {
67              generateDependenciesTable( sink, analysis.getUsedDeclaredArtifacts().iterator() );
68          }
69          sink.section2_();
70  
71          // Generate Used Undeclared dependencies:
72          sink.section2();
73          sink.sectionTitle2();
74          sink.text( bundle.getString( "analyze.report.UsedUndeclaredDependencies" ) );
75          sink.sectionTitle2_();
76          if ( analysis.getUsedUndeclaredArtifacts().isEmpty() )
77          {
78              sink.paragraph();
79              sink.text( bundle.getString( "analyze.report.noDependency" ) );
80              sink.paragraph_();
81              sink.horizontalRule();
82          }
83          else
84          {
85              generateDependenciesTable( sink, analysis.getUsedUndeclaredArtifacts().iterator() );
86          }
87          sink.section2_();
88  
89          // Generate Unused declared dependencies:
90          sink.section2();
91          sink.sectionTitle2();
92          sink.text( bundle.getString( "analyze.report.UnusedDeclaredDependencies" ) );
93          sink.sectionTitle2_();
94          if ( analysis.getUnusedDeclaredArtifacts().isEmpty() )
95          {
96              sink.paragraph();
97              sink.text( bundle.getString( "analyze.report.noDependency" ) );
98              sink.paragraph_();
99              sink.horizontalRule();
100         }
101         else
102         {
103             generateDependenciesTable( sink, analysis.getUnusedDeclaredArtifacts().iterator() );
104         }
105         sink.section2_();
106 
107         sink.section1_();
108 
109         // Closing the report
110         sink.body_();
111         sink.flush();
112         sink.close();
113     }
114 
115     /**
116      * Generate a table for the given dependencies iterator.
117      */
118     public void generateDependenciesTable( Sink sink, Iterator<Artifact> iter )
119     {
120         sink.table();
121 
122         sink.tableRow();
123         sink.tableCell();
124         sink.bold();
125         sink.text( "GroupId" );
126         sink.bold_();
127         sink.tableCell_();
128 
129         sink.tableCell();
130         sink.bold();
131         sink.text( "ArtifactId" );
132         sink.bold_();
133         sink.tableCell_();
134 
135         sink.tableCell();
136         sink.bold();
137         sink.text( "Version" );
138         sink.bold_();
139         sink.tableCell_();
140 
141         sink.tableCell();
142         sink.bold();
143         sink.text( "Scope" );
144         sink.bold_();
145         sink.tableCell_();
146 
147         sink.tableCell();
148         sink.bold();
149         sink.text( "Classifier" );
150         sink.bold_();
151         sink.tableCell_();
152 
153         sink.tableCell();
154         sink.bold();
155         sink.text( "Type" );
156         sink.bold_();
157         sink.tableCell_();
158 
159         sink.tableCell();
160         sink.bold();
161         sink.text( "Optional" );
162         sink.bold_();
163         sink.tableCell_();
164 
165         sink.tableRow_();
166         while ( iter.hasNext() )
167         {
168             Artifact artifact = iter.next();
169 
170             sink.tableRow();
171             sink.tableCell();
172             sink.text( artifact.getGroupId() );
173             sink.tableCell_();
174             sink.tableCell();
175             sink.text( artifact.getArtifactId() );
176             sink.tableCell_();
177             sink.tableCell();
178             sink.text( artifact.getVersion() );
179             sink.tableCell_();
180             sink.tableCell();
181             sink.text( artifact.getScope() );
182             sink.tableCell_();
183             sink.tableCell();
184             sink.text( artifact.getClassifier() );
185             sink.tableCell_();
186             sink.tableCell();
187             sink.text( artifact.getType() );
188             sink.tableCell_();
189             sink.tableCell();
190             if ( artifact.isOptional() )
191             {
192                 sink.text( "" );
193             }
194             else
195             {
196                 sink.text( "false" );
197             }
198 
199             sink.tableCell_();
200             sink.tableRow_();
201         }
202 
203         sink.table_();
204         sink.horizontalRule();
205     }
206 }