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