Home | History | Annotate | Download | only in interpreter
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.clearsilver.jsilver.interpreter;
     18 
     19 import com.google.clearsilver.jsilver.autoescape.EscapeMode;
     20 import com.google.clearsilver.jsilver.exceptions.JSilverIOException;
     21 import com.google.clearsilver.jsilver.resourceloader.ResourceLoader;
     22 import com.google.clearsilver.jsilver.syntax.SyntaxTreeBuilder;
     23 import com.google.clearsilver.jsilver.syntax.TemplateSyntaxTree;
     24 
     25 import java.io.IOException;
     26 import java.io.Reader;
     27 import java.io.StringReader;
     28 
     29 /**
     30  * Loads a template from disk, and parses it into an AST. Does not do any caching.
     31  */
     32 public class LoadingTemplateFactory implements TemplateFactory {
     33 
     34   private final SyntaxTreeBuilder syntaxTreeBuilder = new SyntaxTreeBuilder();
     35 
     36   public TemplateSyntaxTree find(String templateName, ResourceLoader resourceLoader,
     37       EscapeMode escapeMode) {
     38     try {
     39       Reader reader = resourceLoader.openOrFail(templateName);
     40       try {
     41         return syntaxTreeBuilder.parse(reader, templateName, escapeMode);
     42       } finally {
     43         reader.close();
     44       }
     45     } catch (IOException e) {
     46       throw new JSilverIOException(e);
     47     }
     48   }
     49 
     50   public TemplateSyntaxTree createTemp(String content, EscapeMode escapeMode) {
     51     return syntaxTreeBuilder.parse(new StringReader(content), "", escapeMode);
     52   }
     53 
     54 }
     55