1 package org.apache.turbine.services.mimetype.util;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.File;
58 import java.io.FileReader;
59 import java.io.StringReader;
60 import java.io.InputStream;
61 import java.io.InputStreamReader;
62 import java.io.BufferedReader;
63 import java.io.IOException;
64 import java.util.Map;
65 import java.util.HashMap;
66 import java.util.StringTokenizer;
67
68 /***
69 * This class defines mappings between MIME types and the corresponding
70 * file name extensions. The mappings are defined as lines formed
71 * by a MIME type name followed by a list of extensions separated
72 * by a whitespace.
73 *
74 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
75 * @version $Id: MimeTypeMapper.java,v 1.1.1.1 2001/08/16 05:09:08 jvanzyl Exp $
76 */
77 public class MimeTypeMapper
78 {
79 /***
80 * Mappings between MIME types and file name extensions.
81 */
82 private HashMap mimeTypeExtensions = new HashMap();
83 protected HashMap extensionMimeTypes = new HashMap();
84
85 /***
86 * Constructs an empty MIME type mapper.
87 */
88 public MimeTypeMapper()
89 {
90 }
91
92 /***
93 * Constructs a mapper reading from a stream.
94 *
95 * @param input an input stream.
96 * @throws IOException for an incorrect stream.
97 */
98 public MimeTypeMapper(InputStream input)
99 throws IOException
100 {
101 parse(new BufferedReader(
102 new InputStreamReader(input,CharSetMap.DEFAULT_CHARSET)));
103 }
104
105 /***
106 * Constructs a mapper reading from a file.
107 *
108 * @param file an input file.
109 * @throws IOException for an incorrect file.
110 */
111 public MimeTypeMapper(File file)
112 throws IOException
113 {
114 FileReader freader = new FileReader(file);
115 try
116 {
117 parse(new BufferedReader(freader));
118 }
119 finally
120 {
121 try
122 {
123 freader.close();
124 }
125 catch (IOException x)
126 {
127 }
128 }
129 }
130
131 /***
132 * Constructs a mapper reading from a file path.
133 *
134 * @param path an input file path.
135 * @throws IOException for an incorrect file.
136 */
137 public MimeTypeMapper(String path)
138 throws IOException
139 {
140 this(new File(path));
141 }
142
143 /***
144 * Sets a MIME content type mapping to extensions.
145 *
146 * @param spec a MIME type extension specification to parse.
147 */
148 public void setContentType(String spec)
149 {
150 try
151 {
152 parse(new BufferedReader(new StringReader(spec)));
153 }
154 catch (IOException x)
155 {
156 }
157 }
158
159 /***
160 * Gets a MIME content type corresponding to a specified file name extension.
161 *
162 * @param ext a file name extension.
163 * @return the corresponding MIME type as a string or null.
164 */
165 public String getContentType(String ext)
166 {
167 return (String) mimeTypeExtensions.get(ext);
168 }
169
170 /***
171 * Gets a file name extension corresponding to a specified MIME content type.
172 *
173 * @param mime a MIME type as a string.
174 * @return the corresponding file name extension or null.
175 */
176 public String getExtension(String type)
177 {
178 return (String) extensionMimeTypes.get(type);
179 }
180
181 /***
182 * Parses MIME type extensions.
183 *
184 * @param reader a reader to parse.
185 * @throws IOException for an incorrect reader.
186 */
187 protected synchronized void parse(BufferedReader reader)
188 throws IOException
189 {
190 int l,count = 0;
191 String next;
192 String str = null;
193 HashMap mimeTypes = (HashMap) extensionMimeTypes.clone();
194 HashMap extensions = (HashMap) mimeTypeExtensions.clone();
195 while ((next = reader.readLine()) != null)
196 {
197 str = str == null ? next : str + next;
198 if ((l = str.length()) == 0)
199 {
200 str = null;
201 continue;
202 }
203 // Check for continuation line.
204 if (str.charAt(l - 1) != '//')
205 {
206 count += parseMimeTypeExtension(str,mimeTypes,extensions);
207 str = null;
208 }
209 else
210 {
211 str = str.substring(0,l - 1);
212 }
213 }
214 if (str != null)
215 {
216 count += parseMimeTypeExtension(str,mimeTypes,extensions);
217 }
218 if (count > 0)
219 {
220 extensionMimeTypes = mimeTypes;
221 mimeTypeExtensions = extensions;
222 }
223 }
224
225 /***
226 * Parses a MIME type extension.
227 *
228 * @param spec an extension specification to parse.
229 * @param mimeTypes a map of MIME types.
230 * @param extensions a map of extensions.
231 * @return the number of file name extensions parsed.
232 */
233 protected int parseMimeTypeExtension(String spec,
234 Map mimeTypes,
235 Map extensions)
236 {
237 int count = 0;
238 spec = spec.trim();
239 if ((spec.length() > 0) &&
240 (spec.charAt(0) != '#'))
241 {
242 StringTokenizer tokens = new StringTokenizer(spec);
243 String type = tokens.nextToken();
244 String ext;
245 while (tokens.hasMoreTokens())
246 {
247 ext = tokens.nextToken();
248 if (ext.length() == 0)
249 {
250 continue;
251 }
252 extensions.put(ext,type);
253 if (count++ == 0)
254 {
255 mimeTypes.put(type,ext);
256 }
257 }
258 }
259 return count;
260 }
261 }
This page was automatically generated by Maven