如何阻止Jawr处理CSS文件的某些部分?

时间:2012-04-20 19:48:31

标签: java css jawr

我目前正在使用Jawr压缩和捆绑我的css,javascript和图片文件。

Jawr目前正在转换我的css文件中的所有url()链接,无论它们是否为图像。例如:

@font-face {
    font-family: 'NothingYouCouldSay';
    src: url('../fonts/NothingYouCouldSay.eot') format('eot');
    src: local("☺"), url('../fonts/NothingYouCouldSay.woff') format('woff'), url("../fonts/NothingYouCouldSay.otf") format("opentype"), url('../fonts/NothingYouCouldSay.ttf') format('truetype'), url('../fonts/NothingYouCouldSay.svg') format('svg');
    font-weight: normal;
    font-style: normal;
}

Jawr正在转换所有url()值,但是当web服务器运行时,无法找到资源,因为我已将图像servlet配置为仅监听* .png& * .JPG。

@font-face {
    font-family: 'NothingYouCouldSay';
    src: url('../../../cb1130234589/resources/fonts/NothingYouCouldSay.eot') format('eot');
    src: local("☺"), url('../../../cb1130234589/resources/fonts/NothingYouCouldSay.woff') format('woff'), url("../../../cb1130234589/resources/fonts/NothingYouCouldSay.otf") format("opentype"), url('../../../cb1130234589/resources/fonts/NothingYouCouldSay.ttf') format('truetype'), url('../../../cb1130234589/resources/fonts/NothingYouCouldSay.svg') format('svg');
    font-weight: normal;
    font-style: normal;
}

如果我将* .woff添加到图像servlet映射中,那么servlet会抱怨文件的mime类型不被理解。

有什么方法可以让Jawr不处理这些特定的特定URL?

1 个答案:

答案 0 :(得分:2)

所以,在尝试了一些不同的想法后,我最终编写了自己的自定义后处理器来处理这个问题。我尽可能多地重用现有的Jawr代码,这意味着如果Jawr更改它的底层代码,它可能会非常脆弱。

无论如何,这是我写的:

package com.bullethq.jawr.postprocessor;

import net.jawr.web.resource.FileNameUtils;
import net.jawr.web.resource.bundle.factory.util.PathNormalizer;
import net.jawr.web.resource.bundle.postprocess.BundleProcessingStatus;
import net.jawr.web.resource.bundle.postprocess.impl.CSSURLPathRewriterPostProcessor;
import net.jawr.web.resource.bundle.postprocess.impl.PostProcessorCssImageUrlRewriter;

import java.io.IOException;

public class CustomCssUrlPathRewriterPostProcessor extends CSSURLPathRewriterPostProcessor {

    public static final String CUSTOM_URL_PATH_REWRITER = "customcsspathrewriter";

    public CustomCssUrlPathRewriterPostProcessor() {
        super(CUSTOM_URL_PATH_REWRITER);
    }

    // ========================================================================
    // ========================================================================
    // ========================================================================
    @Override
    protected PostProcessorCssImageUrlRewriter createImageUrlRewriter(BundleProcessingStatus status) {
        return new CustomPostProcessorCssImageUrlRewriter(status);
    }

    // ========================================================================
    // ========================================================================
    // ========================================================================
    public class CustomPostProcessorCssImageUrlRewriter extends PostProcessorCssImageUrlRewriter {

        public CustomPostProcessorCssImageUrlRewriter(BundleProcessingStatus status) {
            super(status);
        }

        // ========================================================================
        // ========================================================================
        // ========================================================================
        @Override
        protected String getUrlPath(String match, String originalPath, String newCssPath) throws IOException {
            String url = match.substring(match.indexOf('(') + 1, match.lastIndexOf(')')).trim();

            // Remove any quotes if necessary.
            String quoteStr = "";
            if (url.startsWith("'") || url.startsWith("\"")) {
                quoteStr = String.valueOf(url.charAt(0));
                url = url.substring(1, url.length() - 1);
            }

            // We now check if the url ends in a known image file extension
            // If not, the url is ignored.
            if (FileNameUtils.hasImageExtension(url)) {
                return super.getUrlPath(match, originalPath, newCssPath);
            } else {
                // We need to rewrite the path, as any relative URLs will
                // not resolve correctly if Jawr has changed the CSS path.
                url = PathNormalizer.concatWebPath(originalPath, url);
                url = PathNormalizer.getRelativeWebPath(PathNormalizer.getParentPath(newCssPath), url);
                return "url(" + quoteStr + url + quoteStr + ")";
            }
        }
    }
}

然后,你需要配置Jawr在jawr.properties中使用这个自定义后期处理器:

jawr.custom.postprocessors.customcsspathrewriter.class=com.bullethq.jawr.postprocessor.CustomCssUrlPathRewriterPostProcessor
jawr.css.bundle.factory.filepostprocessors=customcsspathrewriter