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        final String indentLevel10 = SiteUtil.getNewlineAndIndentSpaces(10);
067        sink.rawText(indentLevel10);
068        sink.link(linkToParentModule);
069        sink.text(parentModule);
070        sink.link_();
071        final String indentLevel8 = SiteUtil.getNewlineAndIndentSpaces(8);
072        sink.rawText(indentLevel8);
073        sink.paragraph_();
074    }
075
076    /**
077     * Returns relative link to the parent module for the given module class.
078     *
079     * @param parentModule parent module name.
080     * @param moduleName the module name we are looking for the parent of.
081     * @return relative link to the parent module.
082     * @throws MacroExecutionException if link to the parent module cannot be constructed.
083     */
084    private static String getLinkToParentModule(String parentModule, String moduleName)
085            throws MacroExecutionException {
086        final Path templatePath = SiteUtil.getTemplatePath(moduleName);
087        if (templatePath == null) {
088            throw new MacroExecutionException(
089                    String.format(Locale.ROOT, "Could not find template for %s", moduleName));
090        }
091        final Path templatePathParent = templatePath.getParent();
092        if (templatePathParent == null) {
093            throw new MacroExecutionException("Failed to get parent path for " + templatePath);
094        }
095        return templatePathParent
096                .relativize(Path.of("src", "site/xdoc", "config.xml"))
097                .toString()
098                .replace(".xml", ".html")
099                .replace('\\', '/')
100                + "#" + parentModule;
101    }
102}