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 
00617     static UnicodeString autoQuoteApostrophe(const UnicodeString& pattern, 
00618         UErrorCode& status);
00619 
00631     virtual UClassID getDynamicClassID(void) const;
00632 
00644     static UClassID U_EXPORT2 getStaticClassID(void);
00645     
00646 private:
00647 
00648     Locale              fLocale;
00649     UnicodeString       fPattern;
00650     Format**            formatAliases; // see getFormats
00651     int32_t             formatAliasesCapacity;
00652 
00653     MessageFormat(); // default constructor not implemented
00654 
00655     /*
00656      * A structure representing one subformat of this MessageFormat.
00657      * Each subformat has a Format object, an offset into the plain
00658      * pattern text fPattern, and an argument number.  The argument
00659      * number corresponds to the array of arguments to be formatted.
00660      * @internal
00661      */
00662     class Subformat {
00663     public:
00667         Format* format; // formatter
00671         int32_t offset; // offset into fPattern
00675         int32_t arg;    // 0-based argument number
00676 
00682         Subformat& operator=(const Subformat& that) {
00683             format = that.format ? that.format->clone() : NULL;
00684             offset = that.offset;
00685             arg = that.arg;
00686             return *this;
00687         }
00688 
00692         UBool operator==(const Subformat& that) const {
00693             // Do cheap comparisons first
00694             return offset == that.offset &&
00695                    arg == that.arg &&
00696                    ((format == that.format) || // handles NULL
00697                     (*format == *that.format));
00698         }
00699 
00703         UBool operator!=(const Subformat& that) const {
00704             return !operator==(that);
00705         }
00706     };
00707 
00712     Subformat* subformats;
00713     int32_t    subformatCount;
00714     int32_t    subformatCapacity;
00715 
00724     Formattable::Type* argTypes;
00725     int32_t            argTypeCount;
00726     int32_t            argTypeCapacity;
00727 
00728     // Variable-size array management
00729     UBool allocateSubformats(int32_t capacity);
00730     UBool allocateArgTypes(int32_t capacity);
00731 
00739     NumberFormat* defaultNumberFormat;
00740     DateFormat*   defaultDateFormat;
00741 
00746     const NumberFormat* getDefaultNumberFormat(UErrorCode&) const;
00747     const DateFormat*   getDefaultDateFormat(UErrorCode&) const;
00748 
00755     static int32_t findKeyword( const UnicodeString& s,
00756                                 const UChar * const *list);
00757 
00774     UnicodeString&  format( const Formattable* arguments,
00775                             int32_t cnt,
00776                             UnicodeString& appendTo,
00777                             FieldPosition& status,
00778                             int32_t recursionProtection,
00779                             UErrorCode& success) const;
00780 
00781     void             makeFormat(int32_t offsetNumber,
00782                                 UnicodeString* segments,
00783                                 UParseError& parseError,
00784                                 UErrorCode& success);
00785 
00789     NumberFormat* createIntegerFormat(const Locale& locale, UErrorCode& status) const;
00790 
00800     static void copyAndFixQuotes(const UnicodeString& appendTo, int32_t start, int32_t end, UnicodeString& target);
00801 
00810     const Formattable::Type* getArgTypeList(int32_t& listCount) const {
00811         listCount = argTypeCount;
00812         return argTypes; 
00813     }
00814 
00815     friend class MessageFormatAdapter; // getFormatTypeList() access
00816 };
00817 
00818 inline UnicodeString&
00819 MessageFormat::format(const Formattable& obj,
00820                       UnicodeString& appendTo,
00821                       UErrorCode& status) const {
00822     return Format::format(obj, appendTo, status);
00823 }
00824 U_NAMESPACE_END
00825 
00826 #endif /* #if !UCONFIG_NO_FORMATTING */
00827 
00828 #endif // _MSGFMT
00829 //eof
00830 

Generated on Tue Aug 29 17:26:23 2006 for ICU 3.6 by  doxygen 1.4.6-NO