001/*
002 * Copyright 2012-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.groovy;
018
019import java.io.File;
020import java.io.IOException;
021import java.io.StringWriter;
022import java.net.URL;
023import java.util.Collections;
024import java.util.Map;
025
026import groovy.lang.Writable;
027import groovy.text.GStringTemplateEngine;
028import groovy.text.Template;
029import groovy.text.TemplateEngine;
030import org.codehaus.groovy.control.CompilationFailedException;
031
032/**
033 * Helpful utilities for working with Groovy {@link Template}s.
034 *
035 * @author Dave Syer
036 */
037public abstract class GroovyTemplate {
038
039        public static String template(String name)
040                        throws IOException, CompilationFailedException, ClassNotFoundException {
041                return template(name, Collections.<String, Object>emptyMap());
042        }
043
044        public static String template(String name, Map<String, ?> model)
045                        throws IOException, CompilationFailedException, ClassNotFoundException {
046                return template(new GStringTemplateEngine(), name, model);
047        }
048
049        public static String template(TemplateEngine engine, String name,
050                        Map<String, ?> model) throws IOException, CompilationFailedException,
051                                        ClassNotFoundException {
052                Writable writable = getTemplate(engine, name).make(model);
053                StringWriter result = new StringWriter();
054                writable.writeTo(result);
055                return result.toString();
056        }
057
058        private static Template getTemplate(TemplateEngine engine, String name)
059                        throws CompilationFailedException, ClassNotFoundException, IOException {
060
061                File file = new File("templates", name);
062                if (file.exists()) {
063                        return engine.createTemplate(file);
064                }
065
066                ClassLoader classLoader = GroovyTemplate.class.getClassLoader();
067                URL resource = classLoader.getResource("templates/" + name);
068                if (resource != null) {
069                        return engine.createTemplate(resource);
070                }
071
072                return engine.createTemplate(name);
073        }
074
075}