Search in sources :

Example 6 with ECSSVersion

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

the class CSSReader21SpecialFuncTest method testReadSpecialGood.

@Test
public void testReadSpecialGood() {
    final ECSSVersion eVersion = ECSSVersion.CSS30;
    final Charset aCharset = StandardCharsets.UTF_8;
    final File aFile = new File("src/test/resources/testfiles/css21/good/artificial/test-url.css");
    final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, aCharset, eVersion);
    assertNotNull(aCSS);
    final String sCSS = new CSSWriter(eVersion, false).getCSSAsString(aCSS);
    assertNotNull(sCSS);
    if (false)
        s_aLogger.info(sCSS);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ECSSVersion(com.helger.css.ECSSVersion) Charset(java.nio.charset.Charset) CSSWriter(com.helger.css.writer.CSSWriter) File(java.io.File) Test(org.junit.Test)

Example 7 with ECSSVersion

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

the class CSSReader method readFromReader.

/**
 * Read the CSS from the passed {@link IReaderProvider}. If the CSS contains
 * an explicit <code>@charset</code> rule, it is ignored and the charset used
 * to create the reader is used instead! Also the fallback charset from the
 * {@link CSSReaderSettings} is ignored.
 *
 * @param aRP
 *        The reader provider to use. The reader is retrieved exactly once and
 *        closed anyway. May not be <code>null</code>.
 * @param aSettings
 *        The settings to be used for reading the CSS. May not be
 *        <code>null</code>.
 * @return <code>null</code> if reading failed, the CSS declarations
 *         otherwise.
 * @since 3.8.2
 */
@Nullable
public static CascadingStyleSheet readFromReader(@Nonnull final IHasReader aRP, @Nonnull final CSSReaderSettings aSettings) {
    ValueEnforcer.notNull(aRP, "ReaderProvider");
    ValueEnforcer.notNull(aSettings, "Settings");
    // Create the reader
    final Reader aReader = aRP.getReader();
    if (aReader == null) {
        // Failed to open reader
        return null;
    }
    // No charset determination, as the Reader already has an implicit Charset
    final ECSSVersion eVersion = aSettings.getCSSVersion();
    try {
        final CSSCharStream aCharStream = new CSSCharStream(aReader);
        aCharStream.setTabSize(aSettings.getTabSize());
        // Use the default CSS parse error handler if none is provided
        ICSSParseErrorHandler aRealParseErrorHandler = aSettings.getCustomErrorHandler();
        if (aRealParseErrorHandler == null)
            aRealParseErrorHandler = getDefaultParseErrorHandler();
        // Use the default CSS exception handler if none is provided
        ICSSParseExceptionCallback aRealParseExceptionHandler = aSettings.getCustomExceptionHandler();
        if (aRealParseExceptionHandler == null)
            aRealParseExceptionHandler = getDefaultParseExceptionHandler();
        final boolean bBrowserCompliantMode = aSettings.isBrowserCompliantMode();
        final CSSNode aNode = _readStyleSheet(aCharStream, eVersion, aRealParseErrorHandler, aRealParseExceptionHandler, bBrowserCompliantMode);
        // Failed to parse content as CSS?
        if (aNode == null)
            return null;
        // Get the interpret error handler
        ICSSInterpretErrorHandler aRealInterpretErrorHandler = aSettings.getInterpretErrorHandler();
        if (aRealInterpretErrorHandler == null)
            aRealInterpretErrorHandler = getDefaultInterpretErrorHandler();
        // Convert the AST to a domain object
        return CSSHandler.readCascadingStyleSheetFromNode(eVersion, aNode, aRealInterpretErrorHandler);
    } finally {
        StreamHelper.close(aReader);
    }
}
Also used : ICSSParseErrorHandler(com.helger.css.reader.errorhandler.ICSSParseErrorHandler) ICSSInterpretErrorHandler(com.helger.css.reader.errorhandler.ICSSInterpretErrorHandler) ECSSVersion(com.helger.css.ECSSVersion) CSSCharStream(com.helger.css.parser.CSSCharStream) IHasReader(com.helger.commons.io.IHasReader) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) Reader(java.io.Reader) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback) CSSNode(com.helger.css.parser.CSSNode) Nullable(javax.annotation.Nullable)

Example 8 with ECSSVersion

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

the class CSSReaderDeclarationList method readFromReader.

/**
 * Read the CSS from the passed {@link Reader}.
 *
 * @param aReader
 *        The reader to use. Will be closed automatically after reading -
 *        independent of success or error. May not be <code>null</code>.
 * @param aSettings
 *        The settings to be used for reading the CSS. May not be
 *        <code>null</code>.
 * @return <code>null</code> if reading failed, the CSS declarations
 *         otherwise.
 * @since 3.8.2
 */
