Skip to content

Commit

Permalink
API cleanup: prepare moving all static methods and fields from Langua…
Browse files Browse the repository at this point in the history
…ge to a new class Languages - for now, keep the deprecated methods/fields
  • Loading branch information
danielnaber committed Feb 23, 2015
1 parent 908d3ac commit 88c1ed0
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 37 deletions.
38 changes: 12 additions & 26 deletions languagetool-core/src/main/java/org/languagetool/Language.java
Expand Up @@ -59,7 +59,7 @@ public abstract class Language {


private static final String PROPERTIES_PATH = "META-INF/org/languagetool/language-module.properties"; private static final String PROPERTIES_PATH = "META-INF/org/languagetool/language-module.properties";
private static final String PROPERTIES_KEY = "languageClasses"; private static final String PROPERTIES_KEY = "languageClasses";

private static List<Language> externalLanguages = new ArrayList<>(); private static List<Language> externalLanguages = new ArrayList<>();


private final List<String> externalRuleFiles = new ArrayList<>(); private final List<String> externalRuleFiles = new ArrayList<>();
Expand All @@ -71,9 +71,11 @@ public abstract class Language {
/** /**
* All languages supported by LanguageTool. This includes at least a "demo" language * All languages supported by LanguageTool. This includes at least a "demo" language
* for testing. * for testing.
* @deprecated use {@link Languages#getWithDemoLanguage()} instead (deprecated since 2.9)
*/ */
public static Language[] LANGUAGES = getLanguages(); public static Language[] LANGUAGES = getLanguages();


// TODO: remove once LANGUAGES is removed
private static Language[] getLanguages() { private static Language[] getLanguages() {
final List<Language> languages = new ArrayList<>(); final List<Language> languages = new ArrayList<>();
final Set<String> languageClassNames = new HashSet<>(); final Set<String> languageClassNames = new HashSet<>();
Expand Down Expand Up @@ -124,13 +126,15 @@ private static Language createLanguageObjects(URL url, String className) {


/** /**
* All languages supported by LanguageTool, but without the demo language. * All languages supported by LanguageTool, but without the demo language.
* @deprecated use {@link Languages#get()} instead (deprecated since 2.9)
*/ */
public static final Language[] REAL_LANGUAGES = getRealLanguages(); public static final Language[] REAL_LANGUAGES = getRealLanguages();


/** /**
* Returns all languages supported by LanguageTool but without the demo language. * Returns all languages supported by LanguageTool but without the demo language.
* In contrast to Language.REAL_LANGUAGES contains external languages as well. * In contrast to Language.REAL_LANGUAGES contains external languages as well.
* @return All supported languages. * @return All supported languages.
* @deprecated use {@link Languages#get()} instead (deprecated since 2.9)
* @since 2.6 * @since 2.6
*/ */
public static Language[] getRealLanguages() { public static Language[] getRealLanguages() {
Expand Down Expand Up @@ -447,7 +451,6 @@ public final String getShortNameWithCountryAndVariant() {
* starting symbols must match exactly the sequence of ending symbols. * starting symbols must match exactly the sequence of ending symbols.
* @deprecated will be moved to GenericUnpairedBracketsRule (deprecated since 2.8) * @deprecated will be moved to GenericUnpairedBracketsRule (deprecated since 2.8)
*/ */
@Deprecated
public String[] getUnpairedRuleStartSymbols() { public String[] getUnpairedRuleStartSymbols() {
return new String[]{ "[", "(", "{", "\"", "'" }; return new String[]{ "[", "(", "{", "\"", "'" };
} }
Expand All @@ -457,7 +460,6 @@ public String[] getUnpairedRuleStartSymbols() {
* @deprecated will be moved to GenericUnpairedBracketsRule (deprecated since 2.8) * @deprecated will be moved to GenericUnpairedBracketsRule (deprecated since 2.8)
* @see #getUnpairedRuleStartSymbols() * @see #getUnpairedRuleStartSymbols()
*/ */
@Deprecated
public String[] getUnpairedRuleEndSymbols() { public String[] getUnpairedRuleEndSymbols() {
return new String[]{ "]", ")", "}", "\"", "'" }; return new String[]{ "]", ")", "}", "\"", "'" };
} }
Expand Down Expand Up @@ -488,6 +490,7 @@ synchronized List<PatternRule> getPatternRules() throws IOException {


/** /**
* Re-inits the built-in languages and adds the specified ones. * Re-inits the built-in languages and adds the specified ones.
* @deprecated (deprecated since 2.9)
*/ */
public static void reInit(final List<Language> languages) { public static void reInit(final List<Language> languages) {
LANGUAGES = new Language[BUILTIN_LANGUAGES.length + languages.size()]; LANGUAGES = new Language[BUILTIN_LANGUAGES.length + languages.size()];
Expand All @@ -502,14 +505,14 @@ public static void reInit(final List<Language> languages) {


/** /**
* Return languages that are not built-in but have been added manually. * Return languages that are not built-in but have been added manually.
* @deprecated
*/ */
public static List<Language> getExternalLanguages() { public static List<Language> getExternalLanguages() {
return externalLanguages; return externalLanguages;
} }


/** /**
* Return all languages supported by LanguageTool. * @deprecated use {@link Languages#get()} but note that is has slightly different semantics (no external languages) (deprecated since 2.9)
* @return A list of all languages, including external ones and country variants (e.g. {@code en-US})
*/ */
public static List<Language> getAllLanguages() { public static List<Language> getAllLanguages() {
final List<Language> langList = new ArrayList<>(); final List<Language> langList = new ArrayList<>();
Expand All @@ -519,10 +522,7 @@ public static List<Language> getAllLanguages() {
} }


/** /**
* Get the Language object for the given language name. * @deprecated use {@link Languages#getLanguageForName(String)} (deprecated since 2.9)
*
* @param languageName e.g. <code>English</code> or <code>German</code> (case is significant)
* @return a Language object or {@code null} if there is no such language
*/ */
@Nullable @Nullable
public static Language getLanguageForName(final String languageName) { public static Language getLanguageForName(final String languageName) {
Expand All @@ -535,11 +535,7 @@ public static Language getLanguageForName(final String languageName) {
} }


/** /**
* Get the Language object for the given short language name. * @deprecated use {@link Languages#getLanguageForShortName(String)} (deprecated since 2.9)
*
* @param langCode e.g. <code>en</code> or <code>es-US</code>
* @return a Language object
* @throws IllegalArgumentException if the language is not supported or if the language code is invalid
*/ */
public static Language getLanguageForShortName(final String langCode) { public static Language getLanguageForShortName(final String langCode) {
final Language language = getLanguageForShortNameOrNull(langCode); final Language language = getLanguageForShortNameOrNull(langCode);
Expand All @@ -557,13 +553,7 @@ public static Language getLanguageForShortName(final String langCode) {
} }


/** /**
* Return whether a language with the given language code is supported. Which languages * @deprecated use {@link Languages#isLanguageSupported(String)} (deprecated since 2.9)
* are supported depends on the classpath when the {@code Language} object is initialized.
*
* @param langCode e.g. {@code en} or {@code en-US}
* @return true if the language is supported
* @throws IllegalArgumentException in some cases of an invalid language code format
* @since 2.1
*/ */
public static boolean isLanguageSupported(final String langCode) { public static boolean isLanguageSupported(final String langCode) {
return getLanguageForShortNameOrNull(langCode) != null; return getLanguageForShortNameOrNull(langCode) != null;
Expand Down Expand Up @@ -617,11 +607,7 @@ private static Language getLanguageForShortNameOrNull(final String langCode) {
} }


/** /**
* Get the best match for a locale, using American English as the final fallback if nothing * @deprecated use {@link Languages#getLanguageForLocale(java.util.Locale)} (deprecated since 2.9)
* else fits. The returned language will be a country variant language (e.g. British English, not just English)
* if available.
* @since 1.8
* @throws RuntimeException if no language was found and American English as a fallback is not available
*/ */
public static Language getLanguageForLocale(final Locale locale) { public static Language getLanguageForLocale(final Locale locale) {
final Language language = getLanguageForLanguageNameAndCountry(locale); final Language language = getLanguageForLanguageNameAndCountry(locale);
Expand Down
106 changes: 106 additions & 0 deletions languagetool-core/src/main/java/org/languagetool/Languages.java
@@ -0,0 +1,106 @@
/* LanguageTool, a natural language style checker
* Copyright (C) 2015 Daniel Naber (http://www.danielnaber.de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package org.languagetool;

import java.util.*;

/**
* Helper methods to list all supported languages and to get language objects
* by their name or language code etc.
* @since 2.9
*/
public final class Languages {

private static final List<Language> LANGUAGES = getAllLanguages();

/**
* Language classes are detected at runtime by searching the classpath for files named
* {@code META-INF/org/languagetool/language-module.properties}. Those file(s)
* need to contain a key {@code languageClasses} which specifies the fully qualified
* class name(s), e.g. {@code org.languagetool.language.English}. Use commas to specify
* more than one class.
* @return an unmodifiable list of all supported languages
*/
public static List<Language> get() {
List<Language> result = new ArrayList<>();
for (Language lang : LANGUAGES) {
if (!"xx".equals(lang.getShortName())) { // skip demo language
result.add(lang);
}
}
return Collections.unmodifiableList(result);
}

/**
* Like {@link #get()} but the list contains also LanguageTool's internal 'Demo'
* language, if available. Only useful for tests.
* @return an unmodifiable list
*/
public static List<Language> getWithDemoLanguage() {
return LANGUAGES;
}

private static List<Language> getAllLanguages() {
return Collections.unmodifiableList(Arrays.asList(Language.LANGUAGES));
}

/**
* Get the Language object for the given language name.
*
* @param languageName e.g. <code>English</code> or <code>German</code> (case is significant)
* @return a Language object or {@code null} if there is no such language
*/
public static Language getLanguageForName(final String languageName) {
return Language.getLanguageForName(languageName);
}

/**
* Get the Language object for the given short language name.
*
* @param langCode e.g. <code>en</code> or <code>es-US</code>
* @return a Language object
* @throws IllegalArgumentException if the language is not supported or if the language code is invalid
*/
public static Language getLanguageForShortName(final String langCode) {
return Language.getLanguageForShortName(langCode);
}

/**
* Return whether a language with the given language code is supported. Which languages
* are supported depends on the classpath when the {@code Language} object is initialized.
*
* @param langCode e.g. {@code en} or {@code en-US}
* @return true if the language is supported
* @throws IllegalArgumentException in some cases of an invalid language code format
*/
public static boolean isLanguageSupported(final String langCode) {
return Language.isLanguageSupported(langCode);
}

/**
* Get the best match for a locale, using American English as the final fallback if nothing
* else fits. The returned language will be a country variant language (e.g. British English, not just English)
* if available.
* @throws RuntimeException if no language was found and American English as a fallback is not available
*/
public static Language getLanguageForLocale(final Locale locale) {
return Language.getLanguageForLocale(locale);
}

}
3 changes: 3 additions & 0 deletions languagetool-standalone/CHANGES.txt
Expand Up @@ -47,6 +47,9 @@ LanguageTool Change Log
misspelled words. misspelled words.


-API: -API:
-All static methods and fields from class 'Language' have been moved to the new
class 'Languages'. For now, the methods/fields in class Language still exist
but have been deprecated.
-LanguageIdentifierTools has been removed. Use LanguageIdentifier instead. -LanguageIdentifierTools has been removed. Use LanguageIdentifier instead.
-Removed (Default)ResourceDataBroker.setResourceDir() and setRulesDir() -Removed (Default)ResourceDataBroker.setResourceDir() and setRulesDir()
as these can be set with the constructor as these can be set with the constructor
Expand Down
Expand Up @@ -42,9 +42,13 @@ public LanguageComboBox(ResourceBundle messages, String extLangSuffix) {
populateLanguageBox(); populateLanguageBox();
} }


final void populateLanguageBox() { final void populateLanguageBox(List<Language> externalLanguages) {
removeAllItems(); removeAllItems();
initAllLanguages(); initAllLanguages(externalLanguages);
}

final void populateLanguageBox() {
populateLanguageBox(Collections.<Language>emptyList());
} }


void selectLanguage(Language language) { void selectLanguage(Language language) {
Expand All @@ -55,7 +59,7 @@ void selectLanguage(Language language) {
} }
} }


private void initAllLanguages() { private void initAllLanguages(List<Language> externalLanguages) {
applyComponentOrientation( applyComponentOrientation(
ComponentOrientation.getOrientation(Locale.getDefault())); ComponentOrientation.getOrientation(Locale.getDefault()));
languages.clear(); languages.clear();
Expand All @@ -67,6 +71,9 @@ private void initAllLanguages() {
languages.add(language); languages.add(language);
} }
} }
for (Language externalLanguage : externalLanguages) {
addItem(externalLanguage);
}
Collections.sort(languages, langComparator); Collections.sort(languages, langComparator);
for (final Language language : languages) { for (final Language language : languages) {
addItem(language); addItem(language);
Expand Down
Expand Up @@ -38,6 +38,7 @@
import java.awt.event.*; import java.awt.event.*;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.ResourceBundle; import java.util.ResourceBundle;
Expand Down Expand Up @@ -68,6 +69,7 @@ public final class Main {
private static final int WINDOW_HEIGHT = 550; private static final int WINDOW_HEIGHT = 550;


private final ResourceBundle messages; private final ResourceBundle messages;
private final List<Language> externalLanguages = new ArrayList<>();


private JFrame frame; private JFrame frame;
private JDialog taggerDialog; private JDialog taggerDialog;
Expand Down Expand Up @@ -136,14 +138,12 @@ private void saveFile(boolean newFile) {
} }


private void addLanguage() throws InstantiationException, IllegalAccessException { private void addLanguage() throws InstantiationException, IllegalAccessException {
final LanguageManagerDialog lmd = new LanguageManagerDialog(frame, Language.getExternalLanguages()); final LanguageManagerDialog dialog = new LanguageManagerDialog(frame, externalLanguages);
lmd.show(); dialog.show();
try { List<Language> newExtLanguages = dialog.getLanguages();
Language.reInit(lmd.getLanguages()); externalLanguages.clear();
} catch (RuleFilenameException e) { externalLanguages.addAll(newExtLanguages);
Tools.showErrorMessage(e, frame); languageBox.populateLanguageBox(externalLanguages);
}
languageBox.populateLanguageBox();
languageBox.selectLanguage(ltSupport.getLanguage()); languageBox.selectLanguage(ltSupport.getLanguage());
} }


Expand Down

0 comments on commit 88c1ed0

Please sign in to comment.