KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > template > CmsXmlControlFile


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/CmsXmlControlFile.java,v $
3  * Date : $Date: 2005/06/27 23:22:20 $
4  * Version: $Revision: 1.5 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2001 The OpenCms Group
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 OpenCms, please see the
22  * OpenCms Website: http://www.opencms.org
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  */

28
29 package com.opencms.template;
30
31 import org.opencms.file.CmsFile;
32 import org.opencms.file.CmsObject;
33 import org.opencms.file.CmsResource;
34 import org.opencms.file.CmsResourceFilter;
35 import org.opencms.importexport.CmsCompatibleCheck;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.CmsLog;
38 import org.opencms.staticexport.CmsLinkManager;
39
40 import com.opencms.legacy.CmsLegacyException;
41 import com.opencms.template.cache.CmsElementDefinition;
42 import com.opencms.template.cache.CmsElementDefinitionCollection;
43
44 import java.util.Enumeration JavaDoc;
45 import java.util.Hashtable JavaDoc;
46 import java.util.Vector JavaDoc;
47
48 import org.w3c.dom.Document JavaDoc;
49 import org.w3c.dom.Element JavaDoc;
50 import org.w3c.dom.Node JavaDoc;
51 import org.w3c.dom.NodeList JavaDoc;
52
53 /**
54  * Content definition for "clickable" and user requestable XML body files.
55  *
56  * @author Alexander Lucas
57  * @version $Revision: 1.5 $ $Date: 2005/06/27 23:22:20 $
58  *
59  * @deprecated Will not be supported past the OpenCms 6 release.
60  */

61 public class CmsXmlControlFile extends A_CmsXmlContent {
62
63     /**
64      * Default constructor.
65      */

66     public CmsXmlControlFile() {
67
68         super();
69     }
70
71     /**
72      * Constructor for creating a new object containing the content
73      * of the given filename.
74      *
75      * @param cms CmsObject object for accessing system resources.
76      * @param file name of the body file that shoul be read.
77      * @throws CmsException if something goes wrong
78      */

79     public CmsXmlControlFile(CmsObject cms, CmsFile file)
80     throws CmsException {
81
82         super();
83         init(cms, file);
84     }
85
86     /**
87      * Constructor for creating a new object containing the content
88      * of the given filename.
89      *
90      * @param cms CmsObject object for accessing system resources.
91      * @param filename name of the body file that shoul be read.
92      * @throws CmsException if something goes wrong
93      */

94     public CmsXmlControlFile(CmsObject cms, String JavaDoc filename)
95     throws CmsException {
96
97         super();
98         init(cms, filename);
99     }
100
101     /**
102      * Constructor for creating a new object containing the given content
103      * for the given filename.
104      *
105      * @param cms for accessing system resources
106      * @param filename name of the file that shoul be stored in this XML file cache
107      * @param content XML file to parse
108      * @throws CmsException if something goes wrong
109      */

110     public CmsXmlControlFile(CmsObject cms, String JavaDoc filename, String JavaDoc content)
111     throws CmsException {
112
113         super();
114         init(cms, filename, content);
115     }
116
117     /**
118      * Gets a description of this content type.
119      * @return Content type description.
120      */

121     public String JavaDoc getContentDescription() {
122
123         return "OpenCms XML page file";
124     }
125
126     /**
127      * Gets the template class of a given subelement definition.
128      * @param elementName Name of the subelement.
129      * @return Name of the template class.
130      * @throws CmsException if something goes wrong
131      */

132     public String JavaDoc getElementClass(String JavaDoc elementName) throws CmsException {
133
134         return getDataValue("ELEMENTDEF." + elementName + ".CLASS");
135     }
136
137     /**
138      * Gets a collection of element definitions.<p>
139      *
140      * @return a collection of element definitions
141      * @throws CmsException if something goes wrong
142      */

143     public CmsElementDefinitionCollection getElementDefinitionCollection() throws CmsException {
144
145         CmsElementDefinitionCollection result = new CmsElementDefinitionCollection();
146         Enumeration JavaDoc elementDefinitions = getElementDefinitions();
147         while (elementDefinitions.hasMoreElements()) {
148             String JavaDoc elementName = (String JavaDoc)elementDefinitions.nextElement();
149
150             String JavaDoc elementClass = null;
151             String JavaDoc elementTemplate = null;
152             String JavaDoc elementTs = null;
153             if (isElementClassDefined(elementName)) {
154                 elementClass = getElementClass(elementName);
155             }
156             if (isElementTemplateDefined(elementName)) {
157                 elementTemplate = getElementTemplate(elementName);
158             }
159             if (isElementTemplSelectorDefined(elementName)) {
160                 elementTs = getElementTemplSelector(elementName);
161             }
162             Hashtable JavaDoc elementParameters = getElementParameters(elementName);
163             if (elementClass == null) {
164                 elementClass = CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS;
165             }
166             if (elementTemplate != null) {
167                 elementTemplate = CmsLinkManager.getAbsoluteUri(elementTemplate, getAbsoluteFilename());
168             }
169             result.add(new CmsElementDefinition(
170                 elementName,
171                 elementClass,
172                 elementTemplate,
173                 elementTs,
174                 elementParameters));
175         }
176         return result;
177     }
178
179     /**
180      * Gets an enumeration of all names of the subelement definition in the
181      * body file.
182      * @return Enumeration with of names.
183      * @throws CmsException if something goes wrong
184      */

185     public Enumeration JavaDoc getElementDefinitions() throws CmsException {
186
187         NodeList JavaDoc elementDefTags = getXmlDocument().getDocumentElement().getChildNodes();
188         return getNamesFromNodeList(elementDefTags, "ELEMENTDEF", false);
189     }
190
191     /**
192      * Gets the value of a single parameter of a given subelement definition.
193      * @param elementName Name of the subelement.
194      * @param parameterName Name of the requested parameter.
195      * @return the element parameter value
196      * @throws CmsException if something goes wrong
197      */

198     public String JavaDoc getElementParameter(String JavaDoc elementName, String JavaDoc parameterName) throws CmsException {
199
200         return getDataValue("ELEMENTDEF." + elementName + ".PARAMETER." + parameterName);
201     }
202
203     /**
204      * Gets an enumeration of all parameter names of a given subelement definition.
205      * @param elementName Name of the subelement.
206      * @return Enumeration of all names.
207      * @throws CmsException if something goes wrong
208      */

209     public Enumeration JavaDoc getElementParameterNames(String JavaDoc elementName) throws CmsException {
210
211         Element JavaDoc elementDefinition = getData("elementdef." + elementName);
212         NodeList JavaDoc parameterTags = elementDefinition.getChildNodes();
213         return getNamesFromNodeList(parameterTags, "PARAMETER", false);
214     }
215
216     /**
217      * Get a hashtable containing all parameters and thies values of a given subelement definition.
218      * @param elementName Name of the subelement.
219      * @return Enumeration of all names.
220      * @throws CmsException if something goes wrong
221      */

222     public Hashtable JavaDoc getElementParameters(String JavaDoc elementName) throws CmsException {
223
224         Hashtable JavaDoc result = new Hashtable JavaDoc();
225         Element JavaDoc elementDefinition = getData("elementdef." + elementName);
226         NodeList JavaDoc parameterTags = elementDefinition.getChildNodes();
227
228         int numElements = parameterTags.getLength();
229         for (int i = 0; i < numElements; i++) {
230             Node JavaDoc n = parameterTags.item(i);
231             if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().toLowerCase().equals("parameter")) {
232                 String JavaDoc name = ((Element JavaDoc)n).getAttribute("name");
233                 if (name != null && !"".equals(name)) {
234                     result.put(name, getTagValue((Element JavaDoc)n));
235                 }
236             }
237         }
238         return result;
239     }
240
241     /**
242      * Gets the filename of the master template file of a given subelement definition.
243      * @param elementName Name of the subelement.
244      * @return Filename of the template file.
245      * @throws CmsException if something goes wrong
246      */

247     public String JavaDoc getElementTemplate(String JavaDoc elementName) throws CmsException {
248
249         //return getDataValue("ELEMENTDEF." + elementName + ".TEMPLATE");
250
String JavaDoc result = getDataValue("ELEMENTDEF." + elementName + ".TEMPLATE");
251         return result;
252     }
253
254     /**
255      * Gets the filename of the master template file of a given subelement definition.
256      * @param elementName Name of the subelement.
257      * @return Filename of the template file.
258      * @throws CmsException if something goes wrong
259      */

260     public String JavaDoc getElementTemplSelector(String JavaDoc elementName) throws CmsException {
261
262         return getDataValue("ELEMENTDEF." + elementName + ".TEMPLATESELECTOR");
263     }
264
265     /**
266      * Gets the filename of the master template file defined in
267      * the body file.
268      * @return Filename of the template file.
269      * @throws CmsException if something goes wrong
270      */

271     public String JavaDoc getMasterTemplate() throws CmsException {
272
273         String JavaDoc result = getDataValue("mastertemplate");
274         if (result == null || "".equals(result)) {
275             if (CmsLog.getLog(this).isErrorEnabled()) {
276                 CmsLog.getLog(this).error("<MASTERTEMPLATE> tag not found in file " + getAbsoluteFilename());
277             }
278             removeFromFileCache();
279             throw new CmsLegacyException("\"MASTERTEMPLATE\" definition tag not found in file "
280                 + getAbsoluteFilename()
281                 + ".", CmsLegacyException.C_XML_TAG_MISSING);
282         }
283         return result;
284     }
285
286     /**
287      * Gets the value of a single parameter of the master template.
288      * @param parameterName Name of the requested parameter.
289      * @return the parameter value
290      * @throws CmsException if something goes wrong
291      */

292     public String JavaDoc getParameter(String JavaDoc parameterName) throws CmsException {
293
294         return getDataValue("PARAMETER." + parameterName);
295     }
296
297     /**
298      * Gets an enumeration of all parameter names of the master template.
299      * @return Enumeration of all names.
300      * @throws CmsException if something goes wrong
301      */

302     public Enumeration JavaDoc getParameterNames() throws CmsException {
303
304         NodeList JavaDoc parameterTags = getXmlDocument().getDocumentElement().getChildNodes();
305         return getNamesFromNodeList(parameterTags, "PARAMETER", false);
306     }
307
308     /**
309      * Gets the template class defined in the body file.
310      * @return Name of the template class.
311      * @throws CmsException if something goes wrong
312      */

313     public String JavaDoc getTemplateClass() throws CmsException {
314
315         return getDataValue("class");
316     }
317
318     /**
319      * Gets the expected tagname for the XML documents of this content type.<p>
320      *
321      * @return Expected XML tagname.
322      */

323     public String JavaDoc getXmlDocumentTagName() {
324
325         return "PAGE";
326     }
327
328     /**
329      * Checks if the body file contains a definition of the
330      * template class name for a given subelement definition.
331      * @param elementName Name of the subelement.
332      * @return <code>true</code> if a definition exists, <code>false</code> otherwise.
333      */

334     public boolean isElementClassDefined(String JavaDoc elementName) {
335
336         return this.hasData("ELEMENTDEF." + elementName + ".CLASS");
337     }
338
339     /**
340      * Checks if the body file contains a definition of the
341      * template file name for a given subelement definition.
342      * @param elementName Name of the subelement.
343      * @return <code>true</code> if a definition exists, <code>false</code> otherwise.
344      */

345     public boolean isElementTemplateDefined(String JavaDoc elementName) {
346
347         return this.hasData("ELEMENTDEF." + elementName + ".TEMPLATE");
348     }
349
350     /**
351      * Checks if the body file contains a definition of the
352      * template selector for a given subelement definition.
353      * @param elementName Name of the subelement.
354      * @return <code>true</code> if a definition exists, <code>false</code> otherwise.
355      */

356     public boolean isElementTemplSelectorDefined(String JavaDoc elementName) {
357
358         return this.hasData("ELEMENTDEF." + elementName + ".TEMPLATESELECTOR");
359     }
360
361     /**
362      * Sets the template class of a given subelement definition.
363      * @param elementName Name of the subelement.
364      * @param classname Classname to be set.
365      */

366     public void setElementClass(String JavaDoc elementName, String JavaDoc classname) {
367
368         createElementDef(elementName);
369         setData("ELEMENTDEF." + elementName + ".CLASS", classname);
370     }
371
372     /**
373      * Set the value of a single parameter of a given subelement definition.
374      * @param elementName Name of the subelement.
375      * @param parameterName Name of the requested parameter.
376      * @param parameterValue Value to be set
377      */

378     public void setElementParameter(String JavaDoc elementName, String JavaDoc parameterName, String JavaDoc parameterValue) {
379
380         createElementDef(elementName);
381         if (!hasData("ELEMENTDEF." + elementName + ".PARAMETER." + parameterName)) {
382             Document JavaDoc doc = getXmlDocument();
383             Element JavaDoc e = doc.createElement("PARAMETER");
384             e.setAttribute("name", parameterName);
385             e.appendChild(doc.createTextNode(parameterValue));
386             setData("ELEMENTDEF." + elementName + ".PARAMETER." + parameterName, e);
387         } else {
388             setData("ELEMENTDEF." + elementName + ".PARAMETER." + parameterName, parameterValue);
389         }
390     }
391
392     /**
393      * Sets the filename of the master template file of a given subelement definition.
394      * @param elementName Name of the subelement.
395      * @param filename Filename to be set.
396      */

397     public void setElementTemplate(String JavaDoc elementName, String JavaDoc filename) {
398
399         createElementDef(elementName);
400         setData("ELEMENTDEF." + elementName + ".TEMPLATE", filename);
401     }
402
403     /**
404      * Sets the filename of the master template file of a given subelement definition.
405      * @param elementName Name of the subelement.
406      * @param templateSelector Template selector to be set.
407      */

408     public void setElementTemplSelector(String JavaDoc elementName, String JavaDoc templateSelector) {
409
410         createElementDef(elementName);
411         setData("ELEMENTDEF." + elementName + ".TEMPLATESELECTOR", templateSelector);
412     }
413
414     /**
415      * Sets the filename of the master template file defined in
416      * the body file.
417      * @param template Filename of the template file.
418      */

419     public void setMasterTemplate(String JavaDoc template) {
420
421         setData("masterTemplate", template);
422     }
423
424     /**
425      * Set the value of a single parameter of the master template.
426      * @param parameterName Name of the requested parameter.
427      * @param parameterValue Value to be set
428      */

429     public void setParameter(String JavaDoc parameterName, String JavaDoc parameterValue) {
430
431         if (!hasData("PARAMETER." + parameterName)) {
432             Document JavaDoc doc = getXmlDocument();
433             Element JavaDoc e = doc.createElement("PARAMETER");
434             e.setAttribute("name", parameterName);
435             e.appendChild(doc.createTextNode(parameterValue));
436             setData("PARAMETER." + parameterName, e);
437         } else {
438             setData("PARAMETER." + parameterName, parameterValue);
439         }
440     }
441
442     /**
443      * Set the template class used for the master Template.<p>
444      *
445      * @param templateClass Name of the template class.
446      */

447     public void setTemplateClass(String JavaDoc templateClass) {
448
449         setData("class", templateClass);
450     }
451
452     /**
453      * Validates a given body path.<p>
454      *
455      * After a folder is moved or renamed, the XML control files still contain the old body path.
456      * This method first tries to read the given body path from the XML control file. If this path
457      * is invalid, it tries to read the body file in "/system/bodies/" + current folder + filename.
458      *
459      * @param cms the user's CmsObject instance
460      * @param bodyPath the body path that gets validated
461      * @param page the page of which the body path gets validated
462      * @return the original body path if it valid, or "/system/bodies/" + current folder + filename
463      */

