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