1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
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          
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  
52  
53  
54  
55  
56  
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  
76  
77  
78  
79  
80  
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 }