View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.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   * A macro that inserts a notes subsection of module from its Javadoc.
40   */
41  @Component(role = Macro.class, hint = "notes")
42  public class NotesMacro extends AbstractMacro {
43  
44      /** "Notes:" line with new line accounted. */
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       * Gets the end index of the Notes.
76       *
77       * @param moduleJavadoc javadoc of module.
78       * @param propertyNamesSet Set with property names.
79       * @return the end index.
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  }