themeinstaller/source/src/com/nokia/tools/themeinstaller/localisation/LocalisationStore.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:  Parses the localisation settings file and creates Localisation
       
    15  *                instances for each theme under install.
       
    16  *
       
    17 */
       
    18 
       
    19 
       
    20 package com.nokia.tools.themeinstaller.localisation;
       
    21 
       
    22 import java.io.File;
       
    23 import java.util.Enumeration;
       
    24 import java.util.Vector;
       
    25 
       
    26 import org.w3c.dom.Document;
       
    27 import org.w3c.dom.NamedNodeMap;
       
    28 import org.w3c.dom.Node;
       
    29 import org.w3c.dom.NodeList;
       
    30 
       
    31 import com.nokia.tools.themeinstaller.installationmanager.Lock;
       
    32 import com.nokia.tools.themeinstaller.odtconverter.IParseOperationListener;
       
    33 import com.nokia.tools.themeinstaller.xmlparser.XMLParser;
       
    34 
       
    35 /**
       
    36  * Parses the localisation settings file and creates Localisation
       
    37  * instances for each theme under install.
       
    38  */
       
    39 public class LocalisationStore
       
    40     {
       
    41 
       
    42     // CONSTANTS
       
    43     // Localisation settings element names
       
    44     private static final String THEME_ELEMENT = "theme";
       
    45 
       
    46     // Theme element attributes
       
    47     private static final String APP_UID_ATTR = "appuid";
       
    48     private static final String PROVIDER_UID_ATTR = "provideruid";
       
    49     private static final String THEME_UID_ATTR = "themeuid";
       
    50 
       
    51     // DTD elements
       
    52     private static final String MAIN_DTD_ELEMENT = "maindtd";
       
    53     private static final String SEARCH_TREE_ELEMENT = "searchtree";
       
    54     private static final String DIR_ELEMENT = "dir";
       
    55     private static final String DTD_INCLUDE_ELEMENT = "dtdinclude";
       
    56     private static final String ENTITY_ELEMENT = "entity";
       
    57 
       
    58     // DTD include element attributes
       
    59     private static final String FILE_ATTR = "file";
       
    60     private static final String ALL_ATTR = "all";
       
    61     private static final String TRUE = "true";
       
    62 
       
    63     // Radix for uid number conversion
       
    64     private static final int RADIX = 16;
       
    65 
       
    66     // Localisation store
       
    67     private Vector iLocalisations;
       
    68 
       
    69     // Lock for waiting the parse operation completion
       
    70     private Lock iLock;
       
    71 
       
    72     // Parsed localisation setting files
       
    73     private Vector iParsedFileNames;
       
    74 
       
    75     // Singleton instance
       
    76     private static LocalisationStore sInstance = null;
       
    77 
       
    78     /**
       
    79      * Constructor.
       
    80      * @param aFileName Localisation settings file
       
    81      */
       
    82     private LocalisationStore( File aFile )
       
    83         {
       
    84         iLock = new Lock();
       
    85         iLocalisations = new Vector();
       
    86         iParsedFileNames = new Vector();
       
    87         }
       
    88 
       
    89     /**
       
    90      * Get a LocalisationStore instance (singleton).
       
    91      * @param aFileName Localisation settings file name
       
    92      * @return LocalisationStore instance
       
    93      */
       
    94     public static LocalisationStore getInstance( File aFile )
       
    95         {
       
    96         if( sInstance == null )
       
    97             {
       
    98             sInstance = new LocalisationStore( aFile );
       
    99             }
       
   100         if( !sInstance.alreadyParsed( aFile ) )
       
   101             {
       
   102             Document d = sInstance.parseSettings( aFile );
       
   103             sInstance.createLocalisations( d );
       
   104             }
       
   105 
       
   106         return sInstance;
       
   107         }
       
   108 
       
   109     /**
       
   110      * Get Localisation instance for a theme. If the localisation
       
   111      * can not be found, null is returned.
       
   112      * @param aApplicationUid Theme application uid
       
   113      * @param aProviderUid Theme provider uid
       
   114      * @param aThemeUid Theme uid
       
   115      * @return Localisation instance containing the localisation information
       
   116      * for the theme
       
   117      * @throws IllegalArgumentException if the localisation can not be found
       
   118      */
       
   119     public Localisation getLocalisation( long aApplicationUid,
       
   120             long aProviderUid,
       
   121             long aThemeUid )
       
   122         {
       
   123         Localisation l = findLocalisation(
       
   124                 aApplicationUid, aProviderUid, aThemeUid );
       
   125         if( l == null )
       
   126             {
       
   127             throw new IllegalArgumentException(
       
   128                     "Can't find localisation information with Uid's : ApplicationUid: "
       
   129                             + aApplicationUid + ", ProviderUid: "
       
   130                             + aProviderUid + ", ThemeUid: " + aThemeUid );
       
   131             }
       
   132 
       
   133         return l;
       
   134         }
       
   135 
       
   136     /**
       
   137      * Find the Localisation instance from the internal list.
       
   138      * @param aApplicationUid Theme application uid
       
   139      * @param aProviderUid Theme provider uid
       
   140      * @param aThemeUid Theme uid
       
   141      * @return Localisation instance containing the localisation information
       
   142      * for the theme. If the localisation can not be found, null is returned
       
   143      */
       
   144     private Localisation findLocalisation( long aApplicationUid,
       
   145             long aProviderUid,
       
   146             long aThemeUid )
       
   147         {
       
   148         // Seek through all localisation instances to find the right one
       
   149         Enumeration e = iLocalisations.elements();
       
   150         Localisation l = null;
       
   151         while( e.hasMoreElements() )
       
   152             {
       
   153             l = ( Localisation )e.nextElement();
       
   154             Settings s = l.getSettings();
       
   155             if( aApplicationUid == s.getAppUid() &&
       
   156                 aProviderUid == s.getProviderUid() &&
       
   157                 aThemeUid == s.getThemeUid() )
       
   158                 {
       
   159                 return l;
       
   160                 }
       
   161             }
       
   162         return null;
       
   163         }
       
   164 
       
   165     /**
       
   166      * Check if the settings file has already been parsed.
       
   167      * @param aSettings Localisation settings
       
   168      * @return true if the localisation settings file has already been parsed
       
   169      */
       
   170     private boolean alreadyParsed( File aSettings )
       
   171         {
       
   172         Enumeration e = iParsedFileNames.elements();
       
   173         while( e.hasMoreElements() )
       
   174             {
       
   175             String s = ( String )e.nextElement();
       
   176             if( s.equals( aSettings.getPath() ) )
       
   177                 {
       
   178                 return true;
       
   179                 }
       
   180             }
       
   181 
       
   182         return false;
       
   183         }
       
   184 
       
   185     /**
       
   186      * Create localisation instances for the store.
       
   187      * @param aDocument DOM Document containing the settings
       
   188      */
       
   189     private void createLocalisations( Document aDocument )
       
   190         {
       
   191         // Set application uid
       
   192         NodeList nodes = aDocument.getElementsByTagName( THEME_ELEMENT );
       
   193         for( int i = 0; i < nodes.getLength(); i++ )
       
   194             {
       
   195             Settings settings = new Settings();
       
   196             Node theme = nodes.item( i );
       
   197             NamedNodeMap list = theme.getAttributes();
       
   198 
       
   199             // Read application, provider and theme uids from
       
   200             // the element attributes
       
   201             for ( int j = 0; j < list.getLength(); j++ )
       
   202                 {
       
   203                 Node attr = list.item( j );
       
   204 
       
   205                 if ( attr.getNodeType() == Node.ATTRIBUTE_NODE )
       
   206                     {
       
   207                     String name = attr.getNodeName();
       
   208                     String value = attr.getNodeValue();
       
   209 
       
   210                     if( APP_UID_ATTR.equals( name ) )
       
   211                         {
       
   212                         settings.setAppUid( Long.valueOf(
       
   213                                 value, RADIX ).longValue() );
       
   214                         }
       
   215                     else if( PROVIDER_UID_ATTR.equals( name ) )
       
   216                         {
       
   217                         settings.setProviderUid( Long.valueOf(
       
   218                                 value, RADIX ).longValue() );
       
   219                         }
       
   220                     else if( THEME_UID_ATTR.equals( name ) )
       
   221                         {
       
   222                         settings.setThemeUid( Long.valueOf(
       
   223                                 value, RADIX ).longValue() );
       
   224                         }
       
   225                     }
       
   226                 }
       
   227 
       
   228             // Process settings of a theme
       
   229             Node element = theme.getFirstChild();
       
   230             while( element != null )
       
   231                 {
       
   232                 String elementName = element.getNodeName();
       
   233 
       
   234                 // Process a main dtd element
       
   235                 if( MAIN_DTD_ELEMENT.equals( elementName ) )
       
   236                     {
       
   237                     // Search tree
       
   238                     Node maindtdChild = element.getFirstChild();
       
   239                     while( maindtdChild != null )
       
   240                         {
       
   241                         if( SEARCH_TREE_ELEMENT.equals(
       
   242                                 maindtdChild.getNodeName() ) )
       
   243                             {
       
   244                             settings.addSearchTree( parseSearchTree( maindtdChild ) );
       
   245                             }
       
   246                         maindtdChild = maindtdChild.getNextSibling();
       
   247                         }
       
   248                     }
       
   249                 // Process a dtd include element
       
   250                 else if( DTD_INCLUDE_ELEMENT.equals( elementName ) )
       
   251                     {
       
   252                     IncludeSetting incl = new IncludeSetting();
       
   253 
       
   254                     // Attributes: file, all
       
   255                     NamedNodeMap inclAttr = element.getAttributes();
       
   256                     for ( int j = 0; j < inclAttr.getLength(); j++ )
       
   257                         {
       
   258                         Node attr = inclAttr.item( j );
       
   259 
       
   260                         if ( attr.getNodeType() == Node.ATTRIBUTE_NODE )
       
   261                             {
       
   262                             String name = attr.getNodeName();
       
   263                             String value = attr.getNodeValue();
       
   264 
       
   265                             if( FILE_ATTR.equals( name ) )
       
   266                                 {
       
   267                                 // Set file attribute
       
   268                                 incl.setFile( value );
       
   269                                 }
       
   270                             else if( ALL_ATTR.equals( name ) )
       
   271                                 {
       
   272                                 // Set all attribute
       
   273                                 incl.setIncludeAll( TRUE.equals( value ) );
       
   274                                 }
       
   275                             }
       
   276                         }
       
   277 
       
   278                     Node inclNode = element.getFirstChild();
       
   279                     while( inclNode != null )
       
   280                         {
       
   281                         // Search tree
       
   282                         if( SEARCH_TREE_ELEMENT.equals( inclNode.getNodeName() ) )
       
   283                             {
       
   284                             // Add all directories to the search tree
       
   285                             incl.addSearchTree( parseSearchTree( inclNode ) );
       
   286                             }
       
   287                         // Entities
       
   288                         else if( ENTITY_ELEMENT.equals( inclNode.getNodeName() ) )
       
   289                             {
       
   290                             // Add all entity elements
       
   291                             incl.addEntity( inclNode.getTextContent() );
       
   292                             }
       
   293 
       
   294                         inclNode = inclNode.getNextSibling();
       
   295                         }
       
   296 
       
   297                     settings.addInclude( incl );
       
   298                     }
       
   299                 element = element.getNextSibling();
       
   300                 }
       
   301 
       
   302             // Check if localisation for the theme already exists
       
   303             Localisation l = findLocalisation( settings.getAppUid(),
       
   304                         settings.getProviderUid(), settings.getThemeUid() );
       
   305 
       
   306             if( l == null )
       
   307                 {
       
   308                 // Add a new localisation to the store
       
   309                 iLocalisations.add( new Localisation( settings ) );
       
   310                 }
       
   311             else
       
   312                 {
       
   313                 throw new IllegalArgumentException( "Localisation Store: " +
       
   314                         "Localisation settings already exists for theme: " +
       
   315                         "appuid: " + settings.getAppUid() +
       
   316                         ", provideruid: " + settings.getProviderUid() +
       
   317                         ", themeuid: " + settings.getThemeUid() );
       
   318                 }
       
   319             }
       
   320         }
       
   321 
       
   322     /**
       
   323      * Read localisation settings file to a DOM Document.
       
   324      * @param aSettings Localisation settings file
       
   325      * @return DOM Document containing the settings data
       
   326      */
       
   327     private Document parseSettings( File aSettings )
       
   328         {
       
   329         // Create a parse operation listener
       
   330         IParseOperationListener listener = new IParseOperationListener()
       
   331             {
       
   332             public void parseOperationCompleted( int aErr, String aReason )
       
   333                 {
       
   334                 iLock.unLock();
       
   335                 if ( aErr != 0 )
       
   336                     {
       
   337                     throw new IllegalArgumentException(
       
   338                             "Localisation settings parsing failed: "
       
   339                             + aErr + ", " + aReason );
       
   340                     }
       
   341                 }
       
   342             };
       
   343 
       
   344         // Parse the settings file
       
   345         XMLParser parser = new XMLParser( aSettings.getPath() );
       
   346         parser.addListener( listener );
       
   347 
       
   348         try
       
   349             {
       
   350             parser.parse();
       
   351             }
       
   352         catch ( Exception e )
       
   353             {
       
   354             throw new IllegalArgumentException(
       
   355                     "Localisation settings parsing failed: "
       
   356                     + e.getMessage() );
       
   357             }
       
   358 
       
   359         // Wait for the operation completion
       
   360         iLock.lock();
       
   361 
       
   362         iParsedFileNames.add( aSettings.getPath() );
       
   363 
       
   364         // Return the document that was formed
       
   365         return parser.getDOMDocument();
       
   366         }
       
   367 
       
   368     /**
       
   369      * Parse a search tree of child nodes.
       
   370      * @param aParent Search tree node of which child nodes define the
       
   371      * search directories
       
   372      * @return List of directory names as strings
       
   373      */
       
   374     private Vector parseSearchTree( Node aParent )
       
   375         {
       
   376         Vector result = new Vector();
       
   377 
       
   378         Node dir = aParent.getFirstChild();
       
   379         while( dir != null )
       
   380             {
       
   381             if( DIR_ELEMENT.equals( dir.getNodeName() ) )
       
   382                 {
       
   383                 result.add( dir.getTextContent() );
       
   384                 }
       
   385             dir = dir.getNextSibling();
       
   386             }
       
   387 
       
   388         return result;
       
   389         }
       
   390 
       
   391     }