You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2013/06/09 22:35:58 UTC
svn commit: r1491284 - in /sis/branches/JDK7: ./ application/sis-app/
application/sis-app/src/main/java/org/apache/sis/console/
core/sis-utility/src/main/java/org/apache/sis/internal/util/
core/sis-utility/src/main/java/org/apache/sis/setup/ core/sis-u...
Author: desruisseaux
Date: Sun Jun 9 20:35:57 2013
New Revision: 1491284
URL: http://svn.apache.org/r1491284
Log:
First draft of the command line interface as a port of the About main class.
Added:
sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/AboutSC.java (with props)
sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/HelpCS.java (with props)
sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidCommandException.java (with props)
sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidOptionException.java (with props)
sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Option.java (with props)
sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/SubCommand.java (with props)
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/About.java
- copied, changed from r1491233, sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/About.java
Removed:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/About.java
Modified:
sis/branches/JDK7/application/sis-app/pom.xml
sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Command.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Supervisor.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
sis/branches/JDK7/pom.xml
Modified: sis/branches/JDK7/application/sis-app/pom.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-app/pom.xml?rev=1491284&r1=1491283&r2=1491284&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-app/pom.xml (original)
+++ sis/branches/JDK7/application/sis-app/pom.xml Sun Jun 9 20:35:57 2013
@@ -31,13 +31,58 @@
<version>0.3-jdk7-SNAPSHOT</version>
</parent>
+
+ <!-- ===========================================================
+ Module Description
+ =========================================================== -->
<artifactId>sis-app</artifactId>
<packaging>bundle</packaging>
- <name>Apache SIS app</name>
- <url>http://sis.apache.org</url>
+ <name>Apache SIS application</name>
+ <description>
+ <!-- Left alignment because this description will be copied in META-INF/MANIFEST.MF
+ The leading space after the first line is necessary for proper formatting. -->
+Console application.
+ </description>
+
+
+ <!-- ===========================================================
+ Developers and Contributors
+ =========================================================== -->
+ <developers>
+ <developer>
+ <name>Martin Desruisseaux</name>
+ <id>desruisseaux</id>
+ <email>desruisseaux@apache.org</email>
+ <organization>Geomatys</organization>
+ <organizationUrl>http://www.geomatys.com</organizationUrl>
+ <timezone>+1</timezone>
+ <roles>
+ <role>developer</role>
+ </roles>
+ </developer>
+ </developers>
+
+ <!-- ===========================================================
+ Build configuration
+ =========================================================== -->
<build>
<plugins>
+
+ <!-- Declare the main class -->
+ <plugin>
+ <artifactId>maven-jar-plugin</artifactId>
+ <configuration>
+ <archive>
+ <manifest>
+ <mainClass>org.apache.sis.console.Command</mainClass>
+ </manifest>
+ </archive>
+ <skipIfEmpty>true</skipIfEmpty>
+ </configuration>
+ </plugin>
+
+ <!-- Package as OSGi bundle -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
@@ -46,7 +91,7 @@
<Export-Package>
org.apache.sis.cli
</Export-Package>
- <Main-Class>org.apache.sis.cli.SISCLI</Main-Class>
+ <Main-Class>org.apache.sis.console.Command</Main-Class>
</instructions>
</configuration>
</plugin>
Added: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/AboutSC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/AboutSC.java?rev=1491284&view=auto
==============================================================================
--- sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/AboutSC.java (added)
+++ sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/AboutSC.java [UTF-8] Sun Jun 9 20:35:57 2013
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.console;
+
+import java.util.EnumSet;
+import org.apache.sis.setup.About;
+import org.apache.sis.util.Version;
+
+
+/**
+ * The "about" subcommand.
+ * By default this sub-command prints all information except the {@link About#LIBRARIES} section,
+ * because the later is considered too verbose. Available options are:
+ *
+ * <ul>
+ * <li>{@code --brief}: prints only Apache SIS version number.</li>
+ * <li>{@code --verbose}: prints all information including the libraries.</li>
+ * </ul>
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+final class AboutSC extends SubCommand {
+ /**
+ * Creates the {@code "about"} sub-command.
+ */
+ AboutSC(final String[] args) throws InvalidOptionException {
+ super(args, EnumSet.of(Option.LOCALE, Option.ENCODING, Option.BRIEF, Option.VERBOSE));
+ }
+
+ /**
+ * Prints the information to the output stream.
+ */
+ @Override
+ public void run() {
+ final String configuration;
+ if (options.containsKey(Option.BRIEF)) {
+ configuration = "Apache SIS version " + Version.SIS;
+ } else {
+ final EnumSet<About> sections = EnumSet.allOf(About.class);
+ if (!options.containsKey(Option.VERBOSE)) {
+ sections.remove(About.LIBRARIES);
+ }
+ configuration = About.configuration(sections, locale).toString();
+ }
+ out.write(configuration);
+ out.write(System.lineSeparator());
+ out.flush();
+ }
+}
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/AboutSC.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/AboutSC.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Modified: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Command.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Command.java?rev=1491284&r1=1491283&r2=1491284&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Command.java [UTF-8] (original)
+++ sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Command.java [UTF-8] Sun Jun 9 20:35:57 2013
@@ -16,13 +16,133 @@
*/
package org.apache.sis.console;
+import java.io.Console;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import org.apache.sis.util.resources.Errors;
+
+
/**
- * Command line interface for Apache SIS
+ * Command line interface for Apache SIS.
+ * The main method can be invoked from the command-line as below
+ * (the filename needs to be completed with the actual version number):
+ *
+ * {@preformat java
+ * java -jar target/binaries/sis-app.jar
+ * }
+ *
+ * "{@code target/binaries}" is the default location where SIS JAR files are grouped together
+ * with their dependencies after a Maven build. This directory can be replaced by any path to
+ * a directory providing the required dependencies.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
*/
-public class Command {
+public class Command implements Runnable {
+ /**
+ * The code given to {@link System#exit(int)} when the program failed because of a unknown sub-command.
+ */
+ public static final int INVALID_COMMAND_EXIT_CODE = 1;
+
+ /**
+ * The code given to {@link System#exit(int)} when the program failed because of an illegal user argument.
+ */
+ public static final int INVALID_OPTION_EXIT_CODE = 2;
+
+ /**
+ * The code given to {@link System#exit(int)} when a file given in argument uses an unknown file format.
+ */
+ public static final int UNKNOWN_STORAGE_EXIT_CODE = 10;
+
+ /**
+ * The code given to {@link System#exit(int)} when the program failed because of an {@link java.io.IOException}.
+ */
+ public static final int IO_EXCEPTION_EXIT_CODE = 100;
+
+ /**
+ * The code given to {@link System#exit(int)} when the program failed because of an {@link java.sql.SQLException}.
+ */
+ public static final int SQL_EXCEPTION_EXIT_CODE = 101;
+
+ /**
+ * The sub-command to execute.
+ */
+ private final SubCommand command;
+
+ /**
+ * Creates a new command for the given arguments. The first value in the given array shall be the
+ * command name. All other values are options.
+ *
+ * @param args The command-line arguments.
+ * @throws InvalidCommandException If an invalid command has been given.
+ * @throws InvalidOptionException If the given arguments contain an invalid option.
+ */
+ protected Command(final String[] args) throws InvalidCommandException, InvalidOptionException {
+ if (args.length == 0) {
+ command = new HelpCS(args);
+ } else {
+ final String name = args[0];
+ switch (name.toLowerCase()) {
+ case "about": {
+ command = new AboutSC(args);
+ break;
+ }
+ case "help": {
+ command = new HelpCS(args);
+ break;
+ }
+ default: {
+ throw new InvalidCommandException(Errors.format(Errors.Keys.UnknownCommand_1, name), name);
+ }
+ }
+ }
+ }
+
+ /**
+ * Runs the command.
+ */
+ @Override
+ public void run() {
+ command.run();
+ }
+
+ /**
+ * Prints the message of the given exception. This method is invoked only when the error occurred before
+ * the {@link SubCommand} has been built, otherwise the {@link SubCommand#err} printer shall be used.
+ */
+ private static void error(final Exception e) {
+ final Console console = System.console();
+ if (console != null) {
+ final PrintWriter err = console.writer();
+ err.println(e.getLocalizedMessage());
+ err.flush();
+ } else {
+ final PrintStream err = System.err;
+ err.println(e.getLocalizedMessage());
+ err.flush();
+ }
+ }
+
/**
- * Main method
+ * Prints the information to the standard output stream.
+ *
+ * @param args Command-line options.
*/
- public static void main(String[] args) {
+ public static void main(final String[] args) {
+ final Command c;
+ try {
+ c = new Command(args);
+ } catch (InvalidCommandException e) {
+ error(e);
+ System.exit(INVALID_COMMAND_EXIT_CODE);
+ return;
+ } catch (InvalidOptionException e) {
+ error(e);
+ System.exit(INVALID_OPTION_EXIT_CODE);
+ return;
+ }
+ c.run();
}
-}
\ No newline at end of file
+}
Added: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/HelpCS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/HelpCS.java?rev=1491284&view=auto
==============================================================================
--- sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/HelpCS.java (added)
+++ sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/HelpCS.java [UTF-8] Sun Jun 9 20:35:57 2013
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.console;
+
+import java.util.EnumSet;
+
+
+/**
+ * The "help" subcommand.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+final class HelpCS extends SubCommand {
+ /**
+ * Creates the {@code "about"} sub-command.
+ */
+ HelpCS(final String[] args) throws InvalidOptionException {
+ super(args, EnumSet.of(Option.LOCALE, Option.ENCODING));
+ }
+
+ /**
+ * Prints the help instructions.
+ */
+ @Override
+ public void run() {
+ // TODO
+ }
+}
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/HelpCS.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/HelpCS.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Added: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidCommandException.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidCommandException.java?rev=1491284&view=auto
==============================================================================
--- sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidCommandException.java (added)
+++ sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidCommandException.java [UTF-8] Sun Jun 9 20:35:57 2013
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.console;
+
+
+/**
+ * Thrown when a unknown command has been given by the user on the command-line.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+public class InvalidCommandException extends Exception {
+ /**
+ * For cross-version compatibility.
+ */
+ private static final long serialVersionUID = 4751525514150949949L;
+
+ /**
+ * The name of the invalid command.
+ */
+ private final String command;
+
+ /**
+ * Constructs an exception with the specified detail message.
+ *
+ * @param message The detail message.
+ * @param command The name of the invalid command.
+ */
+ public InvalidCommandException(final String message, final String command) {
+ super(message);
+ this.command = command;
+ }
+
+ /**
+ * Returns the name of the invalid command.
+ *
+ * @return The name of the invalid command.
+ */
+ public String getCommand() {
+ return command;
+ }
+}
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidCommandException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidCommandException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Added: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidOptionException.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidOptionException.java?rev=1491284&view=auto
==============================================================================
--- sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidOptionException.java (added)
+++ sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidOptionException.java [UTF-8] Sun Jun 9 20:35:57 2013
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.console;
+
+
+/**
+ * Thrown when an illegal option has been given by the user on the command-line.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+public class InvalidOptionException extends Exception {
+ /**
+ * For cross-version compatibility.
+ */
+ private static final long serialVersionUID = 2947996310387227986L;
+
+ /**
+ * The name of the invalid option.
+ */
+ private final String option;
+
+ /**
+ * Constructs an exception with the specified detail message.
+ *
+ * @param message The detail message.
+ * @param option The name of the invalid option.
+ */
+ public InvalidOptionException(final String message, final String option) {
+ super(message);
+ this.option = option;
+ }
+
+ /**
+ * Constructs an exception with the specified detail message and cause.
+ *
+ * @param message The detail message.
+ * @param cause The cause of this exception.
+ * @param option The name of the invalid option.
+ */
+ public InvalidOptionException(final String message, final Throwable cause, final String option) {
+ super(message, cause);
+ this.option = option;
+ }
+
+ /**
+ * Returns the name of the invalid option.
+ *
+ * @return The name of the invalid option.
+ */
+ public String getOption() {
+ return option;
+ }
+}
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidOptionException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/InvalidOptionException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Added: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Option.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Option.java?rev=1491284&view=auto
==============================================================================
--- sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Option.java (added)
+++ sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Option.java [UTF-8] Sun Jun 9 20:35:57 2013
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.console;
+
+import org.apache.sis.util.resources.Errors;
+
+
+/**
+ * A command-line option.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+enum Option {
+ /**
+ * The locale for the output produced by the command.
+ */
+ LOCALE(true),
+
+ /**
+ * The encoding for the output produced by the command.
+ */
+ ENCODING(true),
+
+ /**
+ * The timezone for the dates to be formatted.
+ */
+ TIMEZONE(true),
+
+ /**
+ * Whether colorized output shall be enabled.
+ */
+ COLORS(true),
+
+ /**
+ * Whether the output should contains only brief information.
+ * This option expects no value.
+ */
+ BRIEF(false),
+
+ /**
+ * Whether the output should contains more detailed information.
+ * This option expects no value.
+ */
+ VERBOSE(false);
+
+ /**
+ * The prefix to prepend to option names.
+ */
+ static final String PREFIX = "--";
+
+ /**
+ * Boolean values accepted on the command line. Values at even indices are {@code false}
+ * and values at odd indices are {@code true}.
+ *
+ * @see #parseBoolean(String)
+ */
+ private static final String[] BOOLEAN_VALUES = {
+ "false", "true",
+ "off", "on",
+ "yes", "no"
+ };
+
+ /**
+ * Whether this option expects a value.
+ */
+ final boolean hasValue;
+
+ /**
+ * Creates a new option.
+ *
+ * @param Whether this option expects a value.
+ */
+ private Option(final boolean hasValue) {
+ this.hasValue = hasValue;
+ }
+
+ /**
+ * Parses the given value as a boolean.
+ *
+ * @param value The value to parse.
+ * @return The value as a boolean.
+ * @throws InvalidOptionException If the given value is not recognized as a boolean.
+ */
+ boolean parseBoolean(final String value) throws InvalidOptionException {
+ for (int i=0; i<BOOLEAN_VALUES.length; i++) {
+ if (value.equalsIgnoreCase(BOOLEAN_VALUES[i])) {
+ return (i & 1) != 0;
+ }
+ }
+ final String name = name().toLowerCase();
+ throw new InvalidOptionException(Errors.format(Errors.Keys.IllegalOptionValue_2, name, value), name);
+ }
+}
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Option.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/Option.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Added: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/SubCommand.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/SubCommand.java?rev=1491284&view=auto
==============================================================================
--- sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/SubCommand.java (added)
+++ sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/SubCommand.java [UTF-8] Sun Jun 9 20:35:57 2013
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.console;
+
+import java.util.Locale;
+import java.util.EnumSet;
+import java.util.EnumMap;
+import java.io.Console;
+import java.io.PrintWriter;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
+import org.apache.sis.util.Locales;
+import org.apache.sis.util.resources.Errors;
+import org.apache.sis.internal.util.X364;
+
+
+/**
+ * Base class of sub-commands.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+abstract class SubCommand implements Runnable {
+ /**
+ * The command-line options allowed by this sub-command, together with their values.
+ */
+ protected final EnumMap<Option,String> options;
+
+ /**
+ * The locale specified by the {@code "--locale"} option. If no such option was
+ * provided, then this field is set to the {@linkplain Locale#getDefault() default locale}.
+ */
+ protected final Locale locale;
+
+ /**
+ * The encoding specified by the {@code "--encoding"} option. If no such option was provided,
+ * then this field is set to the {@linkplain Charset#defaultCharset() default charset}.
+ */
+ protected final Charset encoding;
+
+ /**
+ * {@code true} if colors can be applied for ANSI X3.64 compliant terminal.
+ * This is the value specified by the {@code --colors} arguments if present,
+ * or a value inferred from the system otherwise.
+ */
+ protected final Boolean colors;
+
+ /**
+ * Output stream to the console. This output stream uses the encoding
+ * specified by the {@code "--encoding"} argument, if presents.
+ */
+ protected final PrintWriter out;
+
+ /**
+ * Error stream to the console. This stream always uses the locale encoding, since its output will
+ * typically be sent to the console even if the user redirected the standard output to a file.
+ */
+ protected final PrintWriter err;
+
+ /**
+ * Creates a new sub-command.
+ *
+ * @param args The command-line arguments provided by the user.
+ * The first element in this array will be ignored, because it is assumed to be the sub-command name.
+ * @param validOptions The command-line options allowed by this sub-command.
+ * @throws InvalidOptionException If an illegal option has been provided, or the option has an illegal value.
+ */
+ protected SubCommand(final String[] args, final EnumSet<Option> validOptions) throws InvalidOptionException {
+ options = new EnumMap<>(Option.class);
+ for (int i=1; i<args.length; i++) {
+ final String arg = args[i];
+ if (arg.startsWith(Option.PREFIX)) {
+ final String name = arg.substring(Option.PREFIX.length());
+ final Option option;
+ try {
+ option = Option.valueOf(name.toUpperCase());
+ } catch (IllegalArgumentException e) {
+ throw new InvalidOptionException(Errors.format(Errors.Keys.UnknownOption_1, name), e, name);
+ }
+ if (!validOptions.contains(option)) {
+ throw new InvalidOptionException(Errors.format(Errors.Keys.UnknownOption_1, name), name);
+ }
+ String value = null;
+ if (option.hasValue) {
+ if (++i >= args.length) {
+ throw new InvalidOptionException(Errors.format(Errors.Keys.MissingValueForOption_1, name), name);
+ }
+ value = args[i];
+ }
+ if (options.containsKey(option)) {
+ throw new InvalidOptionException(Errors.format(Errors.Keys.DuplicatedOption_1, name), name);
+ }
+ options.put(option, value);
+ }
+ }
+ /*
+ * Process the --locale, --encoding and --colors options.
+ */
+ Option option = null; // In case of IllegalArgumentException.
+ String value = null;
+ final Console console;
+ final boolean explicitEncoding;
+ try {
+ value = options.get(option = Option.LOCALE);
+ locale = (value != null) ? Locales.parse(value) : Locale.getDefault(Locale.Category.DISPLAY);
+
+ value = options.get(option = Option.ENCODING);
+ explicitEncoding = (value != null);
+ encoding = explicitEncoding ? Charset.forName(value) : Charset.defaultCharset();
+
+ value = options.get(option = Option.COLORS);
+ console = System.console();
+ colors = (value != null) ? Option.COLORS.parseBoolean(value) : (console != null) && X364.isAnsiSupported();
+ } catch (IllegalArgumentException e) {
+ final String name = option.name().toLowerCase();
+ throw new InvalidOptionException(Errors.format(Errors.Keys.IllegalOptionValue_2, name, value), name);
+ }
+ /*
+ * Creates the writers.
+ */
+ err = (console != null) ? console.writer() : new PrintWriter(System.err, true);
+ if (!explicitEncoding && console != null) {
+ out = console.writer();
+ } else {
+ if (explicitEncoding) {
+ out = new PrintWriter(new OutputStreamWriter(System.out, encoding), true);
+ } else {
+ out = new PrintWriter(System.out, true);
+ }
+ }
+ }
+
+ /**
+ * Executes the sub-command.
+ */
+ @Override
+ public abstract void run();
+}
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/SubCommand.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/application/sis-app/src/main/java/org/apache/sis/console/SubCommand.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Supervisor.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Supervisor.java?rev=1491284&r1=1491283&r2=1491284&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Supervisor.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Supervisor.java [UTF-8] Sun Jun 9 20:35:57 2013
@@ -32,7 +32,7 @@ import javax.management.JMException;
import javax.management.NotCompliantMBeanException;
import java.lang.management.ManagementFactory;
-import org.apache.sis.util.About;
+import org.apache.sis.setup.About;
import org.apache.sis.util.Localized;
import org.apache.sis.util.ArgumentChecks;
import org.apache.sis.util.logging.Logging;
Copied: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/About.java (from r1491233, sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/About.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/About.java?p2=sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/About.java&p1=sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/About.java&r1=1491233&r2=1491284&rev=1491284&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/About.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/About.java [UTF-8] Sun Jun 9 20:35:57 2013
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.sis.util;
+package org.apache.sis.setup;
import java.util.Arrays;
import java.util.List;
@@ -30,18 +30,17 @@ import java.util.MissingResourceExceptio
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
-import java.io.Console;
import java.io.File;
import java.io.FileFilter;
-import java.io.PrintStream;
-import java.io.PrintWriter;
import java.io.IOException;
import java.text.Format;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.nio.charset.Charset;
+import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.CharSequences;
+import org.apache.sis.util.Version;
import org.apache.sis.util.logging.Logging;
-import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.resources.Vocabulary;
import org.apache.sis.util.collection.TreeTable;
import org.apache.sis.util.collection.TreeTables;
@@ -67,9 +66,6 @@ import static org.apache.sis.util.collec
* <li>Libraries on the classpath and extension directories.</li>
* </ul>
*
- * This class can be invoked from the command line.
- * See the {@link #main(String[])} method for more information.
- *
* @author Martin Desruisseaux (Geomatys)
* @since 0.3
* @version 0.3
@@ -137,70 +133,6 @@ public enum About {
}
/**
- * Prints the information to the standard output stream.
- * This method can be invoked from the command-line as below (the "{@code sis-utility}" file
- * can be replaced by any SIS module, and its filename needs to be completed with the actual
- * version number):
- *
- * {@preformat java
- * java -jar target/binaries/sis-utility.jar
- * }
- *
- * "{@code target/binaries}" is the default location where SIS JAR files are grouped together
- * with their dependencies after a Maven build. This directory can be replaced by any path to
- * a directory providing the required dependencies.
- *
- * <p>By default the above command prints all information except the {@link #LIBRARIES} section,
- * because the later is considered too verbose. Available options are:</p>
- *
- * <ul>
- * <li>{@code --version}: prints only Apache SIS version number.</li>
- * <li>{@code --verbose}: prints all information including the libraries.</li>
- * </ul>
- *
- * @param args Command-line options.
- */
- public static void main(final String[] args) {
- final EnumSet<About> sections = EnumSet.allOf(About.class);
- String configuration = null;
- /*
- * Command-line arguments processing.
- */
- if (args.length == 0) {
- sections.remove(About.LIBRARIES);
- } else {
- final String arg = args[0];
- if (arg.equals("--version")) {
- configuration = "Apache SIS version " + Version.SIS;
- } else if (!arg.equals("--verbose")) {
- System.err.println(Errors.format(Errors.Keys.IllegalArgumentValue_2, 1, arg));
- return;
- }
- if (args.length != 1) { // Checked only after we verified the first argument.
- System.err.println(Errors.format(Errors.Keys.IllegalArgumentValue_2, 2, args[1]));
- return;
- }
- }
- /*
- * Format the tree and write result to the console.
- */
- if (configuration == null) {
- configuration = configuration(sections, Locale.getDefault()).toString();
- }
- final Console console = System.console();
- if (console != null) {
- final PrintWriter out = console.writer();
- out.write(configuration);
- out.write(System.lineSeparator());
- out.flush();
- } else {
- final PrintStream out = System.out;
- out.println(configuration);
- out.flush();
- }
- }
-
- /**
* Returns all known information about the current Apache SIS running environment.
*
* @param locale The locale to use for formatting the texts in the tree.
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java?rev=1491284&r1=1491283&r2=1491284&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java [UTF-8] Sun Jun 9 20:35:57 2013
@@ -124,6 +124,11 @@ public final class Errors extends Indexe
public static final int DuplicatedIdentifier_1 = 38;
/**
+ * Option “{0}” is duplicated.
+ */
+ public static final int DuplicatedOption_1 = 100;
+
+ /**
* Element “{0}” is already present.
*/
public static final int ElementAlreadyPresent_1 = 36;
@@ -211,6 +216,11 @@ public final class Errors extends Indexe
public static final int IllegalLanguageCode_1 = 12;
/**
+ * Option ‘{0}’ can not take the “{1}” value.
+ */
+ public static final int IllegalOptionValue_2 = 101;
+
+ /**
* The [{0} … {1}] range of ordinate values is not valid for the “{2}” axis.
*/
public static final int IllegalOrdinateRange_3 = 5;
@@ -296,6 +306,11 @@ public final class Errors extends Indexe
public static final int MissingRequiredModule_1 = 84;
/**
+ * Missing value for option “{0}”.
+ */
+ public static final int MissingValueForOption_1 = 99;
+
+ /**
* Missing value for property “{0}”.
*/
public static final int MissingValueForProperty_1 = 85;
@@ -471,6 +486,16 @@ public final class Errors extends Indexe
public static final int UnexpectedFileFormat_2 = 92;
/**
+ * Command “{0}” is not recognized.
+ */
+ public static final int UnknownCommand_1 = 102;
+
+ /**
+ * Option “{0}” is not recognized.
+ */
+ public static final int UnknownOption_1 = 98;
+
+ /**
* Type of the “{0}” property is unknown.
*/
public static final int UnknownTypeForProperty_1 = 80;
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties?rev=1491284&r1=1491283&r2=1491284&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties [ISO-8859-1] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties [ISO-8859-1] Sun Jun 9 20:35:57 2013
@@ -35,6 +35,7 @@ CanNotSetPropertyValue_1 = Can no
ClassNotFinal_1 = Class \u2018{0}\u2019 is not final.
CloneNotSupported_1 = Can not clone an object of type \u2018{0}\u2019.
DeadThread_1 = Thread \u201c{0}\u201d is dead.
+DuplicatedOption_1 = Option \u201c{0}\u201d is duplicated.
DuplicatedIdentifier_1 = Identifier \u201c{0}\u201d is duplicated.
ElementAlreadyPresent_1 = Element \u201c{0}\u201d is already present.
EmptyArgument_1 = Argument \u2018{0}\u2019 shall not be empty.
@@ -48,6 +49,7 @@ IllegalArgumentClass_2 = Argume
IllegalArgumentClass_3 = Argument \u2018{0}\u2019 can not be an instance of \u2018{2}\u2019. Expected an instance of \u2018{1}\u2019 or derived type.
IllegalArgumentField_4 = Argument \u2018{0}\u2019 can not take the \u201c{1}\u201d value, because the \u2018{2}\u2019 field can not take the \u201c{3}\u201d value.
IllegalArgumentValue_2 = Argument \u2018{0}\u2019 can not take the \u201c{1}\u201d value.
+IllegalOptionValue_2 = Option \u2018{0}\u2019 can not take the \u201c{1}\u201d value.
IllegalBitsPattern_1 = Illegal bits pattern: {0}.
IllegalClass_2 = Class \u2018{1}\u2019 is illegal. It must be \u2018{0}\u2019 or a derived class.
IllegalFormatPatternForClass_2 = The \u201c{1}\u201d pattern can not be applied to formating of objects of type \u2018{0}\u2019.
@@ -69,9 +71,10 @@ MandatoryAttribute_2 = Attrib
MismatchedCRS = The coordinate reference system must be the same for all objects.
MismatchedDimension_2 = Mismatched object dimension: {0}D and {1}D.
MismatchedDimension_3 = Argument \u2018{0}\u2019 has {2} dimension{2,choice,1#|2#s}, while {1} was expected.
-MissingValueInColumn_1 = Missing value in the \u201c{0}\u201d column.
MissingRequiredModule_1 = This operation requires the \u201c{0}\u201d module.
+MissingValueForOption_1 = Missing value for option \u201c{0}\u201d.
MissingValueForProperty_1 = Missing value for property \u201c{0}\u201d.
+MissingValueInColumn_1 = Missing value in the \u201c{0}\u201d column.
NegativeArgument_2 = Argument \u2018{0}\u2019 shall not be negative. The given value was {1}.
NegativeArrayLength_1 = Can not create a \u201c{0}\u201d array of negative length.
NodeChildOfItself_1 = Node \u201c{0}\u201d can not be a child of itself.
@@ -106,6 +109,8 @@ UnexpectedChange_1 = Unexpe
UnexpectedEndOfFile_1 = Unexpected end of file while reading \u201c{0}\u201d.
UnexpectedEndOfString_1 = More characters were expected at the end of \u201c{0}\u201d.
UnexpectedFileFormat_2 = File \u201c{1}\u201d seems to be encoded in an other format than {0}.
+UnknownCommand_1 = Command \u201c{0}\u201d is not recognized.
+UnknownOption_1 = Option \u201c{0}\u201d is not recognized.
UnknownType_1 = Type \u2018{0}\u2019 is unknown in this context.
UnknownTypeForProperty_1 = Type of the \u201c{0}\u201d property is unknown.
UnmodifiableAffineTransform = This affine transform is unmodifiable.
Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties?rev=1491284&r1=1491283&r2=1491284&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties [ISO-8859-1] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties [ISO-8859-1] Sun Jun 9 20:35:57 2013
@@ -25,6 +25,7 @@ CanNotSetPropertyValue_1 = Ne peu
ClassNotFinal_1 = La classe \u2018{0}\u2019 n\u2019est pas finale.
CloneNotSupported_1 = Un objet de type \u2018{0}\u2019 ne peut pas \u00eatre clon\u00e9.
DeadThread_1 = La t\u00e2che \u201c{0}\u201d est morte.
+DuplicatedOption_1 = L\u2019option \u201c{0}\u201d est dupliqu\u00e9e.
DuplicatedIdentifier_1 = L\u2019identifiant \u201c{0}\u201d est dupliqu\u00e9.
ElementAlreadyPresent_1 = L\u2019\u00e9lement \u201c{0}\u201d est d\u00e9j\u00e0 pr\u00e9sent.
EmptyArgument_1 = L\u2019argument \u2018{0}\u2019 ne doit pas \u00eatre vide.
@@ -38,6 +39,7 @@ IllegalArgumentClass_2 = L\u201
IllegalArgumentClass_3 = L\u2019argument \u2018{0}\u2019 ne peut pas \u00eatre de type \u2018{2}\u2019. Une instance de \u2018{1}\u2019 ou d\u2019un type d\u00e9riv\u00e9 \u00e9tait attendue.
IllegalArgumentField_4 = L\u2019argument \u2018{0}\u2019 n\u2019accepte pas la valeur \u201c{1}\u201d parce que le champs \u2018{2}\u2019 ne peut pas prendre la valeur \u201c{3}\u201d.
IllegalArgumentValue_2 = L\u2019argument \u2018{0}\u2019 n\u2019accepte pas la valeur \u201c{1}\u201d.
+IllegalOptionValue_2 = L\u2019option \u2018{0}\u2019 n\u2019accepte pas la valeur \u201c{1}\u201d.
IllegalBitsPattern_1 = Pattern de bits invalide: {0}.
IllegalClass_2 = La classe \u2018{1}\u2019 est ill\u00e9gale. Il doit s\u2019agir d\u2019une classe \u2018{0}\u2019 ou d\u00e9riv\u00e9e.
IllegalFormatPatternForClass_2 = Le mod\u00e8le \u201c{1}\u201d ne peut pas \u00eatre appliqu\u00e9 au formatage d\u2019objets de type \u2018{0}\u2019.
@@ -60,6 +62,7 @@ MismatchedCRS = Le sys
MismatchedDimension_2 = Les dimensions des objets ({0}D et {1}D) ne concordent pas.
MismatchedDimension_3 = L\u2019argument \u2018{0}\u2019 a {2} dimension{2,choice,1#|2#s}, alors qu\u2019on en attendait {1}.
MissingRequiredModule_1 = Cette op\u00e9ration requiert le module \u201c{0}\u201d.
+MissingValueForOption_1 = Aucune valeur n\u2019a \u00e9t\u00e9 d\u00e9finie pour l\u2019option \u201c{0}\u201d.
MissingValueForProperty_1 = Aucune valeur n\u2019a \u00e9t\u00e9 d\u00e9finie pour la propri\u00e9t\u00e9 \u201c{0}\u201d.
MissingValueInColumn_1 = Il manque une valeur dans la colonne \u201c{0}\u201d.
NegativeArgument_2 = L\u2019argument \u2018{0}\u2019 ne doit pas \u00eatre n\u00e9gatif. La valeur donn\u00e9e \u00e9tait {1}.
@@ -95,6 +98,8 @@ UnexpectedChange_1 = Change
UnexpectedEndOfFile_1 = Fin de fichier inattendue lors de la lecture de \u201c{0}\u201d.
UnexpectedEndOfString_1 = D\u2019autres caract\u00e8res \u00e9taient attendus \u00e0 la fin du texte \u201c{0}\u201d.
UnexpectedFileFormat_2 = Le fichier \u201c{1}\u201d semble \u00eatre encod\u00e9 dans un autre format que {0}.
+UnknownCommand_1 = La commande \u201c{0}\u201d n\u2019est pas reconnue.
+UnknownOption_1 = L\u2019option \u201c{0}\u201d n\u2019est pas reconnue.
UnknownType_1 = Le type \u2018{0}\u2019 n\u2019est pas reconnu dans ce contexte.
UnknownTypeForProperty_1 = Le type de la propri\u00e9t\u00e9 \u201c{0}\u201d est inconnu.
UnmodifiableAffineTransform = Cette transformation affine n\u2019est pas modifiable.
Modified: sis/branches/JDK7/pom.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/pom.xml?rev=1491284&r1=1491283&r2=1491284&view=diff
==============================================================================
--- sis/branches/JDK7/pom.xml (original)
+++ sis/branches/JDK7/pom.xml Sun Jun 9 20:35:57 2013
@@ -433,7 +433,6 @@ Apache SIS is a free software, Java lang
<index>false</index>
<manifest>
<addClasspath>true</addClasspath>
- <mainClass>org.apache.sis.util.About</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>