KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > workplace > CmsAdminDatatypes


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsAdminDatatypes.java,v $
3 * Date : $Date: 2005/05/31 15:51:19 $
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.workplace;
30
31 import org.opencms.file.CmsObject;
32 import org.opencms.file.types.I_CmsResourceType;
33 import org.opencms.i18n.CmsEncoder;
34 import org.opencms.main.CmsException;
35 import org.opencms.main.CmsLog;
36 import org.opencms.main.OpenCms;
37
38 import com.opencms.legacy.CmsLegacyException;
39 import com.opencms.template.A_CmsXmlContent;
40 import com.opencms.template.CmsXmlTemplateFile;
41
42 import java.util.HashMap JavaDoc;
43 import java.util.Hashtable JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 /**
50  * Template class for displaying OpenCms workplace admin datatypes
51  * <P>
52  *
53  * @author Mario Stanke
54  * @version $Revision: 1.5 $ $Date: 2005/05/31 15:51:19 $
55  * @see com.opencms.workplace.CmsXmlWpTemplateFile
56  *
57  * @deprecated Will not be supported past the OpenCms 6 release.
58  */

59
60 public class CmsAdminDatatypes extends CmsWorkplaceDefault {
61
62
63     /** XML datablock tag used for setting the resource type name */
64     private static final String JavaDoc C_TAG_RESTYPE = "restype";
65
66
67     /** XML datablock tag used for setting an entry in the list of datatypes */
68     private static final String JavaDoc C_TYPELISTENTRY = "extensionentry";
69
70
71     /** XML datablock tag used for setting all collected entries */
72     private static final String JavaDoc C_TAG_ALLENTRIES = "allentries";
73
74
75     /** XML datablock tag used for getting a processed resource type entry */
76     private static final String JavaDoc C_TAG_RESTYPEENTRY = "restypeentry";
77
78
79     /** XML datablock tag used for getting a processed separator entry */
80     private static final String JavaDoc C_TAG_SEPARATORENTRY = "separatorentry";
81
82
83     /** XML datablock tag used for getting the complete and processed content to be returned */
84     private static final String JavaDoc C_TAG_SCROLLERCONTENT = "scrollercontent";
85
86     /**
87      * checks if name is in the right format for file extensions and trims leading stars and dots
88      *
89      * @return the formatted String without "*.", '.' or '*'
90      */

91
92     private String JavaDoc format(String JavaDoc name) throws CmsException {
93         int z = 0;
94         while('*' == name.charAt(z) || '.' == name.charAt(z)) {
95             z++;
96         }
97
98         // cut off leading stars and dots
99
String JavaDoc res = name.substring(z, name.length());
100         if(res.indexOf(' ') != -1) {
101
102             // name contained a blank
103
throw new CmsLegacyException(CmsLegacyException.C_BAD_NAME);
104         }
105         return res.toLowerCase(); // extensions not case sensitive
106
}
107
108     /**
109      * Gets the content of a defined section in a given template file and its subtemplates
110      * with the given parameters.
111      *
112      * @see #getContent(CmsObject, String, String, Hashtable, String)
113      * @param cms CmsObject Object for accessing system resources.
114      * @param templateFile Filename of the template file.
115      * @param elementName Element name of this template in our parent template.
116      * @param parameters Hashtable with all template class parameters.
117      * @param templateSelector template section that should be processed.
118      */

119
120     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName,
121             Hashtable JavaDoc parameters, String JavaDoc templateSelector) throws CmsException {
122         if(CmsLog.getLog(this).isDebugEnabled() && C_DEBUG) {
123             CmsLog.getLog(this).debug("Getting content of element "
124                     + ((elementName == null) ? "<root>" : elementName));
125             CmsLog.getLog(this).debug("Template file is: " + templateFile);
126             CmsLog.getLog(this).debug("Selected template section is: "
127                     + ((templateSelector == null) ? "<default>" : templateSelector));
128         }
129
130         CmsXmlWpTemplateFile xmlTemplateDocument = new CmsXmlWpTemplateFile(cms, templateFile);
131         CmsXmlLanguageFile lang = xmlTemplateDocument.getLanguageFile();
132         String JavaDoc action = (String JavaDoc)parameters.get("action");
133         String JavaDoc resTypeName = (String JavaDoc)parameters.get("restype");
134         String JavaDoc extensionName = (String JavaDoc)parameters.get("extension");
135         xmlTemplateDocument.setData("RESTYPE", resTypeName);
136         if("new".equals(action)) {
137             templateSelector = "newextension";
138             String JavaDoc name = (String JavaDoc)parameters.get("NAME");
139             if(name == null || name.equals("")) {
140
141
142             // form has not yet been submitted
143
}
144             else {
145                 try {
146                     String JavaDoc formattedName;
147                     formattedName = format(name);
148                     Map JavaDoc h = OpenCms.getResourceManager().getExtensionMapping();
149                     if(h == null) {
150                         h = new HashMap JavaDoc();
151                     }
152                     if(h.containsKey(formattedName)) {
153                         throw new CmsLegacyException(CmsLegacyException.C_NOT_EMPTY);
154                     }
155                     h.put(formattedName, resTypeName);
156                     // not possible anymore right now
157
// TODO: make this possible again
158
//cms.writeFileExtensions(h);
159
templateSelector = "";
160                 }
161                 catch(CmsException e) {
162                     if ((e instanceof CmsLegacyException) && (((CmsLegacyException)e).getType() == CmsLegacyException.C_NOT_EMPTY)) {
163                         templateSelector = "errorinuse";
164                     }
165                     else {
166                         if ((e instanceof CmsLegacyException) && (((CmsLegacyException)e).getType() == CmsLegacyException.C_BAD_NAME)) {
167                             templateSelector = "errorformat";
168                         }
169                         else {
170                             StringBuffer JavaDoc errmesg = new StringBuffer JavaDoc();
171                             errmesg.append(lang.getLanguageValue("error.reason.newextension1")
172                                     + " '" + name + "' " + lang.getLanguageValue("error.reason.newextension2")
173                                             + " '" + resTypeName + "' "
174                                                     + lang.getLanguageValue("error.reason.newextension3") + "\n\n");
175                             errmesg.append(CmsException.getStackTraceAsString(e));
176                             xmlTemplateDocument.setData("NEWDETAILS", errmesg.toString());
177                             templateSelector = "newerror";
178                         }
179                     }
180                 }
181             }
182         }
183         else {
184             if("delete".equals(action)) {
185                 if("true".equals(parameters.get("sure"))) {
186
187                     // the user is sure to delete the property definition
188
//try {
189
Map JavaDoc h = OpenCms.getResourceManager().getExtensionMapping();
190                         if(h != null) {
191                             h.remove(extensionName);
192                         }
193                         // not possible anymore right now
194
// TODO: make this possible again
195
//cms.writeFileExtensions(h);
196
templateSelector = "";
197                     //}
198
/**catch(CmsException e) {
199                         xmlTemplateDocument.setData("DELETEDETAILS", CmsException.getStackTraceAsString(e));
200                         templateSelector = "errordelete";
201                     }*/

202                 }
203                 else {
204                     templateSelector = "RUsuredelete";
205                 }
206                 xmlTemplateDocument.setData("EXTENSION_NAME", extensionName);
207             }
208         }
209
210         // Now load the template file and start the processing
211
return startProcessing(cms, xmlTemplateDocument, elementName, parameters, templateSelector);
212     }
213
214     /**
215      * Used by the <code>&lt;PREFSSCROLLER&gt;</code> tag for getting
216      * the content of the scroller window.
217      * <P>
218      * Gets all available resource types and returns a list
219      * using the datablocks defined in the own template file.
220      *
221      * @param cms CmsObject Object for accessing system resources.
222      * @param lang reference to the currently valid language file
223      * @param parameters Hashtable containing all user parameters <em>(not used here)</em>.
224      * @return Index representing the user's current filter view in the vectors.
225      * @throws CmsException
226      */

