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.util.Locale;
024
025import org.apache.maven.doxia.macro.AbstractMacro;
026import org.apache.maven.doxia.macro.Macro;
027import org.apache.maven.doxia.macro.MacroExecutionException;
028import org.apache.maven.doxia.macro.MacroRequest;
029import org.apache.maven.doxia.module.xdoc.XdocSink;
030import org.apache.maven.doxia.sink.Sink;
031import org.codehaus.plexus.component.annotations.Component;
032
033/**
034 * A macro that inserts a link to the parent module.
035 */
036@Component(role = Macro.class, hint = "parent-module")
037public class ParentModuleMacro extends AbstractMacro {
038    @Override
039    public void execute(Sink sink, MacroRequest request) throws MacroExecutionException {
040        // until https://github.com/checkstyle/checkstyle/issues/13426
041        if (!(sink instanceof XdocSink)) {
042            throw new MacroExecutionException("Expected Sink to be an XdocSink.");
043        }
044        final String moduleName = (String) request.getParameter("moduleName");
045        final Object instance = SiteUtil.getModuleInstance(moduleName);
046        final Class<?> clss = instance.getClass();
047        createParentModuleParagraph((XdocSink) sink, clss, moduleName);
048    }
049
050    /**
051     * Creates a paragraph with a link to the parent module.
052     *
053     * @param sink the sink to write to.
054     * @param clss the class of the module.
055     * @param moduleName the module name.
056     * @throws MacroExecutionException if the parent module cannot be found.
057     */
058    private static void createParentModuleParagraph(XdocSink sink, Class<?> clss, String moduleName)
059            throws MacroExecutionException {
060        final String parentModule = SiteUtil.getParentModule(clss);
061        final String linkToParentModule = getLinkToParentModule(parentModule, moduleName);
062
063        sink.setInsertNewline(false);
064        sink.paragraph();
065        sink.setInsertNewline(true);
066        sink.rawText(ModuleJavadocParsingUtil.INDENT_LEVEL_10);
067        sink.link(linkToParentModule);
068        sink.text(parentModule);
069        sink.link_();
070        sink.rawText(ModuleJavadocParsingUtil.INDENT_LEVEL_8);
071        sink.paragraph_();
072    }
073
074    /**
075     * Returns relative link to the parent module for the given module class.
076     *
077     * @param parentModule parent module name.
078     * @param moduleName the module name we are looking for the parent of.
079     * @return relative link to the parent module.
080     * @throws MacroExecutionException if link to the parent module cannot be constructed.
081     */
082    private static String getLinkToParentModule(String parentModule, String moduleName)
083            throws MacroExecutionException {
084        final Path templatePath = SiteUtil.getTemplatePath(moduleName);
085        if (templatePath == null) {
086            throw new MacroExecutionException(
087                    String.format(Locale.ROOT, "Could not find template for %s", moduleName));
088        }
089        final Path templatePathParent = templatePath.getParent();
090        if (templatePathParent == null) {
091            throw new MacroExecutionException("Failed to get parent path for " + templatePath);
092        }
093        return templatePathParent
094                .relativize(Path.of("src", "site/xdoc", "config.xml"))
095                .toString()
096                .replace(".xml", ".html")
097                .replace('\\', '/')
098                + "#" + parentModule;
099    }
100}