KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > setup > impl > dl > cmsloader > 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.setup.impl.dl.cmsloader;
10
11 import org.jboss.portal.setup.CmsSetupException;
12 import org.jboss.portal.common.util.XML;
13
14 import org.w3c.dom.Document JavaDoc;
15 import org.w3c.dom.Element JavaDoc;
16 import org.xml.sax.SAXException JavaDoc;
17
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 /**
25  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
26  * Date: Apr 30, 2005; Time: 2:10:44 AM
27  * @since JBoss portal 2.0
28  * Class org.jboss.portal.setup.impl.dl.cmsloader.CMSMimeMappings
29  */

30 public class CMSMimeMappings
31 {
32    //DEfinition of default mime type mappings to file extension
33
private static final HashMap JavaDoc MIME_MAPPINGS = new HashMap JavaDoc();
34    private static final String JavaDoc MAPPINGS_FILE = "org/jboss/portal/setup/impl/dl/cmsloader/mime-mappings.xml";
35
36    CMSMimeMappings() throws CmsSetupException
37    {
38       loadMappings();
39    }
40
41
42    private void loadMappings() throws CmsSetupException
43    {
44       InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(MAPPINGS_FILE);
45       if (is != null)
46       {
47          Document JavaDoc dmt = null;
48          try
49          {
50             dmt = XML.getDocumentBuilderFactory().newDocumentBuilder().parse(is);
51          }
52          catch (SAXException JavaDoc e)
53          {
54             throw new CmsSetupException(e);
55          }
56          catch (IOException JavaDoc e)
57          {
58             throw new CmsSetupException(e);
59          }
60          catch (ParserConfigurationException JavaDoc e)
61          {
62             throw new CmsSetupException(e);
63          }
64          Element JavaDoc docElmt = dmt.getDocumentElement();
65          List JavaDoc mappingsList = XML.getChildren(docElmt, "mime-mapping");
66          for (int i = 0; i < mappingsList.size(); i++)
67          {
68             Element JavaDoc mapping = (org.w3c.dom.Element JavaDoc)mappingsList.get(i);
69             String JavaDoc extension = XML.asString(XML.getUniqueChild(mapping, "extension", true));
70             String JavaDoc mimeType = XML.asString(XML.getUniqueChild(mapping, "mime-type", true));
71             MIME_MAPPINGS.put(extension, mimeType);
72          }
73       }
74    }
75
76    public String JavaDoc getMimeType(String JavaDoc fileExtension)
77    {
78       return (String JavaDoc)MIME_MAPPINGS.get(fileExtension);
79    }
80 }
81
Popular Tags