~ubuntu-branches/ubuntu/vivid/icu4j-4.4/vivid

« back to all changes in this revision

Viewing changes to main/classes/core/src/com/ibm/icu/text/BreakIteratorFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Niels Thykier
  • Date: 2011-08-02 15:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20110802155033-itjzsl21y2lqdonn
Tags: upstream-4.4.2
ImportĀ upstreamĀ versionĀ 4.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *******************************************************************************
 
3
 * Copyright (C) 2002-2010, International Business Machines Corporation and    *
 
4
 * others. All Rights Reserved.                                                *
 
5
 *******************************************************************************
 
6
 */
 
7
package com.ibm.icu.text;
 
8
 
 
9
import java.io.IOException;
 
10
import java.io.InputStream;
 
11
import java.util.Locale;
 
12
import java.util.MissingResourceException;
 
13
 
 
14
import com.ibm.icu.impl.Assert;
 
15
import com.ibm.icu.impl.ICUData;
 
16
import com.ibm.icu.impl.ICULocaleService;
 
17
import com.ibm.icu.impl.ICUResourceBundle;
 
18
import com.ibm.icu.impl.ICUService;
 
19
import com.ibm.icu.impl.ICUService.Factory;
 
20
import com.ibm.icu.util.ULocale;
 
21
import com.ibm.icu.util.UResourceBundle;
 
22
 
 
23
/**
 
24
 * @author Ram
 
25
 *
 
26
 * To change this generated comment edit the template variable "typecomment":
 
27
 * Window>Preferences>Java>Templates.
 
28
 * To enable and disable the creation of type comments go to
 
29
 * Window>Preferences>Java>Code Generation.
 
30
 */
 
