Search in sources :

Example 1 with CSSWriter

use of com.helger.css.writer.CSSWriter in project ph-css by phax.

the class WikiWriteCSS method writeCSS30.

/**
 * Write a CSS 3.0 declaration to a file using UTF-8 encoding.
 *
 * @param aCSS
 *        The CSS to be written to a file. May not be <code>null</code>.
 * @param aFile
 *        The file to be written. May not be <code>null</code>.
 * @return {@link ESuccess#SUCCESS} if everything went okay, and
 *         {@link ESuccess#FAILURE} if an error occurred
 */
public ESuccess writeCSS30(final CascadingStyleSheet aCSS, final File aFile) {
    // 1.param: version to write
    // 2.param: false== non-optimized output
    final CSSWriterSettings aSettings = new CSSWriterSettings(ECSSVersion.CSS30, false);
    try {
        final CSSWriter aWriter = new CSSWriter(aSettings);
        // Write the @charset rule: (optional)
        aWriter.setContentCharset(StandardCharsets.UTF_8.name());
        // Write a nice file header
        aWriter.setHeaderText("This file was generated by phloc-css\nGrab a copy at http://code.google.com/p/phloc-css");
        // Convert the CSS to a String
        final String sCSSCode = aWriter.getCSSAsString(aCSS);
        // Finally write the String to a file
        return SimpleFileIO.writeFile(aFile, sCSSCode, StandardCharsets.UTF_8);
    } catch (final Exception ex) {
        s_aLogger.error("Failed to write the CSS to a file", ex);
        return ESuccess.FAILURE;
    }
}
Also used : CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CSSWriter(com.helger.css.writer.CSSWriter)

Example 2 with CSSWriter

use of com.helger.css.writer.CSSWriter in project ph-css by phax.

the class CSSReader30SpecialFuncTest method testReadWithBOM.

@Test
public void testReadWithBOM() {
    final String sCSSBase = "/* comment */.class{color:red}.class{color:blue}";
    for (final EUnicodeBOM eBOM : EUnicodeBOM.values()) {
        final Charset aDeterminedCharset = eBOM.getCharset();
        if (aDeterminedCharset != null) {
            final CascadingStyleSheet aCSS = CSSReader.readFromStream(new ByteArrayInputStreamProvider(ArrayHelper.getConcatenated(eBOM.getAllBytes(), sCSSBase.getBytes(aDeterminedCharset))), aDeterminedCharset, ECSSVersion.CSS30, new DoNothingCSSParseErrorHandler());
            assertNotNull("Failed to read with BOM " + eBOM, aCSS);
            assertEquals(".class{color:red}.class{color:blue}", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS));
        }
    }
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ByteArrayInputStreamProvider(com.helger.commons.io.streamprovider.ByteArrayInputStreamProvider) EUnicodeBOM(com.helger.commons.charset.EUnicodeBOM) DoNothingCSSParseErrorHandler(com.helger.css.reader.errorhandler.DoNothingCSSParseErrorHandler) Charset(java.nio.charset.Charset) CSSWriter(com.helger.css.writer.CSSWriter) Test(org.junit.Test)

Example 3 with CSSWriter

use of com.helger.css.writer.CSSWriter in project ph-css by phax.

the class CSSReader30SpecialFuncTest method testReadExpressions.

