1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.report.projectinfo;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.net.URLClassLoader;
26  import java.text.MessageFormat;
27  import java.util.Collection;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Map;
32  import java.util.MissingResourceException;
33  import java.util.ResourceBundle;
34  
35  import org.apache.maven.artifact.repository.ArtifactRepository;
36  import org.apache.maven.execution.MavenSession;
37  import org.apache.maven.model.Plugin;
38  import org.apache.maven.plugins.annotations.Component;
39  import org.apache.maven.plugins.annotations.Parameter;
40  import org.apache.maven.project.MavenProject;
41  import org.apache.maven.project.ProjectBuilder;
42  import org.apache.maven.reporting.AbstractMavenReport;
43  import org.apache.maven.repository.RepositorySystem;
44  import org.apache.maven.settings.Settings;
45  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
46  import org.codehaus.plexus.i18n.I18N;
47  import org.codehaus.plexus.interpolation.EnvarBasedValueSource;
48  import org.codehaus.plexus.interpolation.InterpolationException;
49  import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
50  import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
51  import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
52  import org.codehaus.plexus.util.StringUtils;
53  import org.codehaus.plexus.util.xml.Xpp3Dom;
54  
55  
56  
57  
58  
59  
60  
61  public abstract class AbstractProjectInfoReport extends AbstractMavenReport {
62      
63      
64      
65  
66      
67  
68  
69      @Component
70      protected ArtifactResolver resolver;
71  
72      
73  
74  
75      @Component
76      RepositorySystem repositorySystem;
77  
78      
79  
80  
81      @Component
82      private I18N i18n;
83  
84      @Component
85      protected ProjectBuilder projectBuilder;
86  
87      
88      
89      
90  
91      @Parameter(defaultValue = "${session}", readonly = true, required = true)
92      private MavenSession session;
93  
94      
95  
96  
97  
98  
99      @Parameter(defaultValue = "${project.pluginArtifactRepositories}", readonly = true, required = true)
100     protected List<ArtifactRepository> pluginRepositories;
101 
102     
103 
104 
105 
106 
107     @Parameter(defaultValue = "${reactorProjects}", required = true, readonly = true)
108     protected List<MavenProject> reactorProjects;
109 
110     
111 
112 
113 
114 
115     @Parameter(defaultValue = "${settings}", readonly = true, required = true)
116     protected Settings settings;
117 
118     
119 
120 
121 
122 
123 
124     @Parameter(defaultValue = "${project.basedir}/src/site/custom/project-info-reports.properties")
125     protected String customBundle;
126 
127     
128 
129 
130 
131 
132     @Parameter(property = "mpir.skip", defaultValue = "false")
133     private boolean skip;
134 
135     
136 
137 
138 
139 
140 
141     @Parameter(defaultValue = "true")
142     protected boolean skipEmptyReport;
143 
144     
145 
146 
147 
148 
149     @Parameter
150     private List<LicenseMapping> licenseMappings;
151 
152     
153     
154     
155 
156     @Override
157     public boolean canGenerateReport() {
158         return !skip;
159     }
160 
161     @Override
162     public String getCategoryName() {
163         return CATEGORY_PROJECT_INFORMATION;
164     }
165 
166     
167     
168     
169 
170     protected Map<String, String> getLicenseMappings() {
171         Map<String, String> map = new HashMap<>();
172         if (licenseMappings != null) {
173             for (LicenseMapping mapping : licenseMappings) {
174                 for (String from : mapping.getFroms()) {
175                     map.put(from, mapping.getTo());
176                 }
177             }
178         }
179         return map;
180     }
181 
182     
183 
184 
185 
186     protected boolean isEmpty(Collection<?> coll) {
187         return coll == null || coll.isEmpty();
188     }
189 
190     @Override
191     protected MavenProject getProject() {
192         return project;
193     }
194 
195     protected MavenSession getSession() {
196         return session;
197     }
198 
199     protected List<MavenProject> getReactorProjects() {
200         return reactorProjects;
201     }
202 
203     
204 
205 
206 
207     protected Plugin getPlugin(String pluginId) {
208         if ((getProject().getBuild() == null) || (getProject().getBuild().getPluginsAsMap() == null)) {
209             return null;
210         }
211 
212         Plugin plugin = getProject().getBuild().getPluginsAsMap().get(pluginId);
213 
214         if ((plugin == null)
215                 && (getProject().getBuild().getPluginManagement() != null)
216                 && (getProject().getBuild().getPluginManagement().getPluginsAsMap() != null)) {
217             plugin = getProject()
218                     .getBuild()
219                     .getPluginManagement()
220                     .getPluginsAsMap()
221                     .get(pluginId);
222         }
223 
224         return plugin;
225     }
226 
227     
228 
229 
230 
231 
232     protected String getPluginParameter(String pluginId, String param) {
233         Plugin plugin = getPlugin(pluginId);
234         if (plugin != null) {
235             Xpp3Dom xpp3Dom = (Xpp3Dom) plugin.getConfiguration();
236             if (xpp3Dom != null
237                     && xpp3Dom.getChild(param) != null
238                     && StringUtils.isNotEmpty(xpp3Dom.getChild(param).getValue())) {
239                 return xpp3Dom.getChild(param).getValue();
240             }
241         }
242 
243         return null;
244     }
245 
246     
247 
248 
249 
250 
251     protected String getI18nString(Locale locale, String key) {
252         return getI18N(locale).getString("project-info-reports", locale, "report." + getI18Nsection() + '.' + key);
253     }
254 
255     
256 
257 
258 
259     protected I18N getI18N(Locale locale) {
260         if (customBundle != null) {
261             File customBundleFile = new File(customBundle);
262             if (customBundleFile.isFile() && customBundleFile.getName().endsWith(".properties")) {
263                 if (!i18n.getClass().isAssignableFrom(CustomI18N.class)
264                         || !i18n.getDefaultLanguage().equals(locale.getLanguage())) {
265                     
266                     i18n = new CustomI18N(project, settings, customBundleFile, locale, i18n);
267                 }
268             }
269         }
270 
271         return i18n;
272     }
273 
274     
275 
276 
277     protected abstract String getI18Nsection();
278 
279     
280     public String getName(Locale locale) {
281         return getI18nString(locale, "name");
282     }
283 
284     
285     public String getDescription(Locale locale) {
286         return getI18nString(locale, "description");
287     }
288 
289     private static class CustomI18N implements I18N {
290         private final MavenProject project;
291 
292         private final Settings settings;
293 
294         private final String bundleName;
295 
296         private final Locale locale;
297 
298         private final I18N i18nOriginal;
299 
300         private ResourceBundle bundle;
301 
302         private static final Object[] NO_ARGS = new Object[0];
303 
304         CustomI18N(MavenProject project, Settings settings, File customBundleFile, Locale locale, I18N i18nOriginal) {
305             super();
306             this.project = project;
307             this.settings = settings;
308             this.locale = locale;
309             this.i18nOriginal = i18nOriginal;
310             this.bundleName = customBundleFile
311                     .getName()
312                     .substring(0, customBundleFile.getName().indexOf(".properties"));
313 
314             URLClassLoader classLoader = null;
315             try {
316                 classLoader = new URLClassLoader(
317                         new URL[] {customBundleFile.getParentFile().toURI().toURL()}, null);
318             } catch (MalformedURLException e) {
319                 
320             }
321 
322             this.bundle = ResourceBundle.getBundle(this.bundleName, locale, classLoader);
323             if (!this.bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
324                 this.bundle = ResourceBundle.getBundle(this.bundleName, Locale.getDefault(), classLoader);
325             }
326         }
327 
328         
329         public String getDefaultLanguage() {
330             return locale.getLanguage();
331         }
332 
333         
334         public String getDefaultCountry() {
335             return locale.getCountry();
336         }
337 
338         
339         public String getDefaultBundleName() {
340             return bundleName;
341         }
342 
343         
344         public String[] getBundleNames() {
345             return new String[] {bundleName};
346         }
347 
348         
349         public ResourceBundle getBundle() {
350             return bundle;
351         }
352 
353         
354         public ResourceBundle getBundle(String bundleName) {
355             return bundle;
356         }
357 
358         
359         public ResourceBundle getBundle(String bundleName, String languageHeader) {
360             return bundle;
361         }
362 
363         
364         public ResourceBundle getBundle(String bundleName, Locale locale) {
365             return bundle;
366         }
367 
368         
369         public Locale getLocale(String languageHeader) {
370             return new Locale(languageHeader);
371         }
372 
373         
374         public String getString(String key) {
375             return getString(bundleName, locale, key);
376         }
377 
378         
379         public String getString(String key, Locale locale) {
380             return getString(bundleName, locale, key);
381         }
382 
383         
384         public String getString(String bundleName, Locale locale, String key) {
385             String value;
386 
387             if (locale == null) {
388                 locale = getLocale(null);
389             }
390 
391             ResourceBundle rb = getBundle(bundleName, locale);
392             value = getStringOrNull(rb, key);
393 
394             if (value == null) {
395                 
396                 value = i18nOriginal.getString(bundleName, locale, key);
397             }
398 
399             if (!value.contains("${")) {
400                 return value;
401             }
402 
403             final RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
404             try {
405                 interpolator.addValueSource(new EnvarBasedValueSource());
406             } catch (final IOException e) {
407                 
408             }
409 
410             interpolator.addValueSource(new PropertiesBasedValueSource(System.getProperties()));
411             interpolator.addValueSource(new PropertiesBasedValueSource(project.getProperties()));
412             interpolator.addValueSource(new PrefixedObjectValueSource("project", project));
413             interpolator.addValueSource(new PrefixedObjectValueSource("pom", project));
414             interpolator.addValueSource(new PrefixedObjectValueSource("settings", settings));
415 
416             try {
417                 value = interpolator.interpolate(value);
418             } catch (final InterpolationException e) {
419                 
420             }
421 
422             return value;
423         }
424 
425         
426         public String format(String key, Object arg1) {
427             return format(bundleName, locale, key, new Object[] {arg1});
428         }
429 
430         
431         public String format(String key, Object arg1, Object arg2) {
432             return format(bundleName, locale, key, new Object[] {arg1, arg2});
433         }
434 
435         
436         public String format(String bundleName, Locale locale, String key, Object arg1) {
437             return format(bundleName, locale, key, new Object[] {arg1});
438         }
439 
440         
441         public String format(String bundleName, Locale locale, String key, Object arg1, Object arg2) {
442             return format(bundleName, locale, key, new Object[] {arg1, arg2});
443         }
444 
445         
446         public String format(String bundleName, Locale locale, String key, Object[] args) {
447             if (locale == null) {
448                 locale = getLocale(null);
449             }
450 
451             String value = getString(bundleName, locale, key);
452             if (args == null) {
453                 args = NO_ARGS;
454             }
455 
456             MessageFormat messageFormat = new MessageFormat("");
457             messageFormat.setLocale(locale);
458             messageFormat.applyPattern(value);
459 
460             return messageFormat.format(args);
461         }
462 
463         private String getStringOrNull(ResourceBundle rb, String key) {
464             if (rb != null) {
465                 try {
466                     return rb.getString(key);
467                 } catch (MissingResourceException ignored) {
468                     
469                 }
470             }
471             return null;
472         }
473     }
474 }