themeinstaller/source/src/com/nokia/tools/themeinstaller/odtconverter/MimeTypeResolver.java
branchRCL_3
changeset 32 fe49e33862e2
parent 31 b685c59de105
child 33 04b7640f6fb5
equal deleted inserted replaced
31:b685c59de105 32:fe49e33862e2
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Gets the mime- and resource type using file's name extension
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.odtconverter;
       
    20 
       
    21 import java.util.Hashtable;
       
    22 
       
    23 /**
       
    24  * Resolves the MimeType and Resource type of the resource to match the file
       
    25  * name used in the Symbian side. This class is following the behavior of the
       
    26  * Symbian class XnInstallationHandler.cpp in ...Xuikon\UIManager\src\
       
    27  *
       
    28  * In Symbian, MimeType is TDataType that has TBuf8 and TUid. The constructor
       
    29  * taking a descriptor of TDataType is as follows:
       
    30  *
       
    31  * If descriptor begins with "x-epoc/x-app", identifying data as a native
       
    32  * Symbian type, then the associated UID is set to the UID value which is
       
    33  * expected to follow the characters "x-epoc/x-app". If no sensible UID can be
       
    34  * extracted from the characters following: "x-epoc/x-app", then the associated
       
    35  * UID is set to KNullUid.
       
    36  *
       
    37  * Else Uid is set to KNullDesc and the descriptor is the descriptor given in
       
    38  * parameter.
       
    39  *
       
    40  * In ...Xuikon\UIManager\src\XnInstallationHandler.cpp
       
    41  * (ImportCompatibleResourcesL) MimeType is constructed using the Descriptor
       
    42  * that is parsed using file's name extension. Descriptors beginning with
       
    43  * "x-epoc/x-app" aren't used so TUid part is not implemented. Some of the types
       
    44  * are left empty in XnInstallationHandler.cpp. This represented as
       
    45  * EMPTY_MIME_TYPE
       
    46  *
       
    47  * Resource types are defined in Symbian side class XnResource.h
       
    48  *
       
    49  */
       
    50 public class MimeTypeResolver
       
    51     {
       
    52     private static Hashtable iResourceTypeAndMimeType;
       
    53 
       
    54     public static final int E_RESOURCE_NONE = 0;
       
    55     public static final int E_RESOURCERLB = 1;
       
    56     public static final int E_RESOURCEODT = 2;
       
    57     public static final int E_RESOURCEDTD = 3;
       
    58     public static final int E_RESOURCEXML = 4;
       
    59     public static final int E_RESOURCECSS = 5;
       
    60     public static final int E_RESOURCEDAT = 6;
       
    61     public static final int E_RESOURCERES = 7;
       
    62     public static final int E_RESOURCEFBS = 8;
       
    63     public static final int E_RESOURCEMBM = 9;
       
    64     public static final int E_RESOURCEMIF = 10;
       
    65     public static final int E_RESOURCEBMP = 11;
       
    66     public static final int E_RESOURCEJPG = 12;
       
    67     public static final int E_RESOURCEJPEG = 13;
       
    68     public static final int E_RESOURCEPNG = 14;
       
    69     public static final int E_RESOURCEGIF = 15;
       
    70     public static final int E_RESOURCEAAC = 16;
       
    71     public static final int E_RESOURCEWAV = 17;
       
    72     public static final int E_RESOURCEMID = 18;
       
    73     public static final int E_RESOURCEMP3 = 19;
       
    74     public static final int E_RESOURCEM3G = 20;
       
    75     public static final int E_RESOURCESVG = 21;
       
    76     public static final int E_RESOURCESWF = 22;
       
    77     public static final int E_RESOURCEMIME = 23;
       
    78     public static final int E_RESOURCETXT = 24;
       
    79 
       
    80     public static final String KRLB_FILE_EXTENSION =  ".r";
       
    81     public static final String KODT_FILE_EXTENSION =  ".o";
       
    82     public static final String KDTD_FILE_EXTENSION =  ".dtd";
       
    83     public static final String KXML_FILE_EXTENSION =  ".xml";
       
    84     public static final String KCSS_FILE_EXTENSION =  ".css";
       
    85     public static final String KDAT_FILE_EXTENSION =  ".dat";
       
    86     public static final String KFBS_FILE_EXTENSION =  ".fbs";
       
    87     public static final String KMBM_FILE_EXTENSION =  ".mbm";
       
    88     public static final String KMIF_FILE_EXTENSION =  ".mif";
       
    89     public static final String KBMP_FILE_EXTENSION =  ".bmp";
       
    90     public static final String KJPG_FILE_EXTENSION =  ".jpg";
       
    91     public static final String KJPEG_FILE_EXTENSION =  ".jpeg";
       
    92     public static final String KPNG_FILE_EXTENSION =  ".png";
       
    93     public static final String KGIF_FILE_EXTENSION =  ".gif";
       
    94     public static final String KAAC_FILE_EXTENSION =  ".aac";
       
    95     public static final String KWAV_FILE_EXTENSION =  ".wav";
       
    96     public static final String KMID_FILE_EXTENSION =  ".mid";
       
    97     public static final String KMP3_FILE_EXTENSION =  ".mp3";
       
    98     public static final String KM3G_FILE_EXTENSION =  ".m3g";
       
    99     public static final String KSVG_FILE_EXTENSION =  ".svg";
       
   100     public static final String KSWF_FILE_EXTENSION =  ".swf";
       
   101     public static final String KTXT_FILE_EXTENSION =  ".txt";
       
   102 
       
   103     public static final String KAPAAPPTYPEDES = "x-epoc/x-app";
       
   104 
       
   105     public static final String TXT_MIME_TYPE = "text/xml";
       
   106     public static final String MBM_MIME_TYPE = "image/x-epoc-mbm";
       
   107     public static final String EMPTY_MIME_TYPE = "";
       
   108     public static final String EMPTY_FILE_EXTENSION = "";
       
   109     public static final String WAV_MIME_TYPE = "audio/x-wav";
       
   110     public static final String MIDI_MIME_TYPE = "audio/midi";
       
   111     public static final String UNKNOWN_MIME_TYPE = "unknown";
       
   112 
       
   113     public static final String NAME_EXTENSION_SEPARATOR = ".";
       
   114 
       
   115     /**
       
   116      * Instantiates a new mimetype resolver.
       
   117      */
       
   118     public MimeTypeResolver()
       
   119         {
       
   120         iResourceTypeAndMimeType = new Hashtable();
       
   121         iResourceTypeAndMimeType.put( KRLB_FILE_EXTENSION, new ResourceTypeAndMimeType( UNKNOWN_MIME_TYPE, E_RESOURCERLB ) );
       
   122         iResourceTypeAndMimeType.put( KODT_FILE_EXTENSION, new ResourceTypeAndMimeType( UNKNOWN_MIME_TYPE, E_RESOURCEODT ) );
       
   123         iResourceTypeAndMimeType.put( KDTD_FILE_EXTENSION, new ResourceTypeAndMimeType( UNKNOWN_MIME_TYPE, E_RESOURCEDTD ) );
       
   124         iResourceTypeAndMimeType.put( KXML_FILE_EXTENSION, new ResourceTypeAndMimeType( UNKNOWN_MIME_TYPE, E_RESOURCEXML ) );
       
   125         iResourceTypeAndMimeType.put( KCSS_FILE_EXTENSION, new ResourceTypeAndMimeType( UNKNOWN_MIME_TYPE, E_RESOURCECSS ) );
       
   126         iResourceTypeAndMimeType.put( KDAT_FILE_EXTENSION, new ResourceTypeAndMimeType( UNKNOWN_MIME_TYPE, E_RESOURCEDAT ) );
       
   127         iResourceTypeAndMimeType.put( KFBS_FILE_EXTENSION, new ResourceTypeAndMimeType( UNKNOWN_MIME_TYPE, E_RESOURCEFBS ) );
       
   128         iResourceTypeAndMimeType.put( KMBM_FILE_EXTENSION, new ResourceTypeAndMimeType( MBM_MIME_TYPE, E_RESOURCEMBM ) );
       
   129         iResourceTypeAndMimeType.put( KMIF_FILE_EXTENSION, new ResourceTypeAndMimeType( EMPTY_MIME_TYPE, E_RESOURCEMIF ) );
       
   130         iResourceTypeAndMimeType.put( KBMP_FILE_EXTENSION, new ResourceTypeAndMimeType( EMPTY_MIME_TYPE, E_RESOURCEBMP ) );
       
   131         iResourceTypeAndMimeType.put( KJPG_FILE_EXTENSION, new ResourceTypeAndMimeType( EMPTY_MIME_TYPE, E_RESOURCEJPG ) );
       
   132         iResourceTypeAndMimeType.put( KJPEG_FILE_EXTENSION, new ResourceTypeAndMimeType( EMPTY_MIME_TYPE, E_RESOURCEJPEG ) );
       
   133         iResourceTypeAndMimeType.put( KPNG_FILE_EXTENSION, new ResourceTypeAndMimeType( EMPTY_MIME_TYPE, E_RESOURCEPNG ) );
       
   134         iResourceTypeAndMimeType.put( KGIF_FILE_EXTENSION, new ResourceTypeAndMimeType( EMPTY_MIME_TYPE, E_RESOURCEGIF ) );
       
   135         iResourceTypeAndMimeType.put( KAAC_FILE_EXTENSION, new ResourceTypeAndMimeType( EMPTY_MIME_TYPE, E_RESOURCEAAC ) );
       
   136         iResourceTypeAndMimeType.put( KWAV_FILE_EXTENSION, new ResourceTypeAndMimeType( WAV_MIME_TYPE, E_RESOURCEWAV ) );
       
   137         iResourceTypeAndMimeType.put( KMID_FILE_EXTENSION, new ResourceTypeAndMimeType( MIDI_MIME_TYPE, E_RESOURCEMID ) );
       
   138         iResourceTypeAndMimeType.put( KMP3_FILE_EXTENSION, new ResourceTypeAndMimeType( UNKNOWN_MIME_TYPE, E_RESOURCEMP3 ) );
       
   139         iResourceTypeAndMimeType.put( KM3G_FILE_EXTENSION, new ResourceTypeAndMimeType( UNKNOWN_MIME_TYPE, E_RESOURCEM3G ) );
       
   140         iResourceTypeAndMimeType.put( KSVG_FILE_EXTENSION, new ResourceTypeAndMimeType( UNKNOWN_MIME_TYPE, E_RESOURCESVG ) );
       
   141         iResourceTypeAndMimeType.put( KSWF_FILE_EXTENSION, new ResourceTypeAndMimeType( EMPTY_MIME_TYPE, E_RESOURCESWF ) );
       
   142         iResourceTypeAndMimeType.put( KTXT_FILE_EXTENSION, new ResourceTypeAndMimeType( EMPTY_MIME_TYPE, E_RESOURCETXT ) );
       
   143         }
       
   144 
       
   145     /**
       
   146      * Gets the MimeType.
       
   147      *
       
   148      * @param aFileName The file name
       
   149      *
       
   150      * @return The MimeType
       
   151      */
       
   152     public String getMimeType( String aFileName )
       
   153         {
       
   154         return getMimeTypeValue( getFileExtension( aFileName ) );
       
   155         }
       
   156 
       
   157     /**
       
   158      * Gets the resource type.
       
   159      *
       
   160      * @param aFileName The file name
       
   161      *
       
   162      * @return The Resource type
       
   163      */
       
   164     public int getResourceType( String aFileName )
       
   165         {
       
   166         return getResourceTypeValue( getFileExtension( aFileName ) );
       
   167         }
       
   168 
       
   169     /**
       
   170      * Gets the mimetype value.
       
   171      *
       
   172      * @param aKey The hashtable key is filen name extension
       
   173      *
       
   174      * @return The mimetype value
       
   175      */
       
   176     private String getMimeTypeValue( String aKey )
       
   177         {
       
   178         if ( iResourceTypeAndMimeType.containsKey( aKey ) )
       
   179             {
       
   180             ResourceTypeAndMimeType rtamt = ( ResourceTypeAndMimeType ) iResourceTypeAndMimeType.get( aKey );
       
   181             return rtamt.getMimeType();
       
   182             }
       
   183         return EMPTY_MIME_TYPE;
       
   184         }
       
   185 
       
   186     /**
       
   187      * Gets the resource type value.
       
   188      *
       
   189      * @param aKey The hashtable key is file name extension
       
   190      *
       
   191      * @return The resource type value
       
   192      */
       
   193     private int getResourceTypeValue( String aKey )
       
   194         {
       
   195         if ( iResourceTypeAndMimeType.containsKey( aKey ) )
       
   196             {
       
   197             ResourceTypeAndMimeType rtamt = ( ResourceTypeAndMimeType ) iResourceTypeAndMimeType.get( aKey );
       
   198             return rtamt.getResourceType();
       
   199             }
       
   200         return E_RESOURCE_NONE;
       
   201         }
       
   202 
       
   203     /**
       
   204      * Gets the file extension.
       
   205      *
       
   206      * @param aFileName The file name
       
   207      *
       
   208      * @return The file extension
       
   209      */
       
   210     private String getFileExtension( String aFileName )
       
   211         {
       
   212         String fileExtension = "";
       
   213         if ( aFileName.contains( NAME_EXTENSION_SEPARATOR ) )
       
   214             {
       
   215             fileExtension = aFileName.substring(
       
   216                     aFileName.lastIndexOf( NAME_EXTENSION_SEPARATOR ),
       
   217                     aFileName.length() ).toLowerCase();
       
   218             return fileExtension;
       
   219             }
       
   220         return EMPTY_FILE_EXTENSION;
       
   221         }
       
   222 
       
   223     /**
       
   224      * Inner class for containing mimetype - resource type pairs.
       
   225      */
       
   226     private static class ResourceTypeAndMimeType
       
   227         {
       
   228         private String iMimeType;
       
   229         private int iResourceType;
       
   230 
       
   231         /**
       
   232          * Instantiates a new resource type and mime type pair.
       
   233          *
       
   234          * @param aMimeType The mime type
       
   235          * @param aResourceType The resource type
       
   236          */
       
   237         public ResourceTypeAndMimeType( String aMimeType, int aResourceType )
       
   238             {
       
   239             iMimeType = aMimeType;
       
   240             iResourceType = aResourceType;
       
   241             }
       
   242 
       
   243         /**
       
   244          * Gets the mimetype.
       
   245          *
       
   246          * @return The mimetype
       
   247          */
       
   248         public String getMimeType()
       
   249             {
       
   250             return iMimeType;
       
   251             }
       
   252 
       
   253         /**
       
   254          * Gets the resource type.
       
   255          *
       
   256          * @return The resource type
       
   257          */
       
   258         public int getResourceType()
       
   259             {
       
   260             return iResourceType;
       
   261             }
       
   262 
       
   263         }
       
   264 
       
   265     }