KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
26 import org.meshcms.util.*;
27
28 /**
29  * Contains data about site menu customization and theme mappings.
30  */

31 public class SiteInfo {
32   /**
33    * Prefix of the title codes.
34    */

35   public static final String JavaDoc TITLE = "title";
36
37   /**
38    * Prefix of the score codes.
39    */

40   public static final String JavaDoc SCORE = "score";
41
42   /**
43    * Prefix of the theme codes.
44    */

45   public static final String JavaDoc THEME = "theme";
46
47   /**
48    * Prefix of the submenu codes.
49    */

50   public static final String JavaDoc HIDESUBMENU = "hideSubmenu";
51
52   private Properties data;
53   private transient WebSite webSite;
54
55   protected SiteInfo() {
56     data = new Properties();
57   }
58
59   /**
60    * Loads configuration from the config file (if found).
61    *
62    * @return true if the configuration has been loaded, false otherwise
63    */

64   public static SiteInfo load(WebSite webSite) {
65     SiteInfo siteInfo = (SiteInfo)
66         webSite.loadFromXML(webSite.getPropertiesFilePath());
67
68     if (siteInfo == null) {
69       siteInfo = new SiteInfo();
70       siteInfo.setWebSite(webSite);
71     } else {
72       siteInfo.setWebSite(webSite);
73     }
74
75     return siteInfo;
76   }
77
78   /**
79    * Saves the configuration to file.
80    *
81    * @return true if the configuration has been saved, false otherwise
82    */

83   public boolean store() {
84     return webSite.storeToXML(this, webSite.getPropertiesFilePath());
85   }
86
87   /**
88    * @return the theme to be applied to the given path.
89    */

90   public String JavaDoc getPageTheme(Path pagePath) {
91     return Utils.noNull(data.getProperty(getThemeCode(pagePath)));
92   }
93
94   /**
95    * Sets the theme to be applied to the given path. If the value is null or
96    * empty, the theme is removed.
97    */

98   public void setPageTheme(Path pagePath, String JavaDoc theme) {
99     if (Utils.isNullOrEmpty(theme)) {
100       data.remove(getThemeCode(pagePath));
101     } else {
102       data.setProperty(getThemeCode(pagePath), theme);
103     }
104   }
105
106   /**
107    * @return the menu title for a page. If the menu configuration does not
108    * contain a value for this page, the page title itself is returned.
109    */

110   public String JavaDoc getPageTitle(PageInfo pageInfo) {
111     String JavaDoc customTitle = getPageTitle(pageInfo.getPath());
112
113     if (Utils.isNullOrEmpty(customTitle)) {
114       customTitle = pageInfo.getTitle();
115     }
116
117     return customTitle;
118   }
119
120   /**
121    * @return the menu title for the given path (null if not available).
122    */

123   public String JavaDoc getPageTitle(Path pagePath) {
124     return Utils.noNull(data.getProperty(getTitleCode(pagePath)));
125   }
126
127   /**
128    * Sets the menu title for the given path. If the value is null or empty,
129    * the title is removed.
130    */

131   public void setPageTitle(Path pagePath, String JavaDoc title) {
132     if (Utils.isNullOrEmpty(title)) {
133       data.remove(getTitleCode(pagePath));
134     } else {
135       data.setProperty(getTitleCode(pagePath), title);
136     }
137   }
138
139   /**
140    * @return the page score for the given path (0 if not available).
141    */

142   public int getPageScore(Path pagePath) {
143     return Utils.parseInt(data.getProperty(getScoreCode(pagePath)), 0);
144   }
145
146   /**
147    * @return the page score as a string for the given path. An empty string
148    * is returned if the page score is 0.
149    */

150   public String JavaDoc getPageScoreAsString(Path pagePath) {
151     int score = getPageScore(pagePath);
152     return score == 0 ? "" : Integer.toString(score);
153   }
154
155   /**
156    * Sets the page score for the given path.
157    */

158   public void setPageScore(Path pagePath, String JavaDoc score) {
159     setPageScore(pagePath, Utils.parseInt(score, 0));
160   }
161
162   /**
163    * Sets the page score for the given path. If the score is 0, it is removed
164    * since 0 is the default.
165    */

166   public void setPageScore(Path pagePath, int score) {
167     if (score == 0) {
168       data.remove(getScoreCode(pagePath));
169     } else {
170       data.setProperty(getScoreCode(pagePath), Integer.toString(score));
171     }
172   }
173
174   /**
175    * @return the hide submenu for the given path (false if not available).
176    */

177   public boolean getHideSubmenu(Path pagePath) {
178     return Utils.isTrue(data.getProperty(getHideSubmenuCode(pagePath)));
179   }
180
181   /**
182    * @return the hide submenu as a string for the given path. An empty string
183    * is returned if the hide submenu is false.
184    */

185   public String JavaDoc getHideSubmenuAsString(Path pagePath) {
186     boolean hide = getHideSubmenu(pagePath);
187     return hide == false ? "" : Boolean.toString(hide);
188   }
189
190   /**
191    * Sets the hide submenu for the given path.
192    */

193   public void setHideSubmenu(Path pagePath, String JavaDoc hide) {
194     setHideSubmenu(pagePath, Utils.isTrue(hide));
195   }
196
197   /**
198    * Sets the hide submenu for the given path. If the submenu is shown, it is removed
199    * since false is the default.
200    */

201   public void setHideSubmenu(Path pagePath, boolean hide) {
202     if (hide == false) {
203       data.remove(getHideSubmenuCode(pagePath));
204     } else {
205       data.setProperty(getHideSubmenuCode(pagePath), Boolean.toString(hide));
206     }
207   }
208
209   /**
210    * @return the code for the score field of the given path. This code is
211    * used in the HTML configuration form and in the config file.
212    */

213   public static String JavaDoc getScoreCode(Path pagePath) {
214     return SCORE + WebUtils.getMenuCode(pagePath);
215   }
216
217   /**
218    * @return the code for the title field of the given path. This code is
219    * used in the HTML configuration form and in the config file.
220    */

221   public static String JavaDoc getTitleCode(Path pagePath) {
222     return TITLE + WebUtils.getMenuCode(pagePath);
223   }
224
225   /**
226    * @return the code for the theme field of the given path. This code is
227    * used in the HTML configuration form and in the config file.
228    */

229   public static String JavaDoc getThemeCode(Path pagePath) {
230     return THEME + WebUtils.getMenuCode(pagePath);
231   }
232
233   /**
234    * @return the code for the show submenu field of the given path. This code is
235    * used in the HTML configuration form and in the config file.
236    */

237   public static String JavaDoc getHideSubmenuCode(Path pagePath) {
238     return HIDESUBMENU + WebUtils.getMenuCode(pagePath);
239   }
240
241   /**
242    * Sets a generic value. The <code>fieldName</code> parameter is analyzed to
243    * guess what value is going to be set. This is used to save all form fields.
244    *
245    * @return true if the fieldName has been recognized as a valid one, false
246    * otherwise
247    */

248   public boolean setValue(String JavaDoc fieldName, String JavaDoc value) {
249     if (fieldName == null) {
250       return false;
251     }
252
253     if (value != null) {
254       value = value.trim();
255     }
256
257     if (fieldName.startsWith(TITLE)) {
258       if (Utils.isNullOrEmpty(value)) {
259         data.remove(fieldName);
260       } else {
261         data.setProperty(fieldName, Utils.encodeHTML(value));
262       }
263
264       return true;
265     }
266
267     if (fieldName.startsWith(THEME)) {
268       if (Utils.isNullOrEmpty(value)) {
269         data.remove(fieldName);
270       } else {
271         data.setProperty(fieldName, value);
272       }
273
274       return true;
275     }
276
277     if (fieldName.startsWith(SCORE)) {
278       int n = Utils.parseInt(value, 0);
279
280       if (n == 0) {
281         data.remove(fieldName);
282       } else {
283         data.setProperty(fieldName, value);
284       }
285
286       return true;
287     }
288
289     if (fieldName.startsWith(HIDESUBMENU)) {
290         boolean b = Utils.isTrue(value);
291
292         if (b == false) {
293           data.remove(fieldName);
294         } else {
295           data.setProperty(fieldName, "true");
296         }
297
298         return true;
299     }
300
301     return false;
302   }
303
304   protected Path getThemePath(Path pagePath) {
305     do {
306       String JavaDoc theme = getPageTheme(pagePath);
307
308       if (!Utils.isNullOrEmpty(theme)) {
309         if (theme.equals(PageAssembler.EMPTY)) {
310           return null;
311         }
312
313         Path themePath = (Path) webSite.getSiteMap().getThemesMap().get(theme);
314
315         if (themePath != null) {
316           return themePath;
317         }
318       }
319
320       pagePath = pagePath.getParent();
321     } while (!pagePath.isRelative());
322
323     return null;
324   }
325
326   /**
327    * @return the path of the page whose theme is inherited.
328    */

329   public Path getThemeRoot(Path pagePath) {
330     do {
331       String JavaDoc theme = getPageTheme(pagePath);
332
333       if (!Utils.isNullOrEmpty(theme)) {
334         if (webSite.getSiteMap().getThemesMap().get(theme) != null) {
335           return pagePath;
336         }
337       }
338
339       pagePath = pagePath.getParent();
340     } while (!pagePath.isRelative());
341
342     return null;
343   }
344
345   public WebSite getWebSite() {
346     return webSite;
347   }
348
349   public void setWebSite(WebSite webSite) {
350     this.webSite = webSite;
351   }
352
353   /**
354    * Cleans SiteInfo by removing info for pagePaths that no longer exist.
355    *
356    * @deprecated with no replacement, since it's better <em>not</em> to
357    * clean up SiteInfo this way, to retain info about pages that are not in the
358    * site map. A different solution would be to check for non-existing files
359    * instead of pages not in the site map.
360    */

361   protected boolean cleanupSiteInfo() {
362     boolean success = true;
363     SiteInfo newSiteInfo = new SiteInfo();
364     newSiteInfo.setWebSite(webSite);
365     List pagesList = webSite.getSiteMap().getPagesList();
366
367     if (pagesList != null) {
368       Iterator iter = pagesList.iterator();
369
370       while (iter.hasNext()) {
371         Path pagePath = ((PageInfo) iter.next()).getPath();
372         newSiteInfo.setPageTitle(pagePath, getPageTitle(pagePath));
373         newSiteInfo.setPageTheme(pagePath, getPageTheme(pagePath));
374         newSiteInfo.setPageScore(pagePath, getPageScore(pagePath));
375         newSiteInfo.setHideSubmenu(pagePath, getHideSubmenu(pagePath));
376       }
377
378       if (this.data.size() != newSiteInfo.data.size()) {
379         success = newSiteInfo.store();
380         webSite.setSiteInfo(newSiteInfo);
381       }
382     }
383
384     return success;
385   }
386 }
387
Popular Tags