001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.regex.PatternFactory;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.util.ClassLoaderUtil;
023    import com.liferay.portal.util.PropsValues;
024    
025    import java.util.regex.Matcher;
026    import java.util.regex.Pattern;
027    
028    import org.mozilla.javascript.Context;
029    import org.mozilla.javascript.Function;
030    import org.mozilla.javascript.ScriptableObject;
031    
032    /**
033     * @author Eduardo Garcia
034     */
035    public class RTLCSSUtil {
036    
037            public static String getRtlCss(String fileName, String css)
038                    throws Exception {
039    
040                    Context context = Context.enter();
041    
042                    String rtlCss = css;
043    
044                    try {
045                            ScriptableObject scope = context.initStandardObjects();
046    
047                            context.evaluateString(scope, _jsScript, "script", 1, null);
048    
049                            Function function = (Function)scope.get("r2", scope);
050    
051                            Object result = function.call(
052                                    context, scope, scope, new Object[] {css});
053    
054                            rtlCss = (String)Context.jsToJava(result, String.class);
055                    }
056                    catch (Exception e) {
057                            _log.error(
058                                    "Unable to generate RTL version for " + fileName +
059                                            StringPool.COMMA_AND_SPACE + e.getMessage());
060                    }
061                    finally {
062                            Context.exit();
063                    }
064    
065                    return rtlCss;
066            }
067    
068            public static void init() {
069                    try {
070                            _jsScript = StringUtil.read(
071                                    ClassLoaderUtil.getPortalClassLoader(),
072                                    "com/liferay/portal/servlet/filters/dynamiccss" +
073                                            "/dependencies/r2.js");
074                    }
075                    catch (Exception e) {
076                            _log.error(e, e);
077                    }
078            }
079    
080            public static boolean isExcludedPath(String filePath) {
081                    for (Pattern pattern : _patterns) {
082                            Matcher matcher = pattern.matcher(filePath);
083    
084                            if (matcher.matches()) {
085                                    return true;
086                            }
087                    }
088    
089                    return false;
090            }
091    
092            private static final Log _log = LogFactoryUtil.getLog(RTLCSSUtil.class);
093    
094            private static String _jsScript;
095            private static final Pattern[] _patterns = PatternFactory.compile(
096                    PropsValues.RTL_CSS_EXCLUDED_PATHS_REGEXP);
097    
098    }