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