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

org.sonar.plugins.plsqltoad.PlSqlToadPageBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Sonar PL/SQL Toad Plugin
 * Copyright (C) 2012 SonarSource
 * [email protected]
 *
 * 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 generate(Collection files, PlSqlToadPluginConfiguration pluginConf, RulesProfile profile) {
    cleanFolder(pluginConf.getToadOutputDirectory());

    List 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 getPages(Collection files, PlSqlToadPluginConfiguration pluginConf, RulesProfile profile) {
    List result = new ArrayList();

    // 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 files, PlSqlToadPluginConfiguration pluginConf, int page) {
    int offset = page * pluginConf.getToadLimit();
    return (File[]) ArrayUtils.subarray(files.toArray(new File[files.size()]),
        offset,
        offset + pluginConf.getToadLimit());
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy