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