author | jjg |
Fri, 27 May 2016 13:06:58 -0700 | |
changeset 38617 | d93a7f64e231 |
parent 37938 | 42baa89d2156 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
14258 | 2 |
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
37938
42baa89d2156
8075703: jdk.javadoc module exports com.sun.tools.javadoc package which contains a lot of internal API.
jjg
parents:
25874
diff
changeset
|
26 |
package com.sun.tools.javadoc.main; |
10 | 27 |
|
14258 | 28 |
import java.text.BreakIterator; |
29 |
import java.text.Collator; |
|
10 | 30 |
import java.util.Locale; |
31 |
||
32 |
/** |
|
33 |
* This class holds the information about locales. |
|
34 |
* |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
35 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
36 |
* If you write code that depends on this, you do so at your own risk. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
37 |
* This code and its internal interfaces are subject to change or |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
38 |
* deletion without notice.</b> |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
39 |
* |
10 | 40 |
* @since 1.4 |
41 |
* @author Robert Field |
|
42 |
*/ |
|
38617 | 43 |
@Deprecated |
10 | 44 |
class DocLocale { |
45 |
||
46 |
/** |
|
47 |
* The locale name will be set by Main, if option is provided on the |
|
48 |
* command line. |
|
49 |
*/ |
|
50 |
final String localeName; |
|
51 |
||
52 |
/** |
|
14543 | 53 |
* The locale to be used. If user doesn't provide this, |
10 | 54 |
* then set it to default locale value. |
55 |
*/ |
|
56 |
final Locale locale; |
|
57 |
||
58 |
/** |
|
59 |
* The collator for this application. This is to take care of Locale |
|
60 |
* Specific or Natural Language Text sorting. |
|
61 |
*/ |
|
62 |
final Collator collator; |
|
63 |
||
64 |
/** |
|
65 |
* Enclosing DocEnv |
|
66 |
*/ |
|
67 |
private final DocEnv docenv; |
|
68 |
||
69 |
/** |
|
70 |
* Sentence instance from the BreakIterator. |
|
71 |
*/ |
|
72 |
private final BreakIterator sentenceBreaker; |
|
73 |
||
74 |
/** |
|
75 |
* True is we should use <code>BreakIterator</code> |
|
76 |
* to compute first sentence. |
|
77 |
*/ |
|
78 |
private boolean useBreakIterator = false; |
|
79 |
||
80 |
/** |
|
81 |
* The HTML sentence terminators. |
|
82 |
*/ |
|
83 |
static final String[] sentenceTerminators = |
|
84 |
{ |
|
85 |
"<p>", "</p>", "<h1>", "<h2>", |
|
86 |
"<h3>", "<h4>", "<h5>", "<h6>", |
|
87 |
"</h1>", "</h2>", "</h3>", "</h4>", "</h5>", |
|
88 |
"</h6>", "<hr>", "<pre>", "</pre>" |
|
89 |
}; |
|
90 |
||
91 |
/** |
|
92 |
* Constructor |
|
93 |
*/ |
|
94 |
DocLocale(DocEnv docenv, String localeName, boolean useBreakIterator) { |
|
95 |
this.docenv = docenv; |
|
96 |
this.localeName = localeName; |
|
97 |
this.useBreakIterator = useBreakIterator; |
|
98 |
locale = getLocale(); |
|
99 |
if (locale == null) { |
|
100 |
docenv.exit(); |
|
101 |
} else { |
|
14543 | 102 |
Locale.setDefault(locale); // NOTE: updating global state |
10 | 103 |
} |
104 |
collator = Collator.getInstance(locale); |
|
105 |
sentenceBreaker = BreakIterator.getSentenceInstance(locale); |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Get the locale if specified on the command line |
|
110 |
* else return null and if locale option is not used |
|
111 |
* then return default locale. |
|
112 |
*/ |
|
113 |
private Locale getLocale() { |
|
114 |
Locale userlocale = null; |
|
115 |
if (localeName.length() > 0) { |
|
116 |
int firstuscore = localeName.indexOf('_'); |
|
117 |
int seconduscore = -1; |
|
118 |
String language = null; |
|
119 |
String country = null; |
|
120 |
String variant = null; |
|
121 |
if (firstuscore == 2) { |
|
122 |
language = localeName.substring(0, firstuscore); |
|
123 |
seconduscore = localeName.indexOf('_', firstuscore + 1); |
|
124 |
if (seconduscore > 0) { |
|
125 |
if (seconduscore != firstuscore + 3 || |
|
126 |
localeName.length() <= seconduscore + 1) { |
|
127 |
docenv.error(null, "main.malformed_locale_name", localeName); |
|
128 |
return null; |
|
129 |
} |
|
130 |
country = localeName.substring(firstuscore + 1, |
|
131 |
seconduscore); |
|
132 |
variant = localeName.substring(seconduscore + 1); |
|
133 |
} else if (localeName.length() == firstuscore + 3) { |
|
134 |
country = localeName.substring(firstuscore + 1); |
|
135 |
} else { |
|
136 |
docenv.error(null, "main.malformed_locale_name", localeName); |
|
137 |
return null; |
|
138 |
} |
|
139 |
} else if (firstuscore == -1 && localeName.length() == 2) { |
|
140 |
language = localeName; |
|
141 |
} else { |
|
142 |
docenv.error(null, "main.malformed_locale_name", localeName); |
|
143 |
return null; |
|
144 |
} |
|
145 |
userlocale = searchLocale(language, country, variant); |
|
146 |
if (userlocale == null) { |
|
147 |
docenv.error(null, "main.illegal_locale_name", localeName); |
|
148 |
return null; |
|
149 |
} else { |
|
150 |
return userlocale; |
|
151 |
} |
|
152 |
} else { |
|
153 |
return Locale.getDefault(); |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Search the locale for specified language, specified country and |
|
159 |
* specified variant. |
|
160 |
*/ |
|
161 |
private Locale searchLocale(String language, String country, |
|
162 |
String variant) { |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14543
diff
changeset
|
163 |
for (Locale loc : Locale.getAvailableLocales()) { |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14543
diff
changeset
|
164 |
if (loc.getLanguage().equals(language) && |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14543
diff
changeset
|
165 |
(country == null || loc.getCountry().equals(country)) && |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14543
diff
changeset
|
166 |
(variant == null || loc.getVariant().equals(variant))) { |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14543
diff
changeset
|
167 |
return loc; |
10 | 168 |
} |
169 |
} |
|
170 |
return null; |
|
171 |
} |
|
172 |
||
173 |
String localeSpecificFirstSentence(DocImpl doc, String s) { |
|
174 |
if (s == null || s.length() == 0) { |
|
175 |
return ""; |
|
176 |
} |
|
177 |
int index = s.indexOf("-->"); |
|
178 |
if(s.trim().startsWith("<!--") && index != -1) { |
|
179 |
return localeSpecificFirstSentence(doc, s.substring(index + 3, s.length())); |
|
180 |
} |
|
181 |
if (useBreakIterator || !locale.getLanguage().equals("en")) { |
|
182 |
sentenceBreaker.setText(s.replace('\n', ' ')); |
|
183 |
int start = sentenceBreaker.first(); |
|
184 |
int end = sentenceBreaker.next(); |
|
185 |
return s.substring(start, end).trim(); |
|
186 |
} else { |
|
187 |
return englishLanguageFirstSentence(s).trim(); |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Return the first sentence of a string, where a sentence ends |
|
193 |
* with a period followed be white space. |
|
194 |
*/ |
|
195 |
private String englishLanguageFirstSentence(String s) { |
|
196 |
if (s == null) { |
|
197 |
return null; |
|
198 |
} |
|
199 |
int len = s.length(); |
|
200 |
boolean period = false; |
|
201 |
for (int i = 0 ; i < len ; i++) { |
|
202 |
switch (s.charAt(i)) { |
|
203 |
case '.': |
|
204 |
period = true; |
|
205 |
break; |
|
206 |
case ' ': |
|
207 |
case '\t': |
|
208 |
case '\n': |
|
209 |
case '\r': |
|
210 |
case '\f': |
|
211 |
if (period) { |
|
212 |
return s.substring(0, i); |
|
213 |
} |
|
214 |
break; |
|
215 |
case '<': |
|
216 |
if (i > 0) { |
|
217 |
if (htmlSentenceTerminatorFound(s, i)) { |
|
218 |
return s.substring(0, i); |
|
219 |
} |
|
220 |
} |
|
221 |
break; |
|
222 |
default: |
|
223 |
period = false; |
|
224 |
} |
|
225 |
} |
|
226 |
return s; |
|
227 |
} |
|
228 |
||
229 |
/** |
|
230 |
* Find out if there is any HTML tag in the given string. If found |
|
231 |
* return true else return false. |
|
232 |
*/ |
|
233 |
private boolean htmlSentenceTerminatorFound(String str, int index) { |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14543
diff
changeset
|
234 |
for (String terminator : sentenceTerminators) { |
10 | 235 |
if (str.regionMatches(true, index, terminator, |
236 |
0, terminator.length())) { |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14543
diff
changeset
|
237 |
return true; |
10 | 238 |
} |
239 |
} |
|
240 |
return false; |
|
241 |
} |
|
242 |
} |