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.io.FileNotFoundException;
025import java.net.URI;
026
027import org.granite.generator.Input;
028import org.granite.generator.Listener;
029import org.granite.generator.Output;
030import org.granite.generator.TemplateUri;
031import org.granite.generator.Transformer;
032import org.granite.generator.exception.TemplateUriException;
033import org.granite.util.URIUtil;
034
035/**
036 * @author Franck WOLFF
037 */
038public abstract class AbstractGroovyTransformer<I extends Input<?>, O extends Output<?>, C extends GroovyConfiguration>
039        extends Transformer<I, O, C> {
040
041        public AbstractGroovyTransformer() {
042                super();
043        }
044
045        public AbstractGroovyTransformer(GroovyConfiguration config, Listener listener) {
046                super(config, listener);
047        }
048
049        protected GroovyTemplateFactory getTemplateFactory() {
050                return getConfig().getGroovyTemplateFactory();
051        }
052
053    protected GroovyTemplate getTemplate(TemplateUri templateUri) throws TemplateUriException {
054        return getTemplate(templateUri.getUri(), templateUri.isBase());
055    }
056
057    protected GroovyTemplate getTemplate(String path, boolean base) throws TemplateUriException {
058        GroovyTemplateFactory factory = getTemplateFactory();
059        try {
060                path = URIUtil.normalize(path);
061                URI uri = new URI(path);
062                String schemeSpecificPart = uri.getSchemeSpecificPart();
063                if (schemeSpecificPart == null || schemeSpecificPart.length() == 0)
064                        throw new FileNotFoundException("Template path cannot be empty: " + uri);
065                
066                if (URIUtil.isFileURI(uri) && !URIUtil.isAbsolute(uri)) {
067                        URI parent = getConfig().getWorkingDirectory().toURI();
068                        if (parent != null)
069                                uri = parent.resolve(uri.getRawSchemeSpecificPart());
070                }
071            return factory.getTemplate(uri, base);
072        } catch (Exception e) {
073            throw new TemplateUriException(path, e);
074        }
075    }
076}