From 1e7912a3445065f91b884905d436ba172325b8a9 Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Fri, 29 Jun 2012 15:18:38 +0000 Subject: [PATCH] TOOLS: Update the BuildMetadataProtoFromXml tool to build data files for alternate formats as well. Review URL: https://codereview.appspot.com/6355051 --- .../BuildMetadataProtoFromXml.java | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java index 4b0b902cb..9b7f97572 100644 --- a/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java +++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java @@ -35,18 +35,25 @@ import java.util.Map; */ 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 ALTERNATE_FORMATS_FILE_PREFIX = + "/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto"; + private static final String COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME = "CountryCodeToRegionCodeMap"; + private static final String TEST_COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME = + "CountryCodeToRegionCodeMapForTesting"; + private static final String ALTERNATE_COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME = + "CountryCodeToRegionCodeMapForAlternate"; private static final String HELP_MESSAGE = "Usage:\n" + - "BuildMetadataProtoFromXml []\n" + + "BuildMetadataProtoFromXml " + + " []\n" + "\n" + "where:\n" + " inputFile The input file containing phone number metadata in XML format.\n" + @@ -54,6 +61,7 @@ public class BuildMetadataProtoFromXml extends Command { " format (one file per region) and the country code to region code\n" + " mapping file.\n" + " forTesting Flag whether to generate metadata for testing purposes or not.\n" + + " forAlternate Flag whether to generate metadata for alternate formats or not.\n" + " liteBuild Whether to generate the lite-version of the metadata (default:\n" + " false). When set to true certain metadata will be omitted.\n" + " At this moment, example numbers information is omitted.\n" + @@ -65,7 +73,7 @@ public class BuildMetadataProtoFromXml extends Command { COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME + ".java\n" + "\n" + "Example command line invocation:\n" + - "BuildMetadataProtoFromXml PhoneNumberMetadata.xml src false false\n"; + "BuildMetadataProtoFromXml PhoneNumberMetadata.xml src false false false\n"; private static final String GENERATION_COMMENT = "/* This file is automatically generated by {@link BuildMetadataProtoFromXml}.\n" + @@ -80,17 +88,20 @@ public class BuildMetadataProtoFromXml extends Command { @Override public boolean start() { String[] args = getArgs(); - if (args.length != 4 && args.length != 5) { + if (args.length != 5 && args.length != 6) { System.err.println(HELP_MESSAGE); return false; } String inputFile = args[1]; String outputDir = args[2]; boolean forTesting = args[3].equals("true"); - boolean liteBuild = args.length > 4 && args[4].equals("true"); + boolean forAlternate = args[4].equals("true"); + boolean liteBuild = args.length > 5 && args[5].equals("true"); String filePrefix; - if (forTesting) { + if (forAlternate) { + filePrefix = outputDir + ALTERNATE_FORMATS_FILE_PREFIX; + } else if (forTesting) { filePrefix = outputDir + TEST_META_DATA_FILE_PREFIX; } else { filePrefix = outputDir + META_DATA_FILE_PREFIX; @@ -102,9 +113,9 @@ public class BuildMetadataProtoFromXml extends Command { for (PhoneMetadata metadata : metadataCollection.getMetadataList()) { String regionCode = metadata.getId(); - // For non-geographical country calling codes (e.g. +800), use the country calling codes - // instead of the region code to form the file name. - if (regionCode.equals("001")) { + // For non-geographical country calling codes (e.g. +800), or for alternate formats, use the + // country calling codes instead of the region code to form the file name. + if (regionCode.equals("001") || regionCode.isEmpty()) { regionCode = Integer.toString(metadata.getCountryCode()); } PhoneMetadataCollection outMetadataCollection = new PhoneMetadataCollection(); @@ -118,7 +129,8 @@ public class BuildMetadataProtoFromXml extends Command { Map> countryCodeToRegionCodeMap = BuildMetadataFromXml.buildCountryCodeToRegionCodeMap(metadataCollection); - writeCountryCallingCodeMappingToJavaFile(countryCodeToRegionCodeMap, outputDir, forTesting); + writeCountryCallingCodeMappingToJavaFile( + countryCodeToRegionCodeMap, outputDir, forTesting, forAlternate); } catch (Exception e) { e.printStackTrace(); return false; @@ -145,9 +157,13 @@ public class BuildMetadataProtoFromXml extends Command { private static void writeCountryCallingCodeMappingToJavaFile( Map> countryCodeToRegionCodeMap, - String outputDir, boolean forTesting) throws IOException { + String outputDir, boolean forTesting, boolean forAlternate) throws IOException { String mappingClassName; - if (forTesting) { + if (forAlternate) { + // For alternate formats there will be no region codes in the map (and a set would have been + // more appropriate), but we are lazy and re-use existing infrastructure. + mappingClassName = ALTERNATE_COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME; + } else if (forTesting) { mappingClassName = TEST_COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME; } else { mappingClassName = COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME;