1 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 ; 15 import org.w3c.dom.Element ; 16 import org.xml.sax.SAXException ; 17 18 import javax.xml.parsers.ParserConfigurationException ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.io.InputStream ; 22 import java.io.IOException ; 23 24 30 public class CMSMimeMappings 31 { 32 private static final HashMap MIME_MAPPINGS = new HashMap (); 34 private static final String 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 is = Thread.currentThread().getContextClassLoader().getResourceAsStream(MAPPINGS_FILE); 45 if (is != null) 46 { 47 Document dmt = null; 48 try 49 { 50 dmt = XML.getDocumentBuilderFactory().newDocumentBuilder().parse(is); 51 } 52 catch (SAXException e) 53 { 54 throw new CmsSetupException(e); 55 } 56 catch (IOException e) 57 { 58 throw new CmsSetupException(e); 59 } 60 catch (ParserConfigurationException e) 61 { 62 throw new CmsSetupException(e); 63 } 64 Element docElmt = dmt.getDocumentElement(); 65 List mappingsList = XML.getChildren(docElmt, "mime-mapping"); 66 for (int i = 0; i < mappingsList.size(); i++) 67 { 68 Element mapping = (org.w3c.dom.Element )mappingsList.get(i); 69 String extension = XML.asString(XML.getUniqueChild(mapping, "extension", true)); 70 String mimeType = XML.asString(XML.getUniqueChild(mapping, "mime-type", true)); 71 MIME_MAPPINGS.put(extension, mimeType); 72 } 73 } 74 } 75 76 public String getMimeType(String fileExtension) 77 { 78 return (String )MIME_MAPPINGS.get(fileExtension); 79 } 80 } 81 | Popular Tags |