org.sonar.plugins.plsqltoad.PlSqlToadPage.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.plugins.plsqltoad.PlSqlToadPage.java

Source

/*
 * Sonar PL/SQL Toad Plugin
 * Copyright (C) 2012 SonarSource
 * dev@sonar.codehaus.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.plugins.plsqltoad;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.ActiveRule;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class PlSqlToadPage {

    private final File[] files;
    private final PlSqlToadPluginConfiguration pluginConf;
    private final RulesProfile profile;
    private final int page;

    private File iniFile = null;

    public PlSqlToadPage(File[] files, PlSqlToadPluginConfiguration pluginConf, RulesProfile profile, int page) {
        this.files = files.clone();
        this.pluginConf = pluginConf;
        this.profile = profile;
        this.page = page;
    }

    public File getIniFile() {
        if (iniFile == null) {
            throw new IllegalStateException("The ini file has not yet been generated!");
        }

        return iniFile;
    }

    public File getReportFile() {
        return new File(pluginConf.getToadOutputDirectory(), "CodeXpert-" + page + ".xml");
    }

    public void generate() {
        File rstFile = generateRstFile();
        this.iniFile = generateIniFile(rstFile);
    }

    private File generateRstFile() {
        String template = getTemplate("/org/sonar/plsql/toad/plugin/CodeXpert.rst.template");

        template = StringUtils.replace(template, "$title", profile.getName());
        template = StringUtils.replace(template, "$toadRules",
                getActiveRulesXml(profile.getActiveRulesByRepository(PlSqlToadPlugin.KEY_REPOSITORY_TOAD)));
        template = StringUtils.replace(template, "$toad11Rules",
                getActiveRulesXml(profile.getActiveRulesByRepository(PlSqlToadPlugin.KEY_REPOSITORY_TOAD_11)));

        File rst = new File(pluginConf.getToadOutputDirectory(), "Sonar-" + page + ".rst");
        saveToFile(rst, template);

        return rst;
    }

    private static String getActiveRulesXml(List<ActiveRule> activeRules) {
        StringBuilder sb = new StringBuilder();

        for (ActiveRule activeRule : activeRules) {
            String toadKey = PlSqlToadRuleHelper.getToadKey(activeRule);

            if (toadKey != null) {
                sb.append("    <RULE rid=\"");
                sb.append(toadKey);
                sb.append("\" />");
                sb.append(IOUtils.LINE_SEPARATOR_WINDOWS);
            }
        }

        return sb.toString();
    }

    private File generateIniFile(File rst) {
        String template = getTemplate("/org/sonar/plsql/toad/plugin/CodeXpert-"
                + (pluginConf.hasToad10Credentials() ? "w" : "wo") + "-credentials.ini.template");

        template = StringUtils.replace(template, "$rulesetName", profile.getName());
        template = StringUtils.replace(template, "$ruleset", getPath(rst));
        template = StringUtils.replace(template, "$outputDir", getPath(pluginConf.getToadOutputDirectory()));
        template = StringUtils.replace(template, "$page", String.valueOf(page));
        template = StringUtils.replace(template, "$login", pluginConf.getToad10Login());
        template = StringUtils.replace(template, "$password", pluginConf.getToad10Password());

        StringBuilder filesToAnalyze = new StringBuilder();
        int i = 1;
        for (File file : files) {
            filesToAnalyze.append("FILE").append(i++).append("=").append(getPath(file))
                    .append(IOUtils.LINE_SEPARATOR_WINDOWS);

        }
        template = StringUtils.replace(template, "$files", filesToAnalyze.toString());

        File ini = new File(pluginConf.getToadOutputDirectory(), "CodeXpert-" + page + ".ini");
        saveToFile(ini, template);

        return ini;
    }

    private static String getTemplate(String templatePath) {
        InputStream in = PlSqlToadPage.class.getResourceAsStream(templatePath);
        try {
            return IOUtils.toString(in);
        } catch (Exception e) {
            throw new PlSqlToadException("Unable to load the template located at " + templatePath, e);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    private static void saveToFile(File file, String data) {
        try {
            FileUtils.writeStringToFile(file, data);
        } catch (IOException e) {
            throw new PlSqlToadException("Cannot save the generated file : " + file, e);
        }
    }

    protected static String getPath(File file) {
        try {
            return file.getCanonicalPath().replace('\\', '/');
        } catch (IOException e) {
            throw new PlSqlToadException("Unable resolve the canonical path of " + file, e);
        }
    }

}