464     public String JavaDoc validateBodyPath(CmsObject cms, String JavaDoc bodyPath, CmsResource page) {
465
466         String JavaDoc validatedBodyPath = null;
467
468         if (bodyPath == null || "".equals(bodyPath)) {
469             return bodyPath;
470         }
471
472         try {
473             cms.readResource(bodyPath, CmsResourceFilter.ALL);
474             validatedBodyPath = bodyPath;
475         } catch (CmsException e) {
476             if ((e instanceof CmsLegacyException)
477                 && (((CmsLegacyException)e).getType() == CmsLegacyException.C_NOT_FOUND)) {
478
479                 String JavaDoc defaultBodyPath = CmsCompatibleCheck.VFS_PATH_BODIES
480                     + CmsResource.getParentFolder(cms.getSitePath(page)).substring(1)
481                     + page.getName();
482                 try {
483                     cms.readResource(defaultBodyPath, CmsResourceFilter.ALL);
484                     validatedBodyPath = defaultBodyPath;
485                     setElementTemplate(CmsXmlTemplate.C_BODY_ELEMENT, validatedBodyPath);
486                 } catch (CmsException e1) {
487                     validatedBodyPath = null;
488                 }
489             }
490         }
491
492         return validatedBodyPath;
493     }
494
495     /**
496      * Used for setting element definition values.
497      * Checks if the requested element definition already exists.
498      * If so, nothing will happen. If not, a corresponding section
499      * will be created using a hierarchical datablock tag
500      * <code>&lt;ELEMENTDEF name="..."/&gt;</code>
501      *
502      * @param name Name of the element definition section.
503      */

504     private void createElementDef(String JavaDoc name) {
505
506         if (!hasData("ELEMENTDEF." + name)) {
507             Document JavaDoc doc = getXmlDocument();
508             Element JavaDoc e = doc.createElement("ELEMENTDEF");
509             e.setAttribute("name", name);
510             setData("elementdef." + name, e);
511         }
512     }
513
514     /**
515      * Internal utility method to extract the values of the "name" attribute
516      * from defined nodes of a given nodelist.
517      * @param nl NodeList to extract.
518      * @param tag Name of the tag whose "name" attribute should be extracted
519      * @param unnamedAllowed Indicates if unnamed tags are allowed or an exception should
520      * be thrown.
521      * @return Enumeration of all "name" attributes.
522      * @throws CmsException
523      */

524     private Enumeration JavaDoc getNamesFromNodeList(NodeList JavaDoc nl, String JavaDoc tag, boolean unnamedAllowed) throws CmsException {
525
526         int numElements = nl.getLength();
527         Vector JavaDoc collectNames = new Vector JavaDoc();
528         for (int i = 0; i < numElements; i++) {
529             Node JavaDoc n = nl.item(i);
530             if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().toLowerCase().equals(tag.toLowerCase())) {
531                 String JavaDoc name = ((Element JavaDoc)n).getAttribute("name");
532                 if (name == null || "".equals(name)) {
533
534                     // unnamed element found.
535
if (unnamedAllowed) {
536                         name = "(default)";
537                     } else {
538                         if (CmsLog.getLog(this).isErrorEnabled()) {
539                             CmsLog.getLog(this).error(
540                                 "Unnamed <"
541                                     + n.getNodeName()
542                                     + "> found in OpenCms control file "
543                                     + getAbsoluteFilename());
544                         }
545                         throw new CmsLegacyException("Unnamed \""
546                             + n.getNodeName()
547                             + "\" found in OpenCms control file "
548                             + getAbsoluteFilename(), CmsLegacyException.C_XML_TAG_MISSING);
549                     }
550                 }
551                 collectNames.addElement(name);
552             }
553         }
554         return collectNames.elements();
555     }
556
557 }
558
Popular Tags