@Nullable
public static CSSDeclarationList readFromReader(@Nonnull @WillClose final Reader aReader, @Nonnull final CSSReaderSettings aSettings) {
    ValueEnforcer.notNull(aReader, "Reader");
    ValueEnforcer.notNull(aSettings, "Settings");
    final ECSSVersion eVersion = aSettings.getCSSVersion();
    try {
        final CSSCharStream aCharStream = new CSSCharStream(aReader);
        // Use the default CSS parse error handler if none is provided
        ICSSParseErrorHandler aRealParseErrorHandler = aSettings.getCustomErrorHandler();
        if (aRealParseErrorHandler == null)
            aRealParseErrorHandler = getDefaultParseErrorHandler();
        // Use the default CSS exception handler if none is provided
        ICSSParseExceptionCallback aRealParseExceptionHandler = aSettings.getCustomExceptionHandler();
        if (aRealParseExceptionHandler == null)
            aRealParseExceptionHandler = getDefaultParseExceptionHandler();
        final CSSNode aNode = _readStyleDeclaration(aCharStream, eVersion, aRealParseErrorHandler, aRealParseExceptionHandler);
        // Failed to parse content as CSS?
        if (aNode == null)
            return null;
        // Get the interpret error handler
        ICSSInterpretErrorHandler aRealInterpretErrorHandler = aSettings.getInterpretErrorHandler();
        if (aRealInterpretErrorHandler == null)
            aRealInterpretErrorHandler = getDefaultInterpretErrorHandler();
        // Convert the AST to a domain object
        return CSSHandler.readDeclarationListFromNode(eVersion, aNode, aRealInterpretErrorHandler);
    } finally {
        StreamHelper.close(aReader);
    }
}
Also used : ICSSParseErrorHandler(com.helger.css.reader.errorhandler.ICSSParseErrorHandler) ICSSInterpretErrorHandler(com.helger.css.reader.errorhandler.ICSSInterpretErrorHandler) ECSSVersion(com.helger.css.ECSSVersion) CSSCharStream(com.helger.css.parser.CSSCharStream) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback) CSSNode(com.helger.css.parser.CSSNode) Nullable(javax.annotation.Nullable)

Example 9 with ECSSVersion

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

the class CSSReader method readFromStream.

/**
 * Read the CSS from the passed {@link IHasInputStream}. If the CSS contains
 * an explicit charset, the whole CSS is parsed again, with the charset found
 * inside the file, so the passed {@link IHasInputStream} must be able to
 * create a new input stream on second invocation!
 *
 * @param aISP
 *        The input stream provider to use. Must be able to create new input
 *        streams on every invocation, in case an explicit charset node was
 *        found. May not be <code>null</code>.
 * @param aSettings
 *        The settings to be used for reading the CSS. May not be
 *        <code>null</code>.
 * @return <code>null</code> if reading failed, the CSS declarations
 *         otherwise.
 * @since 3.8.2
 */
