All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.pmd.doc.internal.SidebarGenerator Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
/**
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */

package net.sourceforge.pmd.doc.internal;

import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.SystemUtils;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.DumperOptions.LineBreak;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.representer.Representer;

import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.rule.RuleSet;

public class SidebarGenerator {
    private static final String SIDEBAR_YML = "docs/_data/sidebars/pmd_sidebar.yml";

    private final FileWriter writer;
    private final Path sidebarPath;

    public SidebarGenerator(FileWriter writer, Path basePath) {
        this.writer = Objects.requireNonNull(writer, "A file writer must be provided");
        this.sidebarPath = Objects.requireNonNull(basePath, "A base directory must be provided").resolve(SIDEBAR_YML);
    }

    @SuppressWarnings("unchecked")
    private Map extractRuleReference(Map sidebar) {
        List> entries = (List>) sidebar.get("entries");
        Map entry = entries.get(0);
        List> folders = (List>) entry.get("folders");
        return folders.get(3);
    }

    public void generateSidebar(Map> sortedRulesets) throws IOException {
        Map sidebar = loadSidebar();
        Map ruleReference = extractRuleReference(sidebar);
        ruleReference.put("folderitems", generateRuleReferenceSection(sortedRulesets));
        writeSidebar(sidebar);
    }

    List> generateRuleReferenceSection(Map> sortedRulesets) {
        List> newFolderItems = new ArrayList<>();
        for (Map.Entry> entry : sortedRulesets.entrySet()) {
            Map newFolderItem = new LinkedHashMap<>();
            newFolderItem.put("title", null);
            newFolderItem.put("output", "web, pdf");

            Map subfolder = new LinkedHashMap<>();
            newFolderItem.put("subfolders", Arrays.asList(subfolder));

            subfolder.put("title", entry.getKey().getName() + " Rules");
            subfolder.put("output", "web, pdf");
            List> subfolderitems = new ArrayList<>();
            subfolder.put("subfolderitems", subfolderitems);

            Map ruleIndexSubfolderItem = new LinkedHashMap<>();
            ruleIndexSubfolderItem.put("title", "Index");
            ruleIndexSubfolderItem.put("output", "web, pdf");
            ruleIndexSubfolderItem.put("url", "/pmd_rules_" + entry.getKey().getId() + ".html");
            subfolderitems.add(ruleIndexSubfolderItem);

            for (RuleSet ruleset : entry.getValue()) {
                Map subfolderitem = new LinkedHashMap<>();
                subfolderitem.put("title", ruleset.getName());
                subfolderitem.put("output", "web, pdf");
                subfolderitem.put("url", "/pmd_rules_" + entry.getKey().getId() + "_" + RuleSetUtils.getRuleSetFilename(ruleset) + ".html");
                subfolderitems.add(subfolderitem);
            }

            newFolderItems.add(newFolderItem);
        }
        return newFolderItems;
    }

    public Map loadSidebar() throws IOException {
        try (Reader reader = Files.newBufferedReader(sidebarPath, StandardCharsets.UTF_8)) {
            Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
            return yaml.load(reader);
        }
    }

    public void writeSidebar(Map sidebar) throws IOException {
        DumperOptions dumperOptions = new DumperOptions();
        dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK);
        if (SystemUtils.IS_OS_WINDOWS) {
            dumperOptions.setLineBreak(LineBreak.WIN);
        }
        Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()), new Representer(dumperOptions), dumperOptions);
        writer.write(sidebarPath, Arrays.asList(yaml.dump(sidebar)));
        System.out.println("Generated " + sidebarPath);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy