1 23 24 package com.sun.enterprise.web; 25 26 import java.util.*; 27 import java.io.*; 28 29 32 public class MimeMap { 33 34 private static final String MIME_TYPE = "type="; 35 private static final String MIME_EXTS = "exts="; 36 37 private String id; 38 private HashMap mimeMappings; 39 40 45 MimeMap(String id) { 46 this.id = id; 47 } 48 49 52 String getId() { 53 return this.id; 54 } 55 56 61 void load(String file) throws Exception { 62 63 BufferedReader in = new BufferedReader(new FileReader(file)); 64 while (true) { 65 String line = in.readLine(); 67 if (line == null) 68 return; 69 70 int len = line.length(); 71 if (len > 0) { 72 char firstChar = line.charAt(0); 74 if ((firstChar != '#') && (firstChar != '!')) { 75 76 int keyStart = 0; 78 while (keyStart < len 79 && Character.isSpace(line.charAt(keyStart))) { 80 keyStart++; 81 } 82 83 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 int valueStart = keyEnd; 96 while (valueStart<len 97 && Character.isSpace(line.charAt(valueStart))) { 98 valueStart++; 99 } 100 if (valueStart == len) { 101 continue; 103 } 104 int valueEnd = valueStart; 105 while (valueEnd<len 106 && !Character.isSpace(line.charAt(valueEnd))) { 107 valueEnd++; 108 } 109 110 String key = line.substring(keyStart, keyEnd); 111 String value = line.substring(valueStart, valueEnd); 112 113 addMappings(key, value); 114 } 115 } 116 } 117 } 118 119 125 Iterator getExtensions() { 126 Iterator ret = null; 127 if (mimeMappings != null) { 128 ret = mimeMappings.keySet().iterator(); 129 } 130 return ret; 131 } 132 133 141 String getType(String extension) { 142 String ret = null; 143 if (mimeMappings != null) { 144 ret = (String ) mimeMappings.get(extension); 145 } 146 return ret; 147 } 148 149 private void addMappings(String type, String exts) { 150 int index = type.indexOf(MIME_TYPE); 152 if (index == -1) { 153 return; 155 } 156 type = type.substring(index + MIME_TYPE.length()); 157 158 index = exts.indexOf(MIME_EXTS); 160 if (index == -1) { 161 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 ext = null; 172 if (index != -1) { 173 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 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 |