Package com.ibm.icu.text

Examples of com.ibm.icu.text.MessageFormat


    /* @bug 4118594
     * MessageFormat.parse fails for some patterns.
     */
    public void Test4118594()
    {
        MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
        String forParsing = "x, y, z";
        Object[] objs = mf.parse(forParsing, new ParsePosition(0));
        logln("pattern: \"" + mf.toPattern() + "\"");
        logln("text for parsing: \"" + forParsing + "\"");
        if (!objs[0].toString().equals("z"))
            errln("argument0: \"" + objs[0] + "\"");
        mf.setLocale(Locale.US);
        mf.applyPattern("{0,number,#.##}, {0,number,#.#}");
        Object[] oldobjs = {new Double(3.1415)};
        String result = mf.format( oldobjs );
        logln("pattern: \"" + mf.toPattern() + "\"");
        logln("text for parsing: \"" + result + "\"");
        // result now equals "3.14, 3.1"
        if (!result.equals("3.14, 3.1"))
            errln("result = " + result);
        Object[] newobjs = mf.parse(result, new ParsePosition(0));
        // newobjs now equals {new Double(3.1)}
        if (((Number)newobjs[0]).doubleValue() != 3.1) // was (Double) [alan]
            errln( "newobjs[0] = " + newobjs[0]);
    }
View Full Code Here


     */
    public void Test4105380()
    {
        String patternText1 = "The disk \"{1}\" contains {0}.";
        String patternText2 = "There are {0} on the disk \"{1}\"";
        MessageFormat form1 = new MessageFormat(patternText1);
        MessageFormat form2 = new MessageFormat(patternText2);
        double[] filelimits = {0,1,2};
        String[] filepart = {"no files","one file","{0,number} files"};
        ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
        form1.setFormat(1, fileform);
        form2.setFormat(0, fileform);
        Object[] testArgs = {new Long(12373), "MyDisk"};
        logln(form1.format(testArgs));
        logln(form2.format(testArgs));
    }
