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

biz.gabrys.maven.plugins.css.splitter.steadystate.converter.StyleSheetConverter Maven / Gradle / Ivy

Go to download

Splits CSS stylesheets to smaller files ("parts") which contain maximum X rules.

There is a newer version: 2.0.3
Show newest version
/*
 * 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.steadystate.converter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSRuleList;
import org.w3c.dom.css.CSSStyleSheet;

import biz.gabrys.maven.plugins.css.splitter.css.Standard;
import biz.gabrys.maven.plugins.css.splitter.css.type.NodeRule;
import biz.gabrys.maven.plugins.css.splitter.css.type.StyleSheet;

public class StyleSheetConverter {

    private final RuleConverter converter;

    public StyleSheetConverter(final Standard standard, final boolean strict) {
        this(createConverter(standard, strict));
    }

    // for tests
    StyleSheetConverter(final RuleConverter converter) {
        this.converter = converter;
    }

    static MultipleRuleConverter createConverter(final Standard standard, final boolean strict) {
        final List> converters = new ArrayList>();
        converters.add(new StyleRuleConverter());
        converters.add(new MediaRuleConverter(standard, strict));
        converters.add(new FontFaceRuleConverter());
        converters.add(new PageRuleConverter());
        converters.add(new ImportRuleConverter());
        converters.add(new CharsetRuleConverter());
        converters.add(new UnknownRuleConverter());
        return new MultipleRuleConverter(converters);
    }

    public StyleSheet convert(final CSSStyleSheet stylesheet) {
        final List rules = new LinkedList();
        final CSSRuleList ruleList = stylesheet.getCssRules();
        for (int i = 0; i < ruleList.getLength(); ++i) {
            final CSSRule rule = ruleList.item(i);
            if (converter.isSupportedType(rule)) {
                rules.add(converter.convert(rule));
            } else {
                throw new UnsupportedRuleException(rule);
            }
        }
        return new StyleSheet(rules);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy