Browse Source

TOOLS: Update the BuildMetadataProtoFromXml tool to build data files for alternate formats as well.

Review URL: https://codereview.appspot.com/6355051
pull/567/head
Fredrik Roubert 14 years ago
committed by Mihaela Rosca
parent
commit
1e7912a344
1 changed files with 29 additions and 13 deletions
  1. +29
    -13
      tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java

+ 29
- 13
tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java View File

@ -35,18 +35,25 @@ import java.util.Map;
*/ */
public class BuildMetadataProtoFromXml extends Command { public class BuildMetadataProtoFromXml extends Command {
private static final String PACKAGE_NAME = "com/google/i18n/phonenumbers"; private static final String PACKAGE_NAME = "com/google/i18n/phonenumbers";
private static final String META_DATA_FILE_PREFIX = private static final String META_DATA_FILE_PREFIX =
"/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto"; "/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto";
private static final String TEST_META_DATA_FILE_PREFIX = private static final String TEST_META_DATA_FILE_PREFIX =
"/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting"; "/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 = private static final String COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME =
"CountryCodeToRegionCodeMap"; "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 = private static final String HELP_MESSAGE =
"Usage:\n" + "Usage:\n" +
"BuildMetadataProtoFromXml <inputFile> <outputDir> <forTesting> [<liteBuild>]\n" +
"BuildMetadataProtoFromXml <inputFile> <outputDir>" +
" <forTesting> <forAlternate> [<liteBuild>]\n" +
"\n" + "\n" +
"where:\n" + "where:\n" +
" inputFile The input file containing phone number metadata in XML format.\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" + " format (one file per region) and the country code to region code\n" +
" mapping file.\n" + " mapping file.\n" +
" forTesting Flag whether to generate metadata for testing purposes or not.\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" + " liteBuild Whether to generate the lite-version of the metadata (default:\n" +
" false). When set to true certain metadata will be omitted.\n" + " false). When set to true certain metadata will be omitted.\n" +
" At this moment, example numbers information is 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" + COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME + ".java\n" +
"\n" + "\n" +
"Example command line invocation:\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 = private static final String GENERATION_COMMENT =
"/* This file is automatically generated by {@link BuildMetadataProtoFromXml}.\n" + "/* This file is automatically generated by {@link BuildMetadataProtoFromXml}.\n" +
@ -80,17 +88,20 @@ public class BuildMetadataProtoFromXml extends Command {
@Override @Override
public boolean start() { public boolean start() {
String[] args = getArgs(); String[] args = getArgs();
if (args.length != 4 && args.length != 5) {
if (args.length != 5 && args.length != 6) {
System.err.println(HELP_MESSAGE); System.err.println(HELP_MESSAGE);
return false; return false;
} }
String inputFile = args[1]; String inputFile = args[1];
String outputDir = args[2]; String outputDir = args[2];
boolean forTesting = args[3].equals("true"); 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; String filePrefix;
if (forTesting) {
if (forAlternate) {
filePrefix = outputDir + ALTERNATE_FORMATS_FILE_PREFIX;
} else if (forTesting) {
filePrefix = outputDir + TEST_META_DATA_FILE_PREFIX; filePrefix = outputDir + TEST_META_DATA_FILE_PREFIX;
} else { } else {
filePrefix = outputDir + META_DATA_FILE_PREFIX; filePrefix = outputDir + META_DATA_FILE_PREFIX;
@ -102,9 +113,9 @@ public class BuildMetadataProtoFromXml extends Command {
for (PhoneMetadata metadata : metadataCollection.getMetadataList()) { for (PhoneMetadata metadata : metadataCollection.getMetadataList()) {
String regionCode = metadata.getId(); 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()); regionCode = Integer.toString(metadata.getCountryCode());
} }
PhoneMetadataCollection outMetadataCollection = new PhoneMetadataCollection(); PhoneMetadataCollection outMetadataCollection = new PhoneMetadataCollection();
@ -118,7 +129,8 @@ public class BuildMetadataProtoFromXml extends Command {
Map<Integer, List<String>> countryCodeToRegionCodeMap = Map<Integer, List<String>> countryCodeToRegionCodeMap =
BuildMetadataFromXml.buildCountryCodeToRegionCodeMap(metadataCollection); BuildMetadataFromXml.buildCountryCodeToRegionCodeMap(metadataCollection);
writeCountryCallingCodeMappingToJavaFile(countryCodeToRegionCodeMap, outputDir, forTesting);
writeCountryCallingCodeMappingToJavaFile(
countryCodeToRegionCodeMap, outputDir, forTesting, forAlternate);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
@ -145,9 +157,13 @@ public class BuildMetadataProtoFromXml extends Command {
private static void writeCountryCallingCodeMappingToJavaFile( private static void writeCountryCallingCodeMappingToJavaFile(
Map<Integer, List<String>> countryCodeToRegionCodeMap, Map<Integer, List<String>> countryCodeToRegionCodeMap,
String outputDir, boolean forTesting) throws IOException {
String outputDir, boolean forTesting, boolean forAlternate) throws IOException {
String mappingClassName; 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; mappingClassName = TEST_COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME;
} else { } else {
mappingClassName = COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME; mappingClassName = COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME;


Loading…
Cancel
Save