javacommons/utils/javasrc/com/nokia/mj/impl/utils/Formatter.java
branchRCL_3
changeset 60 6c158198356e
parent 46 4376525cdefb
child 76 4ad59aaee882
equal deleted inserted replaced
59:e5618cc85d74 60:6c158198356e
     1 /*
     1 /*
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    26  * <br>
    26  * <br>
    27  * Supported parameters are:
    27  * Supported parameters are:
    28  * <UL>
    28  * <UL>
    29  * <LI>%nU - String in position n
    29  * <LI>%nU - String in position n
    30  * <LI>%U - Next string
    30  * <LI>%U - Next string
       
    31  * <LI>%Ln - Integer in position n
    31  * <LI>%nN - Integer in position n
    32  * <LI>%nN - Integer in position n
    32  * <LI>%N - Next integer
    33  * <LI>%N - Next integer
    33  * <LI>%nC - Character in position n
    34  * <LI>%nC - Character in position n
    34  * <LI>%C - Next character
    35  * <LI>%C - Next character
    35  * <LI>%n - Parameter in position n
    36  * <LI>%n - Parameter in position n
    58 
    59 
    59     /** String with latest replacements */
    60     /** String with latest replacements */
    60     private String replaced;
    61     private String replaced;
    61 
    62 
    62     /** Next replacement index */
    63     /** Next replacement index */
    63     private int nextIndex = 0;
    64     private int nextIndex = (ResourceLoader.getLocaleIdQt() == null? 0: 1);
    64 
    65 
    65     /*** ----------------------------- PUBLIC ------------------------------ */
    66     /*** ----------------------------- PUBLIC ------------------------------ */
    66 
    67 
    67     /**
    68     /**
    68      * Create a new formatter
    69      * Create a new formatter
    69      *
    70      *
    70      * @param pattern formatter pattern
    71      * @param pattern formatter pattern
    71      */
    72      */
    72     public Formatter(String aPattern)
    73     Formatter(String aPattern)
    73     {
    74     {
    74         pattern = aPattern;
    75         pattern = aPattern;
    75         replaced = aPattern;
    76         replaced = aPattern;
    76     }
    77     }
    77 
    78 
    88         if (replace("%" + nextIndex + "U", string) ||
    89         if (replace("%" + nextIndex + "U", string) ||
    89                 replace("%" + nextIndex, string) ||
    90                 replace("%" + nextIndex, string) ||
    90                 replace("%U", string))
    91                 replace("%U", string))
    91         {
    92         {
    92             nextIndex++;
    93             nextIndex++;
    93 
       
    94         }
    94         }
    95         else
    95         else
    96         {
    96         {
    97             Logger.WLOG(Logger.EUtils, "String replacement failed");
    97             Logger.WLOG(Logger.EUtils,
       
    98                         "String replacement failed on parameter " +
       
    99                         nextIndex + ": " + pattern);
    98         }
   100         }
    99         return this;
   101         return this;
   100     }
   102     }
   101 
   103 
   102     /**
   104     /**
   108      */
   110      */
   109     public Formatter arg(int number)
   111     public Formatter arg(int number)
   110     {
   112     {
   111         String localisedNumber = _formatInteger(number);
   113         String localisedNumber = _formatInteger(number);
   112 
   114 
   113         // Try to replace with patterns %nN, %n, %N
   115         // Try to replace with patterns %Ln, %nN, %n, %N
   114         if (replace("%" + nextIndex + "N", localisedNumber) ||
   116         if (replace("%" + "L" + nextIndex, localisedNumber) ||
       
   117                 replace("%" + nextIndex + "N", localisedNumber) ||
   115                 replace("%" + nextIndex, localisedNumber) ||
   118                 replace("%" + nextIndex, localisedNumber) ||
   116                 replace("%N", localisedNumber))
   119                 replace("%N", localisedNumber))
   117         {
   120         {
   118             nextIndex++;
   121             nextIndex++;
   119 
   122 
   120         }
   123         }
   121         else
   124         else
   122         {
   125         {
   123             Logger.WLOG(Logger.EUtils, "Integer replacement failed");
   126             Logger.WLOG(Logger.EUtils,
       
   127                         "Integer replacement failed on parameter " +
       
   128                         nextIndex + ": " + pattern);
   124         }
   129         }
   125         return this;
   130         return this;
   126     }
   131     }
   127 
   132 
   128     /**
   133     /**
   144             nextIndex++;
   149             nextIndex++;
   145 
   150 
   146         }
   151         }
   147         else
   152         else
   148         {
   153         {
   149             Logger.WLOG(Logger.EUtils, "Character replacement failed");
   154             Logger.WLOG(Logger.EUtils,
       
   155                         "Character replacement failed on parameter " +
       
   156                         nextIndex + ": " + pattern);
   150         }
   157         }
   151         return this;
   158         return this;
   152     }
   159     }
   153 
   160 
   154     /**
   161     /**
   206     {
   213     {
   207         String result = replaced;
   214         String result = replaced;
   208 
   215 
   209         // Reset for next usage
   216         // Reset for next usage
   210         replaced = pattern;
   217         replaced = pattern;
   211         nextIndex = 0;
   218         nextIndex = (ResourceLoader.getLocaleIdQt() == null? 0: 1);
   212 
   219 
   213         return result;
   220         return result;
   214     }
   221     }
   215 
   222 
   216     /**
   223     /**