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.List;
24  import java.util.Locale;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.maven.doxia.sink.Sink;
28  import org.apache.maven.doxia.site.Menu;
29  import org.apache.maven.doxia.site.MenuItem;
30  import org.apache.maven.doxia.site.SiteModel;
31  import org.apache.maven.doxia.siterenderer.DocumentRenderingContext;
32  import org.apache.maven.doxia.siterenderer.RendererException;
33  import org.apache.maven.doxia.siterenderer.SiteRenderer;
34  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
35  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
36  import org.apache.maven.plugin.MojoExecution;
37  import org.apache.maven.plugin.logging.Log;
38  import org.codehaus.plexus.i18n.I18N;
39  
40  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
41  
42  /**
43   * Renders a sitemap report.
44   *
45   * @author ltheussl
46   *
47   * @since 2.1
48   */
49  public class SitemapDocumentRenderer implements SitePluginReportDocumentRenderer {
50      private DocumentRenderingContext docRenderingContext;
51  
52      private final String reportMojoInfo;
53  
54      String title;
55  
56      private SiteModel siteModel;
57  
58      private I18N i18n;
59  
60      private final Log log;
61  
62      public SitemapDocumentRenderer(
63              MojoExecution mojoExecution,
64              DocumentRenderingContext docRenderingContext,
65              String title,
66              SiteModel siteModel,
67              I18N i18n,
68              Log log) {
69          this.docRenderingContext = docRenderingContext;
70          this.reportMojoInfo = mojoExecution.getPlugin().getArtifactId()
71                  + ':'
72                  + mojoExecution.getPlugin().getVersion()
73                  + ':'
74                  + mojoExecution.getGoal();
75          this.title = title;
76          this.siteModel = siteModel;
77          this.i18n = i18n;
78          this.log = log;
79      }
80  
81      public void renderDocument(Writer writer, SiteRenderer siteRenderer, SiteRenderingContext siteRenderingContext)
82              throws RendererException, IOException {
83          Locale locale = siteRenderingContext.getLocale();
84  
85          String msg = "Generating \"" + buffer().strong(title) + "\" report";
86          // CHECKSTYLE_OFF: MagicNumber
87          log.info((StringUtils.rightPad(msg, 40) + buffer().strong(" --- ").mojo(reportMojoInfo)));
88          // CHECKSTYLE_ON: MagicNumber
89  
90          SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
91  
92          sink.head();
93  
94          sink.title();
95  
96          sink.text(title);
97  
98          sink.title_();
99  
100         sink.head_();
101 
102         sink.body();
103 
104         sink.section1();
105         sink.sectionTitle1();
106         sink.text(i18n.getString("site-plugin", locale, "site.sitemap.section.title"));
107         sink.sectionTitle1_();
108 
109         sink.paragraph();
110         sink.text(i18n.getString("site-plugin", locale, "site.sitemap.description"));
111         sink.paragraph_();
112 
113         for (Menu menu : siteModel.getMenus()) {
114             sink.section2();
115             sink.sectionTitle2();
116             sink.text(menu.getName());
117             sink.sectionTitle2_();
118             sink.horizontalRule();
119 
120             extractItems(menu.getItems(), sink);
121 
122             sink.section2_();
123         }
124 
125         sink.section1_();
126 
127         sink.body_();
128 
129         sink.flush();
130 
131         sink.close();
132 
133         siteRenderer.mergeDocumentIntoSite(writer, sink, siteRenderingContext);
134     }
135 
136     private static void extractItems(List<MenuItem> items, Sink sink) {
137         if (items == null || items.isEmpty()) {
138             return;
139         }
140 
141         sink.list();
142 
143         for (MenuItem item : items) {
144             sink.listItem();
145             if (item.getHref() != null) {
146                 sink.link(relativePath(item.getHref()));
147             }
148             sink.text(item.getName());
149             if (item.getHref() != null) {
150                 sink.link_();
151             }
152 
153             extractItems(item.getItems(), sink);
154             sink.listItem_();
155         }
156 
157         sink.list_();
158     }
159 
160     // sitemap.html gets generated into top-level so we only have to check leading slashes
161     private static String relativePath(String href) {
162         return href.startsWith("/") ? "." + href : href;
163     }
164 
165     public String getOutputName() {
166         return docRenderingContext.getOutputName();
167     }
168 
169     public DocumentRenderingContext getRenderingContext() {
170         return docRenderingContext;
171     }
172 
173     public boolean isOverwrite() {
174         return true;
175     }
176 
177     public boolean isExternalReport() {
178         return false;
179     }
180 
181     @Override
182     public String getReportMojoInfo() {
183         return reportMojoInfo;
184     }
185 }