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