KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > MimetypeMap


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.content;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.alfresco.config.Config;
26 import org.alfresco.config.ConfigElement;
27 import org.alfresco.config.ConfigLookupContext;
28 import org.alfresco.config.ConfigService;
29 import org.alfresco.error.AlfrescoRuntimeException;
30 import org.alfresco.service.cmr.repository.MimetypeService;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /**
35  * Provides a bidirectional mapping between well-known mimetypes and
36  * the registered file extensions. All mimetypes and extensions
37  * are stored and handled as lowercase.
38  *
39  * @author Derek Hulley
40  */

41 public class MimetypeMap implements MimetypeService
42 {
43     public static final String JavaDoc MIMETYPE_TEXT_PLAIN = "text/plain";
44     public static final String JavaDoc MIMETYPE_TEXT_CSS = "text/css";
45     public static final String JavaDoc MIMETYPE_XML = "text/xml";
46     public static final String JavaDoc MIMETYPE_HTML = "text/html";
47     public static final String JavaDoc MIMETYPE_XHTML = "application/xhtml+xml";
48     public static final String JavaDoc MIMETYPE_PDF = "application/pdf";
49     public static final String JavaDoc MIMETYPE_WORD = "application/msword";
50     public static final String JavaDoc MIMETYPE_EXCEL = "application/vnd.excel";
51     public static final String JavaDoc MIMETYPE_BINARY = "application/octet-stream";
52     public static final String JavaDoc MIMETYPE_PPT = "application/vnd.powerpoint";
53     public static final String JavaDoc MIMETYPE_FLASH = "application/x-shockwave-flash";
54     public static final String JavaDoc MIMETYPE_IMAGE_GIF = "image/gif";
55     public static final String JavaDoc MIMETYPE_IMAGE_JPEG = "image/jpeg";
56     public static final String JavaDoc MIMETYPE_IMAGE_RGB = "image/x-rgb";
57     // Open Document
58
public static final String JavaDoc MIMETYPE_OPENDOCUMENT_TEXT = "application/vnd.oasis.opendocument.text";
59     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_TEXT_TEMPLATE = "application/vnd.oasis.opendocument.text-template";
60     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_GRAPHICS = "application/vnd.oasis.opendocument.graphics";
61     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_GRAPHICS_TEMPLATE= "application/vnd.oasis.opendocument.graphics-template";
62     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_PRESENTATION= "application/vnd.oasis.opendocument.presentation";
63     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_PRESENTATION_TEMPLATE= "application/vnd.oasis.opendocument.presentation-template";
64     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_SPREADSHEET= "application/vnd.oasis.opendocument.spreadsheet";
65     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_SPREADSHEET_TEMPLATE= "application/vnd.oasis.opendocument.spreadsheet-template";
66     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_CHART= "application/vnd.oasis.opendocument.chart";
67     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_CHART_TEMPLATE= "applicationvnd.oasis.opendocument.chart-template";
68     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_IMAGE= "application/vnd.oasis.opendocument.image";
69     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_IMAGE_TEMPLATE= "applicationvnd.oasis.opendocument.image-template";
70     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_FORMULA= "application/vnd.oasis.opendocument.formula";
71     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_FORMULA_TEMPLATE= "applicationvnd.oasis.opendocument.formula-template";
72     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_TEXT_MASTER= "application/vnd.oasis.opendocument.text-master";
73     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_TEXT_WEB= "application/vnd.oasis.opendocument.text-web";
74     public static final String JavaDoc MIMETYPE_OPENDOCUMENT_DATABASE= "application/vnd.oasis.opendocument.database";
75     // Open Office
76
public static final String JavaDoc MIMETYPE_OPENOFFICE1_WRITER = "application/vnd.sun.xml.writer";
77     public static final String JavaDoc MIMETYPE_OPENOFFICE1_CALC = "application/vnd.sun.xml.calc";
78     public static final String JavaDoc MIMETYPE_OPENOFFICE1_DRAW = "application/vnd.sun.xml.draw";
79     public static final String JavaDoc MIMETYPE_OPENOFFICE1_IMPRESS = "application/vnd.sun.xml.impress";
80     // Star Office
81
public static final String JavaDoc MIMETYPE_STAROFFICE5_DRAW = "application/vnd.stardivision.draw";
82     public static final String JavaDoc MIMETYPE_STAROFFICE5_CALC = "application/vnd.stardivision.calc";
83     public static final String JavaDoc MIMETYPE_STAROFFICE5_IMPRESS = "application/vnd.stardivision.impress";
84     public static final String JavaDoc MIMETYPE_STAROFFICE5_IMPRESS_PACKED = "application/vnd.stardivision.impress-packed";
85     public static final String JavaDoc MIMETYPE_STAROFFICE5_CHART = "application/vnd.stardivision.chart";
86     public static final String JavaDoc MIMETYPE_STAROFFICE5_WRITER = "application/vnd.stardivision.writer";
87     public static final String JavaDoc MIMETYPE_STAROFFICE5_WRITER_GLOBAL = "application/vnd.stardivision.writer-global";
88     public static final String JavaDoc MIMETYPE_STAROFFICE5_MATH = "application/vnd.stardivision.math";
89     // Audio
90
public static final String JavaDoc MIMETYPE_MP3 = "audio/x-mpeg";
91     // Alfresco
92
public static final String JavaDoc MIMETYPE_ACP = "application/acp";
93     
94     private static final String JavaDoc CONFIG_AREA = "mimetype-map";
95     private static final String JavaDoc CONFIG_CONDITION = "Mimetype Map";
96     private static final String JavaDoc ELEMENT_MIMETYPES = "mimetypes";
97     private static final String JavaDoc ATTR_MIMETYPE = "mimetype";
98     private static final String JavaDoc ATTR_DISPLAY = "display";
99     private static final String JavaDoc ATTR_DEFAULT = "default";
100     
101     private static final Log logger = LogFactory.getLog(MimetypeMap.class);
102     
103     private ConfigService configService;
104     
105     private List JavaDoc<String JavaDoc> mimetypes;
106     private Map JavaDoc<String JavaDoc, String JavaDoc> extensionsByMimetype;
107     private Map JavaDoc<String JavaDoc, String JavaDoc> mimetypesByExtension;
108     private Map JavaDoc<String JavaDoc, String JavaDoc> displaysByMimetype;
109     private Map JavaDoc<String JavaDoc, String JavaDoc> displaysByExtension;
110     
111     /**
112      * @param configService the config service to use to read mimetypes from
113      */

114     public MimetypeMap(ConfigService configService)
115     {
116         this.configService = configService;
117     }
118     
119     /**
120      * Initialises the map using the configuration service provided
121      */

122     public void init()
123     {
124         this.mimetypes = new ArrayList JavaDoc<String JavaDoc>(40);
125         this.extensionsByMimetype = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(59);
126         this.mimetypesByExtension = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(59);
127         this.displaysByMimetype = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(59);
128         this.displaysByExtension = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(59);
129
130         Config config = configService.getConfig(CONFIG_CONDITION, new ConfigLookupContext(CONFIG_AREA));
131         ConfigElement mimetypesElement = config.getConfigElement(ELEMENT_MIMETYPES);
132         List JavaDoc<ConfigElement> mimetypes = mimetypesElement.getChildren();
133         int count = 0;
134         for (ConfigElement mimetypeElement : mimetypes)
135         {
136             count++;
137             // add to list of mimetypes
138
String JavaDoc mimetype = mimetypeElement.getAttribute(ATTR_MIMETYPE);
139             if (mimetype == null || mimetype.length() == 0)
140             {
141                 logger.warn("Ignoring empty mimetype " + count);
142                 continue;
143             }
144             // we store it as lowercase
145
mimetype = mimetype.toLowerCase();
146             if (this.mimetypes.contains(mimetype))
147             {
148                 throw new AlfrescoRuntimeException("Duplicate mimetype definition: " + mimetype);
149             }
150             this.mimetypes.add(mimetype);
151             // add to map of mimetype displays
152
String JavaDoc mimetypeDisplay = mimetypeElement.getAttribute(ATTR_DISPLAY);
153             if (mimetypeDisplay != null && mimetypeDisplay.length() > 0)
154             {
155                 this.displaysByMimetype.put(mimetype, mimetypeDisplay);
156             }
157             
158             // get all the extensions
159
boolean isFirst = true;
160             List JavaDoc<ConfigElement> extensions = mimetypeElement.getChildren();
161             for (ConfigElement extensionElement : extensions)
162             {
163                 // add to map of mimetypes by extension
164
String JavaDoc extension = extensionElement.getValue();
165                 if (extension == null || extension.length() == 0)
166                 {
167                     logger.warn("Ignoring empty extension for mimetype: " + mimetype);
168                     continue;
169                 }
170                 // put to lowercase
171
extension = extension.toLowerCase();
172                 this.mimetypesByExtension.put(extension, mimetype);
173                 // add to map of extension displays
174
String JavaDoc extensionDisplay = extensionElement.getAttribute(ATTR_DISPLAY);
175                 if (extensionDisplay != null && extensionDisplay.length() > 0)
176                 {
177                     this.displaysByExtension.put(extension, extensionDisplay);
178                 }
179                 else if (mimetypeDisplay != null && mimetypeDisplay.length() > 0)
180                 {
181                     // no display defined for the extension - use the mimetype's display
182
this.displaysByExtension.put(extension, mimetypeDisplay);
183                 }
184                 // add to map of extensions by mimetype if it is the default or first extension
185
String JavaDoc isDefaultStr = extensionElement.getAttribute(ATTR_DEFAULT);
186                 boolean isDefault = Boolean.parseBoolean(isDefaultStr);
187                 if (isDefault || isFirst)
188                 {
189                     this.extensionsByMimetype.put(mimetype, extension);
190                 }
191                 isFirst = false;
192             }
193             // check that there were extensions defined
194
if (extensions.size() == 0)
195             {
196                 logger.warn("No extensions defined for mimetype: " + mimetype);
197             }
198         }
199         
200         // make the collections read-only
201
this.mimetypes = Collections.unmodifiableList(this.mimetypes);
202         this.extensionsByMimetype = Collections.unmodifiableMap(this.extensionsByMimetype);
203         this.mimetypesByExtension = Collections.unmodifiableMap(this.mimetypesByExtension);
204         this.displaysByMimetype = Collections.unmodifiableMap(this.displaysByMimetype);
205         this.displaysByExtension = Collections.unmodifiableMap(this.displaysByExtension);
206     }
207     
208     /**
209      * @param mimetype a valid mimetype
210      * @return Returns the default extension for the mimetype
211      * @throws AlfrescoRuntimeException if the mimetype doesn't exist
212      */

213     public String JavaDoc getExtension(String JavaDoc mimetype)
214     {
215         String JavaDoc extension = extensionsByMimetype.get(mimetype);
216         if (extension == null)
217         {
218             throw new AlfrescoRuntimeException("No extension available for mimetype: " + mimetype);
219         }
220         return extension;
221     }
222
223     public Map JavaDoc<String JavaDoc, String JavaDoc> getDisplaysByExtension()
224     {
225         return displaysByExtension;
226     }
227
228     public Map JavaDoc<String JavaDoc, String JavaDoc> getDisplaysByMimetype()
229     {
230         return displaysByMimetype;
231     }
232
233     public Map JavaDoc<String JavaDoc, String JavaDoc> getExtensionsByMimetype()
234     {
235         return extensionsByMimetype;
236     }
237
238     public List JavaDoc<String JavaDoc> getMimetypes()
239     {
240         return mimetypes;
241     }
242
243     public Map JavaDoc<String JavaDoc, String JavaDoc> getMimetypesByExtension()
244     {
245         return mimetypesByExtension;
246     }
247
248     /**
249      * @see #MIMETYPE_BINARY
250      */

251     public String JavaDoc guessMimetype(String JavaDoc filename)
252     {
253         filename = filename.toLowerCase();
254         String JavaDoc mimetype = MIMETYPE_BINARY;
255         // extract the extension
256
int index = filename.lastIndexOf('.');
257         if (index > -1 && (index < filename.length() - 1))
258         {
259             String JavaDoc extension = filename.substring(index + 1);
260             if (mimetypesByExtension.containsKey(extension))
261             {
262                 mimetype = mimetypesByExtension.get(extension);
263             }
264         }
265         return mimetype;
266     }
267 }
268
Popular Tags