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

Java tutorial

Introduction

Here is the source code for org.sonar.plugins.plsqltoad.PlSqlToadPageBuilder.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.lang.ArrayUtils;
import org.sonar.api.profiles.RulesProfile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public final class PlSqlToadPageBuilder {

    private PlSqlToadPageBuilder() {
    }

    public static List<PlSqlToadPage> generate(Collection<File> files, PlSqlToadPluginConfiguration pluginConf,
            RulesProfile profile) {
        cleanFolder(pluginConf.getToadOutputDirectory());

        List<PlSqlToadPage> pages = getPages(files, pluginConf, profile);
        for (PlSqlToadPage page : pages) {
            page.generate();
        }

        return pages;
    }

    private static void cleanFolder(File folder) {
        try {
            FileUtils.cleanDirectory(folder);
        } catch (IOException e) {
            // Silence please
        } catch (IllegalArgumentException e) {
            // Silence please
        }
    }

    private static List<PlSqlToadPage> getPages(Collection<File> files, PlSqlToadPluginConfiguration pluginConf,
            RulesProfile profile) {
        List<PlSqlToadPage> result = new ArrayList<PlSqlToadPage>();

        // See http://stackoverflow.com/questions/17944/how-to-round-up-the-result-of-integer-division
        int pages = (files.size() - 1) / pluginConf.getToadLimit() + 1;

        for (int page = 0; page < pages; page++) {
            PlSqlToadPage ini = new PlSqlToadPage(getPageFiles(files, pluginConf, page), pluginConf, profile, page);
            result.add(ini);
        }

        return result;
    }

    private static File[] getPageFiles(Collection<File> files, PlSqlToadPluginConfiguration pluginConf, int page) {
        int offset = page * pluginConf.getToadLimit();
        return (File[]) ArrayUtils.subarray(files.toArray(new File[files.size()]), offset,
                offset + pluginConf.getToadLimit());
    }

}