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 java.io.File;
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.io.Writer;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Locale;
29 import java.util.Map;
30
31 import org.apache.commons.io.IOUtils;
32 import org.apache.maven.doxia.site.decoration.DecorationModel;
33 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
34 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
35 import org.apache.maven.doxia.tools.SiteToolException;
36 import org.apache.maven.plugin.MojoExecutionException;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.project.MavenProjectHelper;
39 import org.codehaus.plexus.util.IOUtil;
40 import org.codehaus.plexus.util.ReaderFactory;
41 import org.codehaus.plexus.util.WriterFactory;
42 import org.codehaus.plexus.util.xml.XmlStreamReader;
43 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
44
45
46
47
48
49
50
51
52
53 public class SiteDescriptorAttachMojo
54 extends AbstractSiteMojo
55 {
56
57
58
59
60
61
62 private File basedir;
63
64
65
66
67
68
69
70
71 private MavenProjectHelper projectHelper;
72
73 public void execute()
74 throws MojoExecutionException
75 {
76 List<Locale> localesList = siteTool.getAvailableLocales( locales );
77
78 for ( Locale locale : localesList )
79 {
80 File descriptorFile = siteTool.getSiteDescriptorFromBasedir( toRelative( project.getBasedir(),
81 siteDirectory.getAbsolutePath() ),
82 basedir, locale );
83
84 if ( descriptorFile.exists() )
85 {
86 Map<String, String> props = new HashMap<String, String>();
87 props.put( "reports", "<menu ref=\"reports\"/>" );
88 props.put( "modules", "<menu ref=\"modules\"/>" );
89
90 DecorationModel decoration;
91 XmlStreamReader reader = null;
92 try
93 {
94 reader = ReaderFactory.newXmlReader( descriptorFile );
95 String siteDescriptorContent = IOUtil.toString( reader );
96
97 siteDescriptorContent =
98 siteTool.getInterpolatedSiteDescriptorContent( props, project, siteDescriptorContent,
99 getInputEncoding(), getOutputEncoding() );
100
101 decoration = new DecorationXpp3Reader().read( new StringReader( siteDescriptorContent ) );
102 }
103 catch ( XmlPullParserException e )
104 {
105 throw new MojoExecutionException( "Error parsing site descriptor", e );
106 }
107 catch ( IOException e )
108 {
109 throw new MojoExecutionException( "Error reading site descriptor", e );
110 }
111 catch ( SiteToolException e )
112 {
113 throw new MojoExecutionException( "Error when interpolating site descriptor", e );
114 }
115 finally
116 {
117 IOUtils.closeQuietly( reader );
118 }
119
120 MavenProject parentProject = siteTool.getParentProject( project, reactorProjects, localRepository );
121 if ( parentProject != null && project.getUrl() != null && parentProject.getUrl() != null )
122 {
123 siteTool.populateParentMenu( decoration, locale, project, parentProject, true );
124 }
125 try
126 {
127 siteTool.populateModulesMenu( project, reactorProjects, localRepository, decoration, locale, true );
128 }
129 catch ( SiteToolException e )
130 {
131 throw new MojoExecutionException( "Error when populating modules", e );
132 }
133
134
135 String classifier = null;
136 int index = descriptorFile.getName().lastIndexOf( '.' );
137 if ( index > 0 )
138 {
139 classifier = descriptorFile.getName().substring( 0, index );
140 }
141 else
142 {
143 throw new MojoExecutionException( "Unable to determine the classifier to use" );
144 }
145
146
147 String filename = project.getArtifactId() + "-" + project.getVersion() + "-" + descriptorFile.getName();
148 File interpolatedDescriptorFile = new File( project.getBuild().getDirectory(), filename );
149 interpolatedDescriptorFile.getParentFile().mkdirs();
150
151 Writer writer = null;
152 try
153 {
154
155 writer = WriterFactory.newXmlWriter( interpolatedDescriptorFile );
156 new DecorationXpp3Writer().write( writer, decoration );
157
158 getLog().debug( "Attaching the site descriptor '" + interpolatedDescriptorFile.getAbsolutePath()
159 + "' with classifier '" + classifier + "' to the project." );
160 projectHelper.attachArtifact( project, "xml", classifier, interpolatedDescriptorFile );
161 }
162 catch ( IOException e )
163 {
164 throw new MojoExecutionException( "Unable to store interpolated site descriptor", e );
165 }
166 finally
167 {
168 IOUtils.closeQuietly( writer );
169 }
170 }
171 }
172 }
173 }