227
228     public String JavaDoc getDatatypes(CmsObject cms, A_CmsXmlContent doc,
229             CmsXmlLanguageFile lang, Hashtable JavaDoc parameters, Object JavaDoc callingObj) throws CmsException {
230         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
231         Map JavaDoc extensions = OpenCms.getResourceManager().getExtensionMapping();
232         Map JavaDoc extByFiletypes = turnAround(extensions);
233         CmsXmlTemplateFile templateFile = (CmsXmlTemplateFile)doc;
234         
235         // get all available resource types
236
List JavaDoc allResTypes = OpenCms.getResourceManager().getResourceTypes();
237         for (int i=0; i<allResTypes.size(); i++) {
238             I_CmsResourceType type = (I_CmsResourceType)allResTypes.get(i);
239             String JavaDoc resTypeName = type.getTypeName();
240             Vector JavaDoc suffList = (Vector JavaDoc)extByFiletypes.get(resTypeName);
241             result.append(getResourceEntry(cms, doc, lang, parameters, callingObj, resTypeName, suffList));
242             if(i < (allResTypes.size() - 1)) {
243                 result.append(templateFile.getProcessedDataValue(C_TAG_SEPARATORENTRY, callingObj));
244             }
245         }
246         templateFile.setData(C_TAG_ALLENTRIES, result.toString());
247         return templateFile.getProcessedDataValue(C_TAG_SCROLLERCONTENT, callingObj);
248     }
249
250     /**
251      *
252      * gets the HTML code for entry in the lists of resources.
253      *
254      * @param cms CmsObject Object for accessing system resources.
255      * @param doc the template file which is used
256      * @param lang reference to the currently valid language file
257      * @param parameters Hashtable containing all user parameters <em>(not used here)</em>.
258      * @param callingObject Object for accessing system resources.
259      * @param resTypeName name of the resource type
260      * @param suffList Vector of all extensions of that resource type
261      * @return String which holds a HTML table
262      * @throws CmsException
263      */

