CSSUtils.java 2.37 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang.StringUtils
 */
package com.adobe.aemds.guide.internal;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;

public class CSSUtils {
    private Pattern urlFunctionPattern = Pattern.compile("(\\burl\\(\\s*['\"]?)(.*?)(\\s*['\"]?\\))", 2);
    private Pattern absolutePathPattern = Pattern.compile("^[a-z]*?:?//.*|^/.*", 2);
    private Pattern urlPattern = Pattern.compile("^[a-z]*?:?//.*");

    public String processUrls(String CSS, String contextPath, String absolutePath) {
        if (StringUtils.isNotBlank((String)CSS) && StringUtils.isNotBlank((String)absolutePath)) {
            StringBuffer updatedUrlString = new StringBuffer();
            Matcher matcher = this.urlFunctionPattern.matcher(CSS);
            while (matcher.find()) {
                String url_prefix = matcher.group(1);
                String path = matcher.group(2);
                String absolute_path = this.prependContextPath(this.makePathAbsolute(path, absolutePath), contextPath);
                String url_suffix = matcher.group(3);
                matcher.appendReplacement(updatedUrlString, url_prefix + absolute_path + url_suffix);
            }
            matcher.appendTail(updatedUrlString);
            return updatedUrlString.toString();
        }
        return CSS;
    }

    public boolean isAbsolutePath(String path) {
        if (StringUtils.isEmpty((String)path)) {
            return true;
        }
        path = path.trim();
        Matcher matcher = this.absolutePathPattern.matcher(path);
        return matcher.find();
    }

    public String makePathAbsolute(String path, String relativeTo) {
        if (this.isAbsolutePath(path)) {
            return path;
        }
        return relativeTo + "/" + path;
    }

    public String prependContextPath(String absolutePath, String contextPath) {
        if (!StringUtils.isEmpty((String)contextPath) && !this.isAbsoluteURL(absolutePath)) {
            return contextPath + "" + absolutePath;
        }
        return absolutePath;
    }

    public boolean isAbsoluteURL(String path) {
        if (StringUtils.isEmpty((String)path)) {
            return true;
        }
        path = path.trim();
        Matcher matcher = this.urlPattern.matcher(path);
        return matcher.find();
    }
}