Search in sources :

Example 6 with CSSImportRule

use of com.helger.css.decl.CSSImportRule in project ph-css by phax.

the class CSSWriter method writeCSS.

/**
 * Write the CSS content to the passed writer. No specific charset is used.
 *
 * @param aCSS
 *        The CSS to write. May not be <code>null</code>.
 * @param aWriter
 *        The write to write the text to. May not be <code>null</code>. Is
 *        automatically closed after the writing!
 * @throws IOException
 *         In case writing fails.
 * @throws IllegalStateException
 *         In case some elements cannot be written in the version supplied in
 *         the constructor.
 * @see #getCSSAsString(CascadingStyleSheet)
 */
public void writeCSS(@Nonnull final CascadingStyleSheet aCSS, @Nonnull @WillClose final Writer aWriter) throws IOException {
    ValueEnforcer.notNull(aCSS, "CSS");
    ValueEnforcer.notNull(aWriter, "Writer");
    try {
        final boolean bOptimizedOutput = m_aSettings.isOptimizedOutput();
        final String sNewLineString = m_aSettings.getNewLineString();
        // Write file header
        if (m_bWriteHeaderText && StringHelper.hasText(m_sHeaderText)) {
            aWriter.write("/*");
            aWriter.write(sNewLineString);
            for (final String sLine : StringHelper.getExploded("\n", m_sHeaderText)) {
                aWriter.write(" * " + sLine);
                aWriter.write(sNewLineString);
            }
            aWriter.write(" */");
            aWriter.write(sNewLineString);
        }
        // Charset? Must be the first element before the import
        if (StringHelper.hasText(m_sContentCharset)) {
            aWriter.write("@charset \"" + m_sContentCharset + "\";");
            if (!bOptimizedOutput)
                aWriter.write(sNewLineString);
        }
        // Import rules
        int nRulesEmitted = 0;
        final ICommonsList<CSSImportRule> aImportRules = aCSS.getAllImportRules();
        if (aImportRules.isNotEmpty())
            for (final CSSImportRule aImportRule : aImportRules) {
                aWriter.write(aImportRule.getAsCSSString(m_aSettings));
                ++nRulesEmitted;
            }
        // Namespace rules
        final ICommonsList<CSSNamespaceRule> aNamespaceRules = aCSS.getAllNamespaceRules();
        if (aNamespaceRules.isNotEmpty())
            for (final CSSNamespaceRule aNamespaceRule : aNamespaceRules) {
                aWriter.write(aNamespaceRule.getAsCSSString(m_aSettings));
                ++nRulesEmitted;
            }
        // Main CSS rules
        for (final ICSSTopLevelRule aRule : aCSS.getAllRules()) {
            final String sRuleCSS = aRule.getAsCSSString(m_aSettings);
            if (StringHelper.hasText(sRuleCSS)) {
                if (!bOptimizedOutput && nRulesEmitted > 0)
                    aWriter.write(sNewLineString);
                aWriter.write(sRuleCSS);
                ++nRulesEmitted;
            }
        }
        // Write file footer
        if (m_bWriteFooterText && StringHelper.hasText(m_sFooterText)) {
            aWriter.write("/*");
            aWriter.write(sNewLineString);
            for (final String sLine : StringHelper.getExploded('\n', m_sFooterText)) {
                aWriter.write(" * " + sLine);
                aWriter.write(sNewLineString);
            }
            aWriter.write(" */");
            aWriter.write(sNewLineString);
        }
    } finally {
        StreamHelper.close(aWriter);
    }
}
Also used : ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSImportRule(com.helger.css.decl.CSSImportRule) CSSNamespaceRule(com.helger.css.decl.CSSNamespaceRule)

Example 7 with CSSImportRule

use of com.helger.css.decl.CSSImportRule in project ph-css by phax.

the class CSSVisitor method visitCSS.

/**
 * Visit all CSS elements in the order of their declaration. import rules come
 * first, namespace rules come next and all other top-level rules in the order
 * of their declaration.
 *
 * @param aCSS
 *        The CSS to visit. May not be <code>null</code>.
 * @param aVisitor
 *        The callback to be invoked for each element found. May not be
 *        <code>null</code>.
 */
public static void visitCSS(@Nonnull final CascadingStyleSheet aCSS, @Nonnull final ICSSVisitor aVisitor) {
    ValueEnforcer.notNull(aCSS, "CSS");
    ValueEnforcer.notNull(aVisitor, "Visitor");
    aVisitor.begin();
    try {
        // for all imports
        for (final CSSImportRule aImportRule : aCSS.getAllImportRules()) visitImportRule(aImportRule, aVisitor);
        // for all namespaces
        for (final CSSNamespaceRule aNamespaceRule : aCSS.getAllNamespaceRules()) visitNamespaceRule(aNamespaceRule, aVisitor);
        // for all other top level rules
        for (final ICSSTopLevelRule aTopLevelRule : aCSS.getAllRules()) visitTopLevelRule(aTopLevelRule, aVisitor);
    } finally {
        aVisitor.end();
    }
}
Also used : ICSSTopLevelRule(com.helger.css.decl.ICSSTopLevelRule) CSSImportRule(com.helger.css.decl.CSSImportRule) CSSNamespaceRule(com.helger.css.decl.CSSNamespaceRule)

Aggregations

CSSImportRule (com.helger.css.decl.CSSImportRule)7 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)7 CSSDeclaration (com.helger.css.decl.CSSDeclaration)4 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)4 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)4 CSSNamespaceRule (com.helger.css.decl.CSSNamespaceRule)3 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)3 CSSDeclarationList (com.helger.css.decl.CSSDeclarationList)1 CSSMediaQuery (com.helger.css.decl.CSSMediaQuery)1 CSSMediaRule (com.helger.css.decl.CSSMediaRule)1 CSSURI (com.helger.css.decl.CSSURI)1 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)1 DoNothingCSSInterpretErrorHandler (com.helger.css.reader.errorhandler.DoNothingCSSInterpretErrorHandler)1 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)1 CSSDataURL (com.helger.css.utils.CSSDataURL)1 CSSWriter (com.helger.css.writer.CSSWriter)1 URL (java.net.URL)1 Charset (java.nio.charset.Charset)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1