@Test
public void testReadExpressions() {
    final ECSSVersion eVersion = ECSSVersion.CSS30;
    final CSSWriterSettings aCSSWS = new CSSWriterSettings(eVersion, false);
    final Charset aCharset = StandardCharsets.UTF_8;
    final File aFile = new File("src/test/resources/testfiles/css30/good/artificial/test-expression.css");
    final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, aCharset, eVersion);
    assertNotNull(aCSS);
    assertEquals(1, aCSS.getRuleCount());
    assertEquals(1, aCSS.getStyleRuleCount());
    final CSSStyleRule aSR = aCSS.getStyleRuleAtIndex(0);
    assertEquals("div", aSR.getSelectorsAsCSSString(aCSSWS, 0));
    assertEquals(23, aSR.getDeclarationCount());
    int i = 0;
    for (final CSSDeclaration aDecl : aSR.getAllDeclarations()) {
        final String sExpectedName = Character.toString((char) ('a' + i));
        assertEquals(sExpectedName, aDecl.getProperty());
        ++i;
    }
    CSSDeclaration aDecl;
    ICSSExpressionMember aMember;
    // a: -5
    aDecl = aSR.getDeclarationOfPropertyName("a");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5", aMember.getAsCSSString(aCSSWS));
    // b: +5
    aDecl = aSR.getDeclarationOfPropertyName("b");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5", aMember.getAsCSSString(aCSSWS));
    // c: 5
    aDecl = aSR.getDeclarationOfPropertyName("c");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5", aMember.getAsCSSString(aCSSWS));
    // d: -5.12
    aDecl = aSR.getDeclarationOfPropertyName("d");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5.12", aMember.getAsCSSString(aCSSWS));
    // e: +5.12
    aDecl = aSR.getDeclarationOfPropertyName("e");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5.12", aMember.getAsCSSString(aCSSWS));
    // f: 5.12
    aDecl = aSR.getDeclarationOfPropertyName("f");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5.12", aMember.getAsCSSString(aCSSWS));
    // g: -5.12%
    aDecl = aSR.getDeclarationOfPropertyName("g");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5.12%", aMember.getAsCSSString(aCSSWS));
    // h: +5.12%
    aDecl = aSR.getDeclarationOfPropertyName("h");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5.12%", aMember.getAsCSSString(aCSSWS));
    // i: 5.12%
    aDecl = aSR.getDeclarationOfPropertyName("i");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5.12%", aMember.getAsCSSString(aCSSWS));
    // j: -5px
    aDecl = aSR.getDeclarationOfPropertyName("j");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("-5px", aMember.getAsCSSString(aCSSWS));
    // k: +5px
    aDecl = aSR.getDeclarationOfPropertyName("k");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("+5px", aMember.getAsCSSString(aCSSWS));
    // l: 5px
    aDecl = aSR.getDeclarationOfPropertyName("l");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("5px", aMember.getAsCSSString(aCSSWS));
    // m: 'string1'
    aDecl = aSR.getDeclarationOfPropertyName("m");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("'string1'", aMember.getAsCSSString(aCSSWS));
    // n: "string2"
    aDecl = aSR.getDeclarationOfPropertyName("n");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("\"string2\"", aMember.getAsCSSString(aCSSWS));
    // o: abc
    aDecl = aSR.getDeclarationOfPropertyName("o");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("abc", aMember.getAsCSSString(aCSSWS));
    // p: from
    aDecl = aSR.getDeclarationOfPropertyName("p");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("from", aMember.getAsCSSString(aCSSWS));
    // q: to
    aDecl = aSR.getDeclarationOfPropertyName("q");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("to", aMember.getAsCSSString(aCSSWS));
    // r: url(a.gif)
    aDecl = aSR.getDeclarationOfPropertyName("r");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermURI);
    assertEquals("url(a.gif)", aMember.getAsCSSString(aCSSWS));
    // s: #123
    aDecl = aSR.getDeclarationOfPropertyName("s");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("#123", aMember.getAsCSSString(aCSSWS));
    // t: function(5,6,abc)
    aDecl = aSR.getDeclarationOfPropertyName("t");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberFunction);
    assertEquals("function", ((CSSExpressionMemberFunction) aMember).getFunctionName());
    // 3 parameters and 2 commas
    assertEquals(5, ((CSSExpressionMemberFunction) aMember).getExpression().getMemberCount());
    assertEquals("function(5,6,abc)", aMember.getAsCSSString(aCSSWS));
    // u: calc(4 + 5)
    aDecl = aSR.getDeclarationOfPropertyName("u");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberMath);
    // 2 values and 1 operator
    assertEquals(3, ((CSSExpressionMemberMath) aMember).getMemberCount());
    assertEquals("calc(4 + 5)", aMember.getAsCSSString(aCSSWS));
    // v: inherit
    aDecl = aSR.getDeclarationOfPropertyName("v");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("inherit", aMember.getAsCSSString(aCSSWS));
    // w: u+1234
    aDecl = aSR.getDeclarationOfPropertyName("w");
    assertNotNull(aDecl);
    assertEquals(1, aDecl.getExpression().getMemberCount());
    aMember = aDecl.getExpression().getMemberAtIndex(0);
    assertTrue(aMember instanceof CSSExpressionMemberTermSimple);
    assertEquals("u+1234", aMember.getAsCSSString(aCSSWS));
    // Write result
    final String sCSS = new CSSWriter(aCSSWS).getCSSAsString(aCSS);
    assertNotNull(sCSS);
    if (false)
        s_aLogger.info(sCSS);
}
Also used : CSSExpressionMemberTermURI(com.helger.css.decl.CSSExpressionMemberTermURI) ECSSVersion(com.helger.css.ECSSVersion) Charset(java.nio.charset.Charset) CSSExpressionMemberTermSimple(com.helger.css.decl.CSSExpressionMemberTermSimple) CSSWriter(com.helger.css.writer.CSSWriter) ICSSExpressionMember(com.helger.css.decl.ICSSExpressionMember) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSStyleRule(com.helger.css.decl.CSSStyleRule) CSSExpressionMemberMath(com.helger.css.decl.CSSExpressionMemberMath) CSSDeclaration(com.helger.css.decl.CSSDeclaration) File(java.io.File) CSSExpressionMemberFunction(com.helger.css.decl.CSSExpressionMemberFunction) Test(org.junit.Test)