264
265     private String JavaDoc getResourceEntry(CmsObject cms, A_CmsXmlContent doc, CmsXmlLanguageFile lang,
266             Hashtable JavaDoc parameters, Object JavaDoc callingObject, String JavaDoc resTypeName, Vector JavaDoc suffList)
267                     throws CmsException {
268         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
269         CmsXmlWpTemplateFile templateFile = (CmsXmlWpTemplateFile)doc;
270         templateFile.setData(C_TAG_RESTYPE, resTypeName);
271
272         templateFile.setData(C_TAG_RESTYPE + "_esc", CmsEncoder.escapeWBlanks(resTypeName,
273             cms.getRequestContext().getEncoding()));
274         output.append(templateFile.getProcessedDataValue(C_TAG_RESTYPEENTRY, callingObject));
275         if(suffList != null) {
276             for(int z = 0;z < suffList.size();z++) {
277                 String JavaDoc suffix = (String JavaDoc)suffList.elementAt(z);
278                 templateFile.setData("EXTENSION_NAME", suffix);
279                 templateFile.setData("EXTENSION_NAME_ESC", CmsEncoder.escapeWBlanks(suffix,
280                     cms.getRequestContext().getEncoding()));
281                 output.append(templateFile.getProcessedDataValue(C_TYPELISTENTRY, callingObject));
282             }
283         }
284         return output.toString();
285     }
286
287     /**
288      * Indicates if the results of this class are cacheable.
289      *
290      * @param cms CmsObject Object for accessing system resources
291      * @param templateFile Filename of the template file
292      * @param elementName Element name of this template in our parent template.
293      * @param parameters Hashtable with all template class parameters.
294      * @param templateSelector template section that should be processed.
295      * @return <EM>true</EM> if cacheable, <EM>false</EM> otherwise.
296      */

297
298     public boolean isCacheable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName,
299             Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
300         return false;
301     }
302
303     /**
304      * Given the Hashtable h, interpreted as a binary relation (subset of A x B) with keys in A
305      * and values in B this function returns, as a Hashtable, the same binary relation with keys
306      * in B and values in A stored in Vectors
307      *
308      * @param h the Hashtable to be 'turned around'
309      * @return a Hashtable of Vectors
310      */

311
312     private Map JavaDoc turnAround(Map JavaDoc h) {
313         if(h == null) {
314             return null;
315         }
316         Map JavaDoc g = new HashMap JavaDoc();
317         Iterator JavaDoc en = h.keySet().iterator();
318         while(en.hasNext()) {
319             Object JavaDoc key = en.next();
320             Object JavaDoc value = h.get(key);
321             Vector JavaDoc List = (Vector JavaDoc)g.get(value);
322             if(List == null) {
323                 Vector JavaDoc newEntry = new Vector JavaDoc();
324                 newEntry.addElement(key);
325                 g.put(value, newEntry);
326             }
327             else {
328                 List.addElement(key);
329             }
330         }
331         return g;
332     }
333 }
334
Popular Tags