31
final class BreakIteratorFactory extends BreakIterator.BreakIteratorServiceShim {
 
32
 
 
33
    public Object registerInstance(BreakIterator iter, ULocale locale, int kind) {
 
34
        iter.setText(new java.text.StringCharacterIterator(""));
 
35
        return service.registerObject(iter, locale, kind);
 
36
    }
 
37
 
 
38
    public boolean unregister(Object key) {
 
39
        if (service.isDefault()) {
 
40
            return false;
 
41
        }
 
42
        return service.unregisterFactory((Factory)key);
 
43
    }
 
44
 
 
45
    public Locale[] getAvailableLocales() {
 
46
        if (service == null) {
 
47
            return ICUResourceBundle.getAvailableLocales();
 
48
        } else {
 
49
            return service.getAvailableLocales();
 
50
        }
 
51
    }
 
52
 
 
53
    public ULocale[] getAvailableULocales() {
 
54
        if (service == null) {
 
55
            return ICUResourceBundle.getAvailableULocales();
 
56
        } else {
 
57
            return service.getAvailableULocales();
 
58
        }
 
59
    }
 
60
 
 
61
    public BreakIterator createBreakIterator(ULocale locale, int kind) {
 
62
    // TODO: convert to ULocale when service switches over
 
63
        if (service.isDefault()) {
 
64
            return createBreakInstance(locale, kind);
 
65
        }
 
66
        ULocale[] actualLoc = new ULocale[1];
 
67
        BreakIterator iter = (BreakIterator)service.get(locale, kind, actualLoc);
 
68
        iter.setLocale(actualLoc[0], actualLoc[0]); // services make no distinction between actual & valid
 
69
        return iter;
 
70
    }
 
71
 
 
72
    private static class BFService extends ICULocaleService {
 
73
        BFService() {
 
74
            super("BreakIterator");
 
75
 
 
76
            class RBBreakIteratorFactory extends ICUResourceBundleFactory {
 
77
                protected Object handleCreate(ULocale loc, int kind, ICUService srvc) {
 
78
                    return createBreakInstance(loc, kind);
 
79
                }
 
80
            }
 
81
            registerFactory(new RBBreakIteratorFactory());
 
82
 
 
83
            markDefault();
 
84
        }
 
85
    }
 
86
    static final ICULocaleService service = new BFService();
 
87
 
 
88
 
 
89
    /** KIND_NAMES are the resource key to be used to fetch the name of the
 
90
     *             pre-compiled break rules.  The resource bundle name is "boundaries".
 
91
     *             The value for each key will be the rules to be used for the
 
92
     *             specified locale - "word" -> "word_th" for Thai, for example.
 
93
     *  DICTIONARY_POSSIBLE indexes in the same way, and indicates whether a
 
94
     *             dictionary is a possibility for that type of break.  This is just
 
95
     *             an optimization to avoid a resource lookup where no dictionary is
 
96
     *             ever possible.
 
97
     */
 
98
    private static final String[] KIND_NAMES = {
 
99
            "grapheme", "word", "line", "sentence", "title"
 
100
        };
 
101
    private static final boolean[] DICTIONARY_POSSIBLE = {
 
102
            false,      true,  true,   false,     false
 
103
    };
 
104
 
 
105
 
 
106
    private static BreakIterator createBreakInstance(ULocale locale, int kind) {
 
107
 
 
108
        BreakIterator    iter       = null;
 
109
        ICUResourceBundle rb        = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BRKITR_BASE_NAME, locale);
 
110
        
 
111
        //
 
112
        //  Get the binary rules.  These are needed for both normal RulesBasedBreakIterators
 
113
        //                         and for Dictionary iterators.
 
114
        //
 
115
        InputStream      ruleStream = null;
 
116
        try {
 
117
            String         typeKey       = KIND_NAMES[kind];
 
118
            String         brkfname      = rb.getStringWithFallback("boundaries/" + typeKey);
 
119
            String         rulesFileName = ICUResourceBundle.ICU_BUNDLE +ICUResourceBundle.ICU_BRKITR_NAME+ "/" + brkfname;
 
120
                           ruleStream    = ICUData.getStream(rulesFileName);
 
121
        }
 
122
        catch (Exception e) {
 
123
            throw new MissingResourceException(e.toString(),"","");
 
124
        }
 
125
 
 
126
        //
 
127
        //  Check whether a dictionary exists, and create a DBBI iterator is
 
128
        //   one does.
 
129
        //
 
130
        if (DICTIONARY_POSSIBLE[kind]) {
 
131
            // This type of break iterator could potentially use a dictionary.
 
132
            //
 
133
            try {
 
134
                if (locale.getLanguage().equals("th")){
 
135
                    // If the language is Thai, load the thai compact trie dictionary.
 
136
                    String dictType = "Thai";
 
137
                    String dictFileName = rb.getStringWithFallback("dictionaries/" + dictType);
 
138
                    dictFileName = ICUResourceBundle.ICU_BUNDLE +ICUResourceBundle.ICU_BRKITR_NAME+ "/" + dictFileName;
 
139
                    InputStream is = ICUData.getStream(dictFileName);
 
140
                    iter = new ThaiBreakIterator(ruleStream, is);
 
141
                }
 
142
            } catch (MissingResourceException e) {
 
143
                //  Couldn't find a dictionary.
 
144
                //  This is normal, and will occur whenever creating a word or line
 
145
                //  break iterator for a locale that does not have a BreakDictionaryData
 
146
                //  resource - meaning for all but Thai.
 
147
                //  Fall through to creating a normal RulebasedBreakIterator.
 
148
            } catch (IOException e) {
 
149
                Assert.fail(e);
 
150
            }
 
151
         }
 
152
 
 
153
        if (iter == null) {
 
154
            //
 
155
            // Create a normal RuleBasedBreakIterator.
 
156
            //    We have determined that this is not supposed to be a dictionary iterator.
 
157
            //
 
158
            try {
 
159
                iter = RuleBasedBreakIterator.getInstanceFromCompiledRules(ruleStream);
 
160
            }
 
161
            catch (IOException e) {
 
162
                // Shouldn't be possible to get here.
 
163
                // If it happens, the compiled rules are probably corrupted in some way.
 
164
                Assert.fail(e);
 
165
           }
 
166
        }
 
167
        // TODO: Determine valid and actual locale correctly.
 
168
        ULocale uloc = ULocale.forLocale(rb.getLocale());
 
169
        iter.setLocale(uloc, uloc);
 
170
        
 
171
        return iter;
 
172
 
 
173
    }
 
174
 
 
175
}