Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 205   Methods: 7
NCLOC: 121   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MimeTable.java 17.6% 13.8% 14.3% 15.1%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: MimeTable.java 541515 2007-05-25 02:45:06Z vgritsenko $
 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    * MimeTable maps a file extension to a mime type.
 41    *
 42    * @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
 43    * @version $Revision: 541515 $, $Date: 2007-05-24 19:45:06 -0700 (Thu, 24 May 2007) $
 44    */
 45    public class MimeTable {
 46   
 47    /** the default mime type for binaries */
 48    public static final String BINARY_MIME_TYPE = "application/octet-stream";
 49   
 50    /** the default mime type for xml documents */
 51    public static final String XML_MIME_TYPE = "text/xml";
 52   
 53    /** holds the extensions/mime types */
 54    protected static Hashtable mimeMappings = new Hashtable();
 55   
 56    private final static Log log = LogFactory.getLog(MimeTable.class);
 57   
 58    /**
 59    * Configures the Mime table.
 60    * the configuration can look like the Configuration in the
 61    * addMimeConfig(Configuration config) Method<br />
 62    * <br/>
 63    * or if the Configuration should be loaded out of an extra file:
 64    * <pre>
 65    * &lt;mime location="/some/path"&gt;
 66    * </pre>
 67    * or if the Configuration should be loaded out of the Classpath:
 68    * <pre>
 69    * &lt;mime location="resource://org.a.MyFile"&gt;
 70    * </pre>
 71    */
 72  0 public static void configure(String location) {
 73    // load mime table
 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    * Add the given mime Document to this mime table.
 100    * the mime Document looks like this:
 101    * <pre>
 102    * &lt;mime&gt;
 103    * &lt;mime-mapping&gt;
 104    * &lt;extension&gt;xml&lt;/extension&gt;
 105    * &lt;mime-type&gt;xml&lt;/mime-type&gt;
 106    * &lt;/mime-mapping&gt;
 107    * ...
 108    * &lt;/mime&gt;
 109    * </pre>
 110    *
 111    * @param doc mime config.
 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    * Returns the mime type for the given name.
 132    * If no mime for this name is available the parameter defaultXml specifies if
 133    * the xml mime type or the binary mime type are returned.
 134    *
 135    * @param resourceName
 136    * @return mime type for this resource name
 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    }