001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022
023package org.granite.generator.gsp;
024
025import java.net.URI;
026import java.nio.charset.Charset;
027import java.util.ArrayList;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032/**
033 * @author Franck WOLFF
034 */
035public class GroovyTemplateFactory {
036
037    ///////////////////////////////////////////////////////////////////////////
038    // Fields.
039
040    private final Map<URI, GroovyTemplate> templatesMap = new HashMap<URI, GroovyTemplate>();
041
042    ///////////////////////////////////////////////////////////////////////////
043    // Constructor.
044
045    public GroovyTemplateFactory() {
046    }
047
048    ///////////////////////////////////////////////////////////////////////////
049    // Checking.
050
051    public boolean isOutdated() {
052        for (GroovyTemplate template : templatesMap.values()) {
053                if (template.isOutdated())
054                        return true;
055        }
056        return false;
057    }
058
059    public void cleanOutdated() {
060        List<URI> outdated = new ArrayList<URI>();
061        for (GroovyTemplate template : templatesMap.values()) {
062                if (template.isOutdated())
063                        outdated.add(template.getUri());
064        }
065        for (URI uri : outdated)
066                templatesMap.remove(uri);
067    }
068
069    ///////////////////////////////////////////////////////////////////////////
070    // Cleanup.
071
072    public void clear() {
073        templatesMap.clear();
074    }
075
076    public void clear(URI uri) {
077        templatesMap.remove(uri);
078    }
079
080    ///////////////////////////////////////////////////////////////////////////
081    // Template loading.
082
083    public GroovyTemplate getTemplate(URI uri, boolean baseTemplate) {
084        return getTemplate(uri, baseTemplate, Charset.defaultCharset());
085    }
086
087    public GroovyTemplate getTemplate(URI uri, boolean baseTemplate, Charset charset) {
088        if (uri == null || charset == null)
089            throw new IllegalArgumentException("uri and charset cannot be null");
090
091        GroovyTemplate template = templatesMap.get(uri);
092
093        if (template == null) {
094            template = new GroovyTemplate(uri, baseTemplate, charset);
095            templatesMap.put(uri, template);
096        }
097
098        return template;
099    }
100}