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