biz.gabrys.maven.plugins.css.splitter.split.StyleSheetSplliter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of css-splitter-maven-plugin
Show all versions of css-splitter-maven-plugin
Splits CSS stylesheets to smaller files ("parts") which contain maximum X rules.
/*
* CSS Splitter Maven Plugin
* http://css-splitter-maven-plugin.projects.gabrys.biz/
*
* Copyright (c) 2015 Adam Gabryś
*
* This file is licensed under the BSD 3-Clause (the "License").
* You may not use this file except in compliance with the License.
* You may obtain:
* - a copy of the License at project page
* - a template of the License at https://opensource.org/licenses/BSD-3-Clause
*/
package biz.gabrys.maven.plugins.css.splitter.split;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import biz.gabrys.maven.plugins.css.splitter.css.type.NodeRule;
import biz.gabrys.maven.plugins.css.splitter.css.type.StyleSheet;
public class StyleSheetSplliter {
private final int limit;
private final RulesSplitter splitter;
public StyleSheetSplliter(final int limit) {
this.limit = limit;
final List splitters = new ArrayList();
splitters.add(new StyleRuleSplitter());
splitters.add(new ComplexRuleSplitter());
splitter = new RulesSplitter(new MultipleRuleSplitter(splitters));
}
// for tests
StyleSheetSplliter(final int limit, final RulesSplitter splitter) {
this.limit = limit;
this.splitter = splitter;
}
public List split(final StyleSheet stylesheet) {
if (stylesheet.getRules().isEmpty()) {
return Arrays.asList(stylesheet);
}
final List> rulesMatrix = splitToMatrix(stylesheet);
return convertToSheets(rulesMatrix);
}
private List> splitToMatrix(final StyleSheet stylesheet) {
final List> rulesMatrix = new LinkedList>();
List currentRules = stylesheet.getRules();
while (!currentRules.isEmpty()) {
final RulesContainer container = splitter.split(currentRules, limit);
rulesMatrix.add(container.before);
currentRules = container.after;
}
return rulesMatrix;
}
private static List convertToSheets(final List> rulesMatrix) {
final List sheets = new ArrayList(rulesMatrix.size());
for (final List sheetRules : rulesMatrix) {
sheets.add(new StyleSheet(sheetRules));
}
return sheets;
}
}