KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > modules > CmsLayoutXmlContentHandler


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/modules/CmsLayoutXmlContentHandler.java,v $
3  * Date : $Date: 2006/03/27 14:52:59 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.frontend.templateone.modules;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsProperty;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsResourceFilter;
39 import org.opencms.jsp.util.CmsTemplateContentListItem;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.OpenCms;
42 import org.opencms.util.CmsMacroResolver;
43 import org.opencms.util.CmsStringUtil;
44 import org.opencms.xml.CmsXmlException;
45 import org.opencms.xml.content.CmsDefaultXmlContentHandler;
46 import org.opencms.xml.content.CmsXmlContent;
47 import org.opencms.xml.content.Messages;
48 import org.opencms.xml.types.I_CmsXmlContentValue;
49
50 import java.util.ArrayList JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Locale JavaDoc;
53
54 /**
55  * A special XML content handler, will be used by all XML contents that create layout files.<p>
56  *
57  * Layout files with properties attached to them are used to create
58  * structured content lists in the center or on the right side of template one.<p>
59  *
60  * @author Andreas Zahner
61  *
62  * @version $Revision: 1.2 $
63  *
64  * @since 6.1.7
65  */

66 public class CmsLayoutXmlContentHandler extends CmsDefaultXmlContentHandler {
67
68     /** The resource type name of the configuration files using this handler. */
69     public static final String JavaDoc CONFIG_RESTYPE_NAME = "layout";
70
71     /** The element name for the layout item collector name. */
72     public static final String JavaDoc ELEMENT_COLLECTOR = "Collector";
73
74     /** The element name for the layout item list count. */
75     public static final String JavaDoc ELEMENT_COUNT = "Count";
76
77     /** The element name for the layout. */
78     public static final String JavaDoc ELEMENT_LAYOUT = "Layout";
79
80     /** The element name for the layout item resource type. */
81     public static final String JavaDoc ELEMENT_RESOURCETYPE = "Type";
82
83     /** The element name for the layout item VFS folder. */
84     public static final String JavaDoc ELEMENT_VFSFOLDER = "Folder";
85
86     /** The name of the leyout column property. */
87     public static final String JavaDoc PROPERTY_LAYOUT_COLUMN = "layout.column";
88
89     /**
90      * Creates a new instance of the default XML content handler.<p>
91      */

92     public CmsLayoutXmlContentHandler() {
93
94         super();
95     }
96
97     /**
98      * This overwrites the super implementation to revolve additional fixed property mappings.<p>
99      *
100      * @see org.opencms.xml.content.I_CmsXmlContentHandler#prepareForWrite(org.opencms.file.CmsObject, org.opencms.xml.content.CmsXmlContent, org.opencms.file.CmsFile)
101      */

102     public CmsFile prepareForWrite(CmsObject cms, CmsXmlContent content, CmsFile file) throws CmsException {
103
104         super.prepareForWrite(cms, content, file);
105         resolveLayoutMappings(cms, content);
106         removeEmptyLayoutMappings(cms, content);
107         return file;
108     }
109
110     /**
111      * Removes the layout property values on resources for non-existing, optional elements.<p>
112      *
113      * @param cms the current users OpenCms context
114      * @param content the XML content to remove the property values for
115      *
116      * @throws CmsException in case of read/write errors accessing the OpenCms VFS
117      */

118     protected void removeEmptyLayoutMappings(CmsObject cms, CmsXmlContent content) throws CmsException {
119
120         super.removeEmptyMappings(cms, content);
121
122         String JavaDoc rootPath = null;
123         List JavaDoc siblings = null;
124
125         // get root path of the file
126
if (rootPath == null) {
127             rootPath = content.getFile().getRootPath();
128         }
129
130         try {
131             // try / catch to ensure site root is always restored
132
cms.getRequestContext().saveSiteRoot();
133             cms.getRequestContext().setSiteRoot("/");
134
135             // read all siblings of the file
136
if (siblings == null) {
137                 siblings = cms.readSiblings(rootPath, CmsResourceFilter.IGNORE_EXPIRATION);
138             }
139
140             for (int i = 0; i < siblings.size(); i++) {
141
142                 // get sibline filename and locale
143
String JavaDoc filename = ((CmsResource)siblings.get(i)).getRootPath();
144                 Locale JavaDoc locale = OpenCms.getLocaleManager().getDefaultLocale(cms, filename);
145
146                 if (!content.hasLocale(locale)) {
147                     // only remove property if the locale fits
148
continue;
149                 }
150
151                 List JavaDoc nestedItems = content.getValues(ELEMENT_LAYOUT, locale);
152
153                 // delete properties for an eventually deleted nested layout item
154
int deleteIndex = nestedItems.size() + 1;
155
156                 // create resolver to resolve property definition names
157
CmsMacroResolver resolver = CmsMacroResolver.newInstance();
158                 resolver.addMacro(CmsTemplateContentListItem.MACRO_LISTINDEX, "" + deleteIndex);
159
160                 List JavaDoc properties = new ArrayList JavaDoc(4);
161
162                 // delete the resource type property
163
CmsProperty p = new CmsProperty(
164                     resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_TYPE),
165                     CmsProperty.DELETE_VALUE,
166                     null);
167                 properties.add(p);
168
169                 // delete the vfs folder property
170
p = new CmsProperty(
171                     resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_FOLDER),
172                     CmsProperty.DELETE_VALUE,
173                     null);
174                 properties.add(p);
175
176                 // delete the list count property
177
p = new CmsProperty(
178                     resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_COUNT),
179                     CmsProperty.DELETE_VALUE,
180                     null);
181                 properties.add(p);
182
183                 // delete the collector property
184
p = new CmsProperty(
185                     resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_COLLECTOR),
186                     CmsProperty.DELETE_VALUE,
187                     null);
188                 properties.add(p);
189
190                 // write the deleted property values
191
cms.writePropertyObjects(filename, properties);
192             }
193
194         } finally {
195             // restore the saved site root
196
cms.getRequestContext().restoreSiteRoot();
197         }
198     }
199
200     /**
201      * Resolves the special layout property mappings for the resource.<p>
202      *
203      * These mappings are not defined in the XSD, they are fixed for the layout configuration XML contents.<p>
204      *
205      * @param cms the current users OpenCms context
206      * @param content the XML content to map the layout property values for
207      * @throws CmsException in case of read/write errors accessing the OpenCms VFS
208      */

