javacommons/utils/javasrc/com/nokia/mj/impl/utils/Formatter.java
changeset 76 4ad59aaee882
parent 61 bf7ee68962da
child 79 2f468c1958d0
child 83 26b2b12093af
equal deleted inserted replaced
69:773449708c84 76:4ad59aaee882
    46  *   String message = formatter.arg(5).arg("photos").toString();
    46  *   String message = formatter.arg(5).arg("photos").toString();
    47  *   // Becomes :There are 5 files in folder "photos"
    47  *   // Becomes :There are 5 files in folder "photos"
    48  * </pre>
    48  * </pre>
    49  * <br>
    49  * <br>
    50  * Limitation: more than 10 positional arguments are not supported (only 0...9)
    50  * Limitation: more than 10 positional arguments are not supported (only 0...9)
    51  *
    51  * <br>
    52  * @author Nokia Corporation
    52  * Note that Formatter supports Avkon and Qt based localisation.
    53  * @version 1.0
    53  * Text parameter indices start from 0 when Avkon is used and from 1
       
    54  * when Qt is used.
    54  */
    55  */
    55 public class Formatter
    56 public class Formatter
    56 {
    57 {
    57     /** Original pattern string */
    58     /** Original pattern string */
    58     private String pattern;
    59     private String pattern;
    59 
    60 
    60     /** String with latest replacements */
    61     /** String with latest replacements */
    61     private String replaced;
    62     private String replaced;
    62 
    63 
       
    64     /**
       
    65      * Platform localisation type.
       
    66      * Either ResourceLoader.AVKON or ResourceLoader.QT. */
       
    67     private final int locType;
       
    68 
       
    69     /**
       
    70      * The first text parameter replacement index. For Avkon based
       
    71      * localisation this is 0, for Qt based localisation this is 1.
       
    72      */
       
    73     private final int startIndex;
       
    74 
    63     /** Next replacement index */
    75     /** Next replacement index */
    64     private int nextIndex = (ResourceLoader.getLocaleIdQt() == null? 0: 1);
    76     private int nextIndex;
    65 
    77 
    66     /*** ----------------------------- PUBLIC ------------------------------ */
    78     /*** ----------------------------- PUBLIC ------------------------------ */
    67 
    79 
    68     /**
    80     /**
    69      * Create a new formatter
    81      * Create a new formatter
    70      *
    82      *
    71      * @param pattern formatter pattern
    83      * @param pattern formatter pattern
    72      */
    84      */
    73     Formatter(String aPattern)
    85     Formatter(String aPattern)
       
    86     {
       
    87         this(aPattern, ResourceLoader.AVKON);
       
    88     }
       
    89 
       
    90     /**
       
    91      * Create a new formatter
       
    92      *
       
    93      * @param pattern formatter pattern
       
    94      * @param aLocType platform localisation type
       
    95      */
       
    96     Formatter(String aPattern, int aLocType)
    74     {
    97     {
    75         pattern = aPattern;
    98         pattern = aPattern;
    76         replaced = aPattern;
    99         replaced = aPattern;
       
   100         locType = aLocType;
       
   101         startIndex = (locType == ResourceLoader.QT? 1: 0);
       
   102         nextIndex = startIndex;
    77     }
   103     }
    78 
   104 
    79     /**
   105     /**
    80      * Replace the lowest numbered parameter in the string, which is not yet
   106      * Replace the lowest numbered parameter in the string, which is not yet
    81      * replaced.
   107      * replaced.
   213     {
   239     {
   214         String result = replaced;
   240         String result = replaced;
   215 
   241 
   216         // Reset for next usage
   242         // Reset for next usage
   217         replaced = pattern;
   243         replaced = pattern;
   218         nextIndex = (ResourceLoader.getLocaleIdQt() == null? 0: 1);
   244         nextIndex = startIndex;
   219 
   245 
   220         return result;
   246         return result;
   221     }
       
   222 
       
   223     /**
       
   224      * Gets a clone of this formatter. This can be used for caching preparsed
       
   225      * Formatters.
       
   226      *
       
   227      * @return clone of the formatter, as if new Formatter were created with
       
   228      * same pattern as current one.
       
   229      */
       
   230     public Formatter getClone()
       
   231     {
       
   232         return new Formatter(pattern);
       
   233     }
   247     }
   234 
   248 
   235     /**
   249     /**
   236      * Formats localised text with specified parameters from an array.
   250      * Formats localised text with specified parameters from an array.
   237      *
   251      *
   266             }
   280             }
   267         }
   281         }
   268         return toString();
   282         return toString();
   269     }
   283     }
   270     /**
   284     /**
   271      * Applies convertion from european digits into arabic-indic digits 
   285      * Applies convertion from european digits into arabic-indic digits
   272      * based on existing language settings
   286      * based on existing language settings
   273      *
   287      *
   274      * @param str String which might contain european digits
   288      * @param str String which might contain european digits
   275      * @return A string identical with the provided string but with the 
   289      * @return A string identical with the provided string but with the
   276      *         european digits (if any) converted to arabic-indic digits
   290      *         european digits (if any) converted to arabic-indic digits
   277      */
   291      */
   278     public static String formatDigits(String str)
   292     public static String formatDigits(String str)
   279     {
   293     {
   280         return _formatDigits(str);
   294         return _formatDigits(str);
   381     /**
   395     /**
   382      * Applies convertion from european digits into arabic-indic digits
   396      * Applies convertion from european digits into arabic-indic digits
   383      * based on existing language settings
   397      * based on existing language settings
   384      *
   398      *
   385      * @param str String which might contain european digits
   399      * @param str String which might contain european digits
   386      * @return A string identical with the provided string but with the 
   400      * @return A string identical with the provided string but with the
   387      *         european digits (if any) converted to arabic-indic digits
   401      *         european digits (if any) converted to arabic-indic digits
   388      */
   402      */
   389     private static native String _formatDigits(String str);
   403     private static native String _formatDigits(String str);
   390 }
   404 }