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.freemarker;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import freemarker.cache.TemplateLoader;
020    
021    import java.io.Reader;
022    import java.io.StringReader;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    /**
028     * @author Michael C. Han
029     */
030    public class StringTemplateLoader implements TemplateLoader {
031    
032            public void closeTemplateSource(Object templateSource) {
033            }
034    
035            public Object findTemplateSource(String name) {
036                    return _templates.get(name);
037            }
038    
039            public long getLastModified(Object templateSource) {
040                    StringTemplateSource stringTemplateSource =
041                            (StringTemplateSource)templateSource;
042    
043                    return stringTemplateSource._lastModified;
044            }
045    
046            public Reader getReader(Object templateSource, String encoding) {
047                    StringTemplateSource stringTemplateSource =
048                            (StringTemplateSource)templateSource;
049    
050                    return new StringReader(stringTemplateSource._templateSource);
051            }
052    
053            public void putTemplate(String name, String templateSource) {
054                    putTemplate(name, templateSource, System.currentTimeMillis());
055            }
056    
057            public void putTemplate(
058                    String name, String templateSource, long lastModified) {
059    
060                    _templates.put(
061                            name, new StringTemplateSource(name, templateSource, lastModified));
062            }
063    
064            public void removeTemplate(String name) {
065                    _templates.remove(name);
066            }
067    
068            private Map<String, StringTemplateSource> _templates =
069                    new HashMap<String, StringTemplateSource>();
070    
071            private class StringTemplateSource {
072    
073                    public StringTemplateSource(
074                            String name, String templateSource, long lastModified) {
075    
076                            if (name == null) {
077                                    throw new IllegalArgumentException("Name is null");
078                            }
079    
080                            if (templateSource == null) {
081                                    throw new IllegalArgumentException("Template source is null");
082                            }
083    
084                            if (lastModified < -1) {
085                                    throw new IllegalArgumentException(
086                                            "Last modified is less than -1");
087                            }
088    
089                            _name = name;
090                            _templateSource = templateSource;
091                            _lastModified = lastModified;
092                    }
093    
094                    @Override
095                    public boolean equals(Object obj) {
096                            if (this == obj) {
097                                    return true;
098                            }
099    
100                            if (!(obj instanceof StringTemplateSource)) {
101                                    return false;
102                            }
103    
104                            StringTemplateSource stringTemplateSource =
105                                    (StringTemplateSource)obj;
106    
107                            if (Validator.equals(_name, stringTemplateSource._name)) {
108                                    return true;
109                            }
110    
111                            return false;
112                    }
113    
114                    @Override
115                    public int hashCode() {
116                            return _name.hashCode();
117                    }
118    
119                    private long _lastModified;
120                    private String _name;
121                    private String _templateSource;
122    
123            }
124    
125    }