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