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.template.StringTemplateResource;
018    import com.liferay.portal.kernel.template.TemplateConstants;
019    import com.liferay.portal.kernel.template.TemplateException;
020    import com.liferay.portal.kernel.template.TemplateResource;
021    import com.liferay.portal.template.AbstractTemplate;
022    import com.liferay.portal.template.TemplateContextHelper;
023    import com.liferay.portal.template.TemplateResourceThreadLocal;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.io.Writer;
027    
028    import org.apache.velocity.Template;
029    import org.apache.velocity.VelocityContext;
030    import org.apache.velocity.app.VelocityEngine;
031    import org.apache.velocity.exception.ParseErrorException;
032    
033    /**
034     * @author Tina Tian
035     */
036    public class VelocityTemplate extends AbstractTemplate {
037    
038            public VelocityTemplate(
039                    TemplateResource templateResource,
040                    TemplateResource errorTemplateResource, VelocityContext velocityContext,
041                    VelocityEngine velocityEngine,
042                    TemplateContextHelper templateContextHelper) {
043    
044                    super(
045                            templateResource, errorTemplateResource, templateContextHelper,
046                            TemplateConstants.LANG_TYPE_VM,
047                            PropsValues.VELOCITY_ENGINE_RESOURCE_MODIFICATION_CHECK_INTERVAL);
048    
049                    if (velocityContext == null) {
050                            _velocityContext = new VelocityContext();
051                    }
052                    else {
053                            _velocityContext = new VelocityContext(velocityContext);
054                    }
055    
056                    _velocityEngine = velocityEngine;
057            }
058    
059            public Object get(String key) {
060                    return _velocityContext.get(key);
061            }
062    
063            public void put(String key, Object value) {
064                    if (value == null) {
065                            return;
066                    }
067    
068                    _velocityContext.put(key, value);
069            }
070    
071            @Override
072            protected void handleException(Exception exception, Writer writer)
073                    throws TemplateException {
074    
075                    put("exception", exception.getMessage());
076    
077                    if (templateResource instanceof StringTemplateResource) {
078                            StringTemplateResource stringTemplateResource =
079                                    (StringTemplateResource)templateResource;
080    
081                            put("script", stringTemplateResource.getContent());
082                    }
083    
084                    if (exception instanceof ParseErrorException) {
085                            ParseErrorException pee = (ParseErrorException)exception;
086    
087                            put("column", pee.getColumnNumber());
088                            put("line", pee.getLineNumber());
089                    }
090    
091                    try {
092                            processTemplate(errorTemplateResource, writer);
093                    }
094                    catch (Exception e) {
095                            throw new TemplateException(
096                                    "Unable to process Velocity template " +
097                                            errorTemplateResource.getTemplateId(),
098                                    e);
099                    }
100            }
101    
102            @Override
103            protected void processTemplate(
104                            TemplateResource templateResource, Writer writer)
105                    throws Exception {
106    
107                    TemplateResourceThreadLocal.setTemplateResource(
108                            TemplateConstants.LANG_TYPE_VM, templateResource);
109    
110                    try {
111                            Template template = _velocityEngine.getTemplate(
112                                    getTemplateResourceUUID(templateResource),
113                                    TemplateConstants.DEFAUT_ENCODING);
114    
115                            template.merge(_velocityContext, writer);
116                    }
117                    finally {
118                            TemplateResourceThreadLocal.setTemplateResource(
119                                    TemplateConstants.LANG_TYPE_VM, null);
120                    }
121            }
122    
123            private VelocityContext _velocityContext;
124            private VelocityEngine _velocityEngine;
125    
126    }