Fri, 30 Nov 2012 15:14:48 +0000
8002099: Add support for intersection types in cast expression
Summary: Add parser and type-checking support for intersection types in cast expressions
Reviewed-by: jjg
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 */
26 package com.sun.tools.javadoc;
28 import java.text.BreakIterator;
29 import java.text.Collator;
30 import java.util.Locale;
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 {
45 /**
46 * The locale name will be set by Main, if option is provided on the
47 * command line.
48 */
49 final String localeName;
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;
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;
63 /**
64 * Enclosing DocEnv
65 */
66 private final DocEnv docenv;
68 /**
69 * Sentence instance from the BreakIterator.
70 */
71 private final BreakIterator sentenceBreaker;
73 /**
74 * True is we should use <code>BreakIterator</code>
75 * to compute first sentence.
76 */
77 private boolean useBreakIterator = false;
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 };
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 }
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 }
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 Locale[] locales = Locale.getAvailableLocales();
163 for (int i = 0; i < locales.length; i++) {
164 if (locales[i].getLanguage().equals(language) &&
165 (country == null || locales[i].getCountry().equals(country)) &&
166 (variant == null || locales[i].getVariant().equals(variant))) {
167 return locales[i];
168 }
169 }
170 return null;
171 }
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 }
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 }
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) {
234 for (int i = 0; i < sentenceTerminators.length; i++) {
235 String terminator = sentenceTerminators[i];
236 if (str.regionMatches(true, index, terminator,
237 0, terminator.length())) {
238 return true;
239 }
240 }
241 return false;
242 }
243 }