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.io.IOException;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.List;
28  import java.util.Locale;
29  import java.util.regex.Pattern;
30  import java.util.stream.Collectors;
31  
32  import org.apache.maven.doxia.macro.AbstractMacro;
33  import org.apache.maven.doxia.macro.Macro;
34  import org.apache.maven.doxia.macro.MacroExecutionException;
35  import org.apache.maven.doxia.macro.MacroRequest;
36  import org.apache.maven.doxia.sink.Sink;
37  import org.codehaus.plexus.component.annotations.Component;
38  
39  
40  
41  
42  @Component(role = Macro.class, hint = "example")
43  public class ExampleMacro extends AbstractMacro {
44  
45      
46      private static final String XML_CONFIG_START = "/*xml";
47  
48      
49      private static final String XML_CONFIG_END = "*/";
50  
51      
52      private static final String CODE_SNIPPET_START = "// xdoc section -- start";
53  
54      
55      private static final String CODE_SNIPPET_END = "// xdoc section -- end";
56  
57      
58      private static final Pattern XML_PATTERN = Pattern.compile(
59              "^\\s*(<!DOCTYPE\\s+.*?>|<\\?xml\\s+.*?>|<module\\s+.*?>)\\s*",
60              Pattern.DOTALL
61      );
62  
63      
64      private String lastPath = "";
65  
66      
67      private List<String> lastLines = new ArrayList<>();
68  
69      @Override
70      public void execute(Sink sink, MacroRequest request) throws MacroExecutionException {
71          final String path = (String) request.getParameter("path");
72          final String type = (String) request.getParameter("type");
73  
74          List<String> lines = lastLines;
75          if (!path.equals(lastPath)) {
76              lines = readFile("src/xdocs-examples/" + path);
77              lastPath = path;
78              lastLines = lines;
79          }
80  
81          if ("config".equals(type)) {
82              final String config = getConfigSnippet(lines);
83  
84              if (config.isBlank()) {
85                  final String message = String.format(Locale.ROOT,
86                          "Empty config snippet from %s, check"
87                                  + " for xml config snippet delimiters in input file.", path
88                  );
89                  throw new MacroExecutionException(message);
90              }
91  
92              writeSnippet(sink, config);
93          }
94          else if ("code".equals(type)) {
95              String code = getCodeSnippet(lines);
96              
97              if (path.contains("filetabcharacter")) {
98                  code = code.replace("\t", "  ");
99              }
100 
101             if (code.isBlank()) {
102                 final String message = String.format(Locale.ROOT,
103                         "Empty code snippet from %s, check"
104                                 + " for code snippet delimiters in input file.", path
105                 );
106                 throw new MacroExecutionException(message);
107             }
108 
109             writeSnippet(sink, code);
110         }
111         else if ("raw".equals(type)) {
112             final String content = String.join(ModuleJavadocParsingUtil.NEWLINE, lines);
113             writeSnippet(sink, content);
114         }
115         else {
116             final String message = String.format(Locale.ROOT, "Unknown example type: %s", type);
117             throw new MacroExecutionException(message);
118         }
119     }
120 
121     
122 
123 
124 
125 
126 
127 
128     private static List<String> readFile(String path) throws MacroExecutionException {
129         try {
130             final Path exampleFilePath = Path.of(path);
131             return Files.readAllLines(exampleFilePath);
132         }
133         catch (IOException ioException) {
134             final String message = String.format(Locale.ROOT, "Failed to read %s", path);
135             throw new MacroExecutionException(message, ioException);
136         }
137     }
138 
139     
140 
141 
142 
143 
144 
145 
146 
147     private static String getConfigSnippet(Collection<String> lines) {
148         return lines.stream()
149                 .dropWhile(line -> !XML_CONFIG_START.equals(line))
150                 .skip(1)
151                 .takeWhile(line -> !XML_CONFIG_END.equals(line))
152                 .collect(Collectors.joining(ModuleJavadocParsingUtil.NEWLINE));
153     }
154 
155     
156 
157 
158 
159 
160 
161 
162     private static String getCodeSnippet(Collection<String> lines) {
163         return lines.stream()
164                 .dropWhile(line -> !line.contains(CODE_SNIPPET_START))
165                 .skip(1)
166                 .takeWhile(line -> !line.contains(CODE_SNIPPET_END))
167                 .collect(Collectors.joining(ModuleJavadocParsingUtil.NEWLINE));
168     }
169 
170     
171 
172 
173 
174 
175 
176     private static void writeSnippet(Sink sink, String snippet) {
177         sink.rawText("<div class=\"wrapper\">");
178         final boolean isXml = isXml(snippet);
179 
180         final String languageClass;
181         if (isXml) {
182             languageClass = "language-xml";
183         }
184         else {
185             languageClass = "language-java";
186         }
187         sink.rawText("<pre class=\"prettyprint\"><code class=\"" + languageClass + "\">"
188             + ModuleJavadocParsingUtil.NEWLINE);
189         sink.rawText(escapeHtml(snippet).trim() + ModuleJavadocParsingUtil.NEWLINE);
190         sink.rawText("</code></pre>");
191         sink.rawText("</div>");
192     }
193 
194     
195 
196 
197 
198 
199 
200     private static String escapeHtml(String snippet) {
201         return snippet.replace("&", "&")
202                 .replace("<", "<")
203                 .replace(">", ">");
204     }
205 
206     
207 
208 
209 
210 
211 
212     private static boolean isXml(String snippet) {
213         return XML_PATTERN.matcher(snippet.trim()).matches();
214     }
215 }