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