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.template;
016    
017    import com.liferay.portal.kernel.template.Template;
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.kernel.util.StringPool;
022    
023    import java.io.Writer;
024    
025    import java.util.Collection;
026    import java.util.HashMap;
027    import java.util.Map;
028    import java.util.Set;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Tina Tian
034     */
035    public abstract class AbstractTemplate implements Template {
036    
037            public AbstractTemplate(
038                    TemplateResource errorTemplateResource, Map<String, Object> context,
039                    TemplateContextHelper templateContextHelper,
040                    String templateManagerName) {
041    
042                    if (templateContextHelper == null) {
043                            throw new IllegalArgumentException(
044                                    "Template context helper is null");
045                    }
046    
047                    if (templateManagerName == null) {
048                            throw new IllegalArgumentException("Template manager name is null");
049                    }
050    
051                    this.errorTemplateResource = errorTemplateResource;
052    
053                    this.context = new HashMap<>();
054    
055                    if (context != null) {
056                            for (Map.Entry<String, Object> entry : context.entrySet()) {
057                                    put(entry.getKey(), entry.getValue());
058                            }
059                    }
060    
061                    _templateContextHelper = templateContextHelper;
062            }
063    
064            @Override
065            public void clear() {
066                    context.clear();
067            }
068    
069            @Override
070            public boolean containsKey(Object key) {
071                    return context.containsKey(key);
072            }
073    
074            @Override
075            public boolean containsValue(Object value) {
076                    return context.containsValue(value);
077            }
078    
079            @Override
080            public Set<Entry<String, Object>> entrySet() {
081                    return context.entrySet();
082            }
083    
084            @Override
085            public Object get(Object key) {
086                    if (key == null) {
087                            return null;
088                    }
089    
090                    return context.get(key);
091            }
092    
093            @Override
094            public Object get(String key) {
095                    if (key == null) {
096                            return null;
097                    }
098    
099                    return context.get(key);
100            }
101    
102            @Override
103            public String[] getKeys() {
104                    Set<String> keys = context.keySet();
105    
106                    return keys.toArray(new String[keys.size()]);
107            }
108    
109            @Override
110            public boolean isEmpty() {
111                    return context.isEmpty();
112            }
113    
114            @Override
115            public Set<String> keySet() {
116                    return context.keySet();
117            }
118    
119            @Override
120            public void prepare(HttpServletRequest request) {
121                    _templateContextHelper.prepare(this, request);
122            }
123    
124            @Override
125            public Object put(String key, Object value) {
126                    if ((key == null) || (value == null)) {
127                            return null;
128                    }
129    
130                    return context.put(key, value);
131            }
132    
133            @Override
134            public void putAll(Map<? extends String, ? extends Object> map) {
135                    context.putAll(map);
136            }
137    
138            @Override
139            public Object remove(Object key) {
140                    return context.remove(key);
141            }
142    
143            @Override
144            public int size() {
145                    return context.size();
146            }
147    
148            @Override
149            public Collection<Object> values() {
150                    return context.values();
151            }
152    
153            protected void _write(Writer writer) throws TemplateException {
154                    Writer oldWriter = (Writer)get(TemplateConstants.WRITER);
155    
156                    try {
157                            doProcessTemplate(writer);
158                    }
159                    catch (Exception e) {
160                            put(TemplateConstants.WRITER, writer);
161    
162                            handleException(e, writer);
163                    }
164                    finally {
165                            put(TemplateConstants.WRITER, oldWriter);
166                    }
167            }
168    
169            protected String getTemplateResourceUUID(
170                    TemplateResource templateResource) {
171    
172                    return TemplateConstants.TEMPLATE_RESOURCE_UUID_PREFIX.concat(
173                            StringPool.POUND).concat(templateResource.getTemplateId());
174            }
175    
176            protected abstract void handleException(Exception exception, Writer writer)
177                    throws TemplateException;
178    
179            protected Map<String, Object> context;
180            protected TemplateResource errorTemplateResource;
181    
182            private final TemplateContextHelper _templateContextHelper;
183    
184    }