1 18 package net.sf.uitags.util; 19 20 import java.io.IOException ; 21 import java.io.StringWriter ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 26 27 import org.apache.velocity.VelocityContext; 28 import org.apache.velocity.app.Velocity; 29 import org.apache.velocity.exception.MethodInvocationException; 30 import org.apache.velocity.exception.ResourceNotFoundException; 31 32 46 public final class Template { 47 53 private static final String CONFIG = "velocity.properties"; 54 55 public static final String CALENDAR = "uitags/calendar/calendar.vm"; 57 58 public static final String CALENDAR_UPDATE_DATE = 59 "uitags/calendar/updateDate.vm"; 60 61 public static final String CALENDAR_LIST_MONTHS = 62 "uitags/calendar/listMonths.vm"; 63 64 public static final String CALENDAR_LIST_YEARS = 65 "uitags/calendar/listYears.vm"; 66 67 public static final String FORM_GUIDE = "uitags/formGuide/formGuide.vm"; 68 69 public static final String OPTION_TRANSFER = 70 "uitags/optionTransfer/optionTransfer.vm"; 71 72 public static final String OPTION_TRANSFER_RETURN = 73 "uitags/optionTransfer/return.vm"; 74 75 public static final String OPTION_TRANSFER_RETURN_ALL = 76 "uitags/optionTransfer/returnAll.vm"; 77 78 public static final String OPTION_TRANSFER_TRANSFER = 79 "uitags/optionTransfer/transfer.vm"; 80 81 public static final String OPTION_TRANSFER_TRANSFER_ALL = 82 "uitags/optionTransfer/transferAll.vm"; 83 84 public static final String PANEL = "uitags/panel/panel.vm"; 85 86 public static final String PANEL_ANCHOR = "uitags/panel/anchor.vm"; 87 88 public static final String PANEL_DRAG = "uitags/panel/drag.vm"; 89 90 public static final String PANEL_HIDE = "uitags/panel/hide.vm"; 91 92 public static final String PANEL_SHOW = "uitags/panel/show.vm"; 93 94 public static final String PANEL_STICK = "uitags/panel/stick.vm"; 95 96 public static final String SEARCH = "uitags/search/search.vm"; 97 98 public static final String SELECT = "uitags/select/select.vm"; 99 100 public static final String SHIFT = "uitags/shift/shift.vm"; 101 102 public static final String SORT = "uitags/sort/sort.vm"; 103 104 105 106 110 113 private Map params = new HashMap (); 114 117 private String file; 118 119 120 121 125 static { 126 init(); 127 } 128 129 135 private static void init() { 136 Properties props = loadProperties(CONFIG); 137 try { 138 Velocity.init(props); 139 } 140 catch (Exception e) { 141 throw new RuntimeException (e); 144 } 145 } 146 147 154 private static Properties loadProperties(String file) { 155 Properties props = new Properties (); 156 try { 157 props.load(Template.class.getResourceAsStream(file)); 158 } 159 catch (IOException e) { 160 throw new PropertiesLoadingException(e, file); 161 } 162 return props; 163 } 164 165 166 167 171 177 public static Template forName(String name) { 178 return new Template(name); 180 } 181 182 187 private Template(String file) { 188 this.file = file; 189 } 190 191 192 193 197 205 public void map(String key, Object value) { 206 if (key == null) { 207 throw new NullPointerException ( 208 "Key of template parameter can't be null."); 209 } 210 this.params.put(key, value); 211 } 212 213 220 public StringWriter eval() { 221 map("velocityAdapter", new VelocityAdapter()); 222 223 StringWriter result = new StringWriter (); 224 try { 225 Velocity.mergeTemplate( 226 this.file, "ISO8859-1", new VelocityContext(this.params), result); 227 } 228 catch (ResourceNotFoundException e) { 229 throw new RuntimeException (e); 230 } 231 catch (MethodInvocationException e) { 232 throw new RuntimeException (e); 233 } 234 catch (Exception e) { 235 throw new RuntimeException (e); 236 } 237 238 return result; 239 } 240 241 247 public String evalToString() { 248 return eval().toString(); 249 } 250 } 251 | Popular Tags |