KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Rebuilds a page from its components.
30  */

31 public final class PageAssembler {
32   /**
33    * Generic string used to indicate an empty value.
34    */

35   public static final String JavaDoc EMPTY = "(none)";
36
37   /**
38    * The name of the properties for the modules to include in the page.
39    */

40   public static final String JavaDoc MODULES_PARAM = "meshcmsmodules";
41
42   /**
43    * The name of the properties for the mail recipient of the page.
44    */

45   public static final String JavaDoc EMAIL_PARAM = "meshcmsmail";
46
47   private String JavaDoc title;
48   private String JavaDoc head;
49   private String JavaDoc body;
50   private String JavaDoc email;
51   // private StringBuffer htmlTag = new StringBuffer();
52
private StringBuffer JavaDoc bodyTag = new StringBuffer JavaDoc();
53   private Properties mod_templates, mod_args, mod_params;
54
55   public PageAssembler() {
56     mod_templates = new Properties();
57     mod_args = new Properties();
58     mod_params = new Properties();
59   }
60
61   /**
62    * Adds a property to the page.
63    */

64   public void addProperty(String JavaDoc name, String JavaDoc value) {
65     value = Utils.noNull(value).trim();
66
67     if (name.equals("pagetitle")) {
68       title = value;
69     } else if (name.equals("meshcmshead")) {
70       head = value;
71     } else if (name.equals("meshcmsbody")) {
72       body = value;
73     } else if (name.equals(EMAIL_PARAM)) {
74       email = value;
75     } else if (name.startsWith(ModuleDescriptor.TEMPLATE_ID)) {
76       if (!value.equals(EMPTY)) { // EMPTY is the value when "no module" is selected
77
// name is something like sel_location: we need to set location->value
78
// value is the name of the module template
79
mod_templates.setProperty(name.substring(ModuleDescriptor.TEMPLATE_ID.length()), value);
80       }
81     } else if (name.startsWith(ModuleDescriptor.ARGUMENT_ID)) {
82       if (!Utils.isNullOrEmpty(value)) {
83         // name is something like arg_location: we need to set location->value
84
// value is the name of the module argument (i.e. the selected file/folder)
85
mod_args.setProperty(name.substring(ModuleDescriptor.ARGUMENT_ID.length()), value);
86       }
87     } else if (name.startsWith(ModuleDescriptor.PARAMETERS_ID)) {
88         mod_params.setProperty(name.substring(ModuleDescriptor.PARAMETERS_ID.length()), value);
89     } else if (name.startsWith("body.")) {
90       bodyTag.append(' ').append(name.substring(5)).append("=\"").append(value).append('\"');
91     /*
92       } else if (name.startsWith("meta.")) {
93         //
94       } else if (name.startsWith("page.")) {
95         //
96       } else if (name.startsWith("mce_editor")) {
97         //
98       } else {
99         htmlTag.append(' ').append(name).append("=\"").append(value).append('\"');
100     */

101     }
102   }
103
104   /**
105    * @return the complete page.
106    */

107   public String JavaDoc getPage() {
108     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<html");
109     // sb.append(htmlTag);
110

111     Enumeration locations = mod_templates.keys();
112     List modules = new ArrayList();
113
114     // parse all modules stored before by calls to addProperty
115
while (locations.hasMoreElements()) {
116       String JavaDoc loc = locations.nextElement().toString();
117       String JavaDoc template = mod_templates.getProperty(loc);
118
119       if (!Utils.isNullOrEmpty(template)) {
120         String JavaDoc argument = mod_args.getProperty(loc);
121
122         if (Utils.isNullOrEmpty(argument)) {
123           argument = EMPTY;
124         }
125
126         String JavaDoc params = mod_params.getProperty(loc);
127         modules.add(
128             ModuleDescriptor.LOCATION_ID + '=' + loc + ',' +
129             ModuleDescriptor.TEMPLATE_ID + '=' + template + ',' +
130             ModuleDescriptor.ARGUMENT_ID + '=' + argument +
131             (Utils.isNullOrEmpty(params) ? "" : ',' + params)
132         );
133       }
134     }
135
136     if (modules.size() > 0) { // we have modules, so create the html attribute
137
sb.append(' ').append(MODULES_PARAM).append("=\"").append
138         (Utils.generateList(modules, ";")).append('"');
139     }
140
141     if (Utils.checkAddress(email)) { // we have an e-mail address
142
sb.append(' ').append(EMAIL_PARAM).append("=\"").append(email).append('"');
143     }
144
145     sb.append(">\n<head>\n<title>");
146     sb.append(Utils.noNull(title));
147     sb.append("</title>\n");
148     sb.append(Utils.noNull(head));
149     sb.append("\n</head>\n<body");
150     sb.append(bodyTag);
151     sb.append(">\n");
152     sb.append(Utils.noNull(body));
153     sb.append("\n</body>\n</html>\n");
154
155     return sb.toString();
156   }
157 }
158
Popular Tags