View Javadoc

1   package org.apache.maven.plugins.site;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Map;
27  
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.doxia.site.decoration.DecorationModel;
30  import org.apache.maven.doxia.site.decoration.Menu;
31  import org.apache.maven.doxia.site.decoration.MenuItem;
32  import org.apache.maven.doxia.tools.SiteTool;
33  import org.apache.maven.plugin.AbstractMojo;
34  import org.apache.maven.project.MavenProject;
35  import org.apache.maven.reporting.MavenReport;
36  import org.codehaus.plexus.i18n.I18N;
37  import org.codehaus.plexus.util.ReaderFactory;
38  
39  /**
40   * Base class for site mojos.
41   *
42   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
43   */
44  public abstract class AbstractSiteMojo
45      extends AbstractMojo
46  {
47      /**
48       * A comma separated list of locales supported by Maven. The first valid token will be the default Locale
49       * for this instance of the Java Virtual Machine.
50       *
51       * @parameter expression="${locales}"
52       */
53      protected String locales;
54  
55      /**
56       * SiteTool.
57       *
58       * @component
59       */
60      protected SiteTool siteTool;
61  
62      /**
63       * Internationalization.
64       *
65       * @component
66       */
67      protected I18N i18n;
68  
69      /**
70       * Directory containing the site.xml file and the source for apt, fml and xdoc docs.
71       *
72       * @parameter default-value="${basedir}/src/site"
73       */
74      protected File siteDirectory;
75  
76      /**
77       * The maven project.
78       *
79       * @parameter default-value="${project}"
80       * @required
81       * @readonly
82       */
83      protected MavenProject project;
84  
85      /**
86       * The local repository.
87       *
88       * @parameter default-value="${localRepository}"
89       */
90      protected ArtifactRepository localRepository;
91  
92      /**
93       * The reactor projects.
94       *
95       * @parameter default-value="${reactorProjects}"
96       * @required
97       * @readonly
98       */
99      protected List<MavenProject> reactorProjects;
100 
101     /**
102      * Specifies the input encoding.
103      *
104      * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
105      */
106     private String inputEncoding;
107 
108     /**
109      * Specifies the output encoding.
110      *
111      * @parameter expression="${outputEncoding}" default-value="${project.reporting.outputEncoding}"
112      */
113     private String outputEncoding;
114 
115     /**
116      * Gets the input files encoding.
117      *
118      * @return The input files encoding, never <code>null</code>.
119      */
120     protected String getInputEncoding()
121     {
122         return ( inputEncoding == null ) ? ReaderFactory.ISO_8859_1 : inputEncoding;
123     }
124 
125     /**
126      * Gets the effective reporting output files encoding.
127      *
128      * @return The effective reporting output file encoding, never <code>null</code>.
129      */
130     protected String getOutputEncoding()
131     {
132         return ( outputEncoding == null ) ? ReaderFactory.UTF_8 : outputEncoding;
133     }
134 
135     protected void populateReportItems( DecorationModel decorationModel, Locale locale,
136                                         Map<String, MavenReport> reportsByOutputName )
137     {
138         for ( Iterator<Menu> i = decorationModel.getMenus().iterator(); i.hasNext(); )
139         {
140             Menu menu = i.next();
141 
142             populateItemRefs( menu.getItems(), locale, reportsByOutputName );
143         }
144     }
145 
146     private void populateItemRefs( List<MenuItem> items, Locale locale, Map<String, MavenReport> reportsByOutputName )
147     {
148         for ( Iterator<MenuItem> i = items.iterator(); i.hasNext(); )
149         {
150             MenuItem item = i.next();
151 
152             if ( item.getRef() != null )
153             {
154                 MavenReport report = reportsByOutputName.get( item.getRef() );
155 
156                 if ( report != null )
157                 {
158 
159                     if ( item.getName() == null )
160                     {
161                         item.setName( report.getName( locale ) );
162                     }
163 
164                     if ( item.getHref() == null || item.getHref().length() == 0 )
165                     {
166                         item.setHref( report.getOutputName() + ".html" );
167                     }
168                 }
169                 else
170                 {
171                     getLog().warn( "Unrecognised reference: '" + item.getRef() + "'" );
172                     i.remove();
173                 }
174             }
175             populateItemRefs( item.getItems(), locale, reportsByOutputName );
176         }
177     }
178 
179     /**
180      * TODO should be removed see PLXUTILS-61
181      *
182      * @param basedir
183      * @param absolutePath
184      * @return
185      */
186     protected static String toRelative( File basedir, String absolutePath )
187     {
188         String relative;
189 
190         absolutePath = absolutePath.replace( '\\', '/' );
191         String basedirPath = basedir.getAbsolutePath().replace( '\\', '/' );
192 
193         if ( absolutePath.startsWith( basedirPath ) )
194         {
195             relative = absolutePath.substring( basedirPath.length() + 1 );
196         }
197         else
198         {
199             relative = absolutePath;
200         }
201 
202         return relative;
203     }
204 }