2 *******************************************************************************
3 * Copyright (C) 1997-2004, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/18/97 aliu Converted from OpenClass. Added DONE.
13 * 01/13/2000 helena Added UErrorCode parameter to createXXXInstance methods.
14 *****************************************************************************************
17 // *****************************************************************************
18 // This file was generated from the java source file BreakIterator.java
19 // *****************************************************************************
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_BREAK_ITERATION
25 #include "unicode/dbbi.h"
26 #include "unicode/brkiter.h"
27 #include "unicode/udata.h"
28 #include "unicode/ures.h"
36 // *****************************************************************************
37 // class BreakIterator
38 // This class implements methods for finding the location of boundaries in text.
39 // Instances of BreakIterator maintain a current position and scan over text
40 // returning the index of characters where boundaries occur.
41 // *****************************************************************************
45 const int32_t BreakIterator::DONE
= (int32_t)-1;
47 // -------------------------------------
50 BreakIterator::buildInstance(const Locale
& loc
, const char *type
, UBool dict
, UErrorCode
&status
)
53 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
55 const UChar
* brkfname
= NULL
;
56 UResourceBundle brkRulesStack
, brkNameStack
;
57 UResourceBundle
*brkRules
= &brkRulesStack
, *brkName
= &brkNameStack
;
58 BreakIterator
*result
= NULL
;
60 if (U_FAILURE(status
))
63 ures_initStackObject(brkRules
);
64 ures_initStackObject(brkName
);
67 UResourceBundle
*b
= ures_open(NULL
, loc
.getName(), &status
);
69 // Get the "boundaries" array.
70 if (U_SUCCESS(status
)) {
71 brkRules
= ures_getByKeyWithFallback(b
, "boundaries", brkRules
, &status
);
72 // Get the string object naming the rules file
73 brkName
= ures_getByKeyWithFallback(brkRules
, type
, brkName
, &status
);
74 // Get the actual string
75 brkfname
= ures_getString(brkName
, &size
, &status
);
77 // Use the string if we found it
78 if (U_SUCCESS(status
) && brkfname
) {
79 uprv_strncpy(actualLocale
,
80 ures_getLocale(brkName
, &status
),
81 sizeof(actualLocale
)/sizeof(actualLocale
[0]));
82 u_UCharsToChars(brkfname
, fnbuff
, size
+1);
89 UDataMemory
* file
= udata_open(NULL
, "brk", fnbuff
, &status
);
90 if (U_FAILURE(status
)) {
95 // We found the break rules; now see if a dictionary is needed
98 UErrorCode localStatus
= U_ZERO_ERROR
;
99 brkName
= &brkNameStack
;
100 ures_initStackObject(brkName
);
101 brkName
= ures_getByKeyWithFallback(b
, "BreakDictionaryData", brkName
, &localStatus
);
103 if (U_SUCCESS(localStatus
)) {
104 brkfname
= ures_getString(&brkname
, &size
, &localStatus
);
107 if (U_SUCCESS(localStatus
)) {
109 u_UCharsToChars(brkfname
, fnbuff
, size
);
112 result
= new DictionaryBasedBreakIterator(file
, "thaidict.brk", status
);
117 // If there is still no result but we haven't had an error, no dictionary,
118 // so make a non-dictionary break iterator
119 if (U_SUCCESS(status
) && result
== NULL
) {
120 result
= new RuleBasedBreakIterator(file
, status
);
123 // If there is a result, set the valid locale and actual locale
124 if (U_SUCCESS(status
) && result
!= NULL
) {
125 U_LOCALE_BASED(locBased
, *result
);
126 locBased
.setLocaleIDs(ures_getLocaleByType(b
, ULOC_VALID_LOCALE
, &status
), actualLocale
);
131 if (U_FAILURE(status
) && result
!= NULL
) { // Sometimes redundant check, but simple
136 if (result
== NULL
) {
138 if (U_SUCCESS(status
)) {
139 status
= U_MEMORY_ALLOCATION_ERROR
;
146 // Creates a break iterator for word breaks.
147 BreakIterator
* U_EXPORT2
148 BreakIterator::createWordInstance(const Locale
& key
, UErrorCode
& status
)
150 return createInstance(key
, UBRK_WORD
, status
);
153 // -------------------------------------
155 // Creates a break iterator for line breaks.
156 BreakIterator
* U_EXPORT2
157 BreakIterator::createLineInstance(const Locale
& key
, UErrorCode
& status
)
159 return createInstance(key
, UBRK_LINE
, status
);
162 // -------------------------------------
164 // Creates a break iterator for character breaks.
165 BreakIterator
* U_EXPORT2
166 BreakIterator::createCharacterInstance(const Locale
& key
, UErrorCode
& status
)
168 return createInstance(key
, UBRK_CHARACTER
, status
);
171 // -------------------------------------
173 // Creates a break iterator for sentence breaks.
174 BreakIterator
* U_EXPORT2
175 BreakIterator::createSentenceInstance(const Locale
& key
, UErrorCode
& status
)
177 return createInstance(key
, UBRK_SENTENCE
, status
);
180 // -------------------------------------
182 // Creates a break iterator for title casing breaks.
183 BreakIterator
* U_EXPORT2
184 BreakIterator::createTitleInstance(const Locale
& key
, UErrorCode
& status
)
186 return createInstance(key
, UBRK_TITLE
, status
);
189 // -------------------------------------
191 // Gets all the available locales that has localized text boundary data.
192 const Locale
* U_EXPORT2
193 BreakIterator::getAvailableLocales(int32_t& count
)
195 return Locale::getAvailableLocales(count
);
198 // -------------------------------------
199 // Gets the objectLocale display name in the default locale language.
200 UnicodeString
& U_EXPORT2
201 BreakIterator::getDisplayName(const Locale
& objectLocale
,
204 return objectLocale
.getDisplayName(name
);
207 // -------------------------------------
208 // Gets the objectLocale display name in the displayLocale language.
209 UnicodeString
& U_EXPORT2
210 BreakIterator::getDisplayName(const Locale
& objectLocale
,
211 const Locale
& displayLocale
,
214 return objectLocale
.getDisplayName(displayLocale
, name
);
217 // ------------------------------------------
219 // Default constructor and destructor
221 //-------------------------------------------
223 BreakIterator::BreakIterator()
225 fBufferClone
= FALSE
;
226 *validLocale
= *actualLocale
= 0;
229 BreakIterator::~BreakIterator()
233 // ------------------------------------------
237 //-------------------------------------------
238 #if !UCONFIG_NO_SERVICE
240 static ICULocaleService
* gService
= NULL
;
242 // -------------------------------------
244 class ICUBreakIteratorFactory
: public ICUResourceBundleFactory
{
246 virtual UObject
* handleCreate(const Locale
& loc
, int32_t kind
, const ICUService
* /*service*/, UErrorCode
& status
) const {
247 return BreakIterator::makeInstance(loc
, kind
, status
);
251 // -------------------------------------
253 class ICUBreakIteratorService
: public ICULocaleService
{
255 ICUBreakIteratorService()
256 : ICULocaleService(UNICODE_STRING("Break Iterator", 14))
258 UErrorCode status
= U_ZERO_ERROR
;
259 registerFactory(new ICUBreakIteratorFactory(), status
);
262 virtual UObject
* cloneInstance(UObject
* instance
) const {
263 return ((BreakIterator
*)instance
)->clone();
266 virtual UObject
* handleDefault(const ICUServiceKey
& key
, UnicodeString
* /*actualID*/, UErrorCode
& status
) const {
267 LocaleKey
& lkey
= (LocaleKey
&)key
;
268 int32_t kind
= lkey
.kind();
270 lkey
.currentLocale(loc
);
271 return BreakIterator::makeInstance(loc
, kind
, status
);
274 virtual UBool
isDefault() const {
275 return countFactories() == 1;
279 // -------------------------------------
283 // defined in ucln_cmn.h
286 * Release all static memory held by breakiterator.
289 static UBool U_CALLCONV
breakiterator_cleanup(void) {
290 #if !UCONFIG_NO_SERVICE
301 static ICULocaleService
*
306 needsInit
= (UBool
)(gService
== NULL
);
310 ICULocaleService
*tService
= new ICUBreakIteratorService();
312 if (gService
== NULL
) {
315 ucln_common_registerCleanup(UCLN_COMMON_BREAKITERATOR
, breakiterator_cleanup
);
323 // -------------------------------------
329 return gService
!= NULL
;
332 // -------------------------------------
334 URegistryKey U_EXPORT2
335 BreakIterator::registerInstance(BreakIterator
* toAdopt
, const Locale
& locale
, UBreakIteratorType kind
, UErrorCode
& status
)
337 return getService()->registerInstance(toAdopt
, locale
, kind
, status
);
340 // -------------------------------------
343 BreakIterator::unregister(URegistryKey key
, UErrorCode
& status
)
345 if (U_SUCCESS(status
)) {
347 return gService
->unregister(key
, status
);
349 status
= U_ILLEGAL_ARGUMENT_ERROR
;
354 // -------------------------------------
356 StringEnumeration
* U_EXPORT2
357 BreakIterator::getAvailableLocales(void)
359 return getService()->getAvailableLocales();
361 #endif /* UCONFIG_NO_SERVICE */
363 // -------------------------------------
366 BreakIterator::createInstance(const Locale
& loc
, UBreakIteratorType kind
, UErrorCode
& status
)
368 if (U_FAILURE(status
)) {
373 #if !UCONFIG_NO_SERVICE
376 BreakIterator
*result
= (BreakIterator
*)gService
->get(loc
, kind
, &actualLoc
, status
);
377 // TODO: The way the service code works in ICU 2.8 is that if
378 // there is a real registered break iterator, the actualLoc
379 // will be populated, but if the handleDefault path is taken
380 // (because nothing is registered that can handle the
381 // requested locale) then the actualLoc comes back empty. In
382 // that case, the returned object already has its actual/valid
383 // locale data populated (by makeInstance, which is what
384 // handleDefault calls), so we don't touch it. YES, A COMMENT
385 // THIS LONG is a sign of bad code -- so the action item is to
386 // revisit this in ICU 3.0 and clean it up/fix it/remove it.
387 if (U_SUCCESS(status
) && (result
!= NULL
) && *actualLoc
.getName() != 0) {
388 U_LOCALE_BASED(locBased
, *result
);
389 locBased
.setLocaleIDs(actualLoc
.getName(), actualLoc
.getName());
396 return makeInstance(loc
, kind
, status
);
400 // -------------------------------------
403 BreakIterator::makeInstance(const Locale
& loc
, int32_t kind
, UErrorCode
& status
)
406 if (U_FAILURE(status
)) {
410 BreakIterator
*result
= NULL
;
413 result
= BreakIterator::buildInstance(loc
, "grapheme", FALSE
, status
);
416 result
= BreakIterator::buildInstance(loc
, "word", TRUE
, status
);
419 result
= BreakIterator::buildInstance(loc
, "line", TRUE
, status
);
422 result
= BreakIterator::buildInstance(loc
, "sentence", FALSE
, status
);
425 result
= BreakIterator::buildInstance(loc
, "title", FALSE
, status
);
428 status
= U_ILLEGAL_ARGUMENT_ERROR
;
431 if (U_FAILURE(status
)) {
439 BreakIterator::getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const {
440 U_LOCALE_BASED(locBased
, *this);
441 return locBased
.getLocale(type
, status
);
445 BreakIterator::getLocaleID(ULocDataLocaleType type
, UErrorCode
& status
) const {
446 U_LOCALE_BASED(locBased
, *this);
447 return locBased
.getLocaleID(type
, status
);
452 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */