View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.site;
21  
22  import java.nio.file.Path;
23  import java.util.Locale;
24  
25  import org.apache.maven.doxia.macro.AbstractMacro;
26  import org.apache.maven.doxia.macro.Macro;
27  import org.apache.maven.doxia.macro.MacroExecutionException;
28  import org.apache.maven.doxia.macro.MacroRequest;
29  import org.apache.maven.doxia.module.xdoc.XdocSink;
30  import org.apache.maven.doxia.sink.Sink;
31  import org.codehaus.plexus.component.annotations.Component;
32  
33  /**
34   * A macro that inserts a link to the parent module.
35   */
36  @Component(role = Macro.class, hint = "parent-module")
37  public class ParentModuleMacro extends AbstractMacro {
38      @Override
39      public void execute(Sink sink, MacroRequest request) throws MacroExecutionException {
40          // until https://github.com/checkstyle/checkstyle/issues/13426
41          if (!(sink instanceof XdocSink)) {
42              throw new MacroExecutionException("Expected Sink to be an XdocSink.");
43          }
44          final String moduleName = (String) request.getParameter("moduleName");
45          final Object instance = SiteUtil.getModuleInstance(moduleName);
46          final Class<?> clss = instance.getClass();
47          createParentModuleParagraph((XdocSink) sink, clss, moduleName);
48      }
49  
50      /**
51       * Creates a paragraph with a link to the parent module.
52       *
53       * @param sink the sink to write to.
54       * @param clss the class of the module.
55       * @param moduleName the module name.
56       * @throws MacroExecutionException if the parent module cannot be found.
57       */
58      private static void createParentModuleParagraph(XdocSink sink, Class<?> clss, String moduleName)
59              throws MacroExecutionException {
60          final String parentModule = SiteUtil.getParentModule(clss);
61          final String linkToParentModule = getLinkToParentModule(parentModule, moduleName);
62  
63          sink.setInsertNewline(false);
64          sink.paragraph();
65          sink.setInsertNewline(true);
66          sink.rawText(ModuleJavadocParsingUtil.INDENT_LEVEL_10);
67          sink.link(linkToParentModule);
68          sink.text(parentModule);
69          sink.link_();
70          sink.rawText(ModuleJavadocParsingUtil.INDENT_LEVEL_8);
71          sink.paragraph_();
72      }
73  
74      /**
75       * Returns relative link to the parent module for the given module class.
76       *
77       * @param parentModule parent module name.
78       * @param moduleName the module name we are looking for the parent of.
79       * @return relative link to the parent module.
80       * @throws MacroExecutionException if link to the parent module cannot be constructed.
81       */
82      private static String getLinkToParentModule(String parentModule, String moduleName)
83              throws MacroExecutionException {
84          final Path templatePath = SiteUtil.getTemplatePath(moduleName);
85          if (templatePath == null) {
86              throw new MacroExecutionException(
87                      String.format(Locale.ROOT, "Could not find template for %s", moduleName));
88          }
89          final Path templatePathParent = templatePath.getParent();
90          if (templatePathParent == null) {
91              throw new MacroExecutionException("Failed to get parent path for " + templatePath);
92          }
93          return templatePathParent
94                  .relativize(Path.of("src", "site/xdoc", "config.xml"))
95                  .toString()
96                  .replace(".xml", ".html")
97                  .replace('\\', '/')
98                  + "#" + parentModule;
99      }
100 }