209     protected void resolveLayoutMappings(CmsObject cms, CmsXmlContent content) throws CmsException {
210
211         // get the original VFS file from the content
212
CmsFile file = content.getFile();
213         if (file == null) {
214             throw new CmsXmlException(Messages.get().container(Messages.ERR_XMLCONTENT_RESOLVE_FILE_NOT_FOUND_0));
215         }
216
217         // get root path of the file
218
String JavaDoc rootPath = content.getFile().getRootPath();
219         String JavaDoc siteRoot = cms.getRequestContext().getSiteRoot();
220
221         try {
222             // try / catch to ensure site root is always restored
223
cms.getRequestContext().saveSiteRoot();
224             cms.getRequestContext().setSiteRoot("/");
225
226             // read all siblings of the file
227
List JavaDoc siblings = cms.readSiblings(rootPath, CmsResourceFilter.IGNORE_EXPIRATION);
228
229             // for multilanguage mappings, we need to ensure
230
// a) all siblings are handled
231
// b) only the "right" locale is mapped to a sibling
232

233             for (int i = (siblings.size() - 1); i >= 0; i--) {
234                 // get filename
235
String JavaDoc filename = ((CmsResource)siblings.get(i)).getRootPath();
236                 Locale JavaDoc locale = OpenCms.getLocaleManager().getDefaultLocale(cms, filename);
237
238                 if (!content.hasLocale(locale)) {
239                     // only write properties if the locale fits
240
continue;
241                 }
242
243                 // iterate the found layout definitions to set the properties
244
List JavaDoc nestedItems = content.getValues(ELEMENT_LAYOUT, locale);
245                 List JavaDoc properties = new ArrayList JavaDoc(nestedItems.size() * 4);
246                 for (int k = 0; k < nestedItems.size(); k++) {
247                     I_CmsXmlContentValue layoutConfig = (I_CmsXmlContentValue)nestedItems.get(k);
248                     String JavaDoc layoutConfigPath = layoutConfig.getPath() + "/";
249
250                     // create resolver to resolve property definition names
251
CmsMacroResolver resolver = CmsMacroResolver.newInstance();
252                     resolver.addMacro(CmsTemplateContentListItem.MACRO_LISTINDEX, "" + (layoutConfig.getIndex() + 1));
253
254                     // set the resource type property
255
String JavaDoc resType = content.getStringValue(cms, layoutConfigPath + ELEMENT_RESOURCETYPE, locale);
256                     CmsProperty p = new CmsProperty(
257                         resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_TYPE),
258                         resType,
259                         null);
260                     properties.add(p);
261
262                     // set the vfs folder property
263
String JavaDoc folder = content.getStringValue(cms, layoutConfigPath + ELEMENT_VFSFOLDER, locale);
264                     if (CmsStringUtil.isNotEmpty(folder)) {
265                         // folder is set, eventually remove site root from path
266
if (folder.length() > 1 && folder.startsWith(siteRoot)) {
267                             folder = folder.substring(siteRoot.length());
268                         }
269                     } else {
270                         // folder not set, remove property value
271
folder = CmsProperty.DELETE_VALUE;
272                     }
273                     p = new CmsProperty(
274                         resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_FOLDER),
275                         folder,
276                         null);
277                     properties.add(p);
278
279                     // set the list count property
280
String JavaDoc count = content.getStringValue(cms, layoutConfigPath + ELEMENT_COUNT, locale);
281                     p = new CmsProperty(
282                         resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_COUNT),
283                         count,
284                         null);
285                     properties.add(p);
286
287                     // set the collector property
288
String JavaDoc collector = content.getStringValue(cms, layoutConfigPath + ELEMENT_COLLECTOR, locale);
289                     p = new CmsProperty(
290                         resolver.resolveMacros(CmsTemplateContentListItem.PROPERTY_LAYOUT_COLLECTOR),
291                         collector,
292                         null);
293                     properties.add(p);
294                 }
295
296                 // write all property objects
297
cms.writePropertyObjects(filename, properties);
298             }
299
300         } finally {
301             // restore the saved site root
302
cms.getRequestContext().restoreSiteRoot();
303         }
304     }
305
306 }
Popular Tags