1   package org.apache.maven.plugin.changes;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import org.apache.commons.io.IOUtils;
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.artifact.factory.ArtifactFactory;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
27  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
28  import org.apache.maven.artifact.resolver.ArtifactResolver;
29  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
30  import org.apache.maven.artifact.versioning.VersionRange;
31  import org.apache.maven.doxia.sink.render.RenderingContext;
32  import org.apache.maven.doxia.site.decoration.Body;
33  import org.apache.maven.doxia.site.decoration.DecorationModel;
34  import org.apache.maven.doxia.site.decoration.Skin;
35  import org.apache.maven.doxia.siterenderer.Renderer;
36  import org.apache.maven.doxia.siterenderer.RendererException;
37  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
38  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
39  import org.apache.maven.plugin.MojoExecutionException;
40  import org.apache.maven.project.MavenProject;
41  import org.apache.maven.reporting.AbstractMavenReport;
42  import org.apache.maven.reporting.MavenReportException;
43  import org.codehaus.plexus.i18n.I18N;
44  import org.codehaus.plexus.util.ReaderFactory;
45  
46  import java.io.File;
47  import java.io.FileOutputStream;
48  import java.io.OutputStreamWriter;
49  import java.io.IOException;
50  import java.io.Writer;
51  import java.util.HashMap;
52  import java.util.Locale;
53  import java.util.Map;
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  public abstract class AbstractChangesReport
65      extends AbstractMavenReport
66  {
67      
68  
69  
70  
71  
72  
73  
74      private File outputDirectory;
75  
76      
77  
78  
79  
80  
81  
82  
83  
84      private String outputEncoding;
85  
86      
87  
88  
89  
90  
91      protected Renderer siteRenderer;
92  
93      
94  
95  
96  
97  
98  
99  
100     protected MavenProject project;
101 
102     
103 
104 
105 
106 
107 
108 
109     protected ArtifactRepository localRepository;
110 
111     
112 
113 
114     protected ArtifactResolver resolver;
115 
116     
117 
118 
119     protected ArtifactFactory factory;
120 
121     
122 
123 
124 
125 
126     protected I18N i18n;
127 
128     private File getSkinArtifactFile()
129         throws MojoExecutionException
130     {
131         Skin skin = Skin.getDefaultSkin();
132 
133         String version = skin.getVersion();
134         Artifact artifact;
135         try
136         {
137             if ( version == null )
138             {
139                 version = Artifact.RELEASE_VERSION;
140             }
141             VersionRange versionSpec = VersionRange.createFromVersionSpec( version );
142             artifact = factory.createDependencyArtifact( skin.getGroupId(), skin.getArtifactId(), versionSpec, "jar",
143                                                          null, null );
144 
145             resolver.resolve( artifact, project.getRemoteArtifactRepositories(), localRepository );
146         }
147         catch ( InvalidVersionSpecificationException e )
148         {
149             throw new MojoExecutionException( "The skin version '" + version + "' is not valid: " + e.getMessage() );
150         }
151         catch ( ArtifactResolutionException e )
152         {
153             throw new MojoExecutionException( "Unable to find skin", e );
154         }
155         catch ( ArtifactNotFoundException e )
156         {
157             throw new MojoExecutionException( "The skin does not exist: " + e.getMessage() );
158         }
159 
160         return artifact.getFile();
161     }
162 
163     public void execute()
164         throws MojoExecutionException
165     {
166         if ( !canGenerateReport() )
167         {
168             return;
169         }
170 
171         
172         FileOutputStream fileOutputStream = null;
173         try
174         {
175             DecorationModel model = new DecorationModel();
176             model.setBody( new Body() );
177             Map<String, String> attributes = new HashMap<String, String>();
178             attributes.put( "outputEncoding", getOutputEncoding() );
179             Locale locale = Locale.getDefault();
180             SiteRenderingContext siteContext = siteRenderer.createContextForSkin( getSkinArtifactFile(), attributes,
181                                                                                   model, getName( locale ), locale );
182             siteContext.setOutputEncoding( getOutputEncoding() );
183 
184             RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
185 
186             SiteRendererSink sink = new SiteRendererSink( context );
187             generate( sink, locale );
188 
189             outputDirectory.mkdirs();
190 
191             File file = new File( outputDirectory, getOutputName() + ".html" );
192             fileOutputStream = new FileOutputStream( file ) ;
193             Writer writer = new OutputStreamWriter( fileOutputStream, getOutputEncoding() );
194 
195             siteRenderer.generateDocument( writer, sink, siteContext );
196 
197             siteRenderer.copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ),
198                                         outputDirectory );
199         }
200         catch ( RendererException e )
201         {
202             throw new MojoExecutionException(
203                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
204         }
205         catch ( IOException e )
206         {
207             throw new MojoExecutionException(
208                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
209         }
210         catch ( MavenReportException e )
211         {
212             throw new MojoExecutionException(
213                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
214         }
215         finally {
216             IOUtils.closeQuietly( fileOutputStream );
217         }
218     }
219 
220     
221 
222 
223     protected String getOutputDirectory()
224     {
225         return outputDirectory.getAbsolutePath();
226     }
227 
228     
229 
230 
231 
232 
233 
234     protected String getOutputEncoding()
235     {
236         return ( outputEncoding != null ) ? outputEncoding : ReaderFactory.UTF_8;
237     }
238 
239     
240 
241 
242     protected MavenProject getProject()
243     {
244         return project;
245     }
246 
247     
248 
249 
250     protected Renderer getSiteRenderer()
251     {
252         return siteRenderer;
253     }
254 }