Example 4 with CSSWriter

use of com.helger.css.writer.CSSWriter in project ph-css by phax.

the class Issue21Test method testIssue.

@Test
public void testIssue() {
    // Multiple errors contained
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/good/issue21.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setBrowserCompliantMode(true));
    assertNotNull(aCSS);
    if (false)
        System.out.println(new CSSWriter(ECSSVersion.CSS30).getCSSAsString(aCSS));
    assertEquals(2, aCSS.getRuleCount());
    assertEquals(2, aCSS.getStyleRuleCount());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) IReadableResource(com.helger.commons.io.resource.IReadableResource) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 5 with CSSWriter

use of com.helger.css.writer.CSSWriter in project ph-css by phax.

the class Issue33Test method testIssue.

@Test
public void testIssue() {
    // No log message may be issued in this test!
    final String css = "@media \\0screen\\,screen\\9 {.test {margin-left: 0px}}";
    final CSSReaderSettings aSettings = new CSSReaderSettings().setCSSVersion(ECSSVersion.LATEST).setBrowserCompliantMode(true).setInterpretErrorHandler(new DoNothingCSSInterpretErrorHandler());
    final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream(css, aSettings);
    final CSSWriter writer = new CSSWriter(new CSSWriterSettings(ECSSVersion.LATEST, true));
    assertEquals("@media \\0screen\\,screen\\9 {.test{margin-left:0}}", writer.getCSSAsString(cascadingStyleSheet));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) DoNothingCSSInterpretErrorHandler(com.helger.css.reader.errorhandler.DoNothingCSSInterpretErrorHandler) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) Test(org.junit.Test)

Aggregations

CSSWriter (com.helger.css.writer.CSSWriter)28 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)25 Test (org.junit.Test)24 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)12 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)11 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)10 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)8 IReadableResource (com.helger.commons.io.resource.IReadableResource)8 File (java.io.File)8 Charset (java.nio.charset.Charset)7 ECSSVersion (com.helger.css.ECSSVersion)5 CSSDeclaration (com.helger.css.decl.CSSDeclaration)3 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)3 FileSystemRecursiveIterator (com.helger.commons.io.file.FileSystemRecursiveIterator)2 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)2 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)2 CSSExpressionMemberMath (com.helger.css.decl.CSSExpressionMemberMath)2 CSSExpressionMemberTermSimple (com.helger.css.decl.CSSExpressionMemberTermSimple)2 CSSStyleRule (com.helger.css.decl.CSSStyleRule)2 ICSSExpressionMember (com.helger.css.decl.ICSSExpressionMember)2