Home | History | Annotate | Download | only in adaptor
      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.adaptor;
     18 
     19 import com.google.clearsilver.jsilver.JSilver;
     20 import com.google.clearsilver.jsilver.autoescape.EscapeMode;
     21 import com.google.clearsilver.jsilver.data.Data;
     22 import com.google.clearsilver.jsilver.data.LocalAndGlobalData;
     23 import com.google.clearsilver.jsilver.exceptions.JSilverIOException;
     24 import com.google.clearsilver.jsilver.template.HtmlWhiteSpaceStripper;
     25 import com.google.clearsilver.jsilver.template.Template;
     26 
     27 import org.clearsilver.CS;
     28 import org.clearsilver.CSFileLoader;
     29 import org.clearsilver.HDF;
     30 
     31 import java.io.IOException;
     32 
     33 /**
     34  * Adaptor that wraps a JSilver object so it can be used as an CS object.
     35  */
     36 class JCs implements CS {
     37 
     38   private final JHdf localHdf;
     39   private JHdf globalHdf;
     40   private final JSilver jSilver;
     41   private final LoadPathToFileCache loadPathCache;
     42   private Template template = null;
     43   private CSFileLoader csFileLoader;
     44   private ResourceLoaderAdaptor resourceLoaderAdaptor;
     45 
     46   JCs(JHdf hdf, JSilver jSilver, LoadPathToFileCache loadPathCache) {
     47     this.localHdf = hdf;
     48     this.jSilver = jSilver;
     49     this.loadPathCache = loadPathCache;
     50 
     51     resourceLoaderAdaptor = localHdf.getResourceLoaderAdaptor();
     52     csFileLoader = resourceLoaderAdaptor.getCSFileLoader();
     53   }
     54 
     55   /**
     56    * Want to delay creating the JSilver object so we can specify necessary parameters.
     57    */
     58   private JSilver getJSilver() {
     59     return jSilver;
     60   }
     61 
     62   @Override
     63   public void setGlobalHDF(HDF global) {
     64     globalHdf = JHdf.cast(global);
     65   }
     66 
     67   @Override
     68   public HDF getGlobalHDF() {
     69     return globalHdf;
     70   }
     71 
     72   @Override
     73   public void close() {
     74     // Removing unneeded reference, although this is not expected to have the
     75     // performance impact seen in JHdf as in production configurations users
     76     // should be using cached templates so they are long-lived.
     77     template = null;
     78   }
     79 
     80   @Override
     81   public void parseFile(String filename) throws IOException {
     82     try {
     83       if (getEscapeMode().isAutoEscapingMode()) {
     84         if (localHdf.getData().getValue("Config.PropagateEscapeStatus") != null) {
     85           throw new IllegalArgumentException(
     86               "Config.PropagateEscapeStatus does not work with JSilver."
     87                   + "Use JSilverOptions.setPropagateEscapeStatus instead");
     88         }
     89       }
     90       template =
     91           getJSilver().getTemplateLoader().load(filename, resourceLoaderAdaptor, getEscapeMode());
     92     } catch (RuntimeException e) {
     93       Throwable th = e;
     94       if (th instanceof JSilverIOException) {
     95         // JSilverIOException always has an IOException as its cause.
     96         throw ((IOException) th.getCause());
     97       }
     98       throw e;
     99     }
    100   }
    101 
    102   @Override
    103   public void parseStr(String content) {
    104     if (getEscapeMode().isAutoEscapingMode()) {
    105       if (localHdf.getData().getValue("Config.PropagateEscapeStatus") != null) {
    106         throw new IllegalArgumentException(
    107             "Config.PropagateEscapeStatus does not work with JSilver."
    108                 + "Use JSilverOptions.setPropagateEscapeStatus instead");
    109       }
    110     }
    111     template = getJSilver().getTemplateLoader().createTemp("parseStr", content, getEscapeMode());
    112   }
    113 
    114   private EscapeMode getEscapeMode() {
    115     Data data = localHdf.getData();
    116     return getJSilver().getEscapeMode(data);
    117   }
    118 
    119   @Override
    120   public String render() {
    121     if (template == null) {
    122       throw new IllegalStateException("Call parseFile() or parseStr() before " + "render()");
    123     }
    124 
    125     Data data;
    126     if (globalHdf != null) {
    127       // For legacy support we allow users to pass in this option to disable
    128       // the new modification protection for global HDF.
    129       data =
    130           new LocalAndGlobalData(localHdf.getData(), globalHdf.getData(), jSilver.getOptions()
    131               .getAllowGlobalDataModification());
    132     } else {
    133       data = localHdf.getData();
    134     }
    135     Appendable buffer = jSilver.createAppendableBuffer();
    136     try {
    137       Appendable output = buffer;
    138       // For Clearsilver compatibility we check this HDF variable to see if we
    139       // need to turn on whitespace stripping. The preferred approach would be
    140       // to turn it on in the JSilverOptions passed to JSilverFactory
    141       int wsStripLevel = localHdf.getIntValue("ClearSilver.WhiteSpaceStrip", 0);
    142       if (wsStripLevel > 0) {
    143         output = new HtmlWhiteSpaceStripper(output, wsStripLevel);
    144       }
    145       jSilver.render(template, data, output, resourceLoaderAdaptor);
    146       return output.toString();
    147     } catch (IOException ioe) {
    148       throw new RuntimeException(ioe);
    149     } finally {
    150       jSilver.releaseAppendableBuffer(buffer);
    151     }
    152   }
    153 
    154   @Override
    155   public CSFileLoader getFileLoader() {
    156     return csFileLoader;
    157   }
    158 
    159   @Override
    160   public void setFileLoader(CSFileLoader fileLoader) {
    161     if (fileLoader == null && csFileLoader == null) {
    162       return;
    163     }
    164     if (fileLoader != null && fileLoader.equals(csFileLoader)) {
    165       return;
    166     }
    167     csFileLoader = fileLoader;
    168     resourceLoaderAdaptor = new ResourceLoaderAdaptor(localHdf, loadPathCache, csFileLoader);
    169   }
    170 }
    171