themeinstaller/source/src/com/nokia/tools/themeinstaller/localisation/Localisation.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:  This class is responsible for providing the enhanced
       
    15  *                localisation services.
       
    16  *
       
    17 */
       
    18 
       
    19 package com.nokia.tools.themeinstaller.localisation;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.IOException;
       
    23 import java.util.Enumeration;
       
    24 import java.util.Hashtable;
       
    25 import java.util.Observable;
       
    26 import java.util.Observer;
       
    27 import java.util.Vector;
       
    28 
       
    29 import com.nokia.tools.themeinstaller.defrep.DefinitionRepository;
       
    30 import com.nokia.tools.themeinstaller.defrep.operations.FileOperationEvent;
       
    31 import com.nokia.tools.themeinstaller.installationmanager.Lock;
       
    32 
       
    33 /**
       
    34  * This class is responsible for providing the enhanced localisation services.
       
    35  */
       
    36 public class Localisation implements ILocalisation
       
    37     {
       
    38 
       
    39     // The filename for temporary dtd file.
       
    40     private static String COMPOSED_DTD_FILE = "composed";
       
    41 
       
    42     // The extension for dtd file's filename.
       
    43     private static String DTD_FILE_EXTENSION = ".dtd";
       
    44 
       
    45     // Localisation settings of the theme
       
    46     private Settings iSettings;
       
    47 
       
    48     // File operation observer for copy operations
       
    49     private Observer iCopyObserver;
       
    50 
       
    51     // Lock for waiting the copy operation completion
       
    52     private Lock iCopyLock;
       
    53 
       
    54     // Stored file operation event
       
    55     private FileOperationEvent iCopyEvent;
       
    56 
       
    57     // Definition Repository
       
    58     private DefinitionRepository iDefRep;
       
    59 
       
    60     /**
       
    61      * Constructor.
       
    62      * @param aSettings Localisation settings of a theme
       
    63      */
       
    64     public Localisation( Settings aSettings )
       
    65         {
       
    66         iSettings = aSettings;
       
    67         iDefRep = DefinitionRepository.getInstance();
       
    68         iCopyLock = new Lock();
       
    69         iCopyObserver = new Observer()
       
    70             {
       
    71             public void update( Observable aFileOperation, Object aEvent )
       
    72                 {
       
    73                 // Store the event
       
    74                 iCopyEvent = ( FileOperationEvent ) aEvent;
       
    75                 // Open the lock
       
    76                 iCopyLock.unLock();
       
    77                 }
       
    78             };
       
    79         }
       
    80 
       
    81     /* (non-Javadoc)
       
    82      * @see com.nokia.tools.themeinstaller.localisation.ILocalisation#getSettings()
       
    83      */
       
    84     public Settings getSettings()
       
    85         {
       
    86         return iSettings;
       
    87         }
       
    88 
       
    89     /* (non-Javadoc)
       
    90      * @see com.nokia.tools.themeinstaller.localisation.ILocalisation#findDTD()
       
    91      */
       
    92     public File composeDTD( String aFileName, int aLanguage ) throws IOException
       
    93         {
       
    94         // Create a temp dtd file
       
    95         File composed = File.createTempFile(
       
    96                 COMPOSED_DTD_FILE, DTD_FILE_EXTENSION );
       
    97         composed.deleteOnExit();
       
    98 
       
    99         // Copy the main dtd contents to the temp dtd
       
   100         copyDTDContents( findDTD( aFileName ), composed, false );
       
   101 
       
   102         // Loc settings: include settings
       
   103         Enumeration includes = iSettings.getIncludes().elements();
       
   104         IncludeSetting incl = null;
       
   105         Enumeration inclEntities = null;
       
   106         File inclFile = null;
       
   107 
       
   108         // Variables for localisation reading/writing
       
   109         String entity = null;
       
   110         String locString = null;
       
   111         String filename = null;
       
   112         Hashtable entities = new Hashtable();
       
   113         Integer[] formatArgs = { new Integer( aLanguage ) };
       
   114 
       
   115         // Process all include element
       
   116         while( includes.hasMoreElements() )
       
   117             {
       
   118             incl = ( IncludeSetting )includes.nextElement();
       
   119 
       
   120             filename = String.format( incl.getFile(), formatArgs );
       
   121 
       
   122             // Find the DTD to include from
       
   123             inclFile = FileSearch.findFile( incl.getSearchTree(), filename, true );
       
   124 
       
   125             // Include entities to the composed dtd file
       
   126             if( incl.isIncludeAll() )
       
   127                 {
       
   128                 // Append the whole dtd to include to the composed one
       
   129                 copyDTDContents( inclFile, composed, true );
       
   130                 }
       
   131             else
       
   132                 {
       
   133                 // Read the entities to include
       
   134                 inclEntities = incl.getEntities().elements();
       
   135                 while( inclEntities.hasMoreElements() )
       
   136                     {
       
   137                     entity = ( String )inclEntities.nextElement();
       
   138                     locString = DTDReader.readEntity( inclFile, entity );
       
   139                     entities.put( entity, locString );
       
   140                     }
       
   141                 }
       
   142             }
       
   143 
       
   144         // Create and return a temporary DTD file being composed
       
   145         return DTDComposer.writeEntitiesToDtd( composed, entities, true );
       
   146         }
       
   147 
       
   148     /* (non-Javadoc)
       
   149      * @see com.nokia.tools.themeinstaller.localisation.ILocalisation#findDTD(java.lang.String)
       
   150      */
       
   151     public File findDTD( String aFileName ) throws IOException
       
   152         {
       
   153         // Use the file search to find the dtd file
       
   154         Vector searchTree = iSettings.getSearchTree();
       
   155         return FileSearch.findFile( searchTree, aFileName, true );
       
   156         }
       
   157 
       
   158     /**
       
   159      * Uses Definition Repository to copy DTD file contents to another DTD file.
       
   160      * The method is synchronous and waits for the operation completion.
       
   161      * @param aSource Source file
       
   162      * @param aDestination Destination file
       
   163      * @param aAppend Append to the destination file if it already exists
       
   164      * @throws IOException if reading/writing from/to a DTD file fails
       
   165      */
       
   166     private void copyDTDContents( File aSource, File aDestination, boolean aAppend )
       
   167         throws IOException
       
   168         {
       
   169         iDefRep.copy( aSource, aDestination, aAppend, iCopyObserver );
       
   170         iCopyLock.lock();
       
   171 
       
   172         if( iCopyEvent.getErrorCode() != FileOperationEvent.OPERATION_SUCCESSFUL )
       
   173             {
       
   174             throw new IOException( "Localisation: DTD contents copy failed: " +
       
   175                     iCopyEvent.getErrorCode() +  ", File: " +
       
   176                     iCopyEvent.getFile().getPath() );
       
   177             }
       
   178         }
       
   179 
       
   180     }