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