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