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