View Full Code Here

    /* @bug 4120552
     * MessageFormat.parse incorrectly sets errorIndex.
     */
    public void Test4120552()
    {
        MessageFormat mf = new MessageFormat("pattern");
        String texts[] = {"pattern", "pat", "1234"};
        logln("pattern: \"" + mf.toPattern() + "\"");
        for (int i = 0; i < texts.length; i++) {
            ParsePosition pp = new ParsePosition(0);
            Object[] objs = mf.parse(texts[i], pp);
            log("  text for parsing: \"" + texts[i] + "\"");
            if (objs == null) {
                logln("  (incorrectly formatted string)");
                if (pp.getErrorIndex() == -1)
                    errln("Incorrect error index: " + pp.getErrorIndex());
View Full Code Here

     */
    public void Test4142938() {
        String pat = "''Vous'' {0,choice,0#n''|1#}avez s\u00E9lectionne\u00E9 " +
            "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} " +
            "personnel{0,choice,0#s|1#|2#s}.";
        MessageFormat mf = new MessageFormat(pat);

        String[] PREFIX = {
            "'Vous' n'avez s\u00E9lectionne\u00E9 aucun clients personnels.",
            "'Vous' avez s\u00E9lectionne\u00E9 ",
            "'Vous' avez s\u00E9lectionne\u00E9 "
        }
        String[] SUFFIX = {
            null,
            " client personnel.",
            " clients personnels."
        };
   
        for (int i=0; i<3; i++) {
            String out = mf.format(new Object[]{new Integer(i)});
            if (SUFFIX[i] == null) {
                if (!out.equals(PREFIX[i]))
                    errln("" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"");
            }
            else {
View Full Code Here

     * @bug 4112104
     * MessageFormat.equals(null) throws a NullPointerException.  The JLS states
     * that it should return false.
     */
    public void Test4112104() {
        MessageFormat format = new MessageFormat("");
        try {
            // This should NOT throw an exception
            if (format.equals(null)) {
                // It also should return false
                errln("MessageFormat.equals(null) returns false");
            }
        }
        catch (NullPointerException e) {
View Full Code Here

    }
   
    public void test4232154() {
        boolean gotException = false;
        try {
            new MessageFormat("The date is {0:date}");
        } catch (Exception e) {
            gotException = true;
            if (!(e instanceof IllegalArgumentException)) {
                throw new RuntimeException("got wrong exception type");
            }
View Full Code Here

            throw new RuntimeException("didn't get exception for invalid input");
        }
    }
   
    public void test4293229() {
        MessageFormat format = new MessageFormat("'''{'0}'' '''{0}'''");
        Object[] args = { null };
        String expected = "'{0}' '{0}'";
        String result = format.format(args);
        if (!result.equals(expected)) {
            throw new RuntimeException("wrong format result - expected \"" +
                    expected + "\", got \"" + result + "\"");
        }
    }
View Full Code Here

       
      { // Taken from Test4031438().
        String pattern1 = "Impossible {arg1} has occurred -- status code is {arg0} and message is {arg2}.";
        String pattern2 = "Double '' Quotes {ARG_ZERO} test and quoted '{ARG_ONE}' test plus 'other {ARG_TWO} stuff'.";

        MessageFormat messageFormatter = new MessageFormat("");

        try {
            logln("Apply with pattern : " + pattern1);
            messageFormatter.applyPattern(pattern1);
            HashMap paramsMap = new HashMap();
            paramsMap.put("arg0", new Integer(7));
            String tempBuffer = messageFormatter.format(paramsMap);
            if (!tempBuffer.equals("Impossible {arg1} has occurred -- status code is 7 and message is {arg2}."))
                errln("Tests arguments < substitution failed");
            logln("Formatted with 7 : " + tempBuffer);
            ParsePosition status = new ParsePosition(0);
            Map objs = messageFormatter.parseToMap(tempBuffer, status);
            if (objs.get("arg1") != null || objs.get("arg2") != null)
                errln("Parse failed with more than expected arguments");
            for (Iterator keyIter = objs.keySet().iterator();
                 keyIter.hasNext();) {
                String key = (String) keyIter.next();
                if (objs.get(key) != null && !objs.get(key).toString().equals(paramsMap.get(key).toString())) {
                    errln("Parse failed on object " + objs.get(key) + " with argument name : " + key );
                }
            }
            tempBuffer = messageFormatter.format(null);
            if (!tempBuffer.equals("Impossible {arg1} has occurred -- status code is {arg0} and message is {arg2}."))
                errln("Tests with no arguments failed");
            logln("Formatted with null : " + tempBuffer);
            logln("Apply with pattern : " + pattern2);
            messageFormatter.applyPattern(pattern2);
            paramsMap.clear();
            paramsMap.put("ARG_ZERO", new Integer(7));
            tempBuffer = messageFormatter.format(paramsMap);
            if (!tempBuffer.equals("Double ' Quotes 7 test and quoted {ARG_ONE} test plus other {ARG_TWO} stuff."))
                errln("quote format test (w/ params) failed.");
            logln("Formatted with params : " + tempBuffer);
            tempBuffer = messageFormatter.format(null);
            if (!tempBuffer.equals("Double ' Quotes {ARG_ZERO} test and quoted {ARG_ONE} test plus other {ARG_TWO} stuff."))
                errln("quote format test (w/ null) failed.");
            logln("Formatted with null : " + tempBuffer);
            logln("toPattern : " + messageFormatter.toPattern());
        } catch (Exception foo) {
            warnln("Exception when formatting in bug 4031438. "+foo.getMessage());
        }
      }{ // Taken from Test4052223().
        ParsePosition pos = new ParsePosition(0);
        if (pos.getErrorIndex() != -1) {
            errln("ParsePosition.getErrorIndex initialization failed.");
        }
        MessageFormat fmt = new MessageFormat("There are {numberOfApples} apples growing on the {whatKindOfTree} tree.");
        String str = new String("There is one apple growing on the peach tree.");
        Map objs = fmt.parseToMap(str, pos);
        logln("unparsable string , should fail at " + pos.getErrorIndex());
        if (pos.getErrorIndex() == -1)
            errln("Bug 4052223 failed : parsing string " + str);
        pos.setErrorIndex(4);
        if (pos.getErrorIndex() != 4)
            errln("setErrorIndex failed, got " + pos.getErrorIndex() + " instead of 4");
        if (objs != null)
            errln("unparsable string, should return null");
    }{ // Taken from Test4111739().
        MessageFormat format1 = null;
        MessageFormat format2 = null;
        ObjectOutputStream ostream = null;
        ByteArrayOutputStream baos = null;
        ObjectInputStream istream = null;

        try {
            baos = new ByteArrayOutputStream();
            ostream = new ObjectOutputStream(baos);
        } catch(IOException e) {
            errln("Unexpected exception : " + e.getMessage());
            return;
        }

        try {
            format1 = new MessageFormat("pattern{argument}");
            ostream.writeObject(format1);
            ostream.flush();

            byte bytes[] = baos.toByteArray();

            istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
            format2 = (MessageFormat)istream.readObject();
        } catch(Exception e) {
            errln("Unexpected exception : " + e.getMessage());
        }

        if (!format1.equals(format2)) {
            errln("MessageFormats before and after serialization are not" +
                " equal\nformat1 = " + format1 + "(" + format1.toPattern() + ")\nformat2 = " +
                format2 + "(" + format2.toPattern() + ")");
        } else {
            logln("Serialization for MessageFormat is OK.");
        }
    }{ // Taken from Test4116444().
        String[] patterns = {"", "one", "{namedArgument,date,short}"};
        MessageFormat mf = new MessageFormat("");

        for (int i = 0; i < patterns.length; i++) {
            String pattern = patterns[i];
            mf.applyPattern(pattern);
            try {
                Map objs = mf.parseToMap(null, new ParsePosition(0));
                logln("pattern: \"" + pattern + "\"");
                log(" parsedObjects: ");
                if (objs != null) {
                    log("{");
                    for (Iterator keyIter = objs.keySet().iterator();
                         keyIter.hasNext();) {
                        String key = (String)keyIter.next();
                        if (objs.get(key) != null) {
                            err("\"" + objs.get(key).toString() + "\"");
                        } else {
                            log("null");
                        }
                        if (keyIter.hasNext()) {
                            log(",");
                        }
                    }
                    log("}") ;
                } else {
                    log("null");
                }
                logln("");
            } catch (Exception e) {
                errln("pattern: \"" + pattern + "\"");
                errln("  Exception: " + e.getMessage());
            }
        }
    }{ // Taken from Test4114739().
        MessageFormat mf = new MessageFormat("<{arg}>");
        Map objs1 = null;
        Map objs2 = new HashMap();
        Map objs3 = new HashMap();
        objs3.put("arg", null);
        try {
            logln("pattern: \"" + mf.toPattern() + "\"");
            log("format(null) : ");
            logln("\"" + mf.format(objs1) + "\"");
            log("format({})   : ");
            logln("\"" + mf.format(objs2) + "\"");
            log("format({null}) :");
            logln("\"" + mf.format(objs3) + "\"");
        } catch (Exception e) {
            errln("Exception thrown for null argument tests.");
        }
    }{ // Taken from Test4118594().
        String argName = "something_stupid";
        MessageFormat mf = new MessageFormat("{"+ argName + "}, {" + argName + "}, {" + argName + "}");
        String forParsing = "x, y, z";
        Map objs = mf.parseToMap(forParsing, new ParsePosition(0));
        logln("pattern: \"" + mf.toPattern() + "\"");
        logln("text for parsing: \"" + forParsing + "\"");
        if (!objs.get(argName).toString().equals("z"))
            errln("argument0: \"" + objs.get(argName) + "\"");
        mf.setLocale(Locale.US);
        mf.applyPattern("{" + argName + ",number,#.##}, {" + argName + ",number,#.#}");
        Map oldobjs = new HashMap();
        oldobjs.put(argName, new Double(3.1415));
        String result = mf.format( oldobjs );
        logln("pattern: \"" + mf.toPattern() + "\"");
        logln("text for parsing: \"" + result + "\"");
        // result now equals "3.14, 3.1"
        if (!result.equals("3.14, 3.1"))
            errln("result = " + result);
        Map newobjs = mf.parseToMap(result, new ParsePosition(0));
        // newobjs now equals {new Double(3.1)}
        if (((Number)newobjs.get(argName)).doubleValue() != 3.1) // was (Double) [alan]
            errln( "newobjs.get(argName) = " + newobjs.get(argName));
    }{ // Taken from Test4105380().
        String patternText1 = "The disk \"{diskName}\" contains {numberOfFiles}.";
        String patternText2 = "There are {numberOfFiles} on the disk \"{diskName}\"";
        MessageFormat form1 = new MessageFormat(patternText1);
        MessageFormat form2 = new MessageFormat(patternText2);
        double[] filelimits = {0,1,2};
        String[] filepart = {"no files","one file","{numberOfFiles,number} files"};
        ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
        form1.setFormat(1, fileform);
        form2.setFormat(0, fileform);
        Map testArgs = new HashMap();
        testArgs.put("diskName", "MyDisk");
        testArgs.put("numberOfFiles", new Long(12373));
        logln(form1.format(testArgs));
        logln(form2.format(testArgs));
    }{ // Taken from test4293229().
        MessageFormat format = new MessageFormat("'''{'myNamedArgument}'' '''{myNamedArgument}'''");
        Map args = new HashMap();
        String expected = "'{myNamedArgument}' '{myNamedArgument}'";
        String result = format.format(args);
        if (!result.equals(expected)) {
            throw new RuntimeException("wrong format result - expected \"" +
                    expected + "\", got \"" + result + "\"");
        }
    }
View Full Code Here

       
        if(totalScript>previousTotalScripts){
            newScripts = true;
        }
        //Processing old scripts
        MessageFormat format = new MessageFormat(scriptPreamble);
        for(int script=minScript;script<=previousTotalScripts;){
             checkICUVersion = (String)scriptVersionNumber.get(arrayListIndex);
             checkICUVersion = checkICUVersion.substring(checkICUVersion.indexOf("_")+1);
             previousVersion = checkICUVersion.substring(0, checkICUVersion.indexOf("="));
             previousMajor = Integer.parseInt(previousVersion.substring(0,previousVersion.indexOf(".")));
             previousMinor = Integer.parseInt(previousVersion.substring(previousVersion.indexOf(".")+1));
             numberOfScripts = Integer.parseInt(checkICUVersion.substring(checkICUVersion.indexOf("=")+1));
            
             Object args[] = {new Integer(previousMajor), new Integer(previousMinor)};
             //Check for the initial header. It should be written only one time
             if(!initialheader){
                 output.println(format.format(args));
                 initialheader = true;
             }else{
                 if((verMajor-previousMajor)>=1){
                     format = new MessageFormat(scriptPreambleStable);
                     output.println(format.format(args));
                 }else{
                     format = new MessageFormat(scriptPreambleDraft);
                     output.println(format.format(args));
                 }
             }
            
             for(int i=0;i<numberOfScripts;i++){
                 output.print("    ");
                 output.print(scriptData.getTagLabel(script));
                 output.print("ScriptCode = ");
                
                 if (script < 10) {
                     output.print(" ");
                 }
                
                 output.print(script);
                 output.println(",");
                 script++;
             }
             arrayListIndex++;
        }
       
        if(newScripts){//Processing newly added scripts
            format = new MessageFormat(scriptPreambleDraft);
            Object args[] = {new Integer(verMajor), new Integer(verMinor)};
            output.println(format.format(args));
           
            for (int script = previousTotalScripts+1; script <= totalScript; script += 1) {
                output.print("    ");
                output.print(scriptData.getTagLabel(script));
                output.print("ScriptCode = ");
View Full Code Here

       
        if(totalLanguage>previousTotalLanguages){
            newLanguage = true;
        }
        //Processing old languages
        MessageFormat format = new MessageFormat(languagePreamble);
        for(int language=minLanguage;language<=previousTotalLanguages;){
             checkICUVersion = (String)languageVersionNumber.get(arrayListIndex);
             checkICUVersion = checkICUVersion.substring(checkICUVersion.indexOf("_")+1);
             previousVersion = checkICUVersion.substring(0, checkICUVersion.indexOf("="));
             previousMajor = Integer.parseInt(previousVersion.substring(0,previousVersion.indexOf(".")));
             previousMinor = Integer.parseInt(previousVersion.substring(previousVersion.indexOf(".")+1));
             numberOfLanguages = Integer.parseInt(checkICUVersion.substring(checkICUVersion.indexOf("=")+1));
            
             Object args[] = {new Integer(previousMajor), new Integer(previousMinor)};
           
             //Check for the initial header. It should be written only one time
             if(!initialheader){
                 output.println(format.format(args));
                 initialheader = true;
             }else{
                 if((verMajor-previousMajor)>=1){
                     format = new MessageFormat(languagePreambleStable);
                     output.println(format.format(args));
                 }else{
                     format = new MessageFormat(languagePreambleDraft);
                     output.println(format.format(args));
                 }
             }
            
             for(int i=0;i<numberOfLanguages;i++){
                 output.print("    ");
                 output.print(languageData.getTagLabel(language).toLowerCase());
                 output.print("LanguageCode = ");
                
                 if (language < 10) {
                     output.print(" ");
                 }
                
                 output.print(language);
                 output.println(",");
                 language++;
             }
             arrayListIndex++;
        }
        if(newLanguage){
            //Processing newly added languages
            format = new MessageFormat(languagePreambleDraft);
            Object args[] = {new Integer(verMajor), new Integer(verMinor)};
            output.println(format.format(args));
           
            for (int langauge = previousTotalLanguages+1; langauge <= totalLanguage; langauge += 1) {
                output.print("    ");
                output.print(languageData.getTagLabel(langauge).toLowerCase());
                output.print("ScriptCode = ");
View Full Code Here

TOP

Related Classes of com.ibm.icu.text.MessageFormat

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.