001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.site; 021 022import java.nio.file.Path; 023import java.nio.file.Paths; 024import java.util.Set; 025import java.util.regex.Pattern; 026 027import org.apache.maven.doxia.macro.AbstractMacro; 028import org.apache.maven.doxia.macro.Macro; 029import org.apache.maven.doxia.macro.MacroExecutionException; 030import org.apache.maven.doxia.macro.MacroRequest; 031import org.apache.maven.doxia.sink.Sink; 032import org.codehaus.plexus.component.annotations.Component; 033 034import com.puppycrawl.tools.checkstyle.api.DetailNode; 035import com.puppycrawl.tools.checkstyle.meta.JavadocMetadataScraper; 036import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 037 038/** 039 * A macro that inserts a notes subsection of module from its Javadoc. 040 */ 041@Component(role = Macro.class, hint = "notes") 042public class NotesMacro extends AbstractMacro { 043 044 /** "Notes:" line with new line accounted. */ 045 public static final Pattern NOTES_LINE_WITH_NEWLINE = Pattern.compile("\r?\n\\s?" 046 + ModuleJavadocParsingUtil.NOTES); 047 048 @Override 049 public void execute(Sink sink, MacroRequest request) throws MacroExecutionException { 050 final Path modulePath = Paths.get((String) request.getParameter("modulePath")); 051 final String moduleName = CommonUtil.getFileNameWithoutExtension(modulePath.toString()); 052 053 final Set<String> propertyNames = ModuleJavadocParsingUtil.getPropertyNames(moduleName); 054 055 final DetailNode moduleJavadoc = SiteUtil.getModuleJavadoc(moduleName, modulePath); 056 if (moduleJavadoc == null) { 057 throw new MacroExecutionException( 058 "Javadoc of module " + moduleName + " is not found."); 059 } 060 061 final int notesStartIndex = ModuleJavadocParsingUtil 062 .getNotesSectionStartIndex(moduleJavadoc); 063 final int notesEndIndex = getNotesEndIndex(moduleJavadoc, propertyNames); 064 065 final String unprocessedModuleNotes = JavadocMetadataScraper.constructSubTreeText( 066 moduleJavadoc, notesStartIndex, notesEndIndex); 067 final String moduleNotes = NOTES_LINE_WITH_NEWLINE.matcher(unprocessedModuleNotes) 068 .replaceAll(""); 069 070 ModuleJavadocParsingUtil.writeOutJavadocPortion(moduleNotes, sink); 071 072 } 073 074 /** 075 * Gets the end index of the Notes. 076 * 077 * @param moduleJavadoc javadoc of module. 078 * @param propertyNamesSet Set with property names. 079 * @return the end index. 080 */ 081 private static int getNotesEndIndex(DetailNode moduleJavadoc, 082 Set<String> propertyNamesSet) { 083 int notesEndIndex = -1; 084 085 if (propertyNamesSet.isEmpty()) { 086 notesEndIndex += ModuleJavadocParsingUtil.getParentSectionStartIndex(moduleJavadoc); 087 } 088 else { 089 notesEndIndex += ModuleJavadocParsingUtil.getPropertySectionStartIndex( 090 moduleJavadoc, propertyNamesSet); 091 } 092 093 return notesEndIndex; 094 } 095 096}