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
17package org.clearsilver;
18
19import java.io.IOException;
20
21/**
22 * Utility class that delegates all methods of an CS object. Made to
23 * facilitate the transition to CS being an interface and thus not
24 * extensible in the same way as it was.
25 * <p>
26 * This class, and its subclasses must take care to wrap or unwrap HDF and CS
27 * objects as they are passed through from the callers to the delegate object.
28 *
29 */
30public abstract class DelegatedCs implements CS {
31  private final CS cs;
32
33  public DelegatedCs(CS cs) {
34    // Give it an empty HDF. We aren't going to be using the super object anyways.
35    this.cs = cs;
36  }
37
38  public CS getCs() {
39    return cs;
40  }
41
42  /**
43   * Method subclasses are required to override with a method that returns a
44   * new DelegatedHdf object that wraps the specified HDF object.
45   *
46   * @param hdf an HDF object that should be wrapped in a new DelegatedHdf
47   * object of the same type as this current object.
48   * @return an object that is a subclass of DelegatedHdf and which wraps the
49   * given HDF object.
50   */
51  protected abstract DelegatedHdf newDelegatedHdf(HDF hdf);
52
53  public void setGlobalHDF(HDF global) {
54    if (global != null && global instanceof DelegatedHdf) {
55      global = ((DelegatedHdf)global).getHdf();
56    }
57    getCs().setGlobalHDF(global);
58  }
59
60  public HDF getGlobalHDF() {
61    HDF hdf =  getCs().getGlobalHDF();
62    return hdf != null ? newDelegatedHdf(hdf) : null;
63  }
64
65  public void close() {
66    getCs().close();
67  }
68
69  public void parseFile(String filename) throws IOException {
70    getCs().parseFile(filename);
71  }
72  public void parseStr(String content) {
73    getCs().parseStr(content);
74  }
75
76  public String render() {
77    return getCs().render();
78  }
79
80  public CSFileLoader getFileLoader() {
81    return getCs().getFileLoader();
82  }
83
84  public void setFileLoader(CSFileLoader fileLoader) {
85    getCs().setFileLoader(fileLoader);
86  }
87
88}
89