KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > MimeMap


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web;
25
26 import java.util.*;
27 import java.io.*;
28
29 /**
30  * Class representing the parsed mime mapping file of a mime element.
31  */

32 public class MimeMap {
33    
34     private static final String JavaDoc MIME_TYPE = "type=";
35     private static final String JavaDoc MIME_EXTS = "exts=";
36
37     private String JavaDoc id;
38     private HashMap mimeMappings;
39
40     /**
41      * Constructor.
42      *
43      * @param id The mime id of the mime element which this MimeMap represents
44      */

45     MimeMap(String JavaDoc id) {
46         this.id = id;
47     }
48
49     /**
50      * Gets the mime id of the mime element which this MimeMap represents.
51      */

52     String JavaDoc getId() {
53         return this.id;
54     }
55
56     /**
57      * Parses the mime mappings from the given file.
58      *
59      * @param file The mime file
60      */

61     void load(String JavaDoc file) throws Exception JavaDoc {
62
63         BufferedReader in = new BufferedReader(new FileReader(file));
64         while (true) {
65             // Get next line
66
String JavaDoc line = in.readLine();
67             if (line == null)
68                 return;
69
70             int len = line.length();
71             if (len > 0) {
72                 // Ignore comments
73
char firstChar = line.charAt(0);
74                 if ((firstChar != '#') && (firstChar != '!')) {
75
76                     // Find start of key
77
int keyStart = 0;
78                     while (keyStart < len
79                             && Character.isSpace(line.charAt(keyStart))) {
80                         keyStart++;
81                     }
82
83                     // Blank lines are ignored
84
if (keyStart == len) {
85                         continue;
86                     }
87
88                     int keyEnd = keyStart;
89                     while (keyEnd<len
90                             && !Character.isSpace(line.charAt(keyEnd))) {
91                         keyEnd++;
92                     }
93
94                     // Find start of value
95
int valueStart = keyEnd;
96                     while (valueStart<len
97                             && Character.isSpace(line.charAt(valueStart))) {
98                         valueStart++;
99                     }
100                     if (valueStart == len) {
101                         // Ignore this MIME mapping
102
continue;
103                     }
104                     int valueEnd = valueStart;
105                     while (valueEnd<len
106                             && !Character.isSpace(line.charAt(valueEnd))) {
107                         valueEnd++;
108                     }
109
110                     String JavaDoc key = line.substring(keyStart, keyEnd);
111                     String JavaDoc value = line.substring(valueStart, valueEnd);
112
113                     addMappings(key, value);
114                 }
115             }
116         }
117     }
118
119     /**
120      * Returns an iterator over the mime extensions that were parsed
121      *
122      * @return Iterator over the mime extensions that were parsed, or null if
123      * the mime file was empty
124      */

125     Iterator getExtensions() {
126         Iterator ret = null;
127         if (mimeMappings != null) {
128             ret = mimeMappings.keySet().iterator();
129         }
130         return ret;
131     }
132
133     /**
134      * Gets the mime type corresponding to the given extension
135      *
136      * @param extension The mime extension
137      *
138      * @return The mime type corresponding to the given extension, or null if
139      * the given extension does not map to any mime type
140      */

141     String JavaDoc getType(String JavaDoc extension) {
142         String JavaDoc ret = null;
143         if (mimeMappings != null) {
144             ret = (String JavaDoc) mimeMappings.get(extension);
145         }
146         return ret;
147     }
148
149     private void addMappings(String JavaDoc type, String JavaDoc exts) {
150         // Remove "type=" prefix
151
int index = type.indexOf(MIME_TYPE);
152         if (index == -1) {
153             // ignore
154
return;
155         }
156         type = type.substring(index + MIME_TYPE.length());
157
158         // Remove "exts=" prefix
159
index = exts.indexOf(MIME_EXTS);
160         if (index == -1) {
161             // ignore
162
return;
163         }
164
165         if (mimeMappings == null) {
166             mimeMappings = new HashMap();
167     }
168
169         exts = exts.substring(index + MIME_EXTS.length());
170         index = exts.indexOf(',');
171         String JavaDoc ext = null;
172         if (index != -1) {
173             // e.g., exts=aif,aiff,aifc
174
int fromIndex = 0;
175             while (index != -1) {
176                 ext = exts.substring(fromIndex, index).trim();
177                 if (ext.length() > 0) {
178                     mimeMappings.put(ext, type);
179                 }
180                 fromIndex = index+1;
181                 index = exts.indexOf(',', fromIndex);
182             }
183             ext = exts.substring(fromIndex);
184         } else {
185             // e.g., exts=gif
186
ext = exts;
187         }
188
189         if (ext != null) {
190             ext = ext.trim();
191             if (ext.length() > 0) {
192                 mimeMappings.put(ext, type);
193             }
194         }
195     }
196 }
197
Popular Tags