1   package org.apache.maven.model.plugin;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import org.apache.maven.model.Build;
26  import org.apache.maven.model.InputLocation;
27  import org.apache.maven.model.InputSource;
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.model.PluginManagement;
31  import org.apache.maven.model.ReportPlugin;
32  import org.apache.maven.model.ReportSet;
33  import org.apache.maven.model.Reporting;
34  import org.apache.maven.model.building.ModelBuildingRequest;
35  import org.apache.maven.model.building.ModelProblemCollector;
36  import org.apache.maven.model.building.ModelProblemCollectorRequest;
37  import org.apache.maven.model.building.ModelProblem.Severity;
38  import org.apache.maven.model.building.ModelProblem.Version;
39  import org.codehaus.plexus.util.StringUtils;
40  import org.codehaus.plexus.util.xml.Xpp3Dom;
41  
42  
43  
44  
45  
46  
47  
48  @Named
49  @Singleton
50  public class DefaultReportingConverter
51          implements ReportingConverter
52  {
53      private final InputLocation location;
54      {
55          String modelId = "org.apache.maven:maven-model-builder:"
56                  + this.getClass().getPackage().getImplementationVersion() + ":reporting-converter";
57          InputSource inputSource = new InputSource();
58          inputSource.setModelId( modelId );
59          location = new InputLocation( -1, -1, inputSource );
60          location.setLocation( 0, location );
61      }
62  
63      @Override
64      public void convertReporting( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
65      {
66          Reporting reporting = model.getReporting();
67  
68          if ( reporting == null )
69          {
70              return;
71          }
72  
73          Build build = model.getBuild();
74  
75          if ( build == null )
76          {
77              build = new Build();
78              model.setBuild( build );
79              model.setLocation( "build", location );
80          }
81  
82          Plugin sitePlugin = findSitePlugin( build );
83  
84          if ( sitePlugin == null )
85          {
86              sitePlugin = new Plugin();
87              sitePlugin.setArtifactId( "maven-site-plugin" );
88              sitePlugin.setLocation( "artifactId", location );
89              PluginManagement pluginManagement = build.getPluginManagement();
90              if ( pluginManagement == null )
91              {
92                  pluginManagement = new PluginManagement();
93                  build.setPluginManagement( pluginManagement );
94              }
95              pluginManagement.addPlugin( sitePlugin );
96          }
97  
98          Xpp3Dom configuration = (Xpp3Dom) sitePlugin.getConfiguration();
99  
100         if ( configuration == null )
101         {
102             configuration = new Xpp3Dom( "configuration", location );
103             sitePlugin.setConfiguration( configuration );
104         }
105 
106         Xpp3Dom reportPlugins = configuration.getChild( "reportPlugins" );
107 
108         if ( reportPlugins != null )
109         {
110             
111             
112             problems.add( new ModelProblemCollectorRequest( Severity.WARNING, Version.BASE )
113                     .setMessage( "Reporting configuration should be done in <reporting> section, "
114                             + "not in maven-site-plugin <configuration> as reportPlugins parameter." )
115                     .setLocation( sitePlugin.getLocation( "configuration" ) ) );
116             return;
117         }
118 
119         if ( configuration.getChild( "outputDirectory" ) == null )
120         {
121             addDom( configuration, "outputDirectory", reporting.getOutputDirectory(),
122                     reporting.getLocation( "outputDirectory" ) );
123         }
124 
125         reportPlugins = new Xpp3Dom( "reportPlugins", location );
126         configuration.addChild( reportPlugins );
127 
128         boolean hasMavenProjectInfoReportsPlugin = false;
129 
130         
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141         for ( ReportPlugin plugin : reporting.getPlugins() )
142         {
143             Xpp3Dom reportPlugin = convert( plugin );
144             reportPlugins.addChild( reportPlugin );
145 
146             if ( !reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin
147                     && "org.apache.maven.plugins".equals( plugin.getGroupId() )
148                     && "maven-project-info-reports-plugin".equals( plugin.getArtifactId() ) )
149             {
150                 hasMavenProjectInfoReportsPlugin = true;
151             }
152         }
153 
154         if ( !reporting.isExcludeDefaults() && !hasMavenProjectInfoReportsPlugin )
155         {
156             Xpp3Dom dom = new Xpp3Dom( "reportPlugin", location );
157 
158             addDom( dom, "groupId", "org.apache.maven.plugins" );
159             addDom( dom, "artifactId", "maven-project-info-reports-plugin" );
160 
161             reportPlugins.addChild( dom );
162         }
163     }
164 
165     private Plugin findSitePlugin( Build build )
166     {
167         for ( Plugin plugin : build.getPlugins() )
168         {
169             if ( isSitePlugin( plugin ) )
170             {
171                 return plugin;
172             }
173         }
174 
175         PluginManagement pluginManagement = build.getPluginManagement();
176         if ( pluginManagement != null )
177         {
178             for ( Plugin plugin : pluginManagement.getPlugins() )
179             {
180                 if ( isSitePlugin( plugin ) )
181                 {
182                     return plugin;
183                 }
184             }
185         }
186 
187         return null;
188     }
189 
190     private boolean isSitePlugin( Plugin plugin )
191     {
192         return "maven-site-plugin".equals( plugin.getArtifactId() )
193                 && "org.apache.maven.plugins".equals( plugin.getGroupId() );
194     }
195 
196     private Xpp3Dom convert( ReportPlugin plugin )
197     {
198         Xpp3Dom dom = new Xpp3Dom( "reportPlugin", plugin.getLocation( "" ) );
199 
200         addDom( dom, "groupId", plugin.getGroupId(), plugin.getLocation( "groupId" ) );
201         addDom( dom, "artifactId", plugin.getArtifactId(), plugin.getLocation( "artifactId" ) );
202         addDom( dom, "version", plugin.getVersion(), plugin.getLocation( "version" ) );
203 
204         Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
205         if ( configuration != null )
206         {
207             configuration = new Xpp3Dom( configuration );
208             dom.addChild( configuration );
209         }
210 
211         if ( !plugin.getReportSets().isEmpty() )
212         {
213             Xpp3Dom reportSets = new Xpp3Dom( "reportSets", plugin.getLocation( "reportSets" ) );
214             for ( ReportSet reportSet : plugin.getReportSets() )
215             {
216                 Xpp3Dom rs = convert( reportSet );
217                 reportSets.addChild( rs );
218             }
219             dom.addChild( reportSets );
220         }
221 
222         return dom;
223     }
224 
225     private Xpp3Dom convert( ReportSet reportSet )
226     {
227         Xpp3Dom dom = new Xpp3Dom( "reportSet", reportSet.getLocation( "" ) );
228 
229         InputLocation idLocation = reportSet.getLocation( "id" );
230         addDom( dom, "id", reportSet.getId(), idLocation == null ? location : idLocation );
231 
232         Xpp3Dom configuration = (Xpp3Dom) reportSet.getConfiguration();
233         if ( configuration != null )
234         {
235             configuration = new Xpp3Dom( configuration );
236             dom.addChild( configuration );
237         }
238 
239         if ( !reportSet.getReports().isEmpty() )
240         {
241             InputLocation location = reportSet.getLocation( "reports" );
242             Xpp3Dom reports = new Xpp3Dom( "reports", location );
243             int n = 0;
244             for ( String report : reportSet.getReports() )
245             {
246                 addDom( reports, "report", report, ( location == null ) ? null : location.getLocation( n++ ) );
247             }
248             dom.addChild( reports );
249         }
250 
251         return dom;
252     }
253 
254     private void addDom( Xpp3Dom parent, String childName, String childValue )
255     {
256         addDom( parent, childName, childValue, location );
257     }
258 
259     private void addDom( Xpp3Dom parent, String childName, String childValue, InputLocation location )
260     {
261         if ( StringUtils.isNotEmpty( childValue ) )
262         {
263             parent.addChild( newDom( childName, childValue, location ) );
264         }
265     }
266 
267     private Xpp3Dom newDom( String name, String value, InputLocation location )
268     {
269         Xpp3Dom dom = new Xpp3Dom( name, location );
270         dom.setValue( value );
271         return dom;
272     }
273 
274 }