use of com.phloc.css.decl.CSSImportRule in project Asqatasun by Asqatasun.
the class CSSJsoupPhlocContentAdapterImpl method adaptContent.
/**
* For shared css, the adaptation is only made the first time the css is
* encountered.
*
* @param stylesheetContent
* @param resource
* @param currentLocalResourcePath
* @param mediaAttributeValue
*
*/
private void adaptContent(StylesheetContent stylesheetContent, Resource resource, String currentLocalResourcePath, @Nullable List<CSSMediaQuery> mediaList) {
if (stylesheetContent.getAdaptedContent() == null && resource.getResource() != null && StringUtils.isNotBlank(resource.getResource())) {
Charset charset = null;
try {
charset = CSSReader.getCharsetDeclaredInCSS(new ByteArrayInputStreamProvider(resource.getResource().getBytes()));
LOGGER.debug("is css valid CSS2 " + CSSReader.isValidCSS(resource.getResource(), ECSSVersion.CSS21) + " " + stylesheetContent.getURI());
LOGGER.debug("is css valid CSS3 " + CSSReader.isValidCSS(resource.getResource(), ECSSVersion.CSS30) + " " + stylesheetContent.getURI());
} catch (TokenMgrError tme) {
LOGGER.debug(resource.getResource() + " is on error, so invalid");
LOGGER.debug(tme.getMessage());
}
if (charset == null) {
charset = Charset.forName("utf-8");
}
try {
CascadingStyleSheet aCSS = CSSReader.readFromString(resource.getResource(), charset, ECSSVersion.CSS30, new CSSParserExceptionHandlerImpl(stylesheetContent));
// has been set to "on error" and so is not null
if (stylesheetContent.getAdaptedContent() == null) {
if (CollectionUtils.isNotEmpty(mediaList) && MediaQueryTools.canWrapInMediaQuery(aCSS)) {
if (MediaQueryTools.canWrapInMediaQuery(aCSS)) {
aCSS = MediaQueryTools.getWrappedInMediaQuery(aCSS, mediaList);
} else {
LOGGER.warn(stylesheetContent.getURI() + " should be" + "wrapped into " + mediaList + " but it " + "is impossible");
}
}
stylesheetContent.setAdaptedContent(new XStream().toXML(aCSS));
if (aCSS.hasImportRules()) {
for (CSSImportRule cssImportRule : aCSS.getAllImportRules()) {
getImportedResources(cssImportRule, currentLocalResourcePath);
}
}
}
} catch (IllegalArgumentException | TokenMgrError iae) {
stylesheetContent.setAdaptedContent(CSS_ON_ERROR);
}
}
}
Aggregations