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.File;
22  import java.io.IOException;
23  import java.util.List;
24  import java.util.Locale;
25  
26  import org.apache.maven.doxia.module.xdoc.XdocSinkFactory;
27  import org.apache.maven.doxia.sink.Sink;
28  import org.apache.maven.doxia.site.decoration.DecorationModel;
29  import org.apache.maven.doxia.site.decoration.Menu;
30  import org.apache.maven.doxia.site.decoration.MenuItem;
31  import org.codehaus.plexus.i18n.I18N;
32  
33  /**
34   * Generate a sitemap.
35   *
36   * @author ltheussl
37   *
38   * @since 2.1
39   */
40  public class SiteMap {
41  
42      private String encoding;
43      private I18N i18n;
44  
45      /**
46       * Constructor sets default values.
47       *
48       * @param encoding the default encoding to use when writing the output file.
49       * @param i18n the default I18N for translations.
50       */
51      public SiteMap(String encoding, I18N i18n) {
52          this.encoding = encoding;
53          this.i18n = i18n;
54      }
55  
56      /**
57       * Get the value of i18n.
58       *
59       * @return the value of i18n.
60       */
61      public I18N getI18n() {
62          return i18n;
63      }
64  
65      /**
66       * Set the value of i18n.
67       *
68       * @param i18n new value of i18n.
69       */
70      public void setI18n(I18N i18n) {
71          this.i18n = i18n;
72      }
73  
74      /**
75       * Get the encoding to use when writing the output file.
76       *
77       * @return the value of encoding.
78       */
79      public String getEncoding() {
80          return encoding;
81      }
82  
83      /**
84       * Set the encoding to use when writing the output file.
85       *
86       * @param enc new value of encoding.
87       */
88      public void setEncoding(String enc) {
89          this.encoding = enc;
90      }
91  
92      /**
93       * Generates a sitemap.xml in targetDir/xdoc/.
94       * This is a valid xdoc document that can be processed by a Doxia parser.
95       * The file lists all the menus and menu items of the DecorationModel in expanded form.
96       *
97       * @param model the DecorationModel to extract the menus from.
98       * @param targetDir the target output directory. The file will be created in targetDir/xdoc/.
99       * @param locale the Locale for the result.
100      *
101      * @throws IOException if the file cannot be ceated.
102      */
103     public void generate(DecorationModel model, File targetDir, Locale locale) throws IOException {
104         File outputDir = new File(targetDir, "xdoc");
105         Sink sink = new XdocSinkFactory().createSink(outputDir, "sitemap.xml", encoding);
106 
107         try {
108             extract(model, sink, locale);
109         } finally {
110             sink.close();
111         }
112     }
113 
114     private void extract(DecorationModel decoration, Sink sink, Locale locale) {
115         sink.head();
116         sink.title();
117         sink.text(i18n.getString("site-plugin", locale, "site.sitemap.title"));
118         sink.title_();
119         sink.head_();
120         sink.body();
121 
122         sink.section1();
123         sink.sectionTitle1();
124         sink.text(i18n.getString("site-plugin", locale, "site.sitemap.section.title"));
125         sink.sectionTitle1_();
126 
127         sink.paragraph();
128         sink.text(i18n.getString("site-plugin", locale, "site.sitemap.description"));
129         sink.paragraph_();
130 
131         for (Menu menu : decoration.getMenus()) {
132             sink.section3();
133             sink.sectionTitle3();
134             sink.text(menu.getName());
135             sink.sectionTitle3_();
136             sink.horizontalRule();
137 
138             extractItems(menu.getItems(), sink);
139 
140             sink.section3_();
141         }
142 
143         sink.section1_();
144         sink.body_();
145     }
146 
147     private static void extractItems(List<MenuItem> items, Sink sink) {
148         if (items == null || items.isEmpty()) {
149             return;
150         }
151 
152         sink.list();
153 
154         for (MenuItem item : items) {
155             sink.listItem();
156             if (item.getHref() != null) {
157                 sink.link(relativePath(item.getHref()));
158                 sink.text(item.getName());
159                 sink.link_();
160             }
161 
162             extractItems(item.getItems(), sink);
163             sink.listItem_();
164         }
165 
166         sink.list_();
167     }
168 
169     // sitemap.html gets generated into top-level so we only have to check leading slashes
170     private static String relativePath(String href) {
171         return href.startsWith("/") ? "." + href : href;
172     }
173 }