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