001: /* ContentTypes.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Fri Oct 1 12:32:24 2004, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2004 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.util.media;
020:
021: import java.util.Map;
022: import java.util.HashMap;
023: import java.io.InputStream;
024: import java.io.IOException;
025: import java.io.BufferedReader;
026: import java.io.InputStreamReader;
027:
028: import org.zkoss.lang.D;
029: import org.zkoss.lang.Strings;
030: import org.zkoss.mesg.MCommon;
031: import org.zkoss.util.logging.Log;
032:
033: /**
034: * Utilities relevant to content types.
035: *
036: * @author tomyeh
037: */
038: public class ContentTypes {
039: private static final Log log = Log.lookup(ContentTypes.class);
040:
041: /** A map of (String format, String contentType). */
042: private static final Map _fmt2ct = new HashMap(17);
043: /** A map of (String contentType, String format). */
044: private static final Map _ct2fmt = new HashMap(17);
045:
046: protected ContentTypes() {
047: } //prevent from initializing
048:
049: /** Returns the content type of the specified format,
050: * such as "html" and "pdf", or null if not found.
051: */
052: public static final String getContentType(String format) {
053: format = format.trim().toLowerCase();
054: final String ctype;
055: synchronized (_fmt2ct) {
056: ctype = (String) _fmt2ct.get(format);
057: }
058:
059: return ctype;
060: }
061:
062: /** Returns the format of the specified content type, or null if not found.
063: */
064: public static final String getFormat(String ctype) {
065: ctype = ctype.trim().toLowerCase();
066: String format;
067: synchronized (_ct2fmt) {
068: format = (String) _ct2fmt.get(ctype);
069: }
070: if (format == null) {
071: //sometime, content type is "text/html;charset=UTF-8"
072: int j = ctype.indexOf(';');
073: if (j >= 0) {
074: ctype = ctype.substring(0, j);
075: synchronized (_ct2fmt) {
076: format = (String) _ct2fmt.get(ctype);
077: }
078: }
079: if (format == null) {
080: j = ctype.indexOf('/');
081: format = j >= 0 ? ctype.substring(j + 1) : ctype;
082: }
083: }
084: return format;
085: }
086:
087: /** Adds additional binding of the format and content type.
088: *
089: * <p>You rarely need to invoke this method, unless your format is not
090: * by the default mapping.
091: */
092: public static final void put(String format, String ctype) {
093: if (format == null || ctype == null)
094: throw new NullPointerException("format or ctype");
095:
096: synchronized (_fmt2ct) {
097: _fmt2ct.put(format, ctype);
098: }
099: synchronized (_ct2fmt) {
100: _ct2fmt.put(ctype, format);
101: }
102: }
103:
104: static {
105: final String flnm = "/metainfo/org/zkoss/util/media/contentTypes.properties";
106: if (!load(flnm))
107: log.warning(MCommon.FILE_NOT_FOUND, flnm);
108: load("/contentTypes.properties"); //override!
109: }
110:
111: private static final boolean load(String flnm) {
112: final InputStream strm = ContentTypes.class
113: .getResourceAsStream(flnm);
114: if (strm == null)
115: return false;
116:
117: //NOTE: we cannot use Properties.load because there might be replicated
118: //mapping (e.g., jpg=images/jpg, jpg=images/pjpeg)
119: try {
120: final BufferedReader in = new BufferedReader(
121: new InputStreamReader(strm));
122:
123: String line;
124: while ((line = in.readLine()) != null) {
125: final int j = line.indexOf('=');
126: if (j < 0) {
127: final int k = Strings.skipWhitespaces(line, 0);
128: if (k < line.length() && line.charAt(k) != '#')
129: log.warning("Ignored error; illgal format: "
130: + line);
131: continue;
132: }
133:
134: final String format = line.substring(0, j).trim();
135: final String ctype = line.substring(j + 1).trim();
136: if (format.length() == 0 || ctype.length() == 0) {
137: log.warning("Ignored error; illgal format: "
138: + line);
139: continue;
140: }
141:
142: _fmt2ct.put(format, ctype);
143: _ct2fmt.put(ctype, format);
144: }
145: } catch (IOException ex) {
146: log.warning("Ingored error: Unable to read " + flnm, ex);
147: }
148: return true;
149: }
150: }
|