001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2014 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 */
022package org.granite.generator.gsp;
023
024import java.net.URI;
025import java.nio.charset.Charset;
026import java.util.ArrayList;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030
031/**
032 * @author Franck WOLFF
033 */
034public class GroovyTemplateFactory {
035
036    ///////////////////////////////////////////////////////////////////////////
037    // Fields.
038
039    private final Map<URI, GroovyTemplate> templatesMap = new HashMap<URI, GroovyTemplate>();
040
041    ///////////////////////////////////////////////////////////////////////////
042    // Constructor.
043
044    public GroovyTemplateFactory() {
045    }
046
047    ///////////////////////////////////////////////////////////////////////////
048    // Checking.
049
050    public boolean isOutdated() {
051        for (GroovyTemplate template : templatesMap.values()) {
052                if (template.isOutdated())
053                        return true;
054        }
055        return false;
056    }
057
058    public void cleanOutdated() {
059        List<URI> outdated = new ArrayList<URI>();
060        for (GroovyTemplate template : templatesMap.values()) {
061                if (template.isOutdated())
062                        outdated.add(template.getUri());
063        }
064        for (URI uri : outdated)
065                templatesMap.remove(uri);
066    }
067
068    ///////////////////////////////////////////////////////////////////////////
069    // Cleanup.
070
071    public void clear() {
072        templatesMap.clear();
073    }
074
075    public void clear(URI uri) {
076        templatesMap.remove(uri);
077    }
078
079    ///////////////////////////////////////////////////////////////////////////
080    // Template loading.
081
082    public GroovyTemplate getTemplate(URI uri, boolean baseTemplate) {
083        return getTemplate(uri, baseTemplate, Charset.defaultCharset());
084    }
085
086    public GroovyTemplate getTemplate(URI uri, boolean baseTemplate, Charset charset) {
087        if (uri == null || charset == null)
088            throw new IllegalArgumentException("uri and charset cannot be null");
089
090        GroovyTemplate template = templatesMap.get(uri);
091
092        if (template == null) {
093            template = new GroovyTemplate(uri, baseTemplate, charset);
094            templatesMap.put(uri, template);
095        }
096
097        return template;
098    }
099}