msgfmt.h

Go to the documentation of this file.
00001 /*
00002 * Copyright (C) 1997-2006, International Business Machines Corporation and others. All Rights Reserved.
00003 ********************************************************************************
00004 *
00005 * File MSGFMT.H
00006 *
00007 * Modification History:
00008 *
00009 *   Date        Name        Description
00010 *   02/19/97    aliu        Converted from java.
00011 *   03/20/97    helena      Finished first cut of implementation.
00012 *   07/22/98    stephen     Removed operator!= (defined in Format)
00013 *   08/19/2002  srl         Removing Javaisms
00014 ********************************************************************************
00015 */
00016 
00017 #ifndef MSGFMT_H
00018 #define MSGFMT_H
00019 
00020 #include "unicode/utypes.h"
00021 
00027 #if !UCONFIG_NO_FORMATTING
00028 
00029 #include "unicode/format.h"
00030 #include "unicode/locid.h"
00031 #include "unicode/parseerr.h"
00032 
00033 U_NAMESPACE_BEGIN
00034 
00035 class NumberFormat;
00036 class DateFormat;
00037 
00267 class U_I18N_API MessageFormat : public Format {
00268 public:
00274     enum EFormatNumber {
00280         kMaxFormat = 10
00281     };
00282 
00292     MessageFormat(const UnicodeString& pattern,
00293                   UErrorCode &status);
00294 
00303     MessageFormat(const UnicodeString& pattern,
00304                   const Locale& newLocale,
00305                         UErrorCode& status);
00316     MessageFormat(const UnicodeString& pattern,
00317                   const Locale& newLocale,
00318                   UParseError& parseError,
00319                   UErrorCode& status);
00324     MessageFormat(const MessageFormat&);
00325 
00330     const MessageFormat& operator=(const MessageFormat&);
00331 
00336     virtual ~MessageFormat();
00337 
00343     virtual Format* clone(void) const;
00344 
00352     virtual UBool operator==(const Format& other) const;
00353 
00360     virtual void setLocale(const Locale& theLocale);
00361 
00368     virtual const Locale& getLocale(void) const;
00369 
00378     virtual void applyPattern(const UnicodeString& pattern,
00379                               UErrorCode& status);
00390     virtual void applyPattern(const UnicodeString& pattern,
00391                              UParseError& parseError,
00392                              UErrorCode& status);
00393 
00402     virtual UnicodeString& toPattern(UnicodeString& appendTo) const;
00403 
00417     virtual void adoptFormats(Format** formatsToAdopt, int32_t count);
00418 
00430     virtual void setFormats(const Format** newFormats,int32_t cnt);
00431 
00432 
00443     virtual void adoptFormat(int32_t formatNumber, Format* formatToAdopt);
00444 
00454     virtual void setFormat(int32_t formatNumber, const Format& format);
00455 
00467     virtual const Format** getFormats(int32_t& count) const;
00468 
00483     UnicodeString& format(  const Formattable* source,
00484                             int32_t count,
00485                             UnicodeString& appendTo,
00486                             FieldPosition& ignore,
00487                             UErrorCode& status) const;
00488 
00503     static UnicodeString& format(   const UnicodeString& pattern,
00504                                     const Formattable* arguments,
00505                                     int32_t count,
00506                                     UnicodeString& appendTo,
00507                                     UErrorCode& status);
00508 
00526     virtual UnicodeString& format(const Formattable& obj,
00527                                   UnicodeString& appendTo,
00528                                   FieldPosition& pos,
00529                                   UErrorCode& status) const;
00530 
00545     UnicodeString& format(const Formattable& obj,
00546                           UnicodeString& appendTo,
00547                           UErrorCode& status) const;
00548 
00562     virtual Formattable* parse( const UnicodeString& source,
00563                                 ParsePosition& pos,
00564                                 int32_t& count) const;
00565 
00577     virtual Formattable* parse( const UnicodeString& source,
00578                                 int32_t& count,
00579                                 UErrorCode& status) const;
00580 
00593     virtual void parseObject(const UnicodeString& source,
00594                              Formattable& result,
00595                              ParsePosition& pos) const;
00596 
00616     static UnicodeString autoQuoteApostrophe(const UnicodeString& pattern, 
00617         UErrorCode& status);
00618 
00630     virtual UClassID getDynamicClassID(void) const;
00631 
00643     static UClassID U_EXPORT2 getStaticClassID(void);
00644     
00645 private:
00646 
00647     Locale              fLocale;
00648     UnicodeString       fPattern;
00649     Format**            formatAliases; // see getFormats
00650     int32_t             formatAliasesCapacity;
00651 
00652     MessageFormat(); // default constructor not implemented
00653 
00654     /*
00655      * A structure representing one subformat of this MessageFormat.
00656      * Each subformat has a Format object, an offset into the plain
00657      * pattern text fPattern, and an argument number.  The argument
00658      * number corresponds to the array of arguments to be formatted.
00659      * @internal
00660      */
00661     class Subformat {
00662     public:
00666         Format* format; // formatter
00670         int32_t offset; // offset into fPattern
00674         int32_t arg;    // 0-based argument number
00675 
00681         Subformat& operator=(const Subformat& that) {
00682             format = that.format ? that.format->clone() : NULL;
00683             offset = that.offset;
00684             arg = that.arg;
00685             return *this;
00686         }
00687 
00691         UBool operator==(const Subformat& that) const {
00692             // Do cheap comparisons first
00693             return offset == that.offset &&
00694                    arg == that.arg &&
00695                    ((format == that.format) || // handles NULL
00696                     (*format == *that.format));
00697         }
00698 
00702         UBool operator!=(const Subformat& that) const {
00703             return !operator==(that);
00704         }
00705     };
00706 
00711     Subformat* subformats;
00712     int32_t    subformatCount;
00713     int32_t    subformatCapacity;
00714 
00723     Formattable::Type* argTypes;
00724     int32_t            argTypeCount;
00725     int32_t            argTypeCapacity;
00726 
00727     // Variable-size array management
00728     UBool allocateSubformats(int32_t capacity);
00729     UBool allocateArgTypes(int32_t capacity);
00730 
00738     NumberFormat* defaultNumberFormat;
00739     DateFormat*   defaultDateFormat;
00740 
00745     const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
00746     const DateFormat*   getDefaultDateFormat(UErrorCode&) const;
00747 
00754     static int32_t findKeyword( const UnicodeString& s,
00755                                 const UChar * const *list);
00756 
00773     UnicodeString&  format( const Formattable* arguments,
00774                             int32_t cnt,
00775                             UnicodeString& appendTo,
00776                             FieldPosition& status,
00777                             int32_t recursionProtection,
00778                             UErrorCode& success) const;
00779 
00780     void             makeFormat(int32_t offsetNumber,
00781                                 UnicodeString* segments,
00782                                 UParseError& parseError,
00783                                 UErrorCode& success);
00784 
00788     NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const;
00789 
00799     static void copyAndFixQuotes(const UnicodeString& appendTo, int32_t start, int32_t end, UnicodeString& target);
00800 
00809     const Formattable::Type* getArgTypeList(int32_t& listCount) const {
00810         listCount = argTypeCount;
00811         return argTypes; 
00812     }
00813 
00814     friend class MessageFormatAdapter; // getFormatTypeList() access
00815 };
00816 
00817 inline UnicodeString&
00818 MessageFormat::format(const Formattable& obj,
00819                       UnicodeString& appendTo,
00820                       UErrorCode& status) const {
00821     return Format::format(obj, appendTo, status);
00822 }
00823 U_NAMESPACE_END
00824 
00825 #endif /* #if !UCONFIG_NO_FORMATTING */
00826 
00827 #endif // _MSGFMT
00828 //eof
00829 

Generated on Mon Aug 13 07:17:24 2007 for ICU 3.6 by  doxygen 1.5.2