themeinstaller/source/src/com/nokia/tools/themeinstaller/installationmanager/LanguageInstaller.java
branchRCL_3
changeset 17 fe49e33862e2
parent 16 b685c59de105
child 18 04b7640f6fb5
equal deleted inserted replaced
16:b685c59de105 17: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:  Uses ODTConverter for parsing the source files.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.installationmanager;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.IOException;
       
    23 import java.util.Observable;
       
    24 import java.util.Observer;
       
    25 import java.util.Vector;
       
    26 
       
    27 import com.nokia.tools.themeinstaller.localisation.Localisation;
       
    28 import com.nokia.tools.themeinstaller.localisation.LocalisationStore;
       
    29 import com.nokia.tools.themeinstaller.odtconverter.IParseOperationListener;
       
    30 import com.nokia.tools.themeinstaller.odtconverter.ODTConverter;
       
    31 import com.nokia.tools.themeinstaller.odtconverter.ODTDocument;
       
    32 import com.nokia.tools.themeinstaller.odtconverter.ODTHeader;
       
    33 import com.nokia.tools.themeinstaller.odtconverter.ODTResource;
       
    34 
       
    35 /**
       
    36  * Language Installer uses the ODTConverter for parsing the source files of
       
    37  * a language variant.
       
    38  */
       
    39 public class LanguageInstaller
       
    40     extends Observable implements IParseOperationListener
       
    41     {
       
    42 
       
    43     // ODT Document
       
    44     private ODTDocument iODTDocument;
       
    45 
       
    46     // ODT Converter
       
    47     private ODTConverter iConverter;
       
    48 
       
    49     // Theme manifest
       
    50     private IThemeManifest iManifest;
       
    51 
       
    52     // Language variant
       
    53     private LanguageSpecificData iLanguage;
       
    54 
       
    55     // Resource files
       
    56     private Vector iResources;
       
    57 
       
    58     // Resource installer
       
    59     private IResourceInstaller iResourceInstaller;
       
    60 
       
    61     // Localisation settings file
       
    62     private File iLocSettings;
       
    63 
       
    64     /**
       
    65      * Constructor.
       
    66      * @param aListener Listener for the parsing operation conversion
       
    67      * @param aManifest Theme manifest
       
    68      * @param aLanguage Language variant to to install
       
    69      * @param
       
    70      */
       
    71     public LanguageInstaller( Observer aListener,
       
    72                               IThemeManifest aManifest,
       
    73                               LanguageSpecificData aLanguage,
       
    74                               File aLocSettings,
       
    75                               IResourceInstaller aResourceInstaller )
       
    76         {
       
    77         iODTDocument = new ODTDocument();
       
    78         iConverter = new ODTConverter();
       
    79         iConverter.addListener( this );
       
    80         iManifest = aManifest;
       
    81         iLanguage = aLanguage;
       
    82         iLocSettings = aLocSettings;
       
    83         iResources = new Vector();
       
    84         iResourceInstaller = aResourceInstaller;
       
    85         addObserver( aListener );
       
    86         }
       
    87 
       
    88     /**
       
    89      * Add ODT Resources. This method can be used i.e. for adding language
       
    90      * independent ODT Resources.
       
    91      * @param aODTResources a list of resources to add
       
    92      */
       
    93     public void addResources( Vector aResources )
       
    94         {
       
    95         iResources.addAll( aResources );
       
    96         }
       
    97 
       
    98     /**
       
    99      * Start the installation process of the language.
       
   100      * @throws IOException if parse operation fails
       
   101      */
       
   102     public void install() throws IOException
       
   103         {
       
   104         ODTHeader header = createHeader( iManifest, iLanguage );
       
   105         iODTDocument.setODTHeader( header );
       
   106 
       
   107         // Install and add language specific resources
       
   108         Vector langResources = iLanguage.getResources();
       
   109         Vector odtLangResources = iResourceInstaller
       
   110                 .installResources( langResources, header );
       
   111         odtLangResources.addAll( iResources );
       
   112 
       
   113         // Add ODT file as resource
       
   114         ODTResource langODTResource = iResourceInstaller
       
   115                 .createODTResource( header, iManifest.getNameSpace() );
       
   116         odtLangResources.add( langODTResource );
       
   117         iODTDocument.setODTResources( odtLangResources );
       
   118 
       
   119         String dataDir = iManifest.getDataDir();
       
   120         String dtdName = null;
       
   121         File dtdFile = null;
       
   122 
       
   123         // Determine the DTD file to use
       
   124         if( iLanguage.getDTDFile() != null )
       
   125             {
       
   126             // Language specific DTD
       
   127             dtdName = iLanguage.getDTDFile();
       
   128             }
       
   129         else if( iManifest.getDTDFile() != null )
       
   130             {
       
   131             // Language independent DTD
       
   132             dtdName = iManifest.getDTDFile();
       
   133             }
       
   134 
       
   135         // Check if enhanced localisation support is enabled
       
   136         if( iLocSettings != null && dtdName != null )
       
   137             {
       
   138             // Use enhanced localisation: Find and compose the DTD file
       
   139             LocalisationStore ls = LocalisationStore.getInstance( iLocSettings );
       
   140             Localisation l = ls.getLocalisation(
       
   141                     iManifest.getApplicationUid().longValue(),
       
   142                     iManifest.getProviderUid().longValue(),
       
   143                     iManifest.getThemeUid().longValue() );
       
   144             dtdFile = l.composeDTD( dtdName, iLanguage.getLanguageId().intValue() );
       
   145             }
       
   146         else if( dtdName != null )
       
   147             {
       
   148             // Use the standard localisation
       
   149             dtdFile = new File( dataDir + dtdName );
       
   150             }
       
   151 
       
   152         // Add XML to the converter
       
   153         if( dtdFile != null )
       
   154             {
       
   155             // External DTD file found
       
   156             iConverter.addXML( dataDir + iManifest.getXMLFile(),
       
   157                                dtdFile.getPath() );
       
   158             }
       
   159         else
       
   160             {
       
   161             // Do not use external DTD
       
   162             iConverter.addXML( dataDir + iManifest.getXMLFile() );
       
   163             }
       
   164 
       
   165         // Add CSS to the converter, if available
       
   166         if( iLanguage.getCSSFile() != null )
       
   167             {
       
   168             // Use language specific CSS
       
   169             iConverter.addCSS( dataDir + iLanguage.getCSSFile() );
       
   170             }
       
   171         else if( iManifest.getCSSFile() != null )
       
   172             {
       
   173             // Use theme specific CSS
       
   174             iConverter.addCSS( dataDir + iManifest.getCSSFile() );
       
   175             }
       
   176 
       
   177         // Start the installation process
       
   178         try
       
   179             {
       
   180             iConverter.parse();
       
   181             }
       
   182         catch ( Exception e )
       
   183             {
       
   184             throw new IllegalArgumentException( "Failed to read resource files" );
       
   185             }
       
   186         }
       
   187 
       
   188     /**
       
   189      * Creates ODTHeader from manifest data
       
   190      * @param aManifest Theme manifest
       
   191      * @param aLanguage Language. If language is null, no language
       
   192      * specific data is processed.
       
   193      * @return new ODT Header
       
   194      */
       
   195     public static ODTHeader createHeader( IThemeManifest aManifest,
       
   196                                     LanguageSpecificData aLanguage )
       
   197         {
       
   198         ODTHeader header = new ODTHeader();
       
   199         if(null!=aManifest.getApplicationUid())
       
   200         header.put( ODTHeader.ApplicationUID, aManifest.getApplicationUid() );
       
   201         
       
   202         if(null!=aManifest.getProviderUid())
       
   203         header.put( ODTHeader.ProviderUID, aManifest.getProviderUid() );
       
   204         
       
   205         if(null!=aManifest.getThemeUid())
       
   206         header.put( ODTHeader.ThemeUID, aManifest.getThemeUid() );
       
   207         
       
   208         if(null!=aManifest.getProviderName())
       
   209         header.put( ODTHeader.ProviderName, aManifest.getProviderName() );
       
   210         
       
   211         if(null!=aManifest.getThemeFullName())
       
   212         header.put( ODTHeader.ThemeFullName, aManifest.getThemeFullName() );
       
   213         
       
   214         if(null!=aManifest.getThemeShortName())
       
   215         header.put( ODTHeader.ThemeShortName, aManifest.getThemeShortName() );
       
   216         
       
   217         if(null!=aManifest.getThemeVersion())
       
   218         header.put( ODTHeader.ThemeVersion, aManifest.getThemeVersion() );
       
   219         
       
   220         if(null!=aManifest.getScreenSizeX())
       
   221         header.put( ODTHeader.ScreenSizeX, aManifest.getScreenSizeX() );
       
   222         
       
   223         if(null!=aManifest.getScreenSizeY())
       
   224         header.put( ODTHeader.ScreenSizeY, aManifest.getScreenSizeY() );
       
   225         
       
   226         if(null!=aManifest.getThemeStatus())
       
   227         header.put( ODTHeader.Flags, aManifest.getThemeStatus() );
       
   228 
       
   229         // Set language specific data to the header
       
   230         if( aLanguage != null )
       
   231             {
       
   232             // Mandatory fields
       
   233             header.put( ODTHeader.Language, aLanguage.getLanguageId() );
       
   234 
       
   235             // Optional fields
       
   236             String locFullName = aLanguage.getThemeFullName();
       
   237             if( locFullName != null )
       
   238                 {
       
   239                 header.put( ODTHeader.ThemeFullName, locFullName );
       
   240                 }
       
   241             }
       
   242 
       
   243         return header;
       
   244         }
       
   245 
       
   246     /* (non-Javadoc)
       
   247      * @see com.nokia.tools.odtconverter.IParseOperationListener#OperationCompleted(int, java.lang.String)
       
   248      */
       
   249     public void parseOperationCompleted( int aErr, String aReason )
       
   250         {
       
   251         iODTDocument.setDOMDocument( iConverter.getDOMDocument() );
       
   252         super.setChanged();
       
   253         super.notifyObservers(
       
   254                 new LanguageInstallEvent( iODTDocument, aErr, aReason ) );
       
   255         }
       
   256 
       
   257     }