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.util.Set;
23
24 import org.apache.maven.doxia.macro.AbstractMacro;
25 import org.apache.maven.doxia.macro.Macro;
26 import org.apache.maven.doxia.macro.MacroExecutionException;
27 import org.apache.maven.doxia.macro.MacroRequest;
28 import org.apache.maven.doxia.module.xdoc.XdocSink;
29 import org.apache.maven.doxia.sink.Sink;
30 import org.codehaus.plexus.component.annotations.Component;
31
32
33
34
35 @Component(role = Macro.class, hint = "violation-messages")
36 public class ViolationMessagesMacro extends AbstractMacro {
37 @Override
38 public void execute(Sink sink, MacroRequest request) throws MacroExecutionException {
39
40 if (!(sink instanceof XdocSink)) {
41 throw new MacroExecutionException("Expected Sink to be an XdocSink.");
42 }
43 final String checkName = (String) request.getParameter("checkName");
44 final Object instance = SiteUtil.getModuleInstance(checkName);
45 final Class<?> clss = instance.getClass();
46 final Set<String> messageKeys = SiteUtil.getMessageKeys(clss);
47 createListOfMessages((XdocSink) sink, clss, messageKeys);
48 }
49
50
51
52
53
54
55
56
57 private static void createListOfMessages(
58 XdocSink sink, Class<?> clss, Set<String> messageKeys) {
59 final String indentLevel8 = SiteUtil.getNewlineAndIndentSpaces(8);
60
61
62
63
64 sink.setInsertNewline(false);
65 sink.list();
66 sink.setInsertNewline(true);
67
68 for (String messageKey : messageKeys) {
69 createListItem(sink, clss, messageKey);
70 }
71 sink.rawText(indentLevel8);
72 sink.list_();
73 }
74
75
76
77
78
79
80
81
82 private static void createListItem(XdocSink sink, Class<?> clss, String messageKey) {
83 final String messageKeyUrl = constructMessageKeyUrl(clss, messageKey);
84 final String indentLevel10 = SiteUtil.getNewlineAndIndentSpaces(10);
85 final String indentLevel12 = SiteUtil.getNewlineAndIndentSpaces(12);
86 final String indentLevel14 = SiteUtil.getNewlineAndIndentSpaces(14);
87
88 sink.rawText(indentLevel10);
89
90
91
92 sink.setInsertNewline(false);
93 sink.listItem();
94 sink.setInsertNewline(true);
95
96
97 sink.rawText(indentLevel12);
98 sink.link(messageKeyUrl);
99
100 sink.rawText(indentLevel14);
101 sink.rawText(messageKey);
102
103
104 sink.rawText(indentLevel12);
105 sink.link_();
106 sink.rawText(indentLevel10);
107 sink.listItem_();
108 }
109
110
111
112
113
114
115
116
117 private static String constructMessageKeyUrl(Class<?> clss, String messageKey) {
118 return "https://github.com/search?q="
119 + "path%3Asrc%2Fmain%2Fresources%2F"
120 + clss.getPackage().getName().replace(".", "%2F")
121 + "%20path%3A**%2Fmessages*.properties+repo%3Acheckstyle%2F"
122 + "checkstyle+%22" + messageKey + "%22";
123 }
124 }