View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.site.descriptor;
20  
21  import java.io.File;
22  import java.util.List;
23  import java.util.Locale;
24  
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.doxia.site.decoration.DecorationModel;
27  import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler;
28  import org.apache.maven.doxia.tools.SiteToolException;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugins.annotations.Component;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.apache.maven.plugins.site.AbstractSiteMojo;
33  
34  /**
35   * Abstract class to compute effective site decoration model for site descriptors.
36   *
37   * @since 3.5
38   */
39  public abstract class AbstractSiteDescriptorMojo extends AbstractSiteMojo {
40      /**
41       * The component for assembling site decoration model inheritance.
42       */
43      @Component
44      private DecorationModelInheritanceAssembler assembler;
45  
46      /**
47       * Remote repositories used for the project.
48       *
49       * todo this is used for site descriptor resolution - it should relate to the actual project but for some reason
50       *       they are not always filled in
51       */
52      @Parameter(defaultValue = "${project.remoteArtifactRepositories}", readonly = true)
53      protected List<ArtifactRepository> repositories;
54  
55      /**
56       * Directory containing the <code>site.xml</code> file and the source for hand written docs (one directory
57       * per Doxia-source-supported markup types):
58       * see <a href="/doxia/references/index.html">Doxia Markup Languages References</a>).
59       *
60       * @since 2.3
61       */
62      @Parameter(defaultValue = "${basedir}/src/site")
63      protected File siteDirectory;
64  
65      /**
66       * Make links in the site descriptor relative to the project URL.
67       * By default, any absolute links that appear in the site descriptor,
68       * e.g. banner hrefs, breadcrumbs, menu links, etc., will be made relative to project.url.
69       * <p/>
70       * Links will not be changed if this is set to false, or if the project has no URL defined.
71       *
72       * @since 2.3
73       */
74      @Parameter(property = "relativizeDecorationLinks", defaultValue = "true")
75      private boolean relativizeDecorationLinks;
76  
77      protected DecorationModel prepareDecorationModel(Locale locale) throws MojoExecutionException {
78          DecorationModel decorationModel;
79          try {
80              decorationModel = siteTool.getDecorationModel(
81                      siteDirectory, locale, project, reactorProjects, localRepository, repositories);
82          } catch (SiteToolException e) {
83              throw new MojoExecutionException("SiteToolException: " + e.getMessage(), e);
84          }
85  
86          if (relativizeDecorationLinks) {
87              final String url = project.getUrl();
88  
89              if (url == null) {
90                  getLog().warn("No project URL defined - decoration links will not be relativized!");
91              } else {
92                  List<Locale> localesList = getLocales();
93  
94                  // Default is first in the list
95                  Locale defaultLocale = localesList.get(0);
96  
97                  // MSITE-658
98                  final String localeUrl = locale.equals(defaultLocale) ? url : append(url, locale.toString());
99  
100                 getLog().info("Relativizing decoration links with respect to localized project URL: " + localeUrl);
101                 assembler.resolvePaths(decorationModel, localeUrl);
102             }
103         }
104         return decorationModel;
105     }
106 
107     private String append(String url, String path) {
108         return url.endsWith("/") ? (url + path) : (url + '/' + path);
109     }
110 }