themeinstaller/source/src/com/nokia/tools/themeinstaller/localisation/DTDComposer.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:  Composes one dtd file from dtd entities
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.localisation;
       
    20 
       
    21 import java.io.BufferedWriter;
       
    22 import java.io.File;
       
    23 import java.io.FileOutputStream;
       
    24 import java.io.IOException;
       
    25 import java.io.OutputStreamWriter;
       
    26 import java.util.Hashtable;
       
    27 import java.util.Iterator;
       
    28 import java.util.Set;
       
    29 
       
    30 import com.nokia.tools.themeinstaller.odtconverter.ConverterProperties;
       
    31 
       
    32 /**
       
    33  * Composes one dtd file from multiple entities.
       
    34  */
       
    35 public class DTDComposer
       
    36     {
       
    37 
       
    38     /** The Regular expression for entity. */
       
    39     private static final String FORMAT = "<!ENTITY %s \"%s\">";
       
    40 
       
    41     /** Line separator */
       
    42     private static final String CARRIAGE_RETURN =
       
    43             System.getProperty( "line.separator" );
       
    44 
       
    45     /** Property key for character encoding of DTD files */
       
    46     private static final String ENCODING_KEY = "dtd_encoding";
       
    47 
       
    48 
       
    49     /**
       
    50      * Returns dtd entity in right format for file writing.
       
    51      *
       
    52      * @param aEntity the entity
       
    53      * @param aLocString the localisable entity value
       
    54      *
       
    55      * @return Formatted entity
       
    56      */
       
    57     public static String formatEntity( String aEntity, String aLocString )
       
    58         {
       
    59         String[] formatArguments = { aEntity, aLocString };
       
    60         return String.format( FORMAT, formatArguments );
       
    61         }
       
    62 
       
    63     /**
       
    64      * Write entities to dtd file.
       
    65      *
       
    66      * @param aDestination Destination file
       
    67      * @param aEntities Entity - value pairs in hashtable
       
    68      * @param aAppend Append to the original file if it exists
       
    69      *
       
    70      * @return The dtd file
       
    71      *
       
    72      * @throws IOException Signals that an I/O exception has occurred.
       
    73      */
       
    74     public static File writeEntitiesToDtd( File aDestination,
       
    75                                            Hashtable aEntities,
       
    76                                            boolean aAppend )
       
    77         throws IOException
       
    78         {
       
    79 
       
    80         // Read the assumed character encoding of DTD files
       
    81         String encoding =
       
    82                 ConverterProperties.getInstance().getProperty( ENCODING_KEY );
       
    83 
       
    84         BufferedWriter out = null;
       
    85         if( encoding != null )
       
    86             {
       
    87             // Use specified encoding
       
    88             out = new BufferedWriter( new OutputStreamWriter(
       
    89                     new FileOutputStream( aDestination, aAppend ), encoding ) );
       
    90             }
       
    91         else
       
    92             {
       
    93             // Use default encoding
       
    94             out = new BufferedWriter( new OutputStreamWriter(
       
    95                     new FileOutputStream( aDestination, aAppend ) ) );
       
    96             }
       
    97 
       
    98         try
       
    99             {
       
   100             Set entries = aEntities.keySet();
       
   101             Iterator it = entries.iterator();
       
   102 
       
   103             while ( it.hasNext() )
       
   104                 {
       
   105                 String entity = ( String ) it.next();
       
   106                 String locString = ( String ) aEntities.get( entity );
       
   107 
       
   108                 out.write( formatEntity( entity, locString ) + CARRIAGE_RETURN );
       
   109                 }
       
   110             }
       
   111         finally
       
   112             {
       
   113             if ( out != null )
       
   114                 {
       
   115                 out.close();
       
   116                 }
       
   117             }
       
   118 
       
   119         return aDestination;
       
   120         }
       
   121 
       
   122     }