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 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
78
79
80
81
82
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 }