001    /**
002     * Copyright (c) 2000-2013 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.velocity;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.util.PropsUtil;
021    
022    import java.util.Map;
023    
024    import org.apache.velocity.VelocityContext;
025    import org.apache.velocity.app.VelocityEngine;
026    import org.apache.velocity.runtime.RuntimeConstants;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class VelocityUtil {
032    
033            public static String evaluate(String input) throws Exception {
034                    return evaluate(input, null);
035            }
036    
037            public static String evaluate(String input, Map<String, Object> variables)
038                    throws Exception {
039    
040                    VelocityEngine velocityEngine = new VelocityEngine();
041    
042                    velocityEngine.setProperty(
043                            RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
044                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER));
045    
046                    velocityEngine.setProperty(
047                            RuntimeConstants.RUNTIME_LOG_LOGSYSTEM + ".log4j.category",
048                            PropsUtil.get(PropsKeys.VELOCITY_ENGINE_LOGGER_CATEGORY));
049    
050                    velocityEngine.init();
051    
052                    VelocityContext velocityContext = new VelocityContext();
053    
054                    if (variables != null) {
055                            for (Map.Entry<String, Object> entry : variables.entrySet()) {
056                                    String key = entry.getKey();
057                                    Object value = entry.getValue();
058    
059                                    if (Validator.isNotNull(key)) {
060                                            velocityContext.put(key, value);
061                                    }
062                            }
063                    }
064    
065                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
066    
067                    velocityEngine.evaluate(
068                            velocityContext, unsyncStringWriter, VelocityUtil.class.getName(),
069                            input);
070    
071                    return unsyncStringWriter.toString();
072            }
073    
074    }