View Javadoc

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: EffectiveSiteMojo.html 816568 2012-05-08 12:09:38Z hboutemy $
53   * @goal effective-site
54   * @since 2.2
55   */
56  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              List<Locale> localesList = siteTool.getAvailableLocales( locales );
79  
80              SiteRenderingContext context = createSiteRenderingContext( localesList.get( 0 ) );
81  
82              DecorationModel decorationModel = context.getDecoration();
83  
84              StringWriter w = new StringWriter();
85              XMLWriter writer =
86                  new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
87                                            decorationModel.getModelEncoding(), null );
88  
89              writeHeader( writer );
90  
91              writeEffectiveSite( decorationModel, writer );
92  
93              effectiveSite = w.toString();
94          }
95          catch ( IOException e )
96          {
97              throw new MojoExecutionException( "Error during site descriptor calculation", e );
98          }
99  
100         if ( output != null )
101         {
102             try
103             {
104                 writeXmlFile( output, effectiveSite );
105             }
106             catch ( IOException e )
107             {
108                 throw new MojoExecutionException( "Cannot write effective site descriptor to output: " + output, e );
109             }
110 
111             if ( getLog().isInfoEnabled() )
112             {
113                 getLog().info( "Effective site descriptor written to: " + output );
114             }
115         }
116         else
117         {
118             StringBuffer message = new StringBuffer();
119 
120             message.append( "\nEffective site descriptor, after inheritance and interpolation:\n\n" );
121             message.append( effectiveSite );
122             message.append( "\n" );
123 
124             if ( getLog().isInfoEnabled() )
125             {
126                 getLog().info( message.toString() );
127             }
128         }
129     }
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         XmlWriterUtil.writeCommentLineBreak( writer );
139         XmlWriterUtil.writeComment( writer, " " );
140         // Use ISO8601-format for date and time
141         DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ss" );
142         XmlWriterUtil.writeComment( writer,
143                                     "Generated by Maven Site Plugin on "
144                                         + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
145         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-site-plugin/" );
146         XmlWriterUtil.writeComment( writer, " " );
147         XmlWriterUtil.writeCommentLineBreak( writer );
148 
149         XmlWriterUtil.writeLineBreak( writer );
150     }
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         XmlWriterUtil.writeCommentLineBreak( writer );
161         XmlWriterUtil.writeComment( writer, " " );
162         XmlWriterUtil.writeComment( writer, comment );
163         XmlWriterUtil.writeComment( writer, " " );
164         XmlWriterUtil.writeCommentLineBreak( writer );
165 
166         XmlWriterUtil.writeLineBreak( writer );
167     }
168 
169     private void writeEffectiveSite( DecorationModel decorationModel, XMLWriter writer )
170         throws MojoExecutionException
171     {
172         String effectiveSite;
173 
174         StringWriter sWriter = new StringWriter();
175         DecorationXpp3Writer siteWriter = new DecorationXpp3Writer();
176         try
177         {
178             siteWriter.write( sWriter, decorationModel );
179         }
180         catch ( IOException e )
181         {
182             throw new MojoExecutionException( "Cannot serialize site descriptor to XML.", e );
183         }
184 
185         effectiveSite = sWriter.toString();
186 
187         writeComment( writer, "Effective site descriptor for project \'" + project.getId() + "\'" );
188 
189         writer.writeMarkup( effectiveSite );
190     }
191 
192     protected static void writeXmlFile( File output, String content )
193         throws IOException
194     {
195         Writer out = null;
196         try
197         {
198             output.getParentFile().mkdirs();
199 
200             out = WriterFactory.newXmlWriter( output );
201 
202             out.write( content );
203 
204             out.flush();
205         }
206         finally
207         {
208             IOUtil.close( out );
209         }
210     }
211 }