View Javadoc
1   package org.apache.maven.plugins.site.descriptor;
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.List;
24  import java.util.Locale;
25  
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.doxia.site.decoration.DecorationModel;
28  import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler;
29  import org.apache.maven.doxia.tools.SiteToolException;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugins.annotations.Component;
32  import org.apache.maven.plugins.annotations.Parameter;
33  import org.apache.maven.plugins.site.AbstractSiteMojo;
34  
35  /**
36   * Abstract class to compute effective site decoration model for site descriptors.
37   *
38   * @since 3.5
39   */
40  public abstract class AbstractSiteDescriptorMojo
41      extends AbstractSiteMojo
42  {
43      /**
44       * The component for assembling site decoration model inheritance.
45       */
46      @Component
47      private DecorationModelInheritanceAssembler assembler;
48  
49      /**
50       * Remote repositories used for the project.
51       *
52       * @todo this is used for site descriptor resolution - it should relate to the actual project but for some reason
53       *       they are not always filled in
54       */
55      @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true )
56      protected List<ArtifactRepository> repositories;
57  
58      /**
59       * Directory containing the <code>site.xml</code> file and the source for hand written docs (one directory
60       * per Doxia-source-supported markup types):
61       * see <a href="/doxia/references/index.html">Doxia Markup Languages References</a>).
62       *
63       * @since 2.3
64       */
65      @Parameter( defaultValue = "${basedir}/src/site" )
66      protected File siteDirectory;
67  
68      /**
69       * Make links in the site descriptor relative to the project URL.
70       * By default, any absolute links that appear in the site descriptor,
71       * e.g. banner hrefs, breadcrumbs, menu links, etc., will be made relative to project.url.
72       * <p/>
73       * Links will not be changed if this is set to false, or if the project has no URL defined.
74       *
75       * @since 2.3
76       */
77      @Parameter( property = "relativizeDecorationLinks", defaultValue = "true" )
78      private boolean relativizeDecorationLinks;
79  
80      protected DecorationModel prepareDecorationModel( Locale locale )
81          throws MojoExecutionException
82      {
83          DecorationModel decorationModel;
84          try
85          {
86              decorationModel = siteTool.getDecorationModel( siteDirectory, locale, project, reactorProjects,
87                                                             localRepository, repositories );
88          }
89          catch ( SiteToolException e )
90          {
91              throw new MojoExecutionException( "SiteToolException: " + e.getMessage(), e );
92          }
93  
94          if ( relativizeDecorationLinks )
95          {
96              final String url = project.getUrl();
97  
98              if ( url == null )
99              {
100                 getLog().warn( "No project URL defined - decoration links will not be relativized!" );
101             }
102             else
103             {
104                 List<Locale> localesList = getLocales();
105 
106                 // Default is first in the list
107                 Locale defaultLocale = localesList.get( 0 );
108 
109                 // MSITE-658
110                 final String localeUrl = locale.equals( defaultLocale ) ? url : append( url, locale.getLanguage() );
111 
112                 getLog().info( "Relativizing decoration links with respect to localized project URL: " + localeUrl );
113                 assembler.resolvePaths( decorationModel, localeUrl );
114             }
115         }
116         return decorationModel;
117     }
118 
119     private String append( String url, String path )
120     {
121         return url.endsWith( "/" ) ? ( url + path ) : ( url + '/' + path );
122     }
123 }