Coverage Report - org.apache.maven.plugins.site.SiteMap
 
Classes in this File Line Coverage Branch Coverage Complexity
SiteMap
0%
0/54
0%
0/8
0
 
 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$
 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  0
     {
 52  0
         this.encoding = encoding;
 53  0
         this.i18n = i18n;
 54  0
     }
 55  
 
 56  
     /**
 57  
      * Get the value of i18n.
 58  
      *
 59  
      * @return the value of i18n.
 60  
      */
 61  
     public I18N getI18n()
 62  
     {
 63  0
         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  0
         this.i18n = i18n;
 74  0
     }
 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  0
         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  0
         this.encoding = enc;
 94  0
     }
 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  0
         File outputDir = new File( targetDir, "xdoc" );
 111  0
         Sink sink = new XdocSinkFactory().createSink( outputDir, "sitemap.xml", encoding );
 112  
 
 113  
         try
 114  
         {
 115  0
             extract( model, sink, locale );
 116  
         }
 117  
         finally
 118  
         {
 119  0
             sink.close();
 120  0
         }
 121  0
     }
 122  
 
 123  
     private void extract( DecorationModel decoration, Sink sink, Locale locale )
 124  
     {
 125  0
         sink.head();
 126  0
         sink.title();
 127  0
         sink.text( i18n.getString( "site-plugin", locale, "site.sitemap.title" ) );
 128  0
         sink.title_();
 129  0
         sink.head_();
 130  0
         sink.body();
 131  
 
 132  0
         sink.section1();
 133  0
         sink.sectionTitle1();
 134  0
         sink.text( i18n.getString( "site-plugin", locale, "site.sitemap.section.title" ) );
 135  0
         sink.sectionTitle1_();
 136  
 
 137  0
         sink.paragraph();
 138  0
         sink.text( i18n.getString( "site-plugin", locale, "site.sitemap.description" ) );
 139  0
         sink.paragraph_();
 140  
 
 141  0
         for ( Iterator<Menu> it = decoration.getMenus().iterator(); it.hasNext(); )
 142  
         {
 143  0
             Menu menu = it.next();
 144  
 
 145  0
             sink.section3();
 146  0
             sink.sectionTitle3();
 147  0
             sink.text( menu.getName() );
 148  0
             sink.sectionTitle3_();
 149  0
             sink.horizontalRule();
 150  
 
 151  0
             extractItems( menu.getItems(), sink );
 152  
 
 153  0
             sink.section3_();
 154  0
         }
 155  
 
 156  0
         sink.section1_();
 157  0
         sink.body_();
 158  0
     }
 159  
 
 160  
     private void extractItems( List<MenuItem> items, Sink sink )
 161  
     {
 162  0
         if ( items == null || items.isEmpty() )
 163  
         {
 164  0
             return;
 165  
         }
 166  
 
 167  0
         sink.list();
 168  
 
 169  0
         for ( MenuItem item : items )
 170  
         {
 171  0
             sink.listItem();
 172  0
             sink.link( item.getHref() );
 173  0
             sink.text( item.getName() );
 174  0
             sink.link_();
 175  0
             sink.listItem_();
 176  
 
 177  0
             extractItems( item.getItems(), sink );
 178  
         }
 179  
 
 180  0
         sink.list_();
 181  0
     }
 182  
 }