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.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
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
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
53
54
55
56
57
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
79
80
81
82
83
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 }