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;
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 description of module from its Javadoc.
040 */
041@Component(role = Macro.class, hint = "description")
042public class DescriptionMacro extends AbstractMacro {
043
044    @Override
045    public void execute(Sink sink, MacroRequest request) throws MacroExecutionException {
046        final Path modulePath = Paths.get((String) request.getParameter("modulePath"));
047        final String moduleName = CommonUtil.getFileNameWithoutExtension(modulePath.toString());
048
049        final Set<String> propertyNames = ModuleJavadocParsingUtil.getPropertyNames(moduleName);
050        final Map<String, DetailNode> moduleAndPropertiesJavadocs = SiteUtil.getPropertiesJavadocs(
051            propertyNames, moduleName, modulePath);
052
053        final DetailNode moduleJavadoc = moduleAndPropertiesJavadocs.get(moduleName);
054
055        final int descriptionEndIndex = getDescriptionEndIndex(moduleJavadoc, propertyNames);
056        final String moduleDescription = JavadocMetadataScraper.constructSubTreeText(
057            moduleJavadoc, 0, descriptionEndIndex);
058
059        ModuleJavadocParsingUtil.writeOutJavadocPortion(moduleDescription, sink);
060
061    }
062
063    /**
064     * Gets the end index of the description.
065     *
066     * @param moduleJavadoc javadoc of module.
067     * @param propertyNamesSet Set with property names.
068     * @return the end index.
069     */
070    private static int getDescriptionEndIndex(DetailNode moduleJavadoc,
071                                              Set<String> propertyNamesSet) {
072        int descriptionEndIndex = -1;
073
074        final int notesStartingIndex =
075            ModuleJavadocParsingUtil.getNotesSectionStartIndex(moduleJavadoc);
076        if (notesStartingIndex > -1) {
077            descriptionEndIndex += notesStartingIndex;
078        }
079        else if (propertyNamesSet.isEmpty()) {
080            descriptionEndIndex += ModuleJavadocParsingUtil.getParentSectionStartIndex(
081                moduleJavadoc);
082        }
083        else {
084            descriptionEndIndex += ModuleJavadocParsingUtil.getPropertySectionStartIndex(
085                moduleJavadoc, propertyNamesSet);
086        }
087
088        return descriptionEndIndex;
089    }
090
091}