KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > util > core > ResourceTypesManager


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.util.core;
17
18 import org.apache.commons.collections.list.SetUniqueList;
19 import org.apache.commons.digester.Digester;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * <p>Provides common functions to operate with resource types specified in XML file</p>
35  * This class implements Singleton design pattern
36  * <p><a HREF="ResourceTypesManager.java.htm"><i>View Source</i></a></p>
37  *
38  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
39  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
40  * @version $Revision: 1.10 $ $Date: 2006/03/16 11:09:44 $
41  */

42 public class ResourceTypesManager {
43
44     private static final String JavaDoc RESOURCE_TYPES_FILE = "/WEB-INF/resource-types.xml";
45     public static final String JavaDoc INSTANCE_KEY = "com.blandware.atleap.ResourceTypesManager.INSTANCE";
46
47     protected transient final Log log = LogFactory.getLog(ResourceTypesManager.class);
48
49     protected Map JavaDoc resourceTypes = new HashMap JavaDoc();
50
51     /**
52      * Private constructor
53      *
54      * @param servletContext Servlet context
55      */

56     private ResourceTypesManager(ServletContext JavaDoc servletContext) {
57         InputStream JavaDoc is = servletContext.getResourceAsStream(RESOURCE_TYPES_FILE);
58         if ( is == null ) {
59             if ( log.isErrorEnabled() ) {
60                 log.error("Could not locate xml file with resource types: " + RESOURCE_TYPES_FILE);
61             }
62         }
63         try {
64
65             Digester digester = new Digester();
66             digester.push(this);
67             digester.setNamespaceAware(false);
68             digester.setValidating(false);
69
70             digester.addObjectCreate("resourceTypes/resourceType", ResourceType.class);
71             digester.addSetProperties("resourceTypes/resourceType");
72             digester.addCallMethod("resourceTypes/resourceType/mimeType", "addMimeType", 1);
73             digester.addCallParam("resourceTypes/resourceType/mimeType", 0, "identifier");
74             digester.addCallMethod("resourceTypes/resourceType/mimeType/extension", "addExtension", 1);
75             digester.addCallParam("resourceTypes/resourceType/mimeType/extension", 0);
76             digester.addSetNext("resourceTypes/resourceType", "addResourceType", ResourceType.class.getName());
77             digester.parse(is);
78
79         } catch ( IOException JavaDoc e ) {
80             if ( log.isErrorEnabled() ) {
81                 log.error("Could not load xml file with resource types: " + RESOURCE_TYPES_FILE);
82             }
83         } catch ( SAXException JavaDoc e ) {
84             if ( log.isErrorEnabled() ) {
85                 log.error("Could not parse xml file with resource types: " + RESOURCE_TYPES_FILE);
86             }
87         }
88     }
89
90
91     /**
92      * Returns instance of ResourceTypesManager
93      *
94      * @param servletContext The servlet context
95      * @return Instance of ResourceTypesManager
96      */

97     public static ResourceTypesManager getInstance(ServletContext JavaDoc servletContext) {
98         ResourceTypesManager ourInstance = (ResourceTypesManager) servletContext.getAttribute(INSTANCE_KEY);
99         if ( ourInstance == null ) {
100             ourInstance = new ResourceTypesManager(servletContext);
101             servletContext.setAttribute(INSTANCE_KEY, ourInstance);
102         }
103         return ourInstance;
104     }
105
106     /**
107      * Gets resource type identifier by extension
108      *
109      * @param extension Extension to search by
110      * @return resource type identifier
111      */

112     public String JavaDoc getResourceTypeByExtension(String JavaDoc extension) {
113         if ( log.isDebugEnabled() ) {
114             log.debug("Get resource type identifier by extension " + extension);
115         }
116
117         for ( Iterator JavaDoc i = resourceTypes.values().iterator(); i.hasNext(); ) {
118             ResourceType resourceType = (ResourceType) i.next();
119             if ( resourceType.isValidExtension(extension) ) {
120                 return resourceType.getIdentifier();
121             }
122         }
123
124         return null;
125     }
126
127     /**
128      * Gets resource type identifier by file name
129      *
130      * @param filename Name of file to find resource by
131      * @return resource type identifier
132      */

133     public String JavaDoc getResourceTypeByFileName(String JavaDoc filename) {
134         if ( log.isDebugEnabled() ) {
135             log.debug("Get resource type by file name " + filename);
136         }
137
138         // TODO: client separator char may differ from our one, so check this situation
139
// int slash = filename.lastIndexOf(File.separatorChar);
140
int period = filename.lastIndexOf(".");
141         if ( (period >= 0) /*&& (period > slash)*/ ) {
142             String JavaDoc extension = filename.substring(period + 1, filename.length());
143             return getResourceTypeByExtension(extension);
144         } else {
145             return null;
146         }
147     }
148
149     /**
150      * Gets MIME type of resource by extension
151      *
152      * @param extension Extension to search by
153      * @return MIME type
154      */

155     public String JavaDoc getMimeTypeByExtension(String JavaDoc extension) {
156         if ( log.isDebugEnabled() ) {
157             log.debug("Get MIME type identifier by extension " + extension);
158         }
159
160         for ( Iterator JavaDoc i = resourceTypes.values().iterator(); i.hasNext(); ) {
161             ResourceType resourceType = (ResourceType) i.next();
162             Map JavaDoc mimeTypes = resourceType.getMimeTypes();
163             for ( Iterator JavaDoc j = mimeTypes.entrySet().iterator(); j.hasNext(); ) {
164                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) j.next();
165                 List extensions = (List) entry.getValue();
166                 if ( extensions.contains(extension.toLowerCase()) ) {
167                     return (String JavaDoc) entry.getKey();
168                 }
169             }
170         }
171
172         return null;
173     }
174
175     /**
176      * Gets MIME type by file name
177      *
178      * @param filename name of file for which to determine its MIME type
179      * @return MIME type
180      */

