0N/A/*
553N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.javadoc;
0N/A
0N/Aimport java.util.Locale;
0N/Aimport java.util.HashSet;
0N/Aimport java.text.Collator;
0N/Aimport java.text.BreakIterator;
0N/A
0N/A/**
0N/A * This class holds the information about locales.
0N/A *
0N/A * @since 1.4
0N/A * @author Robert Field
0N/A */
0N/Aclass DocLocale {
0N/A
0N/A /**
0N/A * The locale name will be set by Main, if option is provided on the
0N/A * command line.
0N/A */
0N/A final String localeName;
0N/A
0N/A /**
0N/A * The locale to be used. If user doesen't provide this,
0N/A * then set it to default locale value.
0N/A */
0N/A final Locale locale;
0N/A
0N/A /**
0N/A * The collator for this application. This is to take care of Locale
0N/A * Specific or Natural Language Text sorting.
0N/A */
0N/A final Collator collator;
0N/A
0N/A /**
0N/A * Enclosing DocEnv
0N/A */
0N/A private final DocEnv docenv;
0N/A
0N/A /**
0N/A * Sentence instance from the BreakIterator.
0N/A */
0N/A private final BreakIterator sentenceBreaker;
0N/A
0N/A /**
0N/A * True is we should use <code>BreakIterator</code>
0N/A * to compute first sentence.
0N/A */
0N/A private boolean useBreakIterator = false;
0N/A
0N/A /**
0N/A * The HTML sentence terminators.
0N/A */
0N/A static final String[] sentenceTerminators =
0N/A {
0N/A "<p>", "</p>", "<h1>", "<h2>",
0N/A "<h3>", "<h4>", "<h5>", "<h6>",
0N/A "</h1>", "</h2>", "</h3>", "</h4>", "</h5>",
0N/A "</h6>", "<hr>", "<pre>", "</pre>"
0N/A };
0N/A
0N/A /**
0N/A * Constructor
0N/A */
0N/A DocLocale(DocEnv docenv, String localeName, boolean useBreakIterator) {
0N/A this.docenv = docenv;
0N/A this.localeName = localeName;
0N/A this.useBreakIterator = useBreakIterator;
0N/A locale = getLocale();
0N/A if (locale == null) {
0N/A docenv.exit();
0N/A } else {
0N/A Locale.setDefault(locale);
0N/A }
0N/A collator = Collator.getInstance(locale);
0N/A sentenceBreaker = BreakIterator.getSentenceInstance(locale);
0N/A }
0N/A
0N/A /**
0N/A * Get the locale if specified on the command line
0N/A * else return null and if locale option is not used
0N/A * then return default locale.
0N/A */
0N/A private Locale getLocale() {
0N/A Locale userlocale = null;
0N/A if (localeName.length() > 0) {
0N/A int firstuscore = localeName.indexOf('_');
0N/A int seconduscore = -1;
0N/A String language = null;
0N/A String country = null;
0N/A String variant = null;
0N/A if (firstuscore == 2) {
0N/A language = localeName.substring(0, firstuscore);
0N/A seconduscore = localeName.indexOf('_', firstuscore + 1);
0N/A if (seconduscore > 0) {
0N/A if (seconduscore != firstuscore + 3 ||
0N/A localeName.length() <= seconduscore + 1) {
0N/A docenv.error(null, "main.malformed_locale_name", localeName);
0N/A return null;
0N/A }
0N/A country = localeName.substring(firstuscore + 1,
0N/A seconduscore);
0N/A variant = localeName.substring(seconduscore + 1);
0N/A } else if (localeName.length() == firstuscore + 3) {
0N/A country = localeName.substring(firstuscore + 1);
0N/A } else {
0N/A docenv.error(null, "main.malformed_locale_name", localeName);
0N/A return null;
0N/A }
0N/A } else if (firstuscore == -1 && localeName.length() == 2) {
0N/A language = localeName;
0N/A } else {
0N/A docenv.error(null, "main.malformed_locale_name", localeName);
0N/A return null;
0N/A }
0N/A userlocale = searchLocale(language, country, variant);
0N/A if (userlocale == null) {
0N/A docenv.error(null, "main.illegal_locale_name", localeName);
0N/A return null;
0N/A } else {
0N/A return userlocale;
0N/A }
0N/A } else {
0N/A return Locale.getDefault();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Search the locale for specified language, specified country and
0N/A * specified variant.
0N/A */
0N/A private Locale searchLocale(String language, String country,
0N/A String variant) {
0N/A Locale[] locales = Locale.getAvailableLocales();
0N/A for (int i = 0; i < locales.length; i++) {
0N/A if (locales[i].getLanguage().equals(language) &&
0N/A (country == null || locales[i].getCountry().equals(country)) &&
0N/A (variant == null || locales[i].getVariant().equals(variant))) {
0N/A return locales[i];
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A String localeSpecificFirstSentence(DocImpl doc, String s) {
0N/A if (s == null || s.length() == 0) {
0N/A return "";
0N/A }
0N/A int index = s.indexOf("-->");
0N/A if(s.trim().startsWith("<!--") && index != -1) {
0N/A return localeSpecificFirstSentence(doc, s.substring(index + 3, s.length()));
0N/A }
0N/A if (useBreakIterator || !locale.getLanguage().equals("en")) {
0N/A sentenceBreaker.setText(s.replace('\n', ' '));
0N/A int start = sentenceBreaker.first();
0N/A int end = sentenceBreaker.next();
0N/A return s.substring(start, end).trim();
0N/A } else {
0N/A return englishLanguageFirstSentence(s).trim();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the first sentence of a string, where a sentence ends
0N/A * with a period followed be white space.
0N/A */
0N/A private String englishLanguageFirstSentence(String s) {
0N/A if (s == null) {
0N/A return null;
0N/A }
0N/A int len = s.length();
0N/A boolean period = false;
0N/A for (int i = 0 ; i < len ; i++) {
0N/A switch (s.charAt(i)) {
0N/A case '.':
0N/A period = true;
0N/A break;
0N/A case ' ':
0N/A case '\t':
0N/A case '\n':
0N/A case '\r':
0N/A case '\f':
0N/A if (period) {
0N/A return s.substring(0, i);
0N/A }
0N/A break;
0N/A case '<':
0N/A if (i > 0) {
0N/A if (htmlSentenceTerminatorFound(s, i)) {
0N/A return s.substring(0, i);
0N/A }
0N/A }
0N/A break;
0N/A default:
0N/A period = false;
0N/A }
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A /**
0N/A * Find out if there is any HTML tag in the given string. If found
0N/A * return true else return false.
0N/A */
0N/A private boolean htmlSentenceTerminatorFound(String str, int index) {
0N/A for (int i = 0; i < sentenceTerminators.length; i++) {
0N/A String terminator = sentenceTerminators[i];
0N/A if (str.regionMatches(true, index, terminator,
0N/A 0, terminator.length())) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A}