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