Search in sources :

Example 1 with CSSImportRule

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);
        }
    }
}
Also used : CascadingStyleSheet(com.phloc.css.decl.CascadingStyleSheet) ByteArrayInputStreamProvider(com.phloc.commons.io.streamprovider.ByteArrayInputStreamProvider) CSSImportRule(com.phloc.css.decl.CSSImportRule) XStream(com.thoughtworks.xstream.XStream) Charset(java.nio.charset.Charset) CCharset(com.phloc.commons.charset.CCharset) TokenMgrError(com.phloc.css.parser.TokenMgrError)

Aggregations

CCharset (com.phloc.commons.charset.CCharset)1 ByteArrayInputStreamProvider (com.phloc.commons.io.streamprovider.ByteArrayInputStreamProvider)1 CSSImportRule (com.phloc.css.decl.CSSImportRule)1 CascadingStyleSheet (com.phloc.css.decl.CascadingStyleSheet)1 TokenMgrError (com.phloc.css.parser.TokenMgrError)1 XStream (com.thoughtworks.xstream.XStream)1 Charset (java.nio.charset.Charset)1