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.Set;
25 import java.util.regex.Pattern;
26
27 import org.apache.maven.doxia.macro.AbstractMacro;
28 import org.apache.maven.doxia.macro.Macro;
29 import org.apache.maven.doxia.macro.MacroExecutionException;
30 import org.apache.maven.doxia.macro.MacroRequest;
31 import org.apache.maven.doxia.sink.Sink;
32 import org.codehaus.plexus.component.annotations.Component;
33
34 import com.puppycrawl.tools.checkstyle.api.DetailNode;
35 import com.puppycrawl.tools.checkstyle.meta.JavadocMetadataScraper;
36 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
37
38
39
40
41 @Component(role = Macro.class, hint = "notes")
42 public class NotesMacro extends AbstractMacro {
43
44
45 public static final Pattern NOTES_LINE_WITH_NEWLINE = Pattern.compile("\r?\n\\s?"
46 + ModuleJavadocParsingUtil.NOTES);
47
48 @Override
49 public void execute(Sink sink, MacroRequest request) throws MacroExecutionException {
50 final Path modulePath = Paths.get((String) request.getParameter("modulePath"));
51 final String moduleName = CommonUtil.getFileNameWithoutExtension(modulePath.toString());
52
53 final Set<String> propertyNames = ModuleJavadocParsingUtil.getPropertyNames(moduleName);
54
55 final DetailNode moduleJavadoc = SiteUtil.getModuleJavadoc(moduleName, modulePath);
56 if (moduleJavadoc == null) {
57 throw new MacroExecutionException(
58 "Javadoc of module " + moduleName + " is not found.");
59 }
60
61 final int notesStartIndex = ModuleJavadocParsingUtil
62 .getNotesSectionStartIndex(moduleJavadoc);
63 final int notesEndIndex = getNotesEndIndex(moduleJavadoc, propertyNames);
64
65 final String unprocessedModuleNotes = JavadocMetadataScraper.constructSubTreeText(
66 moduleJavadoc, notesStartIndex, notesEndIndex);
67 final String moduleNotes = NOTES_LINE_WITH_NEWLINE.matcher(unprocessedModuleNotes)
68 .replaceAll("");
69
70 ModuleJavadocParsingUtil.writeOutJavadocPortion(moduleNotes, sink);
71
72 }
73
74
75
76
77
78
79
80
81 private static int getNotesEndIndex(DetailNode moduleJavadoc,
82 Set<String> propertyNamesSet) {
83 int notesEndIndex = -1;
84
85 if (propertyNamesSet.isEmpty()) {
86 notesEndIndex += ModuleJavadocParsingUtil.getParentSectionStartIndex(moduleJavadoc);
87 }
88 else {
89 notesEndIndex += ModuleJavadocParsingUtil.getPropertySectionStartIndex(
90 moduleJavadoc, propertyNamesSet);
91 }
92
93 return notesEndIndex;
94 }
95
96 }