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          final String indentLevel10 = SiteUtil.getNewlineAndIndentSpaces(10);
67          sink.rawText(indentLevel10);
68          sink.link(linkToParentModule);
69          sink.text(parentModule);
70          sink.link_();
71          final String indentLevel8 = SiteUtil.getNewlineAndIndentSpaces(8);
72          sink.rawText(indentLevel8);
73          sink.paragraph_();
74      }
75  
76      /**
77       * Returns relative link to the parent module for the given module class.
78       *
79       * @param parentModule parent module name.
80       * @param moduleName the module name we are looking for the parent of.
81       * @return relative link to the parent module.
82       * @throws MacroExecutionException if link to the parent module cannot be constructed.
83       */
84      private static String getLinkToParentModule(String parentModule, String moduleName)
85              throws MacroExecutionException {
86          final Path templatePath = SiteUtil.getTemplatePath(moduleName);
87          if (templatePath == null) {
88              throw new MacroExecutionException(
89                      String.format(Locale.ROOT, "Could not find template for %s", moduleName));
90          }
91          final Path templatePathParent = templatePath.getParent();
92          if (templatePathParent == null) {
93              throw new MacroExecutionException("Failed to get parent path for " + templatePath);
94          }
95          return templatePathParent
96                  .relativize(Path.of("src", "site/xdoc", "config.xml"))
97                  .toString()
98                  .replace(".xml", ".html")
99                  .replace('\\', '/')
100                 + "#" + parentModule;
101     }
102 }