Browse Source

TOOLS: Adding common library for libphonenumber tools.

pull/567/head
Philip Liard 15 years ago
committed by Mihaela Rosca
parent
commit
3185995bd8
5 changed files with 244 additions and 0 deletions
  1. +37
    -0
      tools/java/common/pom.xml
  2. +51
    -0
      tools/java/common/src/com/google/i18n/phonenumbers/tools/Command.java
  3. +78
    -0
      tools/java/common/src/com/google/i18n/phonenumbers/tools/CommandDispatcher.java
  4. +52
    -0
      tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java
  5. +26
    -0
      tools/java/pom.xml

+ 37
- 0
tools/java/common/pom.xml View File

@ -0,0 +1,37 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>tools</artifactId>
<groupId>com.google.i18n.phonenumbers</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.google.i18n.phonenumbers.tools</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Libphonenumber common library for build tools</name>
<description>
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.
</description>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<version>2.3.2</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

+ 51
- 0
tools/java/common/src/com/google/i18n/phonenumbers/tools/Command.java View File

@ -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).
*
* <p> 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;
}
}

+ 78
- 0
tools/java/common/src/com/google/i18n/phonenumbers/tools/CommandDispatcher.java View File

@ -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.
*
* <p> 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());
}
}

+ 52
- 0
tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java View File

@ -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);
}
}
}

+ 26
- 0
tools/java/pom.xml View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.i18n.phonenumbers</groupId>
<artifactId>tools</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Libphonenumber build tools</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<comments>Copyright (C) 2011 Google Inc.</comments>
</license>
</licenses>
<modules>
<module>common</module>
</modules>
</project>

Loading…
Cancel
Save