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.Map; 025import java.util.Set; 026import java.util.regex.Pattern; 027 028import org.apache.maven.doxia.macro.AbstractMacro; 029import org.apache.maven.doxia.macro.Macro; 030import org.apache.maven.doxia.macro.MacroExecutionException; 031import org.apache.maven.doxia.macro.MacroRequest; 032import org.apache.maven.doxia.sink.Sink; 033import org.codehaus.plexus.component.annotations.Component; 034 035import com.puppycrawl.tools.checkstyle.api.DetailNode; 036import com.puppycrawl.tools.checkstyle.meta.JavadocMetadataScraper; 037import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 038 039/** 040 * A macro that inserts a notes subsection of module from its Javadoc. 041 */ 042@Component(role = Macro.class, hint = "notes") 043public class NotesMacro extends AbstractMacro { 044 045 /** "Notes:" line with new line accounted. */ 046 public static final Pattern NOTES_LINE_WITH_NEWLINE = Pattern.compile("\r?\n\\s?" 047 + ModuleJavadocParsingUtil.NOTES); 048 049 @Override 050 public void execute(Sink sink, MacroRequest request) throws MacroExecutionException { 051 final Path modulePath = Paths.get((String) request.getParameter("modulePath")); 052 final String moduleName = CommonUtil.getFileNameWithoutExtension(modulePath.toString()); 053 054 final Set<String> propertyNames = ModuleJavadocParsingUtil.getPropertyNames(moduleName); 055 final Map<String, DetailNode> moduleAndPropertiesJavadocs = SiteUtil.getPropertiesJavadocs( 056 propertyNames, moduleName, modulePath); 057 058 final DetailNode moduleJavadoc = moduleAndPropertiesJavadocs.get(moduleName); 059 if (moduleJavadoc == null) { 060 throw new MacroExecutionException( 061 "Javadoc of module " + moduleName + " is not found."); 062 } 063 064 final int notesStartIndex = ModuleJavadocParsingUtil 065 .getNotesSectionStartIndex(moduleJavadoc); 066 final int notesEndIndex = getNotesEndIndex(moduleJavadoc, propertyNames); 067 068 final String unprocessedModuleNotes = JavadocMetadataScraper.constructSubTreeText( 069 moduleJavadoc, notesStartIndex, notesEndIndex); 070 final String moduleNotes = NOTES_LINE_WITH_NEWLINE.matcher(unprocessedModuleNotes) 071 .replaceAll(""); 072 073 ModuleJavadocParsingUtil.writeOutJavadocPortion(moduleNotes, sink); 074 075 } 076 077 /** 078 * Gets the end index of the Notes. 079 * 080 * @param moduleJavadoc javadoc of module. 081 * @param propertyNamesSet Set with property names. 082 * @return the end index. 083 */ 084 private static int getNotesEndIndex(DetailNode moduleJavadoc, 085 Set<String> propertyNamesSet) { 086 int notesEndIndex = -1; 087 088 if (propertyNamesSet.isEmpty()) { 089 notesEndIndex += ModuleJavadocParsingUtil.getParentSectionStartIndex(moduleJavadoc); 090 } 091 else { 092 notesEndIndex += ModuleJavadocParsingUtil.getPropertySectionStartIndex( 093 moduleJavadoc, propertyNamesSet); 094 } 095 096 return notesEndIndex; 097 } 098 099}