1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.webadmin.util; |
21 |
| |
22 |
| import java.io.BufferedReader; |
23 |
| import java.io.BufferedWriter; |
24 |
| import java.io.FileReader; |
25 |
| import java.io.FileWriter; |
26 |
| import java.io.IOException; |
27 |
| import java.io.InputStream; |
28 |
| import java.io.Writer; |
29 |
| import java.util.Hashtable; |
30 |
| import java.util.HashMap; |
31 |
| import java.util.Iterator; |
32 |
| |
33 |
| import org.apache.xindice.xml.dom.DOMParser; |
34 |
| import org.apache.commons.logging.Log; |
35 |
| import org.apache.commons.logging.LogFactory; |
36 |
| import org.w3c.dom.Document; |
37 |
| import org.w3c.dom.NodeList; |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| public class MimeTable { |
46 |
| |
47 |
| |
48 |
| public static final String BINARY_MIME_TYPE = "application/octet-stream"; |
49 |
| |
50 |
| |
51 |
| public static final String XML_MIME_TYPE = "text/xml"; |
52 |
| |
53 |
| |
54 |
| protected static Hashtable mimeMappings = new Hashtable(); |
55 |
| |
56 |
| private final static Log log = LogFactory.getLog(MimeTable.class); |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
0
| public static void configure(String location) {
|
73 |
| |
74 |
0
| boolean loadFromClasspath = false;
|
75 |
0
| if (location.startsWith("resource://")) {
|
76 |
0
| loadFromClasspath = true;
|
77 |
0
| location = location.substring(11);
|
78 |
| } |
79 |
| |
80 |
0
| try {
|
81 |
0
| if (loadFromClasspath) {
|
82 |
0
| InputStream is = Thread.currentThread().getContextClassLoader().getResource(location).openStream();
|
83 |
0
| if (log.isDebugEnabled()) {
|
84 |
0
| log.debug("Load Configuration from Classpath: " + location);
|
85 |
| } |
86 |
0
| addMimeConfig(DOMParser.toDocument(is));
|
87 |
| } else { |
88 |
0
| if (log.isDebugEnabled()) {
|
89 |
0
| log.debug("Load Configuration from File: " + location);
|
90 |
| } |
91 |
0
| addMimeConfig(DOMParser.toDocument(location));
|
92 |
| } |
93 |
| } catch (Exception e) { |
94 |
0
| log.error("Could not load MimeTable configuration", e);
|
95 |
| } |
96 |
| } |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
1
| public static void addMimeConfig(Document doc) {
|
114 |
1
| NodeList mapping = doc.getChildNodes();
|
115 |
1
| for (int i = 0; i < mapping.getLength(); i++) {
|
116 |
2
| NodeList thisMapping = mapping.item(i).getChildNodes();
|
117 |
2
| String ext = "";
|
118 |
2
| String type = "";
|
119 |
2
| for (int j = 0; j < thisMapping.getLength(); j++) {
|
120 |
277
| if (thisMapping.item(j).getNodeName().equalsIgnoreCase("extension")) {
|
121 |
0
| ext = thisMapping.item(j).getNodeValue();
|
122 |
277
| } else if (thisMapping.item(j).getNodeName().equalsIgnoreCase("mime-type")) {
|
123 |
0
| type = thisMapping.item(j).getNodeValue();
|
124 |
| } |
125 |
| } |
126 |
2
| mimeMappings.put(ext, type);
|
127 |
| } |
128 |
| } |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
0
| public static String getMimeType(String resourceName) {
|
139 |
0
| int split = resourceName.lastIndexOf('.');
|
140 |
0
| String mime = null;
|
141 |
0
| if (split > 0) {
|
142 |
0
| mime = (String) mimeMappings.get(resourceName.substring(split + 1));
|
143 |
| } |
144 |
| |
145 |
0
| if (mime != null) {
|
146 |
0
| return mime;
|
147 |
| } else { |
148 |
0
| return BINARY_MIME_TYPE;
|
149 |
| } |
150 |
| } |
151 |
| |
152 |
0
| private static void writeMappingAsXml(HashMap mappings, Writer out) throws IOException {
|
153 |
0
| out.write("<?xml version=\"1.0\"?>\n");
|
154 |
0
| out.write("<mime>\n");
|
155 |
0
| for (Iterator i = mappings.keySet().iterator(); i.hasNext();) {
|
156 |
0
| String ext = (String) i.next();
|
157 |
0
| String type = (String) mappings.get(ext);
|
158 |
| |
159 |
0
| out.write(" <mime-mapping>\n");
|
160 |
0
| out.write(" <extension>" + ext + "</extension>\n");
|
161 |
0
| out.write(" <mime-type>" + type + "</mime-type>\n");
|
162 |
0
| out.write(" </mime-mapping>\n");
|
163 |
| } |
164 |
0
| out.write("</mime>");
|
165 |
| } |
166 |
| |
167 |
0
| private static HashMap parseApacheHttpdMime(String location) throws IOException {
|
168 |
0
| HashMap mappings = new HashMap();
|
169 |
0
| BufferedReader f = new BufferedReader(new FileReader(location));
|
170 |
0
| String line;
|
171 |
0
| while ((line = f.readLine()) != null) {
|
172 |
0
| int startComment = line.indexOf('#');
|
173 |
0
| if (startComment >= 0) {
|
174 |
0
| line = line.substring(0, startComment);
|
175 |
| } |
176 |
0
| line = line.trim();
|
177 |
0
| String[] parsedLine = line.split(" |\\t");
|
178 |
0
| if (parsedLine.length >= 2) {
|
179 |
0
| for (int i = 1; i < parsedLine.length; i++) {
|
180 |
0
| if (parsedLine[i].length() > 0) {
|
181 |
0
| mappings.put(parsedLine[i], parsedLine[0]);
|
182 |
| } |
183 |
| } |
184 |
| } |
185 |
| } |
186 |
0
| return mappings;
|
187 |
| } |
188 |
| |
189 |
0
| private static void convertApacheHttpdMimeToXml(String source, String destination) throws IOException {
|
190 |
0
| HashMap mappings = parseApacheHttpdMime(source);
|
191 |
0
| BufferedWriter output = new BufferedWriter(new FileWriter(destination));
|
192 |
0
| writeMappingAsXml(mappings, output);
|
193 |
| } |
194 |
| |
195 |
0
| public static void main(String[] args) throws Exception {
|
196 |
0
| if (args.length != 2) {
|
197 |
0
| System.out.println("Convert Apache httpd mime file to a xml file");
|
198 |
0
| System.out.println("Usage: java org.apache.xindice.webadmin.util.MimeTable httpdfile xmlfile");
|
199 |
0
| System.exit(1);
|
200 |
| } |
201 |
| |
202 |
0
| convertApacheHttpdMimeToXml(args[0], args[1]);
|
203 |
0
| System.out.println("Converted " + args[0] + " to " + args[1]);
|
204 |
| } |
205 |
| } |