From: <cr...@us...> - 2008-04-25 05:56:05
|
Revision: 4017 http://jnode.svn.sourceforge.net/jnode/?rev=4017&view=rev Author: crawley Date: 2008-04-24 22:56:03 -0700 (Thu, 24 Apr 2008) Log Message: ----------- Converted LocaleCommand, and added the "--list" option to list all available Locales. Modified Paths: -------------- trunk/shell/descriptors/org.jnode.shell.command.xml trunk/shell/src/shell/org/jnode/shell/command/LocaleCommand.java Modified: trunk/shell/descriptors/org.jnode.shell.command.xml =================================================================== --- trunk/shell/descriptors/org.jnode.shell.command.xml 2008-04-25 03:37:47 UTC (rev 4016) +++ trunk/shell/descriptors/org.jnode.shell.command.xml 2008-04-25 05:56:03 UTC (rev 4017) @@ -160,6 +160,19 @@ </optional> </sequence> </syntax> + <syntax alias="locale"> + <empty description="print the current default Locale name"/> + <option argLabel="list" longName="list" shortName="l" description="list the names of the available Locales"/> + <sequence description="change the default Locale"> + <argument argLabel="language"/> + <optional> + <argument argLabel="country"/> + <optional> + <argument argLabel="variant"/> + </optional> + </optional> + </sequence> + </syntax> <syntax alias="run" description="Run a command file"> <argument argLabel="file"/> </syntax> Modified: trunk/shell/src/shell/org/jnode/shell/command/LocaleCommand.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/command/LocaleCommand.java 2008-04-25 03:37:47 UTC (rev 4016) +++ trunk/shell/src/shell/org/jnode/shell/command/LocaleCommand.java 2008-04-25 05:56:03 UTC (rev 4017) @@ -23,104 +23,111 @@ import java.io.InputStream; import java.io.PrintStream; +import java.util.Arrays; +import java.util.Comparator; import java.util.Locale; +import java.util.TreeSet; -import org.jnode.shell.help.Argument; -import org.jnode.shell.help.Help; -import org.jnode.shell.help.Parameter; -import org.jnode.shell.help.ParsedArguments; -import org.jnode.shell.help.Syntax; -import org.jnode.shell.help.argument.CountryArgument; -import org.jnode.shell.help.argument.LanguageArgument; +import org.jnode.shell.AbstractCommand; +import org.jnode.shell.CommandLine; +import org.jnode.shell.syntax.Argument; +import org.jnode.shell.syntax.CountryArgument; +import org.jnode.shell.syntax.FlagArgument; +import org.jnode.shell.syntax.LanguageArgument; +import org.jnode.shell.syntax.StringArgument; /** - * Change the default locale of JNode + * Manage JNode's default locale. * * @author Fabien DUMINY (fd...@jn...) + * @author cr...@jn... */ -public class LocaleCommand { +public class LocaleCommand extends AbstractCommand { - static final Argument LANGUAGE = new LanguageArgument("language", "language parameter"); - static final Argument COUNTRY = new CountryArgument("country", "country parameter"); - static final Argument VARIANT = new Argument("variant", "variant parameter"); + private final LanguageArgument ARG_LANGUAGE = + new LanguageArgument("language", Argument.OPTIONAL, "the locale's language"); + private final CountryArgument ARG_COUNTRY = + new CountryArgument("country", Argument.OPTIONAL, "the locale's country"); + private final StringArgument ARG_VARIANT = + new StringArgument("variant", Argument.OPTIONAL, "the locale's variant"); + private final FlagArgument FLAG_LIST = + new FlagArgument("list", Argument.OPTIONAL, "if set, list the available Locales"); - static final Parameter PARAM_LANGUAGE = new Parameter(LANGUAGE, Parameter.MANDATORY); - static final Parameter PARAM_COUNTRY = new Parameter(COUNTRY, Parameter.OPTIONAL); - static final Parameter PARAM_VARIANT = new Parameter(VARIANT, Parameter.OPTIONAL); - - public static Help.Info HELP_INFO = new Help.Info( - "locale", - new Syntax[] { - new Syntax("Display the current locale"), - new Syntax("Change the current locale\n\tExample : locale fr FR", - PARAM_LANGUAGE, PARAM_COUNTRY, PARAM_VARIANT) - }); - + public LocaleCommand() { + super("print or change JNode's default Locale"); + registerArguments(ARG_LANGUAGE, ARG_COUNTRY, ARG_VARIANT, FLAG_LIST); + } + public static void main(String[] args) throws Exception { - new LocaleCommand().execute(args, System.in, System.out, System.err); + new LocaleCommand().execute(args); } - /** - * Execute this command - */ - protected void execute(String[] args, InputStream in, PrintStream out, - PrintStream err) throws Exception { + @Override + public void execute(CommandLine commandLine, InputStream in, + PrintStream out, PrintStream err) throws Exception { + if (ARG_LANGUAGE.isSet()) { + final String language = ARG_LANGUAGE.getValue(); - ParsedArguments cmdLine = HELP_INFO.parse(args); - - if (PARAM_LANGUAGE.isSatisfied()) { - final String language = LANGUAGE.getValue(cmdLine); + String country = (ARG_COUNTRY.isSet()) ? ARG_COUNTRY.getValue() : ""; + String variant = (ARG_VARIANT.isSet()) ? ARG_VARIANT.getValue() : ""; - String country = ""; - if(PARAM_COUNTRY.isSatisfied()) - { - country = COUNTRY.getValue(cmdLine); - } - - String variant = ""; - if(PARAM_VARIANT.isSatisfied()) - { - variant = VARIANT.getValue(cmdLine); - } - Locale locale = findLocale(language, country, variant); - if(locale == null) - { - err.println("locale not available "+language+" "+country+" "+variant); - return; + if (locale == null) { + err.println("No Locale is available for " + language + " " + country + " " + variant); + exit(1); } - + out.println("Setting default Locale to " + formatLocale(locale)); Locale.setDefault(locale); - } - - out.println("current locale : " + Locale.getDefault().getDisplayName()); + } + else if (FLAG_LIST.isSet()) { + listLocales(out); + } + else { + out.println("Current default Locale is " + formatLocale(Locale.getDefault())); + } } /** - * Find the locale among the available locales + * List the available Locales in alphabetical order + * + * @param out destination for the listing + */ + private void listLocales(PrintStream out) { + // (The getAvailableLocales() method returns a cloned array ...) + Locale[] locales = Locale.getAvailableLocales(); + TreeSet<Locale> treeSet = new TreeSet<Locale>(new Comparator<Locale>() { + public int compare(Locale o1, Locale o2) { + return o1.getDisplayName().compareTo(o2.getDisplayName()); + } + }); + treeSet.addAll(Arrays.asList(locales)); + for (Locale l : treeSet) { + out.println(formatLocale(l)); + } + } + + private String formatLocale(Locale l) { + return (l.getDisplayName() + " : " + l.getLanguage() + + " " + l.getCountry() + " " + l.getVariant()); + } + + /** + * Find a Locale that matches the supplied language / country / variant triple. * - * @param language - * @param country - * @param variant - * @return + * @param language the language for the required Locale + * @param country the country code for the required Locale + * @param variant the variant for the required Locale + * @return the requested Locale, or <code>null</code> */ protected Locale findLocale(String language, String country, String variant) { - Locale[] locales = Locale.getAvailableLocales(); - Locale locale = null; - - for(int i = 0 ; i < locales.length ; i++) - { - Locale l = locales[i]; - if(l.getCountry().equals(country) && - l.getLanguage().equals(language) && - l.getVariant().equals(variant)) - { - locale = l; - break; - } + for (Locale l : Locale.getAvailableLocales()) { + if (l.getCountry().equals(country) && + l.getLanguage().equals(language) && + l.getVariant().equals(variant)) { + return l; + } } - - return locale; + return null; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |