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 expression="${basedir}/src/site"
73       * @required
74       */
75      protected File siteDirectory;
76  
77      /**
78       * The maven project.
79       *
80       * @parameter expression="${project}"
81       * @required
82       * @readonly
83       */
84      protected MavenProject project;
85  
86      /**
87       * The local repository.
88       *
89       * @parameter expression="${localRepository}"
90       */
91      protected ArtifactRepository localRepository;
92  
93      /**
94       * The reactor projects.
95       *
96       * @parameter expression="${reactorProjects}"
97       * @required
98       * @readonly
99       */
100     protected List reactorProjects;
101 
102     /**
103      * Specifies the input encoding.
104      *
105      * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
106      */
107     private String inputEncoding;
108 
109     /**
110      * Specifies the output encoding.
111      *
112      * @parameter expression="${outputEncoding}" default-value="${project.reporting.outputEncoding}"
113      */
114     private String outputEncoding;
115 
116     /**
117      * Gets the input files encoding.
118      *
119      * @return The input files encoding, never <code>null</code>.
120      */
121     protected String getInputEncoding()
122     {
123         return ( inputEncoding == null ) ? ReaderFactory.ISO_8859_1 : inputEncoding;
124     }
125 
126     /**
127      * Gets the effective reporting output files encoding.
128      *
129      * @return The effective reporting output file encoding, never <code>null</code>.
130      */
131     protected String getOutputEncoding()
132     {
133         return ( outputEncoding == null ) ? ReaderFactory.UTF_8 : outputEncoding;
134     }
135 
136     protected void populateReportItems( DecorationModel decorationModel, Locale locale, Map reportsByOutputName )
137     {
138         for ( Iterator i = decorationModel.getMenus().iterator(); i.hasNext(); )
139         {
140             Menu menu = (Menu) i.next();
141 
142             populateItemRefs( menu.getItems(), locale, reportsByOutputName );
143         }
144     }
145 
146     private void populateItemRefs( List items, Locale locale, Map reportsByOutputName )
147     {
148         for ( Iterator i = items.iterator(); i.hasNext(); )
149         {
150             MenuItem item = (MenuItem) i.next();
151 
152             if ( item.getRef() != null )
153             {
154                 if ( reportsByOutputName.containsKey( item.getRef() ) )
155                 {
156                     MavenReport report = (MavenReport) reportsByOutputName.get( item.getRef() );
157 
158                     if ( item.getName() == null )
159                     {
160                         item.setName( report.getName( locale ) );
161                     }
162 
163                     if ( item.getHref() == null || item.getHref().length() == 0 )
164                     {
165                         item.setHref( report.getOutputName() + ".html" );
166                     }
167                 }
168                 else
169                 {
170                     getLog().warn( "Unrecognised reference: '" + item.getRef() + "'" );
171                     i.remove();
172                 }
173             }
174             populateItemRefs( item.getItems(), locale, reportsByOutputName );
175         }
176     }
177 
178     /**
179      * TODO should be removed see PLXUTILS-61
180      *
181      * @param basedir
182      * @param absolutePath
183      * @return
184      */
185     protected static String toRelative( File basedir, String absolutePath )
186     {
187         String relative;
188 
189         absolutePath = absolutePath.replace( '\\', '/' );
190         String basedirPath = basedir.getAbsolutePath().replace( '\\', '/' );
191 
192         if ( absolutePath.startsWith( basedirPath ) )
193         {
194             relative = absolutePath.substring( basedirPath.length() + 1 );
195         }
196         else
197         {
198             relative = absolutePath;
199         }
200 
201         return relative;
202     }
203 }