1 package org.clearsilver; 2 3 import java.io.*; 4 import java.util.*; 5 6 public class CS { 7 public long csptr; 8 9 protected HDF globalHDF; 10 protected HDF localHDF; 11 12 static { JNI.loadLibrary()13 JNI.loadLibrary(); 14 } 15 CS(HDF ho)16 public CS(HDF ho) { 17 this.globalHDF = null; 18 this.localHDF = ho; 19 csptr = _init(ho.hdfptr); 20 } 21 22 // Construct with global HDF to search if local HDF returns null CS(HDF ho, HDF global)23 public CS(HDF ho, HDF global) { 24 this(ho); 25 26 this.globalHDF = global; 27 if (global != null) { 28 _setGlobalHdf(csptr,global.hdfptr); 29 } 30 } 31 32 // Specify a new/different global HDF setGlobalHDF(HDF global)33 public void setGlobalHDF(HDF global) { 34 _setGlobalHdf(csptr,global.hdfptr); 35 this.globalHDF = global; 36 } 37 38 // Return global hdf in use getGlobalHDF()39 public HDF getGlobalHDF() { 40 return this.globalHDF; 41 } 42 close()43 public void close() { 44 if (csptr != 0) { 45 _dealloc(csptr); 46 csptr = 0; 47 } 48 } 49 finalize()50 public void finalize() { 51 close(); 52 } 53 parseFile(String filename)54 public void parseFile(String filename) { 55 if (csptr == 0) { 56 throw new NullPointerException("CS is closed."); 57 } 58 _parseFile(csptr, filename, fileLoader != null); 59 } 60 parseStr(String content)61 public void parseStr(String content) { 62 if (csptr == 0) { 63 throw new NullPointerException("CS is closed."); 64 } 65 _parseStr(csptr,content); 66 } 67 render()68 public String render() { 69 if (csptr == 0) { 70 throw new NullPointerException("CS is closed."); 71 } 72 return _render(csptr); 73 } 74 75 fileLoad(String filename)76 protected String fileLoad(String filename) throws IOException, 77 FileNotFoundException { 78 if (csptr == 0) { 79 throw new NullPointerException("CS is closed."); 80 } 81 CSFileLoader aFileLoader = fileLoader; 82 if (aFileLoader == null) { 83 throw new NullPointerException("No fileLoader specified."); 84 } else { 85 String result = aFileLoader.load(localHDF, filename); 86 if (result == null) { 87 throw new NullPointerException("CSFileLoader.load() returned null"); 88 } 89 return result; 90 } 91 } 92 93 // The optional CS file loader to use to read in files 94 private CSFileLoader fileLoader = null; 95 96 /** 97 * Get the file loader in use, if any. 98 * @return the file loader in use. 99 */ getFileLoader()100 public CSFileLoader getFileLoader() { 101 return fileLoader; 102 } 103 104 /** 105 * Set the CS file loader to use 106 * @param fileLoader the file loader that should be used. 107 */ setFileLoader(CSFileLoader fileLoader)108 public void setFileLoader(CSFileLoader fileLoader) { 109 this.fileLoader = fileLoader; 110 } 111 112 _init(long ptr)113 private native long _init(long ptr); _dealloc(long ptr)114 private native void _dealloc(long ptr); _parseFile(long ptr, String filename, boolean use_cb)115 private native void _parseFile(long ptr, String filename, 116 boolean use_cb); _parseStr(long ptr, String content)117 private native void _parseStr(long ptr, String content); _render(long ptr)118 private native String _render(long ptr); _setGlobalHdf(long csptr, long hdfptr)119 private native void _setGlobalHdf(long csptr, long hdfptr); 120 }; 121