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.FileNotFoundException;
20import java.io.IOException;
21import java.util.Date;
22import java.util.TimeZone;
23
24/**
25 * Utility class that delegates all methods of an HDF object. Made to
26 * facilitate the transition to HDF being an interface and thus not
27 * extensible in the same way as it was.
28 * <p>
29 * This class, and its subclasses must take care to wrap or unwrap HDF and CS
30 * objects as they are passed through from the callers to the delegate object.
31 */
32public abstract class DelegatedHdf implements HDF {
33
34  private final HDF hdf;
35
36  public DelegatedHdf(HDF hdf) {
37    if (hdf == null) {
38      throw new NullPointerException("Null HDF is not allowed in constructor of DelegatedHdf.");
39    }
40    this.hdf = hdf;
41  }
42
43  /**
44   * Utility function for concrete ClearsilverFactories to unwrap DelegatedHdfs
45   * and get down to a concrete (or unknown) HDF object.
46   * @param hdf the possibly DelegatedHdf to unwrap
47   * @return the innermost non-DelegatedHdf HDF object.
48   */
49  public static HDF getFullyUnwrappedHdf(HDF hdf) {
50    while (hdf instanceof DelegatedHdf) {
51      hdf = ((DelegatedHdf)hdf).getHdf();
52    }
53    return hdf;
54  }
55
56  public HDF getHdf() {
57    return hdf;
58  }
59
60  /**
61   * Method subclasses are required to override with a method that returns a
62   * new DelegatedHdf object that wraps the specified HDF object.
63   *
64   * @param hdf an HDF object that should be wrapped in a new DelegatedHdf
65   * object of the same type as this current object.
66   * @return an object that is a subclass of DelegatedHdf and which wraps the
67   * given HDF object.
68   */
69  protected abstract DelegatedHdf newDelegatedHdf(HDF hdf);
70
71  public void close() {
72    getHdf().close();
73  }
74
75  public boolean readFile(String filename) throws IOException, FileNotFoundException {
76    return getHdf().readFile(filename);
77  }
78
79  public CSFileLoader getFileLoader() {
80    return getHdf().getFileLoader();
81  }
82
83  public void setFileLoader(CSFileLoader fileLoader) {
84    getHdf().setFileLoader(fileLoader);
85  }
86
87  public boolean writeFile(String filename) throws IOException {
88    return getHdf().writeFile(filename);
89  }
90
91  public boolean readString(String data) {
92    return getHdf().readString(data);
93  }
94
95  public String writeString() {
96    return getHdf().writeString();
97  }
98
99  public int getIntValue(String hdfname,
100      int default_value) {
101    return getHdf().getIntValue(hdfname, default_value);
102  }
103
104  public String getValue(String hdfname, String default_value) {
105    return getHdf().getValue(hdfname, default_value);
106  }
107
108  public void setValue(
109      String hdfname, String value) {
110    getHdf().setValue(hdfname, value);
111  }
112
113  public void removeTree(String hdfname) {
114    getHdf().removeTree(hdfname);
115  }
116
117  public void setSymLink(String hdf_name_src,
118      String hdf_name_dest) {
119    getHdf().setSymLink(hdf_name_src, hdf_name_dest);
120  }
121
122  public void exportDate(
123      String hdfname, TimeZone timeZone, Date date) {
124    getHdf().exportDate(hdfname, timeZone, date);
125  }
126
127  public void exportDate(
128      String hdfname, String tz, int tt) {
129    getHdf().exportDate(hdfname, tz, tt);
130  }
131
132  public DelegatedHdf getObj(String hdfpath) {
133    HDF hdf = getHdf().getObj(hdfpath);
134    return hdf != null ? newDelegatedHdf(hdf) : null;
135  }
136
137  public DelegatedHdf getChild(String hdfpath) {
138    HDF hdf = getHdf().getChild(hdfpath);
139    return hdf != null ? newDelegatedHdf(hdf) : null;
140  }
141
142  public DelegatedHdf getRootObj() {
143    HDF hdf = getHdf().getRootObj();
144    return hdf != null ? newDelegatedHdf(hdf) : null;
145  }
146
147  public boolean belongsToSameRoot(HDF hdf) {
148    return getFullyUnwrappedHdf(this).belongsToSameRoot(getFullyUnwrappedHdf(hdf));
149  }
150
151  public DelegatedHdf getOrCreateObj(String hdfpath) {
152    HDF hdf = getHdf().getOrCreateObj(hdfpath);
153    return hdf != null ? newDelegatedHdf(hdf) : null;
154  }
155
156  public String objName() {
157    return getHdf().objName();
158  }
159
160  public String objValue() {
161    return getHdf().objValue();
162  }
163
164  public DelegatedHdf objChild() {
165    HDF hdf = getHdf().objChild();
166    return hdf != null ? newDelegatedHdf(hdf) : null;
167  }
168
169  public DelegatedHdf objNext() {
170    HDF hdf = getHdf().objNext();
171    return hdf != null ? newDelegatedHdf(hdf) : null;
172  }
173
174  public void copy(String hdfpath, HDF src) {
175    if (src != null && src instanceof DelegatedHdf) {
176      src = ((DelegatedHdf)src).getHdf();
177    }
178    getHdf().copy(hdfpath, src);
179  }
180
181  public String dump() {
182    return getHdf().dump();
183  }
184}
185
186