Search in sources :

Example 1 with IHasReader

use of com.helger.commons.io.IHasReader 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)

Aggregations

IHasReader (com.helger.commons.io.IHasReader)1 NonBlockingStringReader (com.helger.commons.io.stream.NonBlockingStringReader)1 ECSSVersion (com.helger.css.ECSSVersion)1 ICSSParseExceptionCallback (com.helger.css.handler.ICSSParseExceptionCallback)1 CSSCharStream (com.helger.css.parser.CSSCharStream)1 CSSNode (com.helger.css.parser.CSSNode)1 ICSSInterpretErrorHandler (com.helger.css.reader.errorhandler.ICSSInterpretErrorHandler)1 ICSSParseErrorHandler (com.helger.css.reader.errorhandler.ICSSParseErrorHandler)1 Reader (java.io.Reader)1 Nullable (javax.annotation.Nullable)1