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