| 1 | |
package org.webslinger.util; |
| 2 | |
|
| 3 | |
import java.io.BufferedReader; |
| 4 | |
import java.io.FileInputStream; |
| 5 | |
import java.io.InputStream; |
| 6 | |
import java.io.InputStreamReader; |
| 7 | |
import java.io.IOException; |
| 8 | |
import java.io.OutputStream; |
| 9 | |
import java.io.OutputStreamWriter; |
| 10 | |
import java.io.Reader; |
| 11 | |
import java.io.Writer; |
| 12 | |
import java.net.URL; |
| 13 | |
import java.util.Enumeration; |
| 14 | |
import java.util.HashMap; |
| 15 | |
import java.util.Iterator; |
| 16 | |
import java.util.Map; |
| 17 | |
import java.util.StringTokenizer; |
| 18 | |
|
| 19 | |
import org.webslinger.io.Charsets; |
| 20 | |
|
| 21 | 26 | public class MimeTypes implements MimeTypeLookup { |
| 22 | 26 | protected final HashMap<String, MimeTypeEntryImpl> byExtension = new HashMap<String, MimeTypeEntryImpl>(); |
| 23 | 26 | protected final HashMap<String, MimeTypeEntryImpl> byMimeType = new HashMap<String, MimeTypeEntryImpl>(); |
| 24 | |
|
| 25 | |
public static void main(String[] args) throws IOException { |
| 26 | 0 | MimeTypes mimeTypes = new MimeTypes(); |
| 27 | 0 | mimeTypes.load(new FileInputStream(args[0]), true); |
| 28 | 0 | } |
| 29 | |
|
| 30 | |
public void load(Enumeration<URL> urls) throws IOException { |
| 31 | 0 | while (urls.hasMoreElements()) { |
| 32 | 0 | URL url = urls.nextElement(); |
| 33 | 0 | load(url.openStream()); |
| 34 | 0 | } |
| 35 | 0 | } |
| 36 | |
|
| 37 | |
public void load(InputStream in) throws IOException { |
| 38 | 26 | load(in, true); |
| 39 | 26 | } |
| 40 | |
|
| 41 | |
public void load(InputStream in, boolean close) throws IOException { |
| 42 | 26 | load(new InputStreamReader(in, Charsets.UTF8), close); |
| 43 | 26 | } |
| 44 | |
|
| 45 | |
public void load(InputStream in, boolean close, boolean clear) throws IOException { |
| 46 | 0 | load(new InputStreamReader(in, Charsets.UTF8), close, true); |
| 47 | 0 | } |
| 48 | |
|
| 49 | |
public void load(Reader in) throws IOException { |
| 50 | 0 | load(in, true); |
| 51 | 0 | } |
| 52 | |
|
| 53 | |
public void load(Reader in, boolean close) throws IOException { |
| 54 | 26 | load(in, close, true); |
| 55 | 26 | } |
| 56 | |
|
| 57 | |
public void load(Reader in, boolean close, boolean clear) throws IOException { |
| 58 | 26 | if (clear) { |
| 59 | 26 | byExtension.clear(); |
| 60 | 26 | byMimeType.clear(); |
| 61 | |
} |
| 62 | 26 | BufferedReader br = in instanceof BufferedReader ? (BufferedReader) in : new BufferedReader(in); |
| 63 | |
String line; |
| 64 | 19500 | while ((line = br.readLine()) != null) { |
| 65 | 19474 | line = line.trim(); |
| 66 | 19474 | int hash = line.indexOf("#"); |
| 67 | 19474 | if (hash != -1) line = line.substring(0, hash); |
| 68 | 19474 | if (line.length() == 0) continue; |
| 69 | 18356 | String[] parts = line.split("[ \t]+"); |
| 70 | 18356 | String mimeType = parts[0]; |
| 71 | |
MimeTypeEntryImpl newEntry, oldEntry; |
| 72 | 18356 | if (parts.length > 1) { |
| 73 | 8554 | String[] extensions = new String[parts.length - 1]; |
| 74 | 8554 | System.arraycopy(parts, 1, extensions, 0, extensions.length); |
| 75 | 8554 | newEntry = new MimeTypeEntryImpl(mimeType, extensions); |
| 76 | 8554 | } else { |
| 77 | 9802 | newEntry = new MimeTypeEntryImpl(mimeType); |
| 78 | |
} |
| 79 | 18356 | oldEntry = byMimeType.put(mimeType, newEntry); |
| 80 | 18356 | if (oldEntry != null) oldEntry.removeAll(); |
| 81 | 30004 | for (String extension: newEntry.getExtensions()) { |
| 82 | 11648 | oldEntry = byExtension.put(extension, newEntry); |
| 83 | 11648 | if (oldEntry != null) oldEntry.removeExtension(extension); |
| 84 | |
} |
| 85 | 18356 | } |
| 86 | 26 | if (close) in.close(); |
| 87 | 26 | } |
| 88 | |
|
| 89 | |
public void write(OutputStream out, boolean close) throws IOException { |
| 90 | 0 | write(new OutputStreamWriter(out, Charsets.UTF8), close); |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
public void write(Writer out, boolean close) throws IOException { |
| 94 | 0 | StringBuilder line = new StringBuilder(); |
| 95 | 0 | for (MimeTypeEntry entry: byMimeType.values()) { |
| 96 | 0 | String mimeType = entry.getMimeType(); |
| 97 | 0 | line.append(mimeType); |
| 98 | 0 | int length = mimeType.length(); |
| 99 | 0 | String[] extensions = entry.getExtensions(); |
| 100 | 0 | for (int i = 0; i < extensions.length; i++) { |
| 101 | 0 | if (i == 0 && length < 48) { |
| 102 | 0 | while (length < 48) { |
| 103 | 0 | line.append('\t'); |
| 104 | 0 | length += 8; |
| 105 | |
} |
| 106 | |
} else { |
| 107 | 0 | line.append(' '); |
| 108 | |
} |
| 109 | 0 | line.append(extensions[i]); |
| 110 | |
} |
| 111 | 0 | line.append('\n'); |
| 112 | 0 | out.write(line.toString()); |
| 113 | 0 | line.setLength(0); |
| 114 | 0 | } |
| 115 | 0 | if (close) out.close(); |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
public String getMimeType(String extension) { |
| 119 | 785 | MimeTypeEntry entry = byExtension.get(extension); |
| 120 | 785 | return entry != null ? entry.getMimeType() : null; |
| 121 | |
} |
| 122 | |
|
| 123 | |
public String[] getExtensions(String mimeType) { |
| 124 | 0 | MimeTypeEntry entry = byMimeType.get(mimeType); |
| 125 | 0 | return entry != null ? entry.getExtensions() : null; |
| 126 | |
} |
| 127 | |
|
| 128 | |
public String getPrimaryExtension(String mimeType) { |
| 129 | 0 | MimeTypeEntry entry = byMimeType.get(mimeType); |
| 130 | 0 | return entry != null ? entry.getPrimaryExtension() : null; |
| 131 | |
} |
| 132 | |
|
| 133 | |
public MimeTypeEntry addMimeType(String mimeType) { |
| 134 | 0 | MimeTypeEntryImpl entry = byMimeType.get(mimeType); |
| 135 | 0 | if (entry == null) { |
| 136 | 0 | entry = new MimeTypeEntryImpl(mimeType); |
| 137 | 0 | byMimeType.put(mimeType, entry); |
| 138 | |
} |
| 139 | 0 | return entry; |
| 140 | |
} |
| 141 | |
|
| 142 | |
public MimeTypeEntry addExtension(String mimeType, String extension) { |
| 143 | 0 | MimeTypeEntryImpl entry = byMimeType.get(mimeType); |
| 144 | 0 | if (entry == null) { |
| 145 | 0 | entry = new MimeTypeEntryImpl(mimeType); |
| 146 | 0 | byMimeType.put(mimeType, entry); |
| 147 | |
} |
| 148 | 0 | entry.addExtension(extension); |
| 149 | 0 | return entry; |
| 150 | |
} |
| 151 | |
|
| 152 | |
public String toString() { |
| 153 | 0 | StringBuilder sb = new StringBuilder(); |
| 154 | 0 | Iterator<MimeTypeEntryImpl> it = byMimeType.values().iterator(); |
| 155 | 0 | while (it.hasNext()) { |
| 156 | 0 | MimeTypeEntry entry = it.next(); |
| 157 | 0 | sb.append(entry); |
| 158 | 0 | if (it.hasNext()) sb.append("\n\t"); |
| 159 | 0 | } |
| 160 | 0 | return sb.toString(); |
| 161 | |
} |
| 162 | |
|
| 163 | |
public interface MimeTypeEntry { |
| 164 | |
String getMimeType(); |
| 165 | |
String[] getExtensions(); |
| 166 | |
String getPrimaryExtension(); |
| 167 | |
void addExtension(String extension); |
| 168 | |
void removeExtension(String extension); |
| 169 | |
void setPrimaryExtension(String extension); |
| 170 | |
} |
| 171 | |
|
| 172 | 26 | protected class MimeTypeEntryImpl implements MimeTypeEntry { |
| 173 | |
protected final String mimeType; |
| 174 | |
protected String[] extensions; |
| 175 | |
|
| 176 | |
protected MimeTypeEntryImpl(String mimeType) { |
| 177 | 9802 | this(mimeType, new String[0]); |
| 178 | 9802 | } |
| 179 | |
|
| 180 | 18356 | protected MimeTypeEntryImpl(String mimeType, String[] extensions) { |
| 181 | 18356 | this.mimeType = mimeType; |
| 182 | 18356 | this.extensions = extensions; |
| 183 | 18356 | } |
| 184 | |
|
| 185 | |
public String getMimeType() { |
| 186 | 693 | return mimeType; |
| 187 | |
} |
| 188 | |
|
| 189 | |
public String[] getExtensions() { |
| 190 | 18356 | return extensions; |
| 191 | |
} |
| 192 | |
|
| 193 | |
public String getPrimaryExtension() { |
| 194 | 0 | return extensions.length > 0 ? extensions[0] : null; |
| 195 | |
} |
| 196 | |
|
| 197 | |
public void removeExtension(String extension) { |
| 198 | 338 | for (int i = 0; i < extensions.length; i++) { |
| 199 | 338 | if (extensions[i].equals(extension)) { |
| 200 | 286 | String[] newExtensions = new String[extensions.length - 1]; |
| 201 | 286 | System.arraycopy(extensions, 0, newExtensions, 0, i); |
| 202 | 286 | if (i + 1 < extensions.length) System.arraycopy(extensions, i + 1, newExtensions, i, newExtensions.length - i); |
| 203 | 286 | extensions = newExtensions; |
| 204 | 286 | MimeTypeEntryImpl existing = byExtension.get(extension); |
| 205 | 286 | if (existing != this) return; |
| 206 | 0 | byExtension.remove(extension); |
| 207 | 0 | break; |
| 208 | |
} |
| 209 | |
} |
| 210 | 0 | } |
| 211 | |
|
| 212 | |
public void setPrimaryExtension(String extension) { |
| 213 | 0 | for (int i = 0; i < extensions.length; i++) { |
| 214 | 0 | if (extensions[i].equals(extension)) { |
| 215 | 0 | if (i != 0) { |
| 216 | 0 | String tmp = extensions[0]; |
| 217 | 0 | extensions[0] = extensions[i]; |
| 218 | 0 | extensions[i] = tmp; |
| 219 | |
} |
| 220 | 0 | return; |
| 221 | |
} |
| 222 | |
} |
| 223 | 0 | throw new IllegalArgumentException("Extension(" + extension + ") not found for mime type(" + getMimeType() + ")"); |
| 224 | |
} |
| 225 | |
|
| 226 | |
public void addExtension(String extension) { |
| 227 | 0 | for (String ext: extensions) { |
| 228 | 0 | if (ext.equals(extension)) return; |
| 229 | |
} |
| 230 | 0 | String[] newExtensions = new String[extensions.length + 1]; |
| 231 | 0 | System.arraycopy(extensions, 0, newExtensions, 0, extensions.length); |
| 232 | 0 | newExtensions[extensions.length] = extension; |
| 233 | 0 | extensions = newExtensions; |
| 234 | 0 | MimeTypeEntryImpl old = byExtension.put(extension, this); |
| 235 | 0 | if (old == null) return; |
| 236 | 0 | old.removeExtension(extension); |
| 237 | 0 | } |
| 238 | |
|
| 239 | |
protected void removeAll() { |
| 240 | 0 | for (String extension: extensions) { |
| 241 | 0 | byExtension.remove(extension); |
| 242 | |
} |
| 243 | 0 | extensions = new String[0]; |
| 244 | 0 | } |
| 245 | |
|
| 246 | |
public String toString() { |
| 247 | 0 | StringBuilder sb = new StringBuilder(); |
| 248 | 0 | sb.append("MT(").append(getMimeType()); |
| 249 | 0 | String[] extensions = getExtensions(); |
| 250 | 0 | if (extensions.length > 0) { |
| 251 | 0 | sb.append(':'); |
| 252 | 0 | for (int i = 0; i < extensions.length; i++) { |
| 253 | 0 | if (i != 0) sb.append(' '); |
| 254 | 0 | sb.append(extensions[i]); |
| 255 | |
} |
| 256 | |
} |
| 257 | 0 | sb.append(')'); |
| 258 | 0 | return sb.toString(); |
| 259 | |
} |
| 260 | |
} |
| 261 | |
} |