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);
}
}
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();
}
}
Aggregations