001
014
015 package com.liferay.portal.servlet.filters.dynamiccss;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.portal.util.ClassLoaderUtil;
021 import com.liferay.portal.util.PropsValues;
022
023 import java.util.regex.Matcher;
024 import java.util.regex.Pattern;
025
026 import org.mozilla.javascript.Context;
027 import org.mozilla.javascript.Function;
028 import org.mozilla.javascript.ScriptableObject;
029
030
033 public class RTLCSSUtil {
034
035 public static String getRtlCss(String fileName, String css)
036 throws Exception {
037
038 Context context = Context.enter();
039
040 String rtlCss = css;
041
042 try {
043 ScriptableObject scope = context.initStandardObjects();
044
045 context.evaluateString(scope, _jsScript, "script", 1, null);
046
047 Function function = (Function)scope.get("r2", scope);
048
049 Object result = function.call(
050 context, scope, scope, new Object[] {css});
051
052 rtlCss = (String)Context.jsToJava(result, String.class);
053 }
054 catch (Exception e) {
055 if (_log.isDebugEnabled()) {
056 _log.debug("Unable to parse " + fileName + " to RTL");
057 }
058 }
059 finally {
060 Context.exit();
061 }
062
063 return rtlCss;
064 }
065
066 public static void init() {
067 try {
068 _jsScript = StringUtil.read(
069 ClassLoaderUtil.getPortalClassLoader(),
070 "com/liferay/portal/servlet/filters/dynamiccss" +
071 "/dependencies/r2.js");
072 }
073 catch (Exception e) {
074 _log.error(e, e);
075 }
076 }
077
078 public static boolean isExcludedPath(String filePath) {
079 for (Pattern pattern : _RTL_CSS_EXCLUDED_PATHS_PATTERNS) {
080 Matcher matcher = pattern.matcher(filePath);
081
082 if (matcher.matches()) {
083 return true;
084 }
085 }
086
087 return false;
088 }
089
090 private static Log _log = LogFactoryUtil.getLog(RTLCSSUtil.class);
091
092 private static Pattern[] _RTL_CSS_EXCLUDED_PATHS_PATTERNS;
093
094 static {
095 String[] rtlCssExcludedPathsRegexp =
096 PropsValues.RTL_CSS_EXCLUDED_PATHS_REGEXP;
097
098 _RTL_CSS_EXCLUDED_PATHS_PATTERNS =
099 new Pattern[rtlCssExcludedPathsRegexp.length];
100
101 for (int i = 0; i < rtlCssExcludedPathsRegexp.length; i++) {
102 _RTL_CSS_EXCLUDED_PATHS_PATTERNS[i] = Pattern.compile(
103 rtlCssExcludedPathsRegexp[i]);
104 }
105 }
106
107 private static String _jsScript;
108
109 }