1 22 23 package org.meshcms.core; 24 25 import java.io.*; 26 import java.text.*; 27 import java.util.*; 28 import org.meshcms.taglib.*; 29 import org.meshcms.util.*; 30 31 34 public class ModuleDescriptor { 35 38 public static final String LOCATION_ID = "m_loc"; 39 40 43 public static final String ARGUMENT_ID = "m_arg"; 44 45 48 public static final String TEMPLATE_ID = "m_tpl"; 49 50 53 public static final String PARAMETERS_ID = "m_apm"; 54 55 private String location; 56 private String template; 57 private String argument; 58 private Properties advancedParams; 59 60 private Path pagePath; 61 private Path modulePath; 62 private String dateFormat; 63 private String style; 64 65 68 public ModuleDescriptor() { 69 } 71 72 75 public ModuleDescriptor(String data) { 76 init(data); 77 } 78 79 82 public void init(String data) { 83 if (data.indexOf(LOCATION_ID) < 0) { String [] values = Utils.tokenize(data, ":"); 85 advancedParams = new Properties(); 86 87 if (values != null) { 88 switch (values.length) { 89 case 1: setLocation(""); 91 setTemplate("include.jsp"); 92 setArgument(values[0]); 93 break; 94 case 2: setLocation(""); 96 setTemplate(values[0]); 97 setArgument(values[1]); 98 break; 99 case 3: setLocation(values[0]); 101 setTemplate(values[1]); 102 setArgument(values[2]); 103 } 104 } 105 } else { parseParameters(data); 107 } 108 } 109 110 113 public void parseParameters(String data) { 114 advancedParams = new Properties(); 115 StringTokenizer st = new StringTokenizer(data, ","); 116 String value, param; 117 118 while (st.hasMoreTokens()) { 119 value = st.nextToken().trim(); 120 int eqIdx = value.indexOf('='); 121 122 if (eqIdx != -1) { 123 param = value.substring(0, eqIdx).trim(); 124 value = value.substring(eqIdx + 1).trim(); 125 126 if (param.equals(LOCATION_ID)) { 127 setLocation(value); 128 } else if (param.equals(TEMPLATE_ID)) { 129 setTemplate(value); 130 } else if (param.equals(ARGUMENT_ID)) { 131 setArgument(value); 132 } else { 133 advancedParams.setProperty(param, value); 134 } 135 } 136 } 137 } 138 139 143 public boolean isValid() { 144 return location != null && template != null; 145 } 146 147 152 public String getLocation() { 153 return location; 154 } 155 156 160 public void setLocation(String location) { 161 this.location = location; 162 } 163 164 167 public String getTemplate() { 168 return template; 169 } 170 171 174 public void setTemplate(String template) { 175 this.template = template.endsWith(".jsp") ? 176 template.substring(0, template.length() - 4) : template; 178 } 179 180 185 public String getArgument() { 186 return argument; 187 } 188 189 193 public void setArgument(String s) { 194 argument = PageAssembler.EMPTY.equals(s) ? null : s; 195 } 196 197 200 public Properties getAdvancedParams() { 201 return advancedParams; 202 } 203 204 207 public String getAdvancedParam(String paramName, String defaultValue) { 208 return advancedParams == null ? defaultValue : 209 advancedParams.getProperty(paramName, defaultValue); 210 } 211 212 215 public void setAdvancedParams(Properties advancedParams) { 216 this.advancedParams = advancedParams; 217 } 218 219 222 public Path getPagePath() { 223 return pagePath; 224 } 225 226 229 public void setPagePath(Path pagePath) { 230 this.pagePath = pagePath; 231 } 232 233 236 public Path getModulePath() { 237 return modulePath; 238 } 239 240 public Path getModuleDataPath(WebSite webSite) { 241 return webSite.getModuleDataPath().add(modulePath.getLastElement()); 242 } 243 244 247 public void setModulePath(Path modulePath) { 248 this.modulePath = modulePath; 249 } 250 251 254 public String getDateFormat() { 255 return dateFormat; 256 } 257 258 261 public void setDateFormat(String dateFormat) { 262 this.dateFormat = dateFormat; 263 } 264 265 268 public String getStyle() { 269 return style; 270 } 271 272 275 public void setStyle(String style) { 276 this.style = style; 277 } 278 279 285 public File[] getModuleFiles(WebSite webSite, boolean allowCurrentDir) { 286 Path argPath = getModuleArgumentPath(false); 287 288 if (argPath == null && allowCurrentDir) { 289 argPath = getModuleArgumentDirectoryPath(webSite, true); 290 } 291 292 if (argPath != null && !webSite.isSystem(argPath)) { 293 File moduleFile = webSite.getFile(argPath); 294 File[] files = null; 295 296 if (moduleFile.isDirectory()) { 297 files = moduleFile.listFiles(); 298 } else if (moduleFile.exists()) { 299 files = new File[1]; 300 files[0] = moduleFile; 301 } 302 303 return files; 304 } 305 306 return null; 307 } 308 309 315 public Path getModuleArgumentPath(boolean allowCurrentPath) { 316 Path argPath = null; 317 318 if (argument != null) { 319 argPath = new Path(argument); 320 } else if (allowCurrentPath) { 321 argPath = pagePath; 322 } 323 324 return argPath; 325 } 326 327 333 public Path getModuleArgumentDirectoryPath(WebSite webSite, 334 boolean allowCurrentPath) { 335 Path argPath = getModuleArgumentPath(allowCurrentPath); 336 return (argPath == null || webSite.isSystem(argPath)) ? null : 337 webSite.getDirectory(argPath); 338 } 339 340 347 public String getFullCSSAttribute(String paramName) { 348 String css = getAdvancedParam(paramName, style); 349 return Utils.isNullOrEmpty(css) ? "" : " class=\"" + css + "\""; 350 } 351 352 358 public DateFormat getDateFormat(Locale locale, String paramName) { 359 String paramValue = getAdvancedParam(paramName, dateFormat); 360 DateFormat df = null; 361 362 if (Module.DATE_NORMAL.equals(paramValue)) { 363 df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); 364 } else if (Module.DATE_FULL.equals(paramValue)) { 365 df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, 366 locale); 367 } 368 369 return df; 370 } 371 } 372 | Popular Tags |