1 package org.apache.maven.plugins.site;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.doxia.site.decoration.DecorationModel;
24 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
25 import org.apache.maven.doxia.tools.SiteToolException;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.project.MavenProject;
28 import org.codehaus.plexus.util.IOUtil;
29 import org.codehaus.plexus.util.ReaderFactory;
30 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.io.StringReader;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.Map;
40
41
42
43
44
45
46
47
48
49 public class SiteDescriptorAttachMojo
50 extends AbstractSiteMojo
51 {
52
53
54
55
56
57 private Artifact artifact;
58
59
60
61
62
63
64 private File basedir;
65
66 public void execute()
67 throws MojoExecutionException
68 {
69 List localesList = siteTool.getAvailableLocales( locales );
70
71 for ( Iterator iterator = localesList.iterator(); iterator.hasNext(); )
72 {
73 Locale locale = (Locale) iterator.next();
74
75 File descriptorFile = siteTool.getSiteDescriptorFromBasedir( toRelative( project.getBasedir(),
76 siteDirectory.getAbsolutePath() ),
77 basedir, locale );
78
79 if ( descriptorFile.exists() )
80 {
81 Map props = new HashMap();
82 props.put( "reports", "<menu ref=\"reports\"/>" );
83 props.put( "modules", "<menu ref=\"modules\"/>" );
84
85 DecorationModel decoration;
86 try
87 {
88 String siteDescriptorContent = IOUtil.toString( ReaderFactory.newXmlReader( descriptorFile ) );
89
90 siteDescriptorContent =
91 siteTool.getInterpolatedSiteDescriptorContent( props, project, siteDescriptorContent,
92 getInputEncoding(), getOutputEncoding() );
93
94 decoration = new DecorationXpp3Reader().read( new StringReader( siteDescriptorContent ) );
95 }
96 catch ( XmlPullParserException e )
97 {
98 throw new MojoExecutionException( "Error parsing site descriptor", e );
99 }
100 catch ( IOException e )
101 {
102 throw new MojoExecutionException( "Error reading site descriptor", e );
103 }
104 catch ( SiteToolException e )
105 {
106 throw new MojoExecutionException( "Error when interpoling site descriptor", e );
107 }
108
109 MavenProject parentProject = siteTool.getParentProject( project, reactorProjects, localRepository );
110 if ( parentProject != null && project.getUrl() != null && parentProject.getUrl() != null )
111 {
112 siteTool.populateParentMenu( decoration, locale, project, parentProject, true );
113 }
114 try
115 {
116 siteTool.populateModulesMenu( project, reactorProjects, localRepository, decoration, locale, true );
117 }
118 catch ( SiteToolException e )
119 {
120 throw new MojoExecutionException( "Error when populating modules", e );
121 }
122
123 artifact.addMetadata( new SiteDescriptorArtifactMetadata( artifact, decoration, descriptorFile ) );
124 }
125 }
126 }
127 }