Source Code Cross Referenced for BreakIteratorFactory.java in  » Internationalization-Localization » icu4j » com » ibm » icu » text » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » icu4j » com.ibm.icu.text 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *******************************************************************************
003:         * Copyright (C) 2002-2006, International Business Machines Corporation and    *
004:         * others. All Rights Reserved.                                                *
005:         *******************************************************************************
006:         */
007:        package com.ibm.icu.text;
008:
009:        import java.io.IOException;
010:        import java.io.InputStream;
011:        import java.io.ByteArrayInputStream;
012:
013:        import java.util.Locale;
014:        import java.util.MissingResourceException;
015:        import java.util.ResourceBundle;
016:
017:        import com.ibm.icu.impl.ICUData;
018:        import com.ibm.icu.impl.ICULocaleData;
019:        import com.ibm.icu.impl.ICULocaleService;
020:        import com.ibm.icu.impl.ICUResourceBundle;
021:        import com.ibm.icu.impl.ICUService;
022:        import com.ibm.icu.impl.ICUService.Factory;
023:        import com.ibm.icu.util.ULocale;
024:        import com.ibm.icu.util.UResourceBundle;
025:        import com.ibm.icu.impl.Assert;
026:
027:        /**
028:         * @author Ram
029:         *
030:         * To change this generated comment edit the template variable "typecomment":
031:         * Window>Preferences>Java>Templates.
032:         * To enable and disable the creation of type comments go to
033:         * Window>Preferences>Java>Code Generation.
034:         */
035:        final class BreakIteratorFactory extends
036:                BreakIterator.BreakIteratorServiceShim {
037:
038:            public Object registerInstance(BreakIterator iter, ULocale locale,
039:                    int kind) {
040:                iter.setText(new java.text.StringCharacterIterator(""));
041:                return service.registerObject(iter, locale, kind);
042:            }
043:
044:            public boolean unregister(Object key) {
045:                if (service.isDefault()) {
046:                    return false;
047:                }
048:                return service.unregisterFactory((Factory) key);
049:            }
050:
051:            public Locale[] getAvailableLocales() {
052:                if (service == null) {
053:                    return ICUResourceBundle
054:                            .getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME);
055:                } else {
056:                    return service.getAvailableLocales();
057:                }
058:            }
059:
060:            public ULocale[] getAvailableULocales() {
061:                if (service == null) {
062:                    return ICUResourceBundle
063:                            .getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME);
064:                } else {
065:                    return service.getAvailableULocales();
066:                }
067:            }
068:
069:            public BreakIterator createBreakIterator(ULocale locale, int kind) {
070:                // TODO: convert to ULocale when service switches over
071:                if (service.isDefault()) {
072:                    return createBreakInstance(locale, kind);
073:                }
074:                ULocale[] actualLoc = new ULocale[1];
075:                BreakIterator iter = (BreakIterator) service.get(locale, kind,
076:                        actualLoc);
077:                iter.setLocale(actualLoc[0], actualLoc[0]); // services make no distinction between actual & valid
078:                return iter;
079:            }
080:
081:            private static class BFService extends ICULocaleService {
082:                BFService() {
083:                    super ("BreakIterator");
084:
085:                    class RBBreakIteratorFactory extends
086:                            ICUResourceBundleFactory {
087:                        protected Object handleCreate(ULocale loc, int kind,
088:                                ICUService service) {
089:                            return createBreakInstance(loc, kind);
090:                        }
091:                    }
092:                    registerFactory(new RBBreakIteratorFactory());
093:
094:                    markDefault();
095:                }
096:            }
097:
098:            static final ICULocaleService service = new BFService();
099:
100:            /** KIND_NAMES are the resource key to be used to fetch the name of the
101:             *             pre-compiled break rules.  The resource bundle name is "boundaries".
102:             *             The value for each key will be the rules to be used for the
103:             *             specified locale - "word" -> "word_th" for Thai, for example.
104:             *  DICTIONARY_POSSIBLE indexes in the same way, and indicates whether a
105:             *             dictionary is a possibility for that type of break.  This is just
106:             *             an optimization to avoid a resource lookup where no dictionary is
107:             *             ever possible.
108:             *  @internal
109:             */
110:            private static final String[] KIND_NAMES = { "grapheme", "word",
111:                    "line", "sentence", "title" };
112:            private static final boolean[] DICTIONARY_POSSIBLE = { false, true,
113:                    true, false, false };
114:
115:            private static BreakIterator createBreakInstance(ULocale locale,
116:                    int kind) {
117:
118:                BreakIterator iter = null;
119:                UResourceBundle rb = UResourceBundle.getBundleInstance(
120:                        ICUResourceBundle.ICU_BRKITR_BASE_NAME, locale);
121:
122:                //
123:                //  Get the binary rules.  These are needed for both normal RulesBasedBreakIterators
124:                //                         and for Dictionary iterators.
125:                //
126:                InputStream ruleStream = null;
127:                try {
128:                    ResourceBundle boundaries = (ResourceBundle) rb
129:                            .getObject("boundaries");
130:                    String typeKey = KIND_NAMES[kind];
131:                    String brkfname = boundaries.getString(typeKey);
132:                    String rulesFileName = ICUResourceBundle.ICU_BUNDLE
133:                            + ICUResourceBundle.ICU_BRKITR_NAME + "/"
134:                            + brkfname;
135:                    ruleStream = ICUData.getStream(rulesFileName);
136:                } catch (Exception e) {
137:                    throw new MissingResourceException(e.toString(), "", "");
138:                }
139:
140:                //
141:                //  Check whether a dictionary exists, and create a DBBI iterator is
142:                //   one does.
143:                //
144:                if (DICTIONARY_POSSIBLE[kind]) {
145:                    // This type of break iterator could potentially use a dictionary.
146:                    //
147:                    try {
148:                        //ICUResourceBundle dictRes = (ICUResourceBundle)rb.getObject("BreakDictionaryData");
149:                        //byte[] dictBytes = null;
150:                        //dictBytes = dictRes.getBinary(dictBytes);
151:                        //TODO: Hard code this for now! fix it once CompactTrieDictionary is ported
152:                        if (locale.equals("th")) {
153:                            String fileName = "data/th.brk";
154:                            InputStream is = ICUData.getStream(fileName);
155:                            iter = new DictionaryBasedBreakIterator(ruleStream,
156:                                    is);
157:                        }
158:                    } catch (MissingResourceException e) {
159:                        //  Couldn't find a dictionary.
160:                        //  This is normal, and will occur whenever creating a word or line
161:                        //  break iterator for a locale that does not have a BreakDictionaryData
162:                        //  resource - meaning for all but Thai.
163:                        //  Fall through to creating a normal RulebasedBreakIterator.
164:                    } catch (IOException e) {
165:                        Assert.fail(e);
166:                    }
167:                }
168:
169:                if (iter == null) {
170:                    //
171:                    // Create a normal RuleBasedBreakIterator.
172:                    //    We have determined that this is not supposed to be a dictionary iterator.
173:                    //
174:                    try {
175:                        iter = RuleBasedBreakIterator
176:                                .getInstanceFromCompiledRules(ruleStream);
177:                    } catch (IOException e) {
178:                        // Shouldn't be possible to get here.
179:                        // If it happens, the compiled rules are probably corrupted in some way.
180:                        Assert.fail(e);
181:                    }
182:                }
183:                // TODO: Determine valid and actual locale correctly.
184:                ULocale uloc = ULocale.forLocale(rb.getLocale());
185:                iter.setLocale(uloc, uloc);
186:
187:                return iter;
188:
189:            }
190:
191:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.