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.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   * A macro that inserts a list of the violation messages.
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          // until https://github.com/checkstyle/checkstyle/issues/13426
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       * Iterates through the fields of the class and creates an unordered list.
52       *
53       * @param sink the sink to write to.
54       * @param clss the class of the fields.
55       * @param messageKeys the List of message keys to iterate through.
56       */
57      private static void createListOfMessages(
58              XdocSink sink, Class<?> clss, Set<String> messageKeys) {
59          final String indentLevel8 = SiteUtil.getNewlineAndIndentSpaces(8);
60  
61          // This is a hack to prevent a newline from being inserted by the default sink.
62          // Once we get rid of the custom parser, we can remove this.
63          // until https://github.com/checkstyle/checkstyle/issues/13426
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       * Creates a list item for the given field.
77       *
78       * @param sink the sink to write to.
79       * @param clss the class of the field.
80       * @param messageKey the message key.
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          // Place the <li>.
88          sink.rawText(indentLevel10);
89          // This is a hack to prevent a newline from being inserted by the default sink.
90          // Once we get rid of the custom parser, we can remove this.
91          // until https://github.com/checkstyle/checkstyle/issues/13426
92          sink.setInsertNewline(false);
93          sink.listItem();
94          sink.setInsertNewline(true);
95  
96          // Place an <a>.
97          sink.rawText(indentLevel12);
98          sink.link(messageKeyUrl);
99          // Further indent the text.
100         sink.rawText(indentLevel14);
101         sink.rawText(messageKey);
102 
103         // Place closing </a> and </li> tags.
104         sink.rawText(indentLevel12);
105         sink.link_();
106         sink.rawText(indentLevel10);
107         sink.listItem_();
108     }
109 
110     /**
111      * Constructs a URL to GitHub that searches for the message key.
112      *
113      * @param clss the class of the module.
114      * @param messageKey the message key.
115      * @return the URL to GitHub.
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 }