diff --git a/tools/java/common/pom.xml b/tools/java/common/pom.xml
new file mode 100644
index 000000000..14a8962a0
--- /dev/null
+++ b/tools/java/common/pom.xml
@@ -0,0 +1,37 @@
+
+
Subclass it to create a new command (e.g: code generation step). + * + * @author Philippe Liard + */ +public abstract class Command { + // The arguments provided to this command. The first one is the name of the command. + private String[] args; + + /** + * Entry point of the command called by the CommandDispatcher when requested. This method must be + * implemented by subclasses. + */ + public abstract boolean start(); + + /** + * The name of the command is used by the CommandDispatcher to execute the requested command. The + * Dispatcher will pass along all command-line arguments to this command, so args[0] will be + * always the command name. + */ + public abstract String getCommandName(); + + public String[] getArgs() { + return args; + } + + public void setArgs(String[] args) { + this.args = args; + } +} \ No newline at end of file diff --git a/tools/java/common/src/com/google/i18n/phonenumbers/tools/CommandDispatcher.java b/tools/java/common/src/com/google/i18n/phonenumbers/tools/CommandDispatcher.java new file mode 100644 index 000000000..73b5f016b --- /dev/null +++ b/tools/java/common/src/com/google/i18n/phonenumbers/tools/CommandDispatcher.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * Licensed 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 com.google.i18n.phonenumbers.tools; + +/** + * This class is designed to execute a requested command among a set of provided commands. + * The dispatching is performed according to the requested command name, which is provided as the + * first string of the 'args' array. The 'args' array also contains the command arguments available + * from position 1 to end. The verification of the arguments' consistency is under the + * responsibility of the command since the dispatcher can't be aware of its underlying goals. + * + * @see Command + * @author Philippe Liard + */ +public class CommandDispatcher { + // Command line arguments passed to the command which will be executed. Note that the first one is + // the name of the command. + private final String[] args; + // Supported commands by this dispatcher. + private final Command[] commands; + + public CommandDispatcher(String[] args, Command[] commands) { + this.args = args; + this.commands = commands; + } + + /** + * Executes the command named `args[0]` if any. If the requested command (in args[0]) is not + * supported, display a help message. + * + *
Note that the command name comparison is case sensitive.
+ */
+ public boolean start() {
+ if (args.length != 0) {
+ String requestedCommand = args[0];
+
+ for (Command command : commands) {
+ if (command.getCommandName().equals(requestedCommand)) {
+ command.setArgs(args);
+ return command.start();
+ }
+ }
+ }
+ displayUsage();
+ return false;
+ }
+
+ /**
+ * Displays a message containing the list of the supported commands by this dispatcher.
+ */
+ private void displayUsage() {
+ StringBuilder msg = new StringBuilder("Usage: java -jar /path/to/jar [ ");
+ int i = 0;
+
+ for (Command command : commands) {
+ msg.append(command.getCommandName());
+ if (i++ != commands.length - 1) {
+ msg.append(" | ");
+ }
+ }
+ msg.append(" ] args");
+ System.err.println(msg.toString());
+ }
+}
diff --git a/tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java b/tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java
new file mode 100644
index 000000000..97059bcc3
--- /dev/null
+++ b/tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2011 Google Inc.
+ *
+ * Licensed 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.
+ * under the License.
+ */
+
+package com.google.i18n.phonenumbers.tools;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * Helper class containing methods designed to ease file manipulation.
+ *
+ * @author Philippe Liard
+ */
+public class FileUtils {
+ /**
+ * Silently closes a resource (i.e: don't throw any exception).
+ */
+ private static void close(Closeable closeable) {
+ if (closeable == null) {
+ return;
+ }
+ try {
+ closeable.close();
+ } catch (IOException e) {
+ System.err.println(e.getMessage());
+ }
+ }
+
+ /**
+ * Silently closes multiple resources. This method doesn't throw any exception when an error
+ * occurs when a resource is being closed.
+ */
+ public static void closeFiles(Closeable ... closeables) {
+ for (Closeable closeable : closeables) {
+ close(closeable);
+ }
+ }
+}
diff --git a/tools/java/pom.xml b/tools/java/pom.xml
new file mode 100644
index 000000000..0fb4b82c5
--- /dev/null
+++ b/tools/java/pom.xml
@@ -0,0 +1,26 @@
+
+