181     public String JavaDoc getMimeTypeByFileName(String JavaDoc filename) {
182         if ( log.isDebugEnabled() ) {
183             log.debug("Get MIME type by file name " + filename);
184         }
185
186 // int slash = filename.lastIndexOf(File.separatorChar);
187
int period = filename.lastIndexOf(".");
188         if ( (period >= 0) /*&& (period > slash)*/ ) {
189             String JavaDoc extension = filename.substring(period + 1, filename.length());
190             return getMimeTypeByExtension(extension);
191         } else {
192             return null;
193         }
194     }
195
196     /**
197      * Gets full list of all supported extensions
198      *
199      * @return list of strings
200      */

201     public List getSupportedExtensions() {
202         List extList = new ArrayList JavaDoc();
203         for ( Iterator JavaDoc i = resourceTypes.values().iterator(); i.hasNext(); ) {
204             ResourceType resourceType = (ResourceType) i.next();
205             extList.addAll(resourceType.getSupportedExtensions());
206         }
207         return extList;
208     }
209
210     /**
211      * Gets list of resource type extensions
212      *
213      * @param resourceTypeIdentifier identifier of resource type
214      * @return list of extensions
215      */

216     public List getResourceTypeExtensions(String JavaDoc resourceTypeIdentifier) {
217         ResourceType resourceType = (ResourceType) resourceTypes.get(resourceTypeIdentifier);
218         if ( resourceType != null ) {
219             return resourceType.getSupportedExtensions();
220         } else {
221             return null;
222         }
223     }
224
225     /**
226      * Gets default MIME type identifier for specified resource type
227      *
228      * @param resourceTypeIdentifier identifier of resource type
229      * @return MIME type
230      */

231     public String JavaDoc getDefaultMimeType(String JavaDoc resourceTypeIdentifier) {
232         ResourceType resourceType = (ResourceType) resourceTypes.get(resourceTypeIdentifier);
233         if ( resourceType != null ) {
234             return resourceType.getDefaultMimeType();
235         } else {
236             return null;
237         }
238     }
239
240     /**
241      * Gets default extension for specified MIME type
242      *
243      * @param mimeTypeIdentifier identifier of MIME type
244      * @return extension
245      */

246     public String JavaDoc getDefaultExtension(String JavaDoc mimeTypeIdentifier) {
247         String JavaDoc defaultExtension = null;
248         for ( Iterator JavaDoc i = resourceTypes.values().iterator(); i.hasNext() && defaultExtension == null; ) {
249             ResourceType resourceType = (ResourceType) i.next();
250             defaultExtension = resourceType.getDefaultExtension(mimeTypeIdentifier);
251         }
252         return defaultExtension;
253     }
254
255     /**
256      * Gets resource type name key from i18n bundle
257      *
258      * @param resourceTypeIdentifier identifier of resource type
259      * @return key from bundle
260      */

261     public String JavaDoc getResourceTypeNameKey(String JavaDoc resourceTypeIdentifier) {
262         ResourceType resourceType = (ResourceType) resourceTypes.get(resourceTypeIdentifier);
263         if ( resourceType != null ) {
264             return resourceType.getNameKey();
265         } else {
266             return null;
267         }
268     }
269
270     /**
271      * Registers resource type
272      *
273      * @param resourceType Resource type to register
274      */

275     public void addResourceType(ResourceType resourceType) {
276         resourceTypes.put(resourceType.getIdentifier(), resourceType);
277     }
278
279     /**
280      * Inner class that represents resource type
281      */

