KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > core > ModuleDescriptor


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

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 /**
32  * Stores the description of a module when read from the page.
33  */

34 public class ModuleDescriptor {
35   /**
36    * Location parameter.
37    */

38   public static final String JavaDoc LOCATION_ID = "m_loc";
39
40   /**
41    * Argument parameter.
42    */

43   public static final String JavaDoc ARGUMENT_ID = "m_arg";
44
45   /**
46    * Template parameter.
47    */

48   public static final String JavaDoc TEMPLATE_ID = "m_tpl";
49
50   /**
51    * Advanced parameters.
52    */

53   public static final String JavaDoc PARAMETERS_ID = "m_apm";
54
55   private String JavaDoc location;
56   private String JavaDoc template;
57   private String JavaDoc argument;
58   private Properties advancedParams;
59
60   private Path pagePath;
61   private Path modulePath;
62   private String JavaDoc dateFormat;
63   private String JavaDoc style;
64
65   /**
66    * Creates a new empty instance.
67    */

68   public ModuleDescriptor() {
69     //
70
}
71
72   /**
73    * Creates a new instance and calls {@link #init}.
74    */

75   public ModuleDescriptor(String JavaDoc data) {
76     init(data);
77   }
78
79   /**
80    * Parses the given String to get location, template and argument.
81    */

82   public void init(String JavaDoc data) {
83     if (data.indexOf(LOCATION_ID) < 0) { // old module definition
84
String JavaDoc[] values = Utils.tokenize(data, ":");
85       advancedParams = new Properties();
86
87       if (values != null) {
88         switch (values.length) {
89           case 1: // path only
90
setLocation("");
91             setTemplate("include.jsp");
92             setArgument(values[0]);
93             break;
94           case 2: // template and path
95
setLocation("");
96             setTemplate(values[0]);
97             setArgument(values[1]);
98             break;
99           case 3: // location, template and path
100
setLocation(values[0]);
101             setTemplate(values[1]);
102             setArgument(values[2]);
103         }
104       }
105     } else { // new module definition
106
parseParameters(data);
107     }
108   }
109
110   /**
111    * Parses the given string using the new format (version 3.0).
112    */

113   public void parseParameters(String JavaDoc data) {
114     advancedParams = new Properties();
115     StringTokenizer st = new StringTokenizer(data, ",");
116     String JavaDoc 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   /**
140    * Checks if this module descriptor has been initialized correctly. This is
141    * true if both location and template are not null.
142    */

143   public boolean isValid() {
144     return location != null && template != null;
145   }
146
147   /**
148    * @return the name of the module location.
149    *
150    * @see #setLocation
151    */

152   public String JavaDoc getLocation() {
153     return location;
154   }
155
156   /**
157    * Sets the name of the module location. Each in a page needs a
158    * location name which is unique within the page itself.
159    */

160   public void setLocation(String JavaDoc location) {
161     this.location = location;
162   }
163
164   /**
165    * @return the name of the module template.
166    */

167   public String JavaDoc getTemplate() {
168     return template;
169   }
170
171   /**
172    * Sets the name of the module template.
173    */

174   public void setTemplate(String JavaDoc template) {
175     this.template = template.endsWith(".jsp") ?
176         // old modules were in the form module_name.jsp
177
template.substring(0, template.length() - 4) : template;
178   }
179
180   /**
181    * @return the name of the module argument.
182    *
183    * @see #setArgument(String)
184    */

185   public String JavaDoc getArgument() {
186     return argument;
187   }
188
189   /**
190    * Sets the name of the module argument. The argument is set to null if that
191    * String is equal to {@link PageAssembler#EMPTY}.
192    */

193   public void setArgument(String JavaDoc s) {
194     argument = PageAssembler.EMPTY.equals(s) ? null : s;
195   }
196
197   /**
198    * @return the advanced parameters as a <code>Properties</code> object.
199    */

200   public Properties getAdvancedParams() {
201     return advancedParams;
202   }
203
204   /**
205    * @return the value of the requested advanced parameter.
206    */

207   public String JavaDoc getAdvancedParam(String JavaDoc paramName, String JavaDoc defaultValue) {
208     return advancedParams == null ? defaultValue :
209      advancedParams.getProperty(paramName, defaultValue);
210   }
211
212   /**
213    * Sets the advanced parameters values.
214    */

215   public void setAdvancedParams(Properties advancedParams) {
216     this.advancedParams = advancedParams;
217   }
218
219   /**
220    * @return the path of the page that contains the module.
221    */

222   public Path getPagePath() {
223     return pagePath;
224   }
225
226   /**
227    * Sets the path of the page that contains the module.
228    */

229   public void setPagePath(Path pagePath) {
230     this.pagePath = pagePath;
231   }
232
233   /**
234    * @return the path of the module.
235    */

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   /**
245    * Sets the path of the module.
246    */

247   public void setModulePath(Path modulePath) {
248     this.modulePath = modulePath;
249   }
250
251   /**
252    * @return the date format of the module.
253    */

254   public String JavaDoc getDateFormat() {
255     return dateFormat;
256   }
257
258   /**
259    * Sets the date format of the module.
260    */

261   public void setDateFormat(String JavaDoc dateFormat) {
262     this.dateFormat = dateFormat;
263   }
264
265   /**
266    * @return the CSS style to be applied to the module.
267    */

268   public String JavaDoc getStyle() {
269     return style;
270   }
271
272   /**
273    * Sets the CSS style to be applied to the module.
274    */

275   public void setStyle(String JavaDoc style) {
276     this.style = style;
277   }
278
279   /**
280    * @return the files to be passed to the module.
281    *
282    * @param allowCurrentDir if true and the argument parameter is null, the
283    * files included in the same folder of the page are returned
284    */

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   /**
310    * @return the path passed as argument.
311    *
312    * @param allowCurrentPath if true and the argument parameter is null, the
313    * page path is returned
314    */

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   /**
328    * @return the folder path to be used as argument for the module.
329    *
330    * @param allowCurrentPath if true and the argument parameter is null, the
331    * path of the page is returned
332    */

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   /**
341    * Convenience method to get full HTML class attribute (e.g.
342    * <code> class=&quot;stylename&quot;</code>. The value is searched in the
343    * given advanced parameter or, alternatively, in the
344    * value of the &quot;style&quot; tag attribute. If both are unavailable,
345    * an empty string is returned.
346    */

347   public String JavaDoc getFullCSSAttribute(String JavaDoc paramName) {
348     String JavaDoc css = getAdvancedParam(paramName, style);
349     return Utils.isNullOrEmpty(css) ? "" : " class=\"" + css + "\"";
350   }
351
352   /**
353    * @return format to be used to display the date. The value is searched in the
354    * given advanced parameter or, alternatively, in the
355    * value of the &quot;date&quot; tag attribute. If both are unavailable,
356    * null is returned.
357    */

358   public DateFormat getDateFormat(Locale locale, String JavaDoc paramName) {
359     String JavaDoc 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