From 8b6ab4df67570d860f14c58085e4ed47232d1166 Mon Sep 17 00:00:00 2001 From: "jia.shao.peng" Date: Tue, 5 Apr 2011 12:09:41 +0000 Subject: [PATCH] TOOLS: Adding common library for libphonenumber tools. Patch contributed by philip.liard git-svn-id: http://libphonenumber.googlecode.com/svn/trunk@157 ee073f10-1060-11df-b6a4-87a95322a99c --- tools/java/common/pom.xml | 37 ++++++++++ .../google/i18n/phonenumbers/tools/Command.java | 51 ++++++++++++++ .../i18n/phonenumbers/tools/CommandDispatcher.java | 78 ++++++++++++++++++++++ .../google/i18n/phonenumbers/tools/FileUtils.java | 52 +++++++++++++++ tools/java/pom.xml | 26 ++++++++ 5 files changed, 244 insertions(+) create mode 100644 tools/java/common/pom.xml create mode 100644 tools/java/common/src/com/google/i18n/phonenumbers/tools/Command.java create mode 100644 tools/java/common/src/com/google/i18n/phonenumbers/tools/CommandDispatcher.java create mode 100644 tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java create mode 100644 tools/java/pom.xml diff --git a/tools/java/common/pom.xml b/tools/java/common/pom.xml new file mode 100644 index 0000000..14a8962 --- /dev/null +++ b/tools/java/common/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + + tools + com.google.i18n.phonenumbers + 1.0-SNAPSHOT + + + com.google.i18n.phonenumbers.tools + common + 1.0-SNAPSHOT + Libphonenumber common library for build tools + + This library contains helper classes designed to ease file manipulation and command dispatching + which is required by build tools dealing with code generation and multiple commands invocation + from a single entry point. + + + + src + + + 2.3.2 + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + + + diff --git a/tools/java/common/src/com/google/i18n/phonenumbers/tools/Command.java b/tools/java/common/src/com/google/i18n/phonenumbers/tools/Command.java new file mode 100644 index 0000000..4f812fa --- /dev/null +++ b/tools/java/common/src/com/google/i18n/phonenumbers/tools/Command.java @@ -0,0 +1,51 @@ +/* + * 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; + +/** + * Abstract class defining a common interface for commands provided by build tools (e.g: commands to + * generate code or to download source files). + * + *

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 0000000..73b5f01 --- /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 0000000..97059bc --- /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 0000000..0fb4b82 --- /dev/null +++ b/tools/java/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + com.google.i18n.phonenumbers + tools + pom + 1.0-SNAPSHOT + Libphonenumber build tools + + + UTF-8 + + + + + Apache 2 + http://www.apache.org/licenses/LICENSE-2.0.txt + Copyright (C) 2011 Google Inc. + + + + + common + + + -- 2.7.4