@Nullable
public static CascadingStyleSheet readFromStream(@Nonnull final IHasInputStream aISP, @Nonnull final CSSReaderSettings aSettings) {
    ValueEnforcer.notNull(aISP, "InputStreamProvider");
    ValueEnforcer.notNull(aSettings, "Settings");
    Charset aCharsetToUse;
    // Check if the CSS contains a declared charset or as an alternative use the
    // Charset from the BOM
    Charset aDeclaredCharset;
    try {
        aDeclaredCharset = getCharsetDeclaredInCSS(aISP);
    } catch (final IllegalStateException ex) {
        // Failed to parse CSS at a very low level
        return null;
    }
    if (aDeclaredCharset != null) {
        if (s_aLogger.isDebugEnabled())
            s_aLogger.debug("Reading CSS definition again with explicit charset '" + aDeclaredCharset.name() + "'");
        aCharsetToUse = aDeclaredCharset;
    } else {
        // No charset declared - use fallback
        aCharsetToUse = aSettings.getFallbackCharset();
    }
    // Try to open input stream
    final InputStream aISOrig = aISP.getInputStream();
    if (aISOrig == null)
        return null;
    // Open input stream
    final InputStreamAndCharset aISAndBOM = CharsetHelper.getInputStreamAndCharsetFromBOM(aISOrig);
    final InputStream aIS = aISAndBOM.getInputStream();
    final Reader aReader = StreamHelper.createReader(aIS, aCharsetToUse);
    final ECSSVersion eVersion = aSettings.getCSSVersion();
    try {
        final CSSCharStream aCharStream = new CSSCharStream(aReader);
        aCharStream.setTabSize(aSettings.getTabSize());
        // Use the default CSS parse error handler if none is provided
        ICSSParseErrorHandler aRealParseErrorHandler = aSettings.getCustomErrorHandler();
        if (aRealParseErrorHandler == null)
            aRealParseErrorHandler = getDefaultParseErrorHandler();
        // Use the default CSS exception handler if none is provided
        ICSSParseExceptionCallback aRealParseExceptionHandler = aSettings.getCustomExceptionHandler();
        if (aRealParseExceptionHandler == null)
            aRealParseExceptionHandler = getDefaultParseExceptionHandler();
        final boolean bBrowserCompliantMode = aSettings.isBrowserCompliantMode();
        final CSSNode aNode = _readStyleSheet(aCharStream, eVersion, aRealParseErrorHandler, aRealParseExceptionHandler, bBrowserCompliantMode);
        // Failed to parse content as CSS?
        if (aNode == null)
            return null;
        // Get the interpret error handler
        ICSSInterpretErrorHandler aRealInterpretErrorHandler = aSettings.getInterpretErrorHandler();
        if (aRealInterpretErrorHandler == null)
            aRealInterpretErrorHandler = getDefaultInterpretErrorHandler();
        // Convert the AST to a domain object
        return CSSHandler.readCascadingStyleSheetFromNode(eVersion, aNode, aRealInterpretErrorHandler);
    } finally {
        StreamHelper.close(aReader);
    }
}
Also used : ICSSParseErrorHandler(com.helger.css.reader.errorhandler.ICSSParseErrorHandler) ICSSInterpretErrorHandler(com.helger.css.reader.errorhandler.ICSSInterpretErrorHandler) InputStreamAndCharset(com.helger.commons.charset.CharsetHelper.InputStreamAndCharset) IHasInputStream(com.helger.commons.io.IHasInputStream) InputStream(java.io.InputStream) ECSSVersion(com.helger.css.ECSSVersion) CSSCharStream(com.helger.css.parser.CSSCharStream) Charset(java.nio.charset.Charset) InputStreamAndCharset(com.helger.commons.charset.CharsetHelper.InputStreamAndCharset) IHasReader(com.helger.commons.io.IHasReader) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) Reader(java.io.Reader) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback) CSSNode(com.helger.css.parser.CSSNode) Nullable(javax.annotation.Nullable)

Example 10 with ECSSVersion

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

the class CSSReader30SpecialFuncTest method testReadSpecialGood.

@Test
public void testReadSpecialGood() {
    final ECSSVersion eVersion = ECSSVersion.CSS30;
    final Charset aCharset = StandardCharsets.UTF_8;
    final File aFile = new File("src/test/resources/testfiles/css30/good/artificial/test-postcss-cssnext.css");
    final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, aCharset, eVersion);
    assertNotNull(aCSS);
    final String sCSS = new CSSWriter(eVersion, false).getCSSAsString(aCSS);
    assertNotNull(sCSS);
    if (false)
        s_aLogger.info(sCSS);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) ECSSVersion(com.helger.css.ECSSVersion) Charset(java.nio.charset.Charset) CSSWriter(com.helger.css.writer.CSSWriter) File(java.io.File) Test(org.junit.Test)

Aggregations

ECSSVersion (com.helger.css.ECSSVersion)10 Charset (java.nio.charset.Charset)8 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)7 File (java.io.File)7 Test (org.junit.Test)7 CSSWriter (com.helger.css.writer.CSSWriter)5 ICSSParseExceptionCallback (com.helger.css.handler.ICSSParseExceptionCallback)3 CSSCharStream (com.helger.css.parser.CSSCharStream)3 CSSNode (com.helger.css.parser.CSSNode)3 ICSSInterpretErrorHandler (com.helger.css.reader.errorhandler.ICSSInterpretErrorHandler)3 ICSSParseErrorHandler (com.helger.css.reader.errorhandler.ICSSParseErrorHandler)3 Nullable (javax.annotation.Nullable)3 IHasReader (com.helger.commons.io.IHasReader)2 NonBlockingStringReader (com.helger.commons.io.stream.NonBlockingStringReader)2 CSSDeclaration (com.helger.css.decl.CSSDeclaration)2 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)2 CSSExpressionMemberMath (com.helger.css.decl.CSSExpressionMemberMath)2 CSSExpressionMemberTermSimple (com.helger.css.decl.CSSExpressionMemberTermSimple)2 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)2 CSSStyleRule (com.helger.css.decl.CSSStyleRule)2