diff --git a/java/build.xml b/java/build.xml
index 53b18ef00..0bb36aa78 100644
--- a/java/build.xml
+++ b/java/build.xml
@@ -4,7 +4,6 @@
-
@@ -26,7 +25,6 @@
-
@@ -61,9 +59,6 @@
-
-
-
diff --git a/java/resources/com/google/i18n/phonenumbers/BuildMetadataFromXml.java b/java/resources/com/google/i18n/phonenumbers/BuildMetadataFromXml.java
deleted file mode 100644
index a73ffeed9..000000000
--- a/java/resources/com/google/i18n/phonenumbers/BuildMetadataFromXml.java
+++ /dev/null
@@ -1,344 +0,0 @@
-/*
- * Copyright (C) 2009 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;
-
-import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat;
-import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata;
-import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection;
-import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import java.util.regex.Pattern;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
-/**
- * Library to build phone number metadata from the XML format.
- *
- * @author Shaopeng Jia
- */
-public class BuildMetadataFromXml {
- private static final Logger LOGGER = Logger.getLogger(BuildMetadataFromXml.class.getName());
- private static Boolean liteBuild;
-
- // Build the PhoneMetadataCollection from the input XML file.
- public static PhoneMetadataCollection buildPhoneMetadataCollection(String inputXmlFile,
- boolean liteBuild) throws Exception {
- BuildMetadataFromXml.liteBuild = liteBuild;
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = builderFactory.newDocumentBuilder();
- File xmlFile = new File(inputXmlFile);
- Document document = builder.parse(xmlFile);
- document.getDocumentElement().normalize();
- Element rootElement = document.getDocumentElement();
- NodeList territory = rootElement.getElementsByTagName("territory");
- PhoneMetadataCollection metadataCollection = new PhoneMetadataCollection();
- int numOfTerritories = territory.getLength();
- for (int i = 0; i < numOfTerritories; i++) {
- Element territoryElement = (Element) territory.item(i);
- String regionCode = territoryElement.getAttribute("id");
- PhoneMetadata metadata = loadCountryMetadata(regionCode, territoryElement);
- metadataCollection.addMetadata(metadata);
- }
- return metadataCollection;
- }
-
- // Build a mapping from a country calling code to the region codes which denote the country/region
- // represented by that country code. In the case of multiple countries sharing a calling code,
- // such as the NANPA countries, the one indicated with "isMainCountryForCode" in the metadata
- // should be first.
- public static Map> buildCountryCodeToRegionCodeMap(
- PhoneMetadataCollection metadataCollection) {
- Map> countryCodeToRegionCodeMap =
- new TreeMap>();
- for (PhoneMetadata metadata : metadataCollection.getMetadataList()) {
- String regionCode = metadata.getId();
- int countryCode = metadata.getCountryCode();
- if (countryCodeToRegionCodeMap.containsKey(countryCode)) {
- if (metadata.isMainCountryForCode()) {
- countryCodeToRegionCodeMap.get(countryCode).add(0, regionCode);
- } else {
- countryCodeToRegionCodeMap.get(countryCode).add(regionCode);
- }
- } else {
- // For most countries, there will be only one region code for the country calling code.
- List listWithRegionCode = new ArrayList(1);
- listWithRegionCode.add(regionCode);
- countryCodeToRegionCodeMap.put(countryCode, listWithRegionCode);
- }
- }
- return countryCodeToRegionCodeMap;
- }
-
- private static String validateRE(String regex) {
- return validateRE(regex, false);
- }
-
- private static String validateRE(String regex, boolean removeWhitespace) {
- // Removes all the whitespace and newline from the regexp. Not using pattern compile options to
- // make it work across programming languages.
- if (removeWhitespace) {
- regex = regex.replaceAll("\\s", "");
- }
- Pattern.compile(regex);
- // return regex itself if it is of correct regex syntax
- // i.e. compile did not fail with a PatternSyntaxException.
- return regex;
- }
-
- private static PhoneMetadata loadCountryMetadata(String regionCode, Element element) {
- PhoneMetadata metadata = new PhoneMetadata();
- metadata.setId(regionCode);
- metadata.setCountryCode(Integer.parseInt(element.getAttribute("countryCode")));
- if (element.hasAttribute("leadingDigits")) {
- metadata.setLeadingDigits(validateRE(element.getAttribute("leadingDigits")));
- }
- metadata.setInternationalPrefix(validateRE(element.getAttribute("internationalPrefix")));
- if (element.hasAttribute("preferredInternationalPrefix")) {
- String preferredInternationalPrefix = element.getAttribute("preferredInternationalPrefix");
- metadata.setPreferredInternationalPrefix(preferredInternationalPrefix);
- }
- if (element.hasAttribute("nationalPrefixForParsing")) {
- metadata.setNationalPrefixForParsing(
- validateRE(element.getAttribute("nationalPrefixForParsing")));
- if (element.hasAttribute("nationalPrefixTransformRule")) {
- metadata.setNationalPrefixTransformRule(
- validateRE(element.getAttribute("nationalPrefixTransformRule")));
- }
- }
- String nationalPrefix = "";
- String nationalPrefixFormattingRule = "";
- if (element.hasAttribute("nationalPrefix")) {
- nationalPrefix = element.getAttribute("nationalPrefix");
- metadata.setNationalPrefix(nationalPrefix);
- nationalPrefixFormattingRule =
- getNationalPrefixFormattingRuleFromElement(element, nationalPrefix);
-
- if (!metadata.hasNationalPrefixForParsing()) {
- metadata.setNationalPrefixForParsing(nationalPrefix);
- }
- }
- String carrierCodeFormattingRule = "";
- if (element.hasAttribute("carrierCodeFormattingRule")) {
- carrierCodeFormattingRule = validateRE(
- getDomesticCarrierCodeFormattingRuleFromElement(element, nationalPrefix));
- }
- if (element.hasAttribute("preferredExtnPrefix")) {
- metadata.setPreferredExtnPrefix(element.getAttribute("preferredExtnPrefix"));
- }
- if (element.hasAttribute("mainCountryForCode")) {
- metadata.setMainCountryForCode(true);
- }
- if (element.hasAttribute("leadingZeroPossible")) {
- metadata.setLeadingZeroPossible(true);
- }
-
- // Extract availableFormats
- NodeList numberFormatElements = element.getElementsByTagName("numberFormat");
- boolean hasExplicitIntlFormatDefined = false;
-
- int numOfFormatElements = numberFormatElements.getLength();
- if (numOfFormatElements > 0) {
- for (int i = 0; i < numOfFormatElements; i++) {
- Element numberFormatElement = (Element) numberFormatElements.item(i);
- NumberFormat format = new NumberFormat();
-
- if (numberFormatElement.hasAttribute("nationalPrefixFormattingRule")) {
- format.setNationalPrefixFormattingRule(
- getNationalPrefixFormattingRuleFromElement(numberFormatElement, nationalPrefix));
- } else {
- format.setNationalPrefixFormattingRule(nationalPrefixFormattingRule);
- }
- if (numberFormatElement.hasAttribute("carrierCodeFormattingRule")) {
- format.setDomesticCarrierCodeFormattingRule(validateRE(
- getDomesticCarrierCodeFormattingRuleFromElement(numberFormatElement,
- nationalPrefix)));
- } else {
- format.setDomesticCarrierCodeFormattingRule(carrierCodeFormattingRule);
- }
-
- // Extract the pattern for the national format.
- setLeadingDigitsPatterns(numberFormatElement, format);
- format.setPattern(validateRE(numberFormatElement.getAttribute("pattern")));
-
- NodeList formatPattern = numberFormatElement.getElementsByTagName("format");
- if (formatPattern.getLength() != 1) {
- LOGGER.log(Level.SEVERE,
- "Only one format pattern for a numberFormat element should be defined.");
- throw new RuntimeException("Invalid number of format patterns for country: " +
- regionCode);
- }
- String nationalFormat = formatPattern.item(0).getFirstChild().getNodeValue();
- format.setFormat(nationalFormat);
- metadata.addNumberFormat(format);
-
- // Extract the pattern for international format. If there is no intlFormat, default to
- // using the national format. If the intlFormat is set to "NA" the intlFormat should be
- // ignored.
- NumberFormat intlFormat = new NumberFormat();
- setLeadingDigitsPatterns(numberFormatElement, intlFormat);
- intlFormat.setPattern(numberFormatElement.getAttribute("pattern"));
- NodeList intlFormatPattern = numberFormatElement.getElementsByTagName("intlFormat");
-
- if (intlFormatPattern.getLength() > 1) {
- LOGGER.log(Level.SEVERE,
- "A maximum of one intlFormat pattern for a numberFormat element should be " +
- "defined.");
- throw new RuntimeException("Invalid number of intlFormat patterns for country: " +
- regionCode);
- } else if (intlFormatPattern.getLength() == 0) {
- // Default to use the same as the national pattern if none is defined.
- intlFormat.setFormat(nationalFormat);
- } else {
- String intlFormatPatternValue =
- intlFormatPattern.item(0).getFirstChild().getNodeValue();
- if (!intlFormatPatternValue.equals("NA")) {
- intlFormat.setFormat(intlFormatPatternValue);
- }
- hasExplicitIntlFormatDefined = true;
- }
-
- if (intlFormat.hasFormat()) {
- metadata.addIntlNumberFormat(intlFormat);
- }
- }
- // Only a small number of regions need to specify the intlFormats in the xml. For the majority
- // of countries the intlNumberFormat metadata is an exact copy of the national NumberFormat
- // metadata. To minimize the size of the metadata file, we only keep intlNumberFormats that
- // actually differ in some way to the national formats.
- if (!hasExplicitIntlFormatDefined) {
- metadata.clearIntlNumberFormat();
- }
- }
-
- PhoneNumberDesc generalDesc = new PhoneNumberDesc();
- generalDesc = processPhoneNumberDescElement(generalDesc, element, "generalDesc");
- metadata.setGeneralDesc(generalDesc);
- metadata.setFixedLine(processPhoneNumberDescElement(generalDesc, element, "fixedLine"));
- metadata.setMobile(processPhoneNumberDescElement(generalDesc, element, "mobile"));
- metadata.setTollFree(processPhoneNumberDescElement(generalDesc, element, "tollFree"));
- metadata.setPremiumRate(processPhoneNumberDescElement(generalDesc, element, "premiumRate"));
- metadata.setSharedCost(processPhoneNumberDescElement(generalDesc, element, "sharedCost"));
- metadata.setVoip(processPhoneNumberDescElement(generalDesc, element, "voip"));
- metadata.setPersonalNumber(processPhoneNumberDescElement(generalDesc, element,
- "personalNumber"));
- metadata.setPager(processPhoneNumberDescElement(generalDesc, element, "pager"));
- metadata.setUan(processPhoneNumberDescElement(generalDesc, element, "uan"));
- metadata.setNoInternationalDialling(processPhoneNumberDescElement(generalDesc, element,
- "noInternationalDialling"));
-
- if (metadata.getMobile().getNationalNumberPattern().equals(
- metadata.getFixedLine().getNationalNumberPattern())) {
- metadata.setSameMobileAndFixedLinePattern(true);
- }
- return metadata;
- }
-
- private static void setLeadingDigitsPatterns(Element numberFormatElement, NumberFormat format) {
- NodeList leadingDigitsPatternNodes = numberFormatElement.getElementsByTagName("leadingDigits");
- int numOfLeadingDigitsPatterns = leadingDigitsPatternNodes.getLength();
- if (numOfLeadingDigitsPatterns > 0) {
- for (int i = 0; i < numOfLeadingDigitsPatterns; i++) {
- format.addLeadingDigitsPattern(
- validateRE((leadingDigitsPatternNodes.item(i)).getFirstChild().getNodeValue(), true));
- }
- }
- }
-
- private static String getNationalPrefixFormattingRuleFromElement(Element element,
- String nationalPrefix) {
- String nationalPrefixFormattingRule = element.getAttribute("nationalPrefixFormattingRule");
- // Replace $NP with national prefix and $FG with the first group ($1).
- nationalPrefixFormattingRule =
- nationalPrefixFormattingRule.replaceFirst("\\$NP", nationalPrefix)
- .replaceFirst("\\$FG", "\\$1");
- return nationalPrefixFormattingRule;
- }
-
- private static String getDomesticCarrierCodeFormattingRuleFromElement(Element element,
- String nationalPrefix) {
- String carrierCodeFormattingRule = element.getAttribute("carrierCodeFormattingRule");
- // Replace $FG with the first group ($1) and $NP with the national prefix.
- carrierCodeFormattingRule = carrierCodeFormattingRule.replaceFirst("\\$FG", "\\$1")
- .replaceFirst("\\$NP", nationalPrefix);
- return carrierCodeFormattingRule;
- }
-
- /**
- * Processes a phone number description element from the XML file and returns it as a
- * PhoneNumberDesc. If the description element is a fixed line or mobile number, the general
- * description will be used to fill in the whole element if necessary, or any components that are
- * missing. For all other types, the general description will only be used to fill in missing
- * components if the type has a partial definition. For example, if no "tollFree" element exists,
- * we assume there are no toll free numbers for that locale, and return a phone number description
- * with "NA" for both the national and possible number patterns.
- *
- * @param generalDesc a generic phone number description that will be used to fill in missing
- * parts of the description
- * @param countryElement the XML element representing all the country information
- * @param numberType the name of the number type, corresponding to the appropriate tag in the XML
- * file with information about that type
- * @return complete description of that phone number type
- */
- private static PhoneNumberDesc processPhoneNumberDescElement(PhoneNumberDesc generalDesc,
- Element countryElement,
- String numberType) {
- NodeList phoneNumberDescList = countryElement.getElementsByTagName(numberType);
- PhoneNumberDesc numberDesc = new PhoneNumberDesc();
- if (phoneNumberDescList.getLength() == 0 &&
- (!numberType.equals("fixedLine") && !numberType.equals("mobile") &&
- !numberType.equals("generalDesc"))) {
- numberDesc.setNationalNumberPattern("NA");
- numberDesc.setPossibleNumberPattern("NA");
- return numberDesc;
- }
- numberDesc.mergeFrom(generalDesc);
- if (phoneNumberDescList.getLength() > 0) {
- Element element = (Element) phoneNumberDescList.item(0);
- NodeList possiblePattern = element.getElementsByTagName("possibleNumberPattern");
- if (possiblePattern.getLength() > 0) {
- numberDesc.setPossibleNumberPattern(
- validateRE(possiblePattern.item(0).getFirstChild().getNodeValue(), true));
- }
-
- NodeList validPattern = element.getElementsByTagName("nationalNumberPattern");
- if (validPattern.getLength() > 0) {
- numberDesc.setNationalNumberPattern(
- validateRE(validPattern.item(0).getFirstChild().getNodeValue(), true));
- }
-
- if (!liteBuild) {
- NodeList exampleNumber = element.getElementsByTagName("exampleNumber");
- if (exampleNumber.getLength() > 0) {
- numberDesc.setExampleNumber(exampleNumber.item(0).getFirstChild().getNodeValue());
- }
- }
- }
- return numberDesc;
- }
-}
diff --git a/java/src/com/google/i18n/phonenumbers/Phonemetadata.java b/java/src/com/google/i18n/phonenumbers/Phonemetadata.java
index de9f7c0a3..4dc21a5fc 100644
--- a/java/src/com/google/i18n/phonenumbers/Phonemetadata.java
+++ b/java/src/com/google/i18n/phonenumbers/Phonemetadata.java
@@ -588,6 +588,9 @@ public final class Phonemetadata {
private boolean mainCountryForCode_ = false;
public boolean hasMainCountryForCode() { return hasMainCountryForCode; }
public boolean isMainCountryForCode() { return mainCountryForCode_; }
+ // Method that lets this class have the same interface as the one generated by Protocol Buffers
+ // which is used by C++ build tools.
+ public boolean getMainCountryForCode() { return mainCountryForCode_; }
public PhoneMetadata setMainCountryForCode(boolean value) {
hasMainCountryForCode = true;
mainCountryForCode_ = value;
diff --git a/tools/java/common/src/com/google/i18n/phonenumbers/tools/BuildMetadataFromXml.java b/tools/java/common/src/com/google/i18n/phonenumbers/tools/BuildMetadataFromXml.java
index 3a8787c3b..3b19d736f 100644
--- a/tools/java/common/src/com/google/i18n/phonenumbers/tools/BuildMetadataFromXml.java
+++ b/tools/java/common/src/com/google/i18n/phonenumbers/tools/BuildMetadataFromXml.java
@@ -70,7 +70,7 @@ public class BuildMetadataFromXml {
// Build a mapping from a country calling code to the region codes which denote the country/region
// represented by that country code. In the case of multiple countries sharing a calling code,
- // such as the NANPA countries, the one indicated with "isMainCountryForCode" in the metadata
+ // such as the NANPA countries, the one indicated with "getMainCountryForCode" in the metadata
// should be first.
public static Map> buildCountryCodeToRegionCodeMap(
PhoneMetadataCollection metadataCollection) {
diff --git a/java/resources/com/google/i18n/phonenumbers/BuildMetadataJsonFromXml.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataJsonFromXml.java
similarity index 83%
rename from java/resources/com/google/i18n/phonenumbers/BuildMetadataJsonFromXml.java
rename to tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataJsonFromXml.java
index c25b96a5a..a37fd3bd6 100644
--- a/java/resources/com/google/i18n/phonenumbers/BuildMetadataJsonFromXml.java
+++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataJsonFromXml.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.google.i18n.phonenumbers;
+package com.google.i18n.phonenumbers.tools;
import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat;
import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata;
@@ -33,7 +33,7 @@ import java.util.Map;
*
* @author Nikolaos Trogkanis
*/
-public class BuildMetadataJsonFromXml {
+public class BuildMetadataJsonFromXml extends Command {
private static final String NAMESPACE = "i18n.phonenumbers.metadata";
private static final String HELP_MESSAGE =
@@ -50,24 +50,6 @@ public class BuildMetadataJsonFromXml {
"Example command line invocation:\n" +
"BuildMetadataJsonFromXml PhoneNumberMetadata.xml metadatalite.js true\n";
- static final String COPYRIGHT_NOTICE =
- "/**\n" +
- " * @license\n" +
- " * Copyright (C) 2010 Google Inc.\n" +
- " *\n" +
- " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
- " * you may not use this file except in compliance with the License.\n" +
- " * You may obtain a copy of the License at\n" +
- " *\n" +
- " * http://www.apache.org/licenses/LICENSE-2.0\n" +
- " *\n" +
- " * Unless required by applicable law or agreed to in writing, software\n" +
- " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
- " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
- " * See the License for the specific language governing permissions and\n" +
- " * limitations under the License.\n" +
- " */\n\n";
-
private static final String FILE_OVERVIEW =
"/**\n" +
" * @fileoverview Generated metadata for file\n" +
@@ -90,40 +72,54 @@ public class BuildMetadataJsonFromXml {
" * @type {Object.}\n" +
" */\n";
- public static void main(String[] args) throws Exception {
- if (args.length != 2 && args.length != 3) {
+ @Override
+ public String getCommandName() {
+ return "BuildMetadataJsonFromXml";
+ }
+
+ @Override
+ public boolean start() {
+ String[] args = getArgs();
+
+ if (args.length != 3 && args.length != 4) {
System.err.println(HELP_MESSAGE);
- System.exit(1);
+ return false;
}
- String inputFile = args[0];
- String outputFile = args[1];
- boolean liteBuild = args.length > 2 && args[2].equals("true");
+ String inputFile = args[1];
+ String outputFile = args[2];
+ boolean liteBuild = args.length > 3 && args[3].equals("true");
- PhoneMetadataCollection metadataCollection =
- BuildMetadataFromXml.buildPhoneMetadataCollection(inputFile, liteBuild);
- Map> countryCodeToRegionCodeMap =
- BuildMetadataFromXml.buildCountryCodeToRegionCodeMap(metadataCollection);
+ try {
+ PhoneMetadataCollection metadataCollection =
+ BuildMetadataFromXml.buildPhoneMetadataCollection(inputFile, liteBuild);
+ Map> countryCodeToRegionCodeMap =
+ BuildMetadataFromXml.buildCountryCodeToRegionCodeMap(metadataCollection);
- BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
+ BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
- writer.write(COPYRIGHT_NOTICE);
- Formatter formatter = new Formatter(writer);
- formatter.format(FILE_OVERVIEW, inputFile);
+ writer.write(CopyrightNotice.TEXT);
+ Formatter formatter = new Formatter(writer);
+ formatter.format(FILE_OVERVIEW, inputFile);
- writer.write("goog.provide('" + NAMESPACE + "');\n\n");
+ writer.write("goog.provide('" + NAMESPACE + "');\n\n");
- writer.write(COUNTRY_CODE_TO_REGION_CODE_MAP_COMMENT);
- writer.write(NAMESPACE + ".countryCodeToRegionCodeMap = ");
- writeCountryCodeToRegionCodeMap(countryCodeToRegionCodeMap, writer);
- writer.write(";\n\n");
+ writer.write(COUNTRY_CODE_TO_REGION_CODE_MAP_COMMENT);
+ writer.write(NAMESPACE + ".countryCodeToRegionCodeMap = ");
+ writeCountryCodeToRegionCodeMap(countryCodeToRegionCodeMap, writer);
+ writer.write(";\n\n");
- writer.write(COUNTRY_TO_METADATA_COMMENT);
- writer.write(NAMESPACE + ".countryToMetadata = ");
- writeCountryToMetadataMap(metadataCollection, writer);
- writer.write(";\n");
+ writer.write(COUNTRY_TO_METADATA_COMMENT);
+ writer.write(NAMESPACE + ".countryToMetadata = ");
+ writeCountryToMetadataMap(metadataCollection, writer);
+ writer.write(";\n");
- writer.flush();
- writer.close();
+ writer.flush();
+ writer.close();
+ } catch (Exception e) {
+ System.err.println(HELP_MESSAGE);
+ return false;
+ }
+ return true;
}
// Writes a PhoneMetadataCollection in JSON format.
diff --git a/java/resources/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataProtoFromXml.java
similarity index 68%
rename from java/resources/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java
rename to tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataProtoFromXml.java
index 224660670..2e488c1c6 100644
--- a/java/resources/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java
+++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataProtoFromXml.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.google.i18n.phonenumbers;
+package com.google.i18n.phonenumbers.tools;
import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata;
import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection;
@@ -33,8 +33,12 @@ import java.util.Map;
*
* @author Shaopeng Jia
*/
-public class BuildMetadataProtoFromXml {
- private static final String PACKAGE_NAME = PhoneNumberUtil.class.getPackage().getName();
+public class BuildMetadataProtoFromXml extends Command {
+ private static final String PACKAGE_NAME = "com/google/i18n/phonenumbers";
+ private static final String META_DATA_FILE_PREFIX =
+ "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto";
+ private static final String TEST_META_DATA_FILE_PREFIX =
+ "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting";
private static final String TEST_COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME =
"CountryCodeToRegionCodeMapForTesting";
private static final String COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME =
@@ -55,69 +59,63 @@ public class BuildMetadataProtoFromXml {
" At this moment, example numbers information is omitted.\n" +
"\n" +
"Metadata will be stored in:\n" +
- " " + PhoneNumberUtil.META_DATA_FILE_PREFIX + "_*\n" +
+ " " + META_DATA_FILE_PREFIX + "_*\n" +
"Mapping file will be stored in:\n" +
- " /" + PACKAGE_NAME.replaceAll("\\.", "/") + "/" +
+ " /" + PACKAGE_NAME + "/" +
COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME + ".java\n" +
"\n" +
"Example command line invocation:\n" +
"BuildMetadataProtoFromXml PhoneNumberMetadata.xml src false false\n";
- public static void main(String[] args) throws Exception {
- if (args.length != 3 && args.length != 4) {
+ @Override
+ public String getCommandName() {
+ return "BuildMetadataProtoFromXml";
+ }
+
+ @Override
+ public boolean start() {
+ String[] args = getArgs();
+ if (args.length != 4 && args.length != 5) {
System.err.println(HELP_MESSAGE);
- System.exit(1);
+ return false;
}
- String inputFile = args[0];
- String outputDir = args[1];
- boolean forTesting = args[2].equals("true");
- boolean liteBuild = args.length > 3 && args[3].equals("true");
+ String inputFile = args[1];
+ String outputDir = args[2];
+ boolean forTesting = args[3].equals("true");
+ boolean liteBuild = args.length > 4 && args[4].equals("true");
String filePrefix;
if (forTesting) {
- filePrefix = outputDir + PhoneNumberUtilTest.TEST_META_DATA_FILE_PREFIX;
+ filePrefix = outputDir + TEST_META_DATA_FILE_PREFIX;
} else {
- filePrefix = outputDir + PhoneNumberUtil.META_DATA_FILE_PREFIX;
+ filePrefix = outputDir + META_DATA_FILE_PREFIX;
}
- PhoneMetadataCollection metadataCollection =
- BuildMetadataFromXml.buildPhoneMetadataCollection(inputFile, liteBuild);
-
- for (PhoneMetadata metadata : metadataCollection.getMetadataList()) {
- String regionCode = metadata.getId();
- PhoneMetadataCollection outMetadataCollection = new PhoneMetadataCollection();
- outMetadataCollection.addMetadata(metadata);
- FileOutputStream outputForRegion = new FileOutputStream(filePrefix + "_" + regionCode);
- ObjectOutputStream out = new ObjectOutputStream(outputForRegion);
- outMetadataCollection.writeExternal(out);
- out.close();
- }
+ try {
+ PhoneMetadataCollection metadataCollection =
+ BuildMetadataFromXml.buildPhoneMetadataCollection(inputFile, liteBuild);
+
+ for (PhoneMetadata metadata : metadataCollection.getMetadataList()) {
+ String regionCode = metadata.getId();
+ PhoneMetadataCollection outMetadataCollection = new PhoneMetadataCollection();
+ outMetadataCollection.addMetadata(metadata);
+ FileOutputStream outputForRegion = new FileOutputStream(filePrefix + "_" + regionCode);
+ ObjectOutputStream out = new ObjectOutputStream(outputForRegion);
+ outMetadataCollection.writeExternal(out);
+ out.close();
+ }
- Map> countryCodeToRegionCodeMap =
- BuildMetadataFromXml.buildCountryCodeToRegionCodeMap(metadataCollection);
+ Map> countryCodeToRegionCodeMap =
+ BuildMetadataFromXml.buildCountryCodeToRegionCodeMap(metadataCollection);
- writeCountryCallingCodeMappingToJavaFile(countryCodeToRegionCodeMap, outputDir, forTesting);
+ writeCountryCallingCodeMappingToJavaFile(countryCodeToRegionCodeMap, outputDir, forTesting);
+ } catch (Exception e) {
+ System.err.println(HELP_MESSAGE);
+ return false;
+ }
+ return true;
}
- static final String COPYRIGHT_NOTICE =
- "/*\n" +
- " * Copyright (C) 2010 Google Inc.\n" +
- " *\n" +
- " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
- " * you may not use this file except in compliance with the License.\n" +
- " * You may obtain a copy of the License at\n" +
- " *\n" +
- " * http://www.apache.org/licenses/LICENSE-2.0\n" +
- " *\n" +
- " * Unless required by applicable law or agreed to in writing, software\n" +
- " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
- " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
- " * See the License for the specific language governing permissions and\n" +
- " * limitations under the License.\n" +
- " *\n" +
- " * This file is automatically generated by BuildMetadataProtoFromXml. Please\n" +
- " * don't modify directly.\n" +
- " */\n\n";
private static final String MAPPING_IMPORTS =
"import java.util.ArrayList;\n" +
"import java.util.HashMap;\n" +
@@ -148,7 +146,7 @@ public class BuildMetadataProtoFromXml {
BufferedWriter writer = new BufferedWriter(new FileWriter(mappingFile));
- writer.write(COPYRIGHT_NOTICE);
+ writer.write(CopyrightNotice.TEXT);
if (PACKAGE_NAME.length() > 0) {
writer.write("package " + PACKAGE_NAME + ";\n\n");
}
diff --git a/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/EntryPoint.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/EntryPoint.java
new file mode 100644
index 000000000..cad95181c
--- /dev/null
+++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/EntryPoint.java
@@ -0,0 +1,35 @@
+/*
+ * 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;
+
+/**
+ * Entry point class for Java and JavaScript build tools.
+ *
+ * @author Philippe Liard
+ */
+public class EntryPoint {
+
+ public static void main(String[] args) {
+ boolean status = new CommandDispatcher(args, new Command[] {
+ new BuildMetadataJsonFromXml(),
+ new BuildMetadataProtoFromXml(),
+ new GenerateAreaCodeData(),
+ }).start();
+
+ System.exit(status ? 0 : 1);
+ }
+}
diff --git a/java/resources/com/google/i18n/phonenumbers/geocoding/GenerateAreaCodeData.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/GenerateAreaCodeData.java
similarity index 92%
rename from java/resources/com/google/i18n/phonenumbers/geocoding/GenerateAreaCodeData.java
rename to tools/java/java-build/src/com/google/i18n/phonenumbers/tools/GenerateAreaCodeData.java
index 6652634df..a21e212ec 100644
--- a/java/resources/com/google/i18n/phonenumbers/geocoding/GenerateAreaCodeData.java
+++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/GenerateAreaCodeData.java
@@ -14,7 +14,10 @@
* limitations under the License.
*/
-package com.google.i18n.phonenumbers.geocoding;
+package com.google.i18n.phonenumbers.tools;
+
+import com.google.i18n.phonenumbers.geocoding.AreaCodeMap;
+import com.google.i18n.phonenumbers.geocoding.MappingFileProvider;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
@@ -49,7 +52,7 @@ import java.util.logging.Logger;
*
* @author Philippe Liard
*/
-public class GenerateAreaCodeData {
+public class GenerateAreaCodeData extends Command {
// The path to the input directory containing the languages directories.
private final File inputPath;
// The path to the output directory.
@@ -59,6 +62,15 @@ public class GenerateAreaCodeData {
private static final Logger LOGGER = Logger.getLogger(GenerateAreaCodeData.class.getName());
+ /**
+ * Empty constructor used by the EntryPoint class.
+ */
+ public GenerateAreaCodeData() {
+ inputPath = null;
+ outputPath = null;
+ forTesting = false;
+ }
+
public GenerateAreaCodeData(File inputPath, File outputPath, boolean forTesting)
throws IOException {
if (!inputPath.isDirectory()) {
@@ -258,21 +270,30 @@ public class GenerateAreaCodeData {
}
}
- public static void main(String[] args) {
- if (args.length != 3) {
+ @Override
+ public String getCommandName() {
+ return "GenerateAreaCodeData";
+ }
+
+ @Override
+ public boolean start() {
+ String[] args = getArgs();
+
+ if (args.length != 4) {
LOGGER.log(Level.SEVERE,
"usage: GenerateAreaCodeData /path/to/input/directory /path/to/output/directory" +
" forTesting");
- System.exit(1);
+ return false;
}
try {
GenerateAreaCodeData generateAreaCodeData =
- new GenerateAreaCodeData(new File(args[0]), new File(args[1]),
- Boolean.parseBoolean(args[2]));
+ new GenerateAreaCodeData(new File(args[1]), new File(args[2]),
+ Boolean.parseBoolean(args[3]));
generateAreaCodeData.run();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage());
- System.exit(1);
+ return false;
}
+ return true;
}
}
diff --git a/java/resources/com/google/i18n/phonenumbers/JSArrayBuilder.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/JSArrayBuilder.java
similarity index 98%
rename from java/resources/com/google/i18n/phonenumbers/JSArrayBuilder.java
rename to tools/java/java-build/src/com/google/i18n/phonenumbers/tools/JSArrayBuilder.java
index 75fcbdb6a..fa91b4f18 100644
--- a/java/resources/com/google/i18n/phonenumbers/JSArrayBuilder.java
+++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/tools/JSArrayBuilder.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.google.i18n.phonenumbers;
+package com.google.i18n.phonenumbers.tools;
import java.util.Iterator;
diff --git a/tools/java/pom.xml b/tools/java/pom.xml
index efd7b7d95..e10480e50 100644
--- a/tools/java/pom.xml
+++ b/tools/java/pom.xml
@@ -21,6 +21,7 @@
cpp-build
+ java-build