KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > cms > CMSMimeMappings


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.cms;
10
11 import org.jboss.portal.common.util.XML;
12 import org.jboss.portal.common.util.Tools;
13
14 import org.w3c.dom.Document JavaDoc;
15 import org.w3c.dom.Element JavaDoc;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.List JavaDoc;
19 import java.io.InputStream JavaDoc;
20
21 /**
22  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
23  * Date: Apr 30, 2005; Time: 2:10:44 AM
24  * @since JBoss portal 2.0
25  * Class org.jboss.portal.setup.impl.dl.cmsloader.CMSMimeMappings
26  */

27 public class CMSMimeMappings
28 {
29
30    /** Definition of default mime type mappings to file extension. */
31    private static final String JavaDoc MAPPINGS_FILE = "org/jboss/portal/cms/mime-mappings.xml";
32
33    /** . */
34    private final HashMap JavaDoc mappings = new HashMap JavaDoc();
35
36    public CMSMimeMappings()
37    {
38       InputStream JavaDoc is = null;
39       try
40       {
41          is = Thread.currentThread().getContextClassLoader().getResourceAsStream(MAPPINGS_FILE);
42          Document JavaDoc dmt = XML.getDocumentBuilderFactory().newDocumentBuilder().parse(is);
43          Element JavaDoc docElmt = dmt.getDocumentElement();
44          List JavaDoc mappingsList = XML.getChildren(docElmt, "mime-mapping");
45          for (int i = 0; i < mappingsList.size(); i++)
46          {
47             Element JavaDoc mapping = (Element JavaDoc)mappingsList.get(i);
48             String JavaDoc extension = XML.asString(XML.getUniqueChild(mapping, "extension", true));
49             String JavaDoc mimeType = XML.asString(XML.getUniqueChild(mapping, "mime-type", true));
50             mappings.put(extension, mimeType);
51          }
52       }
53       catch (Exception JavaDoc e)
54       {
55          throw new Error JavaDoc("Cannot load mime mapping file", e);
56       }
57       finally
58       {
59          Tools.safeClose(is);
60       }
61    }
62
63    public String JavaDoc getMimeType(String JavaDoc fileExtension)
64    {
65       return (String JavaDoc)mappings.get(fileExtension);
66    }
67 }
68
Popular Tags