01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.cms;
23:
24: import org.jboss.portal.common.io.IOTools;
25: import org.jboss.portal.common.xml.XMLTools;
26: import org.w3c.dom.Document;
27: import org.w3c.dom.Element;
28:
29: import java.io.InputStream;
30: import java.util.HashMap;
31: import java.util.List;
32:
33: /**
34: * @author <a href="mailto:palber@novell.com">Polina Alber</a> Date: Apr 30, 2005; Time: 2:10:44 AM
35: * @since JBoss portal 2.0 Class org.jboss.portal.setup.impl.dl.cmsloader.CMSMimeMappings
36: */
37: public class CMSMimeMappings {
38:
39: /** Definition of default mime type mappings to file extension. */
40: private static final String MAPPINGS_FILE = "org/jboss/portal/cms/mime-mappings.xml";
41:
42: /** . */
43: private final HashMap mappings = new HashMap();
44:
45: public CMSMimeMappings() {
46: InputStream is = null;
47: try {
48: is = Thread.currentThread().getContextClassLoader()
49: .getResourceAsStream(MAPPINGS_FILE);
50: Document dmt = XMLTools.getDocumentBuilderFactory()
51: .newDocumentBuilder().parse(is);
52: Element docElmt = dmt.getDocumentElement();
53: List mappingsList = XMLTools.getChildren(docElmt,
54: "mime-mapping");
55: for (int i = 0; i < mappingsList.size(); i++) {
56: Element mapping = (Element) mappingsList.get(i);
57: String extension = XMLTools.asString(XMLTools
58: .getUniqueChild(mapping, "extension", true));
59: String mimeType = XMLTools.asString(XMLTools
60: .getUniqueChild(mapping, "mime-type", true));
61: mappings.put(extension, mimeType);
62: }
63: } catch (Exception e) {
64: throw new Error("Cannot load mime mapping file", e);
65: } finally {
66: IOTools.safeClose(is);
67: }
68: }
69:
70: public String getMimeType(String fileExtension) {
71: return (String) mappings.get(fileExtension);
72: }
73: }
|