View Javadoc

1   package org.apache.maven.plugins.site;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Generate a sitemap.
38   *
39   * @author ltheussl
40   * @version $Id: SiteMap.html 816568 2012-05-08 12:09:38Z hboutemy $
41   * @since 2.1
42   */
43  public class SiteMap
44   {
45  
46      private String encoding;
47      private I18N i18n;
48  
49      /**
50       * Constructor sets default values.
51       *
52       * @param encoding the default encoding to use when writing the output file.
53       * @param i18n the default I18N for translations.
54       */
55      public SiteMap( String encoding, I18N i18n )
56      {
57          this.encoding = encoding;
58          this.i18n = i18n;
59      }
60  
61      /**
62       * Get the value of i18n.
63       *
64       * @return the value of i18n.
65       */
66      public I18N getI18n()
67      {
68          return i18n;
69      }
70  
71      /**
72       * Set the value of i18n.
73       *
74       * @param i18n new value of i18n.
75       */
76      public void setI18n( I18N i18n )
77      {
78          this.i18n = i18n;
79      }
80  
81      /**
82       * Get the encoding to use when writing the output file.
83       *
84       * @return the value of encoding.
85       */
86      public String getEncoding()
87      {
88          return encoding;
89      }
90  
91      /**
92       * Set the encoding to use when writing the output file.
93       *
94       * @param enc new value of encoding.
95       */
96      public void setEncoding( String enc )
97      {
98          this.encoding = enc;
99      }
100 
101     /**
102      * Generates a sitemap.xml in targetDir/xdoc/.
103      * This is a valid xdoc document that can be processed by a Doxia parser.
104      * The file lists all the menus and menu items of the DecorationModel in expanded form.
105      *
106      * @param model the DecorationModel to extract the menus from.
107      * @param targetDir the target output directory. The file will be created in targetDir/xdoc/.
108      * @param locale the Locale for the result.
109      *
110      * @throws IOException if the file cannot be ceated.
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     // sitemap.html gets generated into top-level so we only have to check leading slashes
186     private static String relativePath( String href )
187     {
188         return href.startsWith( "/" ) ? "." + href : href;
189     }
190 }