1 /* |
|
2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.javadoc; |
|
27 |
|
28 import java.text.BreakIterator; |
|
29 import java.text.Collator; |
|
30 import java.util.Locale; |
|
31 |
|
32 /** |
|
33 * This class holds the information about locales. |
|
34 * |
|
35 * <p><b>This is NOT part of any supported API. |
|
36 * If you write code that depends on this, you do so at your own risk. |
|
37 * This code and its internal interfaces are subject to change or |
|
38 * deletion without notice.</b> |
|
39 * |
|
40 * @since 1.4 |
|
41 * @author Robert Field |
|
42 */ |
|
43 class DocLocale { |
|
44 |
|
45 /** |
|
46 * The locale name will be set by Main, if option is provided on the |
|
47 * command line. |
|
48 */ |
|
49 final String localeName; |
|
50 |
|
51 /** |
|
52 * The locale to be used. If user doesn't provide this, |
|
53 * then set it to default locale value. |
|
54 */ |
|
55 final Locale locale; |
|
56 |
|
57 /** |
|
58 * The collator for this application. This is to take care of Locale |
|
59 * Specific or Natural Language Text sorting. |
|
60 */ |
|
61 final Collator collator; |
|
62 |
|
63 /** |
|
64 * Enclosing DocEnv |
|
65 */ |
|
66 private final DocEnv docenv; |
|
67 |
|
68 /** |
|
69 * Sentence instance from the BreakIterator. |
|
70 */ |
|
71 private final BreakIterator sentenceBreaker; |
|
72 |
|
73 /** |
|
74 * True is we should use <code>BreakIterator</code> |
|
75 * to compute first sentence. |
|
76 */ |
|
77 private boolean useBreakIterator = false; |
|
78 |
|
79 /** |
|
80 * The HTML sentence terminators. |
|
81 */ |
|
82 static final String[] sentenceTerminators = |
|
83 { |
|
84 "<p>", "</p>", "<h1>", "<h2>", |
|
85 "<h3>", "<h4>", "<h5>", "<h6>", |
|
86 "</h1>", "</h2>", "</h3>", "</h4>", "</h5>", |
|
87 "</h6>", "<hr>", "<pre>", "</pre>" |
|
88 }; |
|
89 |
|
90 /** |
|
91 * Constructor |
|
92 */ |
|
93 DocLocale(DocEnv docenv, String localeName, boolean useBreakIterator) { |
|
94 this.docenv = docenv; |
|
95 this.localeName = localeName; |
|
96 this.useBreakIterator = useBreakIterator; |
|
97 locale = getLocale(); |
|
98 if (locale == null) { |
|
99 docenv.exit(); |
|
100 } else { |
|
101 Locale.setDefault(locale); // NOTE: updating global state |
|
102 } |
|
103 collator = Collator.getInstance(locale); |
|
104 sentenceBreaker = BreakIterator.getSentenceInstance(locale); |
|
105 } |
|
106 |
|
107 /** |
|
108 * Get the locale if specified on the command line |
|
109 * else return null and if locale option is not used |
|
110 * then return default locale. |
|
111 */ |
|
112 private Locale getLocale() { |
|
113 Locale userlocale = null; |
|
114 if (localeName.length() > 0) { |
|
115 int firstuscore = localeName.indexOf('_'); |
|
116 int seconduscore = -1; |
|
117 String language = null; |
|
118 String country = null; |
|
119 String variant = null; |
|
120 if (firstuscore == 2) { |
|
121 language = localeName.substring(0, firstuscore); |
|
122 seconduscore = localeName.indexOf('_', firstuscore + 1); |
|
123 if (seconduscore > 0) { |
|
124 if (seconduscore != firstuscore + 3 || |
|
125 localeName.length() <= seconduscore + 1) { |
|
126 docenv.error(null, "main.malformed_locale_name", localeName); |
|
127 return null; |
|
128 } |
|
129 country = localeName.substring(firstuscore + 1, |
|
130 seconduscore); |
|
131 variant = localeName.substring(seconduscore + 1); |
|
132 } else if (localeName.length() == firstuscore + 3) { |
|
133 country = localeName.substring(firstuscore + 1); |
|
134 } else { |
|
135 docenv.error(null, "main.malformed_locale_name", localeName); |
|
136 return null; |
|
137 } |
|
138 } else if (firstuscore == -1 && localeName.length() == 2) { |
|
139 language = localeName; |
|
140 } else { |
|
141 docenv.error(null, "main.malformed_locale_name", localeName); |
|
142 return null; |
|
143 } |
|
144 userlocale = searchLocale(language, country, variant); |
|
145 if (userlocale == null) { |
|
146 docenv.error(null, "main.illegal_locale_name", localeName); |
|
147 return null; |
|
148 } else { |
|
149 return userlocale; |
|
150 } |
|
151 } else { |
|
152 return Locale.getDefault(); |
|
153 } |
|
154 } |
|
155 |
|
156 /** |
|
157 * Search the locale for specified language, specified country and |
|
158 * specified variant. |
|
159 */ |
|
160 private Locale searchLocale(String language, String country, |
|
161 String variant) { |
|
162 for (Locale loc : Locale.getAvailableLocales()) { |
|
163 if (loc.getLanguage().equals(language) && |
|
164 (country == null || loc.getCountry().equals(country)) && |
|
165 (variant == null || loc.getVariant().equals(variant))) { |
|
166 return loc; |
|
167 } |
|
168 } |
|
169 return null; |
|
170 } |
|
171 |
|
172 String localeSpecificFirstSentence(DocImpl doc, String s) { |
|
173 if (s == null || s.length() == 0) { |
|
174 return ""; |
|
175 } |
|
176 int index = s.indexOf("-->"); |
|
177 if(s.trim().startsWith("<!--") && index != -1) { |
|
178 return localeSpecificFirstSentence(doc, s.substring(index + 3, s.length())); |
|
179 } |
|
180 if (useBreakIterator || !locale.getLanguage().equals("en")) { |
|
181 sentenceBreaker.setText(s.replace('\n', ' ')); |
|
182 int start = sentenceBreaker.first(); |
|
183 int end = sentenceBreaker.next(); |
|
184 return s.substring(start, end).trim(); |
|
185 } else { |
|
186 return englishLanguageFirstSentence(s).trim(); |
|
187 } |
|
188 } |
|
189 |
|
190 /** |
|
191 * Return the first sentence of a string, where a sentence ends |
|
192 * with a period followed be white space. |
|
193 */ |
|
194 private String englishLanguageFirstSentence(String s) { |
|
195 if (s == null) { |
|
196 return null; |
|
197 } |
|
198 int len = s.length(); |
|
199 boolean period = false; |
|
200 for (int i = 0 ; i < len ; i++) { |
|
201 switch (s.charAt(i)) { |
|
202 case '.': |
|
203 period = true; |
|
204 break; |
|
205 case ' ': |
|
206 case '\t': |
|
207 case '\n': |
|
208 case '\r': |
|
209 case '\f': |
|
210 if (period) { |
|
211 return s.substring(0, i); |
|
212 } |
|
213 break; |
|
214 case '<': |
|
215 if (i > 0) { |
|
216 if (htmlSentenceTerminatorFound(s, i)) { |
|
217 return s.substring(0, i); |
|
218 } |
|
219 } |
|
220 break; |
|
221 default: |
|
222 period = false; |
|
223 } |
|
224 } |
|
225 return s; |
|
226 } |
|
227 |
|
228 /** |
|
229 * Find out if there is any HTML tag in the given string. If found |
|
230 * return true else return false. |
|
231 */ |
|
232 private boolean htmlSentenceTerminatorFound(String str, int index) { |
|
233 for (String terminator : sentenceTerminators) { |
|
234 if (str.regionMatches(true, index, terminator, |
|
235 0, terminator.length())) { |
|
236 return true; |
|
237 } |
|
238 } |
|
239 return false; |
|
240 } |
|
241 } |
|