1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
46
47
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 @Override
82 public void renderDocument(Writer writer, SiteRenderer siteRenderer, SiteRenderingContext siteRenderingContext)
83 throws RendererException, IOException {
84 Locale locale = siteRenderingContext.getLocale();
85
86 String msg = "Generating \"" + buffer().strong(title) + "\" report";
87
88 log.info((StringUtils.rightPad(msg, 40) + buffer().strong(" --- ").mojo(reportMojoInfo)));
89
90
91 SiteRendererSink sink = new SiteRendererSink(docRenderingContext);
92
93 sink.head();
94
95 sink.title();
96
97 sink.text(title);
98
99 sink.title_();
100
101 sink.head_();
102
103 sink.body();
104
105 sink.section1();
106 sink.sectionTitle1();
107 sink.text(i18n.getString("site-plugin", locale, "site.sitemap.section.title"));
108 sink.sectionTitle1_();
109
110 sink.paragraph();
111 sink.text(i18n.getString("site-plugin", locale, "site.sitemap.description"));
112 sink.paragraph_();
113
114 for (Menu menu : siteModel.getMenus()) {
115 sink.section2();
116 sink.sectionTitle2();
117 sink.text(menu.getName());
118 sink.sectionTitle2_();
119 sink.horizontalRule();
120
121 extractItems(menu.getItems(), sink);
122
123 sink.section2_();
124 }
125
126 sink.section1_();
127
128 sink.body_();
129
130 sink.flush();
131
132 sink.close();
133
134 siteRenderer.mergeDocumentIntoSite(writer, sink, siteRenderingContext);
135 }
136
137 private static void extractItems(List<MenuItem> items, Sink sink) {
138 if (items == null || items.isEmpty()) {
139 return;
140 }
141
142 sink.list();
143
144 for (MenuItem item : items) {
145 sink.listItem();
146 if (item.getHref() != null) {
147 sink.link(relativePath(item.getHref()));
148 }
149 sink.text(item.getName());
150 if (item.getHref() != null) {
151 sink.link_();
152 }
153
154 extractItems(item.getItems(), sink);
155 sink.listItem_();
156 }
157
158 sink.list_();
159 }
160
161
162 private static String relativePath(String href) {
163 return href.startsWith("/") ? "." + href : href;
164 }
165
166 @Override
167 public String getOutputName() {
168 return docRenderingContext.getOutputName();
169 }
170
171 @Override
172 public String getOutputPath() {
173 return getOutputName();
174 }
175
176 @Override
177 public DocumentRenderingContext getRenderingContext() {
178 return docRenderingContext;
179 }
180
181 @Override
182 public boolean isOverwrite() {
183 return true;
184 }
185
186 @Override
187 public boolean isExternalReport() {
188 return false;
189 }
190
191 @Override
192 public String getReportMojoInfo() {
193 return reportMojoInfo;
194 }
195 }