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.site.render;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Locale;
26  
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.maven.doxia.siterenderer.DocumentRenderingContext;
29  import org.apache.maven.doxia.siterenderer.RendererException;
30  import org.apache.maven.doxia.siterenderer.SiteRenderer;
31  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
32  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
33  import org.apache.maven.plugin.MojoExecution;
34  import org.apache.maven.plugin.logging.Log;
35  import org.apache.maven.reporting.MavenReport;
36  import org.codehaus.plexus.i18n.I18N;
37  
38  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
39  
40  /**
41   * Renders a Maven report.
42   *
43   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
44   */
45  public class CategorySummaryDocumentRenderer implements SitePluginReportDocumentRenderer {
46      private DocumentRenderingContext docRenderingContext;
47  
48      private final String reportMojoInfo;
49  
50      private String title;
51  
52      private String desc1;
53  
54      private String desc2;
55  
56      private I18N i18n;
57  
58      private List<MavenReport> categoryReports;
59  
60      private final Log log;
61  
62      public CategorySummaryDocumentRenderer(
63              MojoExecution mojoExecution,
64              DocumentRenderingContext docRenderingContext,
65              String title,
66              String desc1,
67              String desc2,
68              I18N i18n,
69              List<MavenReport> categoryReports,
70              Log log) {
71          this.docRenderingContext = docRenderingContext;
72          this.reportMojoInfo = mojoExecution.getPlugin().getArtifactId()
73                  + ':'
74                  + mojoExecution.getPlugin().getVersion()
75                  + ':'
76                  + mojoExecution.getGoal();
77          this.title = title;
78          this.desc1 = desc1;
79          this.desc2 = desc2;
80          this.i18n = i18n;
81          this.categoryReports = Collections.unmodifiableList(categoryReports);
82          this.log = log;
83      }
84  
85      public void renderDocument(Writer writer, SiteRenderer siteRenderer, SiteRenderingContext siteRenderingContext)
86              throws RendererException, IOException {
87          String msg = "Generating \"" + buffer().strong(title) + "\" report";
88          // CHECKSTYLE_OFF: MagicNumber
89          log.info((StringUtils.rightPad(msg, 40) + buffer().strong(" --- ").mojo(reportMojoInfo)));
90          // CHECKSTYLE_ON: MagicNumber
91  
92          SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
93  
94          sink.head();
95  
96          sink.title();
97  
98          sink.text(title);
99  
100         sink.title_();
101 
102         sink.head_();
103 
104         sink.body();
105 
106         sink.section1();
107         sink.sectionTitle1();
108         sink.text(title);
109         sink.sectionTitle1_();
110 
111         sink.paragraph();
112         sink.text(desc1 + " ");
113         sink.link("https://maven.apache.org");
114         sink.text("Maven");
115         sink.link_();
116         sink.text(" " + desc2);
117         sink.paragraph_();
118 
119         sink.section2();
120         sink.sectionTitle2();
121         Locale locale = siteRenderingContext.getLocale();
122         sink.text(i18n.getString("site-plugin", locale, "report.category.sectionTitle"));
123         sink.sectionTitle2_();
124 
125         sink.table();
126 
127         sink.tableRows();
128 
129         String name = i18n.getString("site-plugin", locale, "report.category.column.document");
130         String description = i18n.getString("site-plugin", locale, "report.category.column.description");
131 
132         sink.tableRow();
133 
134         sink.tableHeaderCell();
135 
136         sink.text(name);
137 
138         sink.tableHeaderCell_();
139 
140         sink.tableHeaderCell();
141 
142         sink.text(description);
143 
144         sink.tableHeaderCell_();
145 
146         sink.tableRow_();
147 
148         if (categoryReports != null) {
149             for (MavenReport report : categoryReports) {
150                 sink.tableRow();
151                 sink.tableCell();
152                 sink.link(report.getOutputName() + ".html");
153                 sink.text(report.getName(locale));
154                 sink.link_();
155                 sink.tableCell_();
156                 sink.tableCell();
157                 sink.text(report.getDescription(locale));
158                 sink.tableCell_();
159                 sink.tableRow_();
160             }
161         }
162 
163         sink.tableRows_();
164 
165         sink.table_();
166 
167         sink.section2_();
168 
169         sink.section1_();
170 
171         sink.body_();
172 
173         sink.flush();
174 
175         sink.close();
176 
177         siteRenderer.mergeDocumentIntoSite(writer, sink, siteRenderingContext);
178     }
179 
180     public String getOutputName() {
181         return docRenderingContext.getOutputName();
182     }
183 
184     public DocumentRenderingContext getRenderingContext() {
185         return docRenderingContext;
186     }
187 
188     public boolean isOverwrite() {
189         return true;
190     }
191 
192     public boolean isExternalReport() {
193         return false;
194     }
195 
196     @Override
197     public String getReportMojoInfo() {
198         return reportMojoInfo;
199     }
200 }