View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.nio.file.Paths;
24  import java.util.Locale;
25  
26  import org.apache.maven.doxia.macro.AbstractMacro;
27  import org.apache.maven.doxia.macro.Macro;
28  import org.apache.maven.doxia.macro.MacroExecutionException;
29  import org.apache.maven.doxia.macro.MacroRequest;
30  import org.apache.maven.doxia.module.xdoc.XdocSink;
31  import org.apache.maven.doxia.sink.Sink;
32  import org.codehaus.plexus.component.annotations.Component;
33  
34  /**
35   * A macro that inserts a link to the parent module.
36   */
37  @Component(role = Macro.class, hint = "parent-module")
38  public class ParentModuleMacro extends AbstractMacro {
39      @Override
40      public void execute(Sink sink, MacroRequest request) throws MacroExecutionException {
41          // until https://github.com/checkstyle/checkstyle/issues/13426
42          if (!(sink instanceof XdocSink)) {
43              throw new MacroExecutionException("Expected Sink to be an XdocSink.");
44          }
45          final String moduleName = (String) request.getParameter("moduleName");
46          final Object instance = SiteUtil.getModuleInstance(moduleName);
47          final Class<?> clss = instance.getClass();
48          createParentModuleParagraph((XdocSink) sink, clss, moduleName);
49      }
50  
51      /**
52       * Creates a paragraph with a link to the parent module.
53       *
54       * @param sink the sink to write to.
55       * @param clss the class of the module.
56       * @param moduleName the module name.
57       * @throws MacroExecutionException if the parent module cannot be found.
58       */
59      private static void createParentModuleParagraph(XdocSink sink, Class<?> clss, String moduleName)
60              throws MacroExecutionException {
61          final String parentModule = SiteUtil.getParentModule(clss);
62          final String linkToParentModule = getLinkToParentModule(parentModule, moduleName);
63  
64          sink.setInsertNewline(false);
65          sink.paragraph();
66          sink.setInsertNewline(true);
67          final String indentLevel10 = SiteUtil.getNewlineAndIndentSpaces(10);
68          sink.rawText(indentLevel10);
69          sink.link(linkToParentModule);
70          sink.text(parentModule);
71          sink.link_();
72          final String indentLevel8 = SiteUtil.getNewlineAndIndentSpaces(8);
73          sink.rawText(indentLevel8);
74          sink.paragraph_();
75      }
76  
77      /**
78       * Returns relative link to the parent module for the given module class.
79       *
80       * @param parentModule parent module name.
81       * @param moduleName the module name we are looking for the parent of.
82       * @return relative link to the parent module.
83       * @throws MacroExecutionException if link to the parent module cannot be constructed.
84       */
85      private static String getLinkToParentModule(String parentModule, String moduleName)
86              throws MacroExecutionException {
87          final Path templatePath = SiteUtil.getTemplatePath(moduleName);
88          if (templatePath == null) {
89              throw new MacroExecutionException(
90                      String.format(Locale.ROOT, "Could not find template for %s", moduleName));
91          }
92          final Path templatePathParent = templatePath.getParent();
93          if (templatePathParent == null) {
94              throw new MacroExecutionException("Failed to get parent path for " + templatePath);
95          }
96          return templatePathParent
97                  .relativize(Paths.get("src", "xdocs", "config.xml"))
98                  .toString()
99                  .replace(".xml", ".html")
100                 .replace('\\', '/')
101                 + "#" + parentModule;
102     }
103 }