1 package org.apache.maven.plugins.site.render;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import java.util.List;
26 import java.util.Locale;
27
28 import org.apache.maven.doxia.module.xdoc.XdocSinkFactory;
29 import org.apache.maven.doxia.sink.Sink;
30 import org.apache.maven.doxia.site.decoration.DecorationModel;
31 import org.apache.maven.doxia.site.decoration.Menu;
32 import org.apache.maven.doxia.site.decoration.MenuItem;
33
34 import org.codehaus.plexus.i18n.I18N;
35
36
37
38
39
40
41
42
43 public class SiteMap
44 {
45
46 private String encoding;
47 private I18N i18n;
48
49
50
51
52
53
54
55 public SiteMap( String encoding, I18N i18n )
56 {
57 this.encoding = encoding;
58 this.i18n = i18n;
59 }
60
61
62
63
64
65
66 public I18N getI18n()
67 {
68 return i18n;
69 }
70
71
72
73
74
75
76 public void setI18n( I18N i18n )
77 {
78 this.i18n = i18n;
79 }
80
81
82
83
84
85
86 public String getEncoding()
87 {
88 return encoding;
89 }
90
91
92
93
94
95
96 public void setEncoding( String enc )
97 {
98 this.encoding = enc;
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112 public void generate( DecorationModel model, File targetDir, Locale locale )
113 throws IOException
114 {
115 File outputDir = new File( targetDir, "xdoc" );
116 Sink sink = new XdocSinkFactory().createSink( outputDir, "sitemap.xml", encoding );
117
118 try
119 {
120 extract( model, sink, locale );
121 }
122 finally
123 {
124 sink.close();
125 }
126 }
127
128 private void extract( DecorationModel decoration, Sink sink, Locale locale )
129 {
130 sink.head();
131 sink.title();
132 sink.text( i18n.getString( "site-plugin", locale, "site.sitemap.title" ) );
133 sink.title_();
134 sink.head_();
135 sink.body();
136
137 sink.section1();
138 sink.sectionTitle1();
139 sink.text( i18n.getString( "site-plugin", locale, "site.sitemap.section.title" ) );
140 sink.sectionTitle1_();
141
142 sink.paragraph();
143 sink.text( i18n.getString( "site-plugin", locale, "site.sitemap.description" ) );
144 sink.paragraph_();
145
146 for ( Menu menu : decoration.getMenus() )
147 {
148 sink.section3();
149 sink.sectionTitle3();
150 sink.text( menu.getName() );
151 sink.sectionTitle3_();
152 sink.horizontalRule();
153
154 extractItems( menu.getItems(), sink );
155
156 sink.section3_();
157 }
158
159 sink.section1_();
160 sink.body_();
161 }
162
163 private static void extractItems( List<MenuItem> items, Sink sink )
164 {
165 if ( items == null || items.isEmpty() )
166 {
167 return;
168 }
169
170 sink.list();
171
172 for ( MenuItem item : items )
173 {
174 sink.listItem();
175 sink.link( relativePath( item.getHref() ) );
176 sink.text( item.getName() );
177 sink.link_();
178 extractItems( item.getItems(), sink );
179 sink.listItem_();
180 }
181
182 sink.list_();
183 }
184
185
186 private static String relativePath( String href )
187 {
188 return href.startsWith( "/" ) ? "." + href : href;
189 }
190 }