author | dxu |
Tue, 04 Dec 2012 14:07:30 +0000 | |
changeset 14702 | 111342b28e67 |
permissions | -rw-r--r-- |
14702
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
1 |
/* |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
2 |
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
4 |
* |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
10 |
* |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
15 |
* accompanied this code). |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
16 |
* |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
20 |
* |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
23 |
* questions. |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
24 |
*/ |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
25 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
26 |
package sun.nio.fs; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
27 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
28 |
import java.io.IOException; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
29 |
import java.nio.charset.Charset; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
30 |
import java.nio.file.Files; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
31 |
import java.nio.file.Path; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
32 |
import java.security.AccessController; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
33 |
import java.security.PrivilegedAction; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
34 |
import java.util.Collections; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
35 |
import java.util.HashMap; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
36 |
import java.util.List; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
37 |
import java.util.Map; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
38 |
import java.util.regex.Matcher; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
39 |
import java.util.regex.Pattern; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
40 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
41 |
/** |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
42 |
* File type detector that uses a file extension to look up its MIME type |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
43 |
* based on a mime.types file. |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
44 |
*/ |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
45 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
46 |
class MimeTypesFileTypeDetector extends AbstractFileTypeDetector { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
47 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
48 |
// path to mime.types file |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
49 |
private final Path mimeTypesFile; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
50 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
51 |
// map of extension to MIME type |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
52 |
private Map<String,String> mimeTypeMap; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
53 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
54 |
// set to true when file loaded |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
55 |
private volatile boolean loaded = false; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
56 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
57 |
public MimeTypesFileTypeDetector(Path filePath) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
58 |
mimeTypesFile = filePath; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
59 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
60 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
61 |
@Override |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
62 |
protected String implProbeContentType(Path path) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
63 |
Path fn = path.getFileName(); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
64 |
if (fn == null) |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
65 |
return null; // no file name |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
66 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
67 |
String ext = getExtension(fn.toString()); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
68 |
if (ext.isEmpty()) |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
69 |
return null; // no extension |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
70 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
71 |
loadMimeTypes(); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
72 |
if (mimeTypeMap == null || mimeTypeMap.isEmpty()) |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
73 |
return null; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
74 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
75 |
// Case-sensitive search |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
76 |
String mimeType; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
77 |
do { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
78 |
mimeType = mimeTypeMap.get(ext); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
79 |
if (mimeType == null) |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
80 |
ext = getExtension(ext); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
81 |
} while (mimeType == null && !ext.isEmpty()); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
82 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
83 |
return mimeType; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
84 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
85 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
86 |
// Get the extension of a file name. |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
87 |
private static String getExtension(String name) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
88 |
String ext = ""; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
89 |
if (name != null && !name.isEmpty()) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
90 |
int dot = name.indexOf('.'); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
91 |
if ((dot >= 0) && (dot < name.length() - 1)) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
92 |
ext = name.substring(dot + 1); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
93 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
94 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
95 |
return ext; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
96 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
97 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
98 |
/** |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
99 |
* Parse the mime types file, and store the type-extension mappings into |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
100 |
* mimeTypeMap. The mime types file is not loaded until the first probe |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
101 |
* to achieve the lazy initialization. It adopts double-checked locking |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
102 |
* optimization to reduce the locking overhead. |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
103 |
*/ |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
104 |
private void loadMimeTypes() { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
105 |
if (!loaded) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
106 |
synchronized (this) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
107 |
if (!loaded) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
108 |
List<String> lines = AccessController.doPrivileged( |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
109 |
new PrivilegedAction<List<String>>() { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
110 |
@Override |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
111 |
public List<String> run() { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
112 |
try { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
113 |
return Files.readAllLines(mimeTypesFile, |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
114 |
Charset.defaultCharset()); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
115 |
} catch (IOException ignore) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
116 |
return Collections.emptyList(); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
117 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
118 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
119 |
}); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
120 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
121 |
mimeTypeMap = new HashMap<>(lines.size()); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
122 |
String entry = ""; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
123 |
for (String line : lines) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
124 |
entry += line; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
125 |
if (entry.endsWith("\\")) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
126 |
entry = entry.substring(0, entry.length() - 1); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
127 |
continue; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
128 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
129 |
parseMimeEntry(entry); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
130 |
entry = ""; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
131 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
132 |
if (!entry.isEmpty()) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
133 |
parseMimeEntry(entry); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
134 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
135 |
loaded = true; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
136 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
137 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
138 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
139 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
140 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
141 |
/** |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
142 |
* Parse a mime-types entry, which can have the following formats. |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
143 |
* 1) Simple space-delimited format |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
144 |
* image/jpeg jpeg jpg jpe JPG |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
145 |
* |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
146 |
* 2) Netscape key-value pair format |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
147 |
* type=application/x-java-jnlp-file desc="Java Web Start" exts="jnlp" |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
148 |
* or |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
149 |
* type=text/html exts=htm,html |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
150 |
*/ |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
151 |
private void parseMimeEntry(String entry) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
152 |
entry = entry.trim(); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
153 |
if (entry.isEmpty() || entry.charAt(0) == '#') |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
154 |
return; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
155 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
156 |
entry = entry.replaceAll("\\s*#.*", ""); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
157 |
int equalIdx = entry.indexOf('='); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
158 |
if (equalIdx > 0) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
159 |
// Parse a mime-types command having the key-value pair format |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
160 |
final String TYPEEQUAL = "type="; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
161 |
String typeRegex = "\\b" + TYPEEQUAL + |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
162 |
"(\"\\p{Graph}+?/\\p{Graph}+?\"|\\p{Graph}+/\\p{Graph}+\\b)"; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
163 |
Pattern typePattern = Pattern.compile(typeRegex); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
164 |
Matcher typeMatcher = typePattern.matcher(entry); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
165 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
166 |
if (typeMatcher.find()) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
167 |
String type = typeMatcher.group().substring(TYPEEQUAL.length()); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
168 |
if (type.charAt(0) == '"') { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
169 |
type = type.substring(1, type.length() - 1); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
170 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
171 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
172 |
final String EXTEQUAL = "exts="; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
173 |
String extRegex = "\\b" + EXTEQUAL + |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
174 |
"(\"[\\p{Graph}|\\p{Blank}]+?\"|\\p{Graph}+\\b)"; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
175 |
Pattern extPattern = Pattern.compile(extRegex); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
176 |
Matcher extMatcher = extPattern.matcher(entry); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
177 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
178 |
if (extMatcher.find()) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
179 |
String exts = |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
180 |
extMatcher.group().substring(EXTEQUAL.length()); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
181 |
if (exts.charAt(0) == '"') { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
182 |
exts = exts.substring(1, exts.length() - 1); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
183 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
184 |
String[] extList = exts.split("[\\p{Blank}|\\p{Punct}]+"); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
185 |
for (String ext : extList) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
186 |
putIfAbsent(ext, type); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
187 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
188 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
189 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
190 |
} else { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
191 |
// Parse a mime-types command having the space-delimited format |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
192 |
String[] elements = entry.split("\\s+"); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
193 |
int i = 1; |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
194 |
while (i < elements.length) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
195 |
putIfAbsent(elements[i++], elements[0]); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
196 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
197 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
198 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
199 |
|
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
200 |
private void putIfAbsent(String key, String value) { |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
201 |
if (key != null && !key.isEmpty() && |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
202 |
value != null && !value.isEmpty() && |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
203 |
!mimeTypeMap.containsKey(key)) |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
204 |
{ |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
205 |
mimeTypeMap.put(key, value); |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
206 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
207 |
} |
111342b28e67
7142921: (fs) Files.probeContentType reports a MIME type of "text/plain" on Ubuntu 11.04
dxu
parents:
diff
changeset
|
208 |
} |