Coverage Report - org.apache.fulcrum.mimetype.DefaultMimeTypeService
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMimeTypeService
44%
20/45
40%
4/10
1,474
 
 1  
 package org.apache.fulcrum.mimetype;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.util.Locale;
 25  
 
 26  
 import org.apache.avalon.framework.activity.Initializable;
 27  
 import org.apache.avalon.framework.configuration.Configurable;
 28  
 import org.apache.avalon.framework.configuration.Configuration;
 29  
 import org.apache.avalon.framework.context.Context;
 30  
 import org.apache.avalon.framework.context.ContextException;
 31  
 import org.apache.avalon.framework.context.Contextualizable;
 32  
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 33  
 import org.apache.fulcrum.mimetype.util.CharSetMap;
 34  
 import org.apache.fulcrum.mimetype.util.MimeType;
 35  
 import org.apache.fulcrum.mimetype.util.MimeTypeMap;
 36  
 /**
 37  
  * The MimeType Service maintains mappings between MIME types and
 38  
  * the corresponding file name extensions, and between locales and
 39  
  * character encodings.
 40  
  *
 41  
  * <p>The MIME type mappings can be defined in MIME type files
 42  
  * located in user's home directory, Java home directory or
 43  
  * the current class jar. The default mapping file is defined
 44  
  * with the mime.type.file property. In addition, the service maintains
 45  
  * a set of most common mappings.
 46  
  *
 47  
  * <p>The charset mappings can be defined in property files
 48  
  * located in user's home directory, Java home directory or
 49  
  * the current class jar. The default mapping file is defined
 50  
  * with the charset.file property. In addition, the service maintains
 51  
  * a set of most common mappings.
 52  
  *
 53  
  * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
 54  
  * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
 55  
  * @version $Id: DefaultMimeTypeService.java 813677 2009-09-11 06:48:11Z tv $
 56  
  *
 57  
  * @avalon.component name="mimetype" lifestyle="singleton"
 58  
  * @avalon.service type="org.apache.fulcrum.mimetype.MimeTypeService"
 59  
  */
 60  
 public class DefaultMimeTypeService
 61  
     extends AbstractLogEnabled
 62  
     implements MimeTypeService, Configurable, Initializable, Contextualizable
 63  
 {
 64  
     private String applicationRoot;
 65  
     /**
 66  
      * The MIME type file property.
 67  
      */
 68  
     public static final String MIME_TYPES = "mimetypes";
 69  
     /**
 70  
      * The charset file property.
 71  
      */
 72  
     public static final String CHARSETS = "charsets";
 73  
     // path to a mimetypes-file_extension mapping file
 74  
     private String mimetypePath;
 75  
     // path to a charset-language mapping file
 76  
     private String charsetPath;
 77  
     /**
 78  
      * The MIME type map used by the service.
 79  
      */
 80  
     private MimeTypeMap mimeTypeMap;
 81  
     /**
 82  
      * The charset map used by the service.
 83  
      */
 84  
     private CharSetMap charSetMap;
 85  
 
 86  
     /**
 87  
      * Constructs a new service.
 88  
      */
 89  
     public DefaultMimeTypeService()
 90  9
     {
 91  
         // empty
 92  9
     }
 93  
     /**
 94  
      * Sets a MIME content type mapping to extensions to the map.
 95  
      * The extension is specified by a MIME type name followed
 96  
      * by a list of file name extensions separated by a whitespace.
 97  
      *
 98  
      * @param spec a MIME type extension specification to add.
 99  
      */
 100  
     public void setContentType(String spec)
 101  
     {
 102  9
         mimeTypeMap.setContentType(spec);
 103  9
     }
 104  
     /**
 105  
      * Gets the MIME content type for a file as a string.
 106  
      *
 107  
      * @param file The file to look up a MIME type mapping for.
 108  
      * @return the MIME type string.
 109  
      */
 110  
     public String getContentType(File file)
 111  
     {
 112  15
         return mimeTypeMap.getContentType(file);
 113  
     }
 114  
     /**
 115  
      * Gets the MIME content type for a named file as a string.
 116  
      *
 117  
      * @param fileName The name of the file to look up a MIME type
 118  
      * mapping for.
 119  
      * @return the MIME type string.
 120  
      */
 121  
     public String getContentType(String fileName)
 122  
     {
 123  0
         return mimeTypeMap.getContentType(fileName);
 124  
     }
 125  
     /**
 126  
      * Gets the MIME content type for a file name extension as a string.
 127  
      *
 128  
      * @param fileName The name of the file to look up a MIME type
 129  
      * mapping for.
 130  
      * @param def The default MIME type to use if no mapping exists.
 131  
      * @return the MIME type string.
 132  
      */
 133  
     public String getContentType(String fileName, String def)
 134  
     {
 135  0
         return mimeTypeMap.getContentType(fileName, def);
 136  
     }
 137  
     /**
 138  
      * Gets the MIME content type for a file.
 139  
      *
 140  
      * @param file the file.
 141  
      * @return the MIME type.
 142  
      */
 143  
     public MimeType getMimeContentType(File file)
 144  
     {
 145  0
         return mimeTypeMap.getMimeContentType(file);
 146  
     }
 147  
     /**
 148  
      * Gets the MIME content type for a named file.
 149  
      *
 150  
      * @param name the name of the file.
 151  
      * @return the MIME type.
 152  
      */
 153  
     public MimeType getMimeContentType(String name)
 154  
     {
 155  0
         return mimeTypeMap.getMimeContentType(name);
 156  
     }
 157  
     /**
 158  
      * Gets the MIME content type for a file name extension.
 159  
      *
 160  
      * @param ext the file name extension.
 161  
      * @param def the default type if none is found.
 162  
      * @return the MIME type.
 163  
      */
 164  
     public MimeType getMimeContentType(String ext, String def)
 165  
     {
 166  0
         return mimeTypeMap.getMimeContentType(ext, def);
 167  
     }
 168  
     /**
 169  
      * Gets the default file name extension for a MIME type.
 170  
      * Note that the mappers are called in the reverse order.
 171  
      *
 172  
      * @param type the MIME type as a string.
 173  
      * @return the file name extension or null.
 174  
      */
 175  
     public String getDefaultExtension(String type)
 176  
     {
 177  3
         return mimeTypeMap.getDefaultExtension(type);
 178  
     }
 179  
     /**
 180  
      * Gets the default file name extension for a MIME type.
 181  
      * Note that the mappers are called in the reverse order.
 182  
      *
 183  
      * @param mime the MIME type.
 184  
      * @return the file name extension or null.
 185  
      */
 186  
     public String getDefaultExtension(MimeType mime)
 187  
     {
 188  3
         return mimeTypeMap.getDefaultExtension(mime);
 189  
     }
 190  
     /**
 191  
      * Sets a locale-charset mapping.
 192  
      *
 193  
      * @param key the key for the charset.
 194  
      * @param charset the corresponding charset.
 195  
      */
 196  
     public void setCharSet(String key, String charset)
 197  
     {
 198  0
         charSetMap.setCharSet(key, charset);
 199  0
     }
 200  
     /**
 201  
      * Gets the charset for a locale. First a locale specific charset
 202  
      * is searched for, then a country specific one and lastly a language
 203  
      * specific one. If none is found, the default charset is returned.
 204  
      *
 205  
      * @param locale the locale.
 206  
      * @return the charset.
 207  
      */
 208  
     public String getCharSet(Locale locale)
 209  
     {
 210  3
         return charSetMap.getCharSet(locale);
 211  
     }
 212  
     /**
 213  
      * Gets the charset for a locale with a variant. The search
 214  
      * is performed in the following order:
 215  
      * "lang"_"country"_"variant"="charset",
 216  
      * _"counry"_"variant"="charset",
 217  
      * "lang"__"variant"="charset",
 218  
      * __"variant"="charset",
 219  
      * "lang"_"country"="charset",
 220  
      * _"country"="charset",
 221  
      * "lang"="charset".
 222  
      * If nothing of the above is found, the default charset is returned.
 223  
      *
 224  
      * @param locale the locale.
 225  
      * @param variant a variant field.
 226  
      * @return the charset.
 227  
      */
 228  
     public String getCharSet(Locale locale, String variant)
 229  
     {
 230  0
         return charSetMap.getCharSet(locale, variant);
 231  
     }
 232  
     /**
 233  
      * Gets the charset for a specified key.
 234  
      *
 235  
      * @param key the key for the charset.
 236  
      * @return the found charset or the default one.
 237  
      */
 238  
     public String getCharSet(String key)
 239  
     {
 240  0
         return charSetMap.getCharSet(key);
 241  
     }
 242  
     /**
 243  
      * Gets the charset for a specified key.
 244  
      *
 245  
      * @param key the key for the charset.
 246  
      * @param def the default charset if none is found.
 247  
      * @return the found charset or the given default.
 248  
      */
 249  
     public String getCharSet(String key, String def)
 250  
     {
 251  0
         return charSetMap.getCharSet(key, def);
 252  
     }
 253  
 
 254  
     private String getRealPath(String path)
 255  
     {
 256  0
         String absolutePath = null;
 257  0
         if (applicationRoot == null)
 258  
         {
 259  0
             absolutePath = new File(path).getAbsolutePath();
 260  
         }
 261  
         else
 262  
         {
 263  0
             absolutePath = new File(applicationRoot, path).getAbsolutePath();
 264  
         }
 265  0
         return absolutePath;
 266  
     }
 267  
     // ---------------- Avalon Lifecycle Methods ---------------------
 268  
     /**
 269  
      * Avalon component lifecycle method
 270  
      */
 271  
     public void configure(Configuration conf)
 272  
     {
 273  9
         mimetypePath = conf.getAttribute(MIME_TYPES, null);
 274  9
         charsetPath = conf.getAttribute(CHARSETS, null);
 275  9
         if (mimetypePath != null)
 276  
         {
 277  0
             mimetypePath = getRealPath(mimetypePath);
 278  
         }
 279  9
         if (charsetPath != null)
 280  
         {
 281  0
             charsetPath = getRealPath(charsetPath);
 282  
         }
 283  9
     }
 284  
     /**
 285  
      * Avalon component lifecycle method
 286  
      */
 287  
     public void initialize() throws Exception
 288  
     {
 289  9
         if (mimetypePath != null)
 290  
         {
 291  
             try
 292  
             {
 293  0
                 mimeTypeMap = new MimeTypeMap(mimetypePath);
 294  
             }
 295  0
             catch (IOException x)
 296  
             {
 297  0
                 throw new Exception(mimetypePath, x);
 298  0
             }
 299  
         }
 300  
         else
 301  
         {
 302  9
             mimeTypeMap = new MimeTypeMap();
 303  
         }
 304  9
         if (charsetPath != null)
 305  
         {
 306  
             try
 307  
             {
 308  0
                 charSetMap = new CharSetMap(charsetPath);
 309  
             }
 310  0
             catch (IOException x)
 311  
             {
 312  0
                 throw new Exception(charsetPath, x);
 313  0
             }
 314  
         }
 315  
         else
 316  
         {
 317  9
             charSetMap = new CharSetMap();
 318  
         }
 319  9
     }
 320  
 
 321  
     /**
 322  
      * @see org.apache.avalon.framework.context.Contextualizable
 323  
      * @avalon.entry key="urn:avalon:home" type="java.io.File"
 324  
      */
 325  
     public void contextualize(Context context) throws ContextException
 326  
     {
 327  9
         this.applicationRoot = context.get( "urn:avalon:home" ).toString();
 328  9
     }
 329  
 
 330  
 }