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