Coverage Report - org.apache.maven.plugins.site.EffectiveSiteMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
EffectiveSiteMojo
0%
0/60
0%
0/6
2,8
 
 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  
 import java.io.StringWriter;
 25  
 import java.io.Writer;
 26  
 
 27  
 import java.text.DateFormat;
 28  
 import java.text.SimpleDateFormat;
 29  
 
 30  
 import java.util.Date;
 31  
 import java.util.List;
 32  
 import java.util.Locale;
 33  
 
 34  
 import org.apache.maven.doxia.site.decoration.DecorationModel;
 35  
 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
 36  
 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
 37  
 import org.apache.maven.plugin.MojoExecutionException;
 38  
 import org.apache.maven.plugin.MojoFailureException;
 39  
 
 40  
 import org.apache.maven.plugins.annotations.Mojo;
 41  
 import org.apache.maven.plugins.annotations.Parameter;
 42  
 import org.codehaus.plexus.util.IOUtil;
 43  
 import org.codehaus.plexus.util.StringUtils;
 44  
 import org.codehaus.plexus.util.WriterFactory;
 45  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 46  
 import org.codehaus.plexus.util.xml.XMLWriter;
 47  
 import org.codehaus.plexus.util.xml.XmlWriterUtil;
 48  
 
 49  
 /**
 50  
  * Displays the effective site descriptor as an XML for this build, after inheritance and interpolation of
 51  
  * <code>site.xml</code>.
 52  
  *
 53  
  * @author <a href="mailto:hboutemy@apache.org">HervĂ© Boutemy</a>
 54  
  * @version $Id: EffectiveSiteMojo.java 1384337 2012-09-13 13:53:19Z olamy $
 55  
  * @since 2.2
 56  
  */
 57  
 @Mojo( name = "effective-site" )
 58  0
 public class EffectiveSiteMojo
 59  
     extends AbstractSiteRenderingMojo
 60  
 {
 61  
     /**
 62  
      * Optional parameter to write the output of this help in a given file, instead of writing to the console.
 63  
      * <br/>
 64  
      * <b>Note</b>: Could be a relative path.
 65  
      */
 66  
     @Parameter( property = "output" )
 67  
     protected File output;
 68  
 
 69  
     /**
 70  
      * {@inheritDoc}
 71  
      */
 72  
     public void execute()
 73  
         throws MojoExecutionException, MojoFailureException
 74  
     {
 75  
         String effectiveSite;
 76  
 
 77  
         try
 78  
         {
 79  0
             List<Locale> localesList = siteTool.getAvailableLocales( locales );
 80  
 
 81  0
             SiteRenderingContext context = createSiteRenderingContext( localesList.get( 0 ) );
 82  
 
 83  0
             DecorationModel decorationModel = context.getDecoration();
 84  
 
 85  0
             StringWriter w = new StringWriter();
 86  0
             XMLWriter writer =
 87  
                 new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
 88  
                                           decorationModel.getModelEncoding(), null );
 89  
 
 90  0
             writeHeader( writer );
 91  
 
 92  0
             writeEffectiveSite( decorationModel, writer );
 93  
 
 94  0
             effectiveSite = w.toString();
 95  
         }
 96  0
         catch ( IOException e )
 97  
         {
 98  0
             throw new MojoExecutionException( "Error during site descriptor calculation", e );
 99  0
         }
 100  
 
 101  0
         if ( output != null )
 102  
         {
 103  
             try
 104  
             {
 105  0
                 writeXmlFile( output, effectiveSite );
 106  
             }
 107  0
             catch ( IOException e )
 108  
             {
 109  0
                 throw new MojoExecutionException( "Cannot write effective site descriptor to output: " + output, e );
 110  0
             }
 111  
 
 112  0
             if ( getLog().isInfoEnabled() )
 113  
             {
 114  0
                 getLog().info( "Effective site descriptor written to: " + output );
 115  
             }
 116  
         }
 117  
         else
 118  
         {
 119  0
             StringBuilder message = new StringBuilder();
 120  
 
 121  0
             message.append( "\nEffective site descriptor, after inheritance and interpolation:\n\n" );
 122  0
             message.append( effectiveSite );
 123  0
             message.append( "\n" );
 124  
 
 125  0
             if ( getLog().isInfoEnabled() )
 126  
             {
 127  0
                 getLog().info( message.toString() );
 128  
             }
 129  
         }
 130  0
     }
 131  
 
 132  
     /**
 133  
      * Write comments in the Effective POM/settings header.
 134  
      *
 135  
      * @param writer not null
 136  
      */
 137  
     protected static void writeHeader( XMLWriter writer )
 138  
     {
 139  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 140  0
         XmlWriterUtil.writeComment( writer, " " );
 141  
         // Use ISO8601-format for date and time
 142  0
         DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ss" );
 143  0
         XmlWriterUtil.writeComment( writer,
 144  
                                     "Generated by Maven Site Plugin on "
 145  
                                         + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
 146  0
         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-site-plugin/" );
 147  0
         XmlWriterUtil.writeComment( writer, " " );
 148  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 149  
 
 150  0
         XmlWriterUtil.writeLineBreak( writer );
 151  0
     }
 152  
 
 153  
     /**
 154  
      * Write comments in a normalize way.
 155  
      *
 156  
      * @param writer not null
 157  
      * @param comment not null
 158  
      */
 159  
     protected static void writeComment( XMLWriter writer, String comment )
 160  
     {
 161  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 162  0
         XmlWriterUtil.writeComment( writer, " " );
 163  0
         XmlWriterUtil.writeComment( writer, comment );
 164  0
         XmlWriterUtil.writeComment( writer, " " );
 165  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 166  
 
 167  0
         XmlWriterUtil.writeLineBreak( writer );
 168  0
     }
 169  
 
 170  
     private void writeEffectiveSite( DecorationModel decorationModel, XMLWriter writer )
 171  
         throws MojoExecutionException
 172  
     {
 173  
         String effectiveSite;
 174  
 
 175  0
         StringWriter sWriter = new StringWriter();
 176  0
         DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
 177  
         try
 178  
         {
 179  0
             siteWriter.write( sWriter, decorationModel );
 180  
         }
 181  0
         catch ( IOException e )
 182  
         {
 183  0
             throw new MojoExecutionException( "Cannot serialize site descriptor to XML.", e );
 184  0
         }
 185  
 
 186  0
         effectiveSite = sWriter.toString();
 187  
 
 188  0
         writeComment( writer, "Effective site descriptor for project \'" + project.getId() + "\'" );
 189  
 
 190  0
         writer.writeMarkup( effectiveSite );
 191  0
     }
 192  
 
 193  
     protected static void writeXmlFile( File output, String content )
 194  
         throws IOException
 195  
     {
 196  0
         Writer out = null;
 197  
         try
 198  
         {
 199  0
             output.getParentFile().mkdirs();
 200  
 
 201  0
             out = WriterFactory.newXmlWriter( output );
 202  
 
 203  0
             out.write( content );
 204  
 
 205  0
             out.flush();
 206  
         }
 207  
         finally
 208  
         {
 209  0
             IOUtil.close( out );
 210  0
         }
 211  0
     }
 212  
 }