282     public static class ResourceType {
283
284         /**
285          * Identifier of resource type
286          */

287         protected String JavaDoc identifier;
288
289         /**
290          * I18n bundle key for type name
291          */

292         protected String JavaDoc nameKey;
293
294         /**
295          * List of mappings mimeType -&gt; extensions
296          */

297         protected Map JavaDoc mimeTypes = new HashMap JavaDoc();
298
299         /**
300          * Default MIME type
301          */

302         protected String JavaDoc defautMimeType = null;
303
304         /**
305          * List of extensions
306          */

307         protected List extensions = SetUniqueList.decorate(new ArrayList JavaDoc());
308
309         /**
310          * Creates new instance of resource type
311          */

312         public ResourceType() {
313         }
314
315         /**
316          * Creates new instance of resource type with specified identifier
317          *
318          * @param identifier Identifier of this resource type
319          */

320         public ResourceType(String JavaDoc identifier) {
321             this.identifier = identifier;
322         }
323
324         /**
325          * Creates new instance of resource type with specified identifier and name key
326          *
327          * @param identifier Identifier of this resource type
328          * @param nameKey Name key for this resource type
329          */

330         public ResourceType(String JavaDoc identifier, String JavaDoc nameKey) {
331             this.identifier = identifier;
332             this.nameKey = nameKey;
333         }
334
335         /**
336          * Returns identifier of this resource type
337          *
338          * @return Identifier
339          */

340         public String JavaDoc getIdentifier() {
341             return identifier;
342         }
343
344         /**
345          * Sets identifier of this resource type
346          *
347          * @param identifier New identifier
348          */

349         public void setIdentifier(String JavaDoc identifier) {
350             this.identifier = identifier;
351         }
352
353         /**
354          * Returns name key for this resource type
355          *
356          * @return Name key
357          */

358         public String JavaDoc getNameKey() {
359             return nameKey;
360         }
361
362         /**
363          * Sets new name key for this resource type
364          *
365          * @param nameKey New name key
366          */

367         public void setNameKey(String JavaDoc nameKey) {
368             this.nameKey = nameKey;
369         }
370
371         /**
372          * Returns list of mappings of MIME type to list of extensions
373          *
374          * @return List of mappings of MIME types to list of extensions
375          */

376         public Map JavaDoc getMimeTypes() {
377             return mimeTypes;
378         }
379
380         /**
381          * Sets list of mappings of MIME type to list of extensions
382          *
383          * @param mimeTypes New list of mappings
384          */

385         public void setMimeTypes(Map JavaDoc mimeTypes) {
386             this.mimeTypes = mimeTypes;
387         }
388
389         /**
390          * Returns list of extensions supported in this resource type
391          *
392          * @return List of supported extensions
393          */

394         public List getSupportedExtensions() {
395             SetUniqueList extSet = SetUniqueList.decorate(new ArrayList JavaDoc());
396             for ( Iterator JavaDoc i = mimeTypes.values().iterator(); i.hasNext(); ) {
397                 SetUniqueList extensions = (SetUniqueList) i.next();
398                 extSet.addAll(extensions);
399             }
400             return extSet;
401         }
402
403         /**
404          * Returns true if resource of this type can have specified extension
405          *
406          * @param extension Extension to check
407          * @return <code>true</code> if resource of this type can have specified extension
408          */

409         public boolean isValidExtension(String JavaDoc extension) {
410             return getSupportedExtensions().contains(extension.toLowerCase());
411         }
412
413         /**
414          * Returns default MIME type of this resource type
415          *
416          * @return default MIME type
417          */

418         public String JavaDoc getDefaultMimeType() {
419             return defautMimeType;
420         }
421
422         /**
423          * Adds MIME type with empty set of extensions
424          *
425          * @param identifier Identifier of MIME type to add
426          */

427         public void addMimeType(String JavaDoc identifier) {
428             if ( defautMimeType == null ) {
429                 defautMimeType = identifier;
430             }
431             if ( !mimeTypes.containsKey(identifier) ) {
432                 mimeTypes.put(identifier, extensions);
433                 extensions = SetUniqueList.decorate(new ArrayList JavaDoc());
434             }
435         }
436
437         /**
438          * Adds extension
439          *
440          * @param extension Extension to add
441          */

442         public void addExtension(String JavaDoc extension) {
443             if ( extension != null ) {
444                 extensions.add(extension.toLowerCase());
445             }
446         }
447
448         /**
449          * Returns default extensions for specified MIME type
450          *
451          * @param mimeTypeIdentifier Identifier of MIME type to get default extension for
452          * @return default extension for MIME type with specified identifier
453          */

454         public String JavaDoc getDefaultExtension(String JavaDoc mimeTypeIdentifier) {
455             SetUniqueList extensions = (SetUniqueList) mimeTypes.get(mimeTypeIdentifier);
456             if ( extensions != null && !extensions.isEmpty() ) {
457                 return (String JavaDoc) extensions.get(0);
458             } else {
459                 return null;
460             }
461         }
462
463     }
464
465
466 }
467
Popular Tags