Browse Source

Generate RegionCode constants for testing.

This generates the RegionCode class that contains string constants for testing using the build tools.

Context: b/237181793
pull/3623/head
silviobraendle 1 year ago
committed by Silvio Brändle
parent
commit
725b617fac
5 changed files with 70 additions and 14 deletions
  1. +2
    -1
      java/build.xml
  2. +1
    -1
      java/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java
  3. +9
    -11
      java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java
  4. +11
    -0
      tools/java/common/src/com/google/i18n/phonenumbers/BuildMetadataFromXml.java
  5. +47
    -1
      tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java

+ 2
- 1
java/build.xml View File

@ -77,8 +77,9 @@
<arg value="--output-dir=${libphonenumber.test.dir}/com/google/i18n/phonenumbers"/>
<arg value="--data-prefix=data/PhoneNumberMetadataProtoForTesting"/>
<arg value="--mapping-class=CountryCodeToRegionCodeMapForTesting"/>
<arg value="--copyright=2010"/>
<arg value="--copyright=2011"/>
<arg value="--lite-build=false"/>
<arg value="--build-regioncode=true"/>
</exec>
</target>


+ 1
- 1
java/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010 The Libphonenumber Authors
* Copyright (C) 2011 The Libphonenumber Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.


+ 9
- 11
java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java View File

@ -14,19 +14,20 @@
* limitations under the License.
*/
/* This file is automatically generated by {@link BuildMetadataProtoFromXml}.
* Please don't modify it directly.
*/
package com.google.i18n.phonenumbers;
/**
* Class containing string constants of region codes for easier testing.
*/
final class RegionCode {
public class RegionCode {
// Class containing string constants of region codes for easier testing.
// Region code for global networks (e.g. +800 numbers).
static final String UN001 = "001";
static final String AD = "AD";
static final String AE = "AE";
static final String AM = "AM";
static final String AO = "AO";
static final String AQ = "AQ";
static final String AR = "AR";
static final String AU = "AU";
static final String BB = "BB";
@ -34,31 +35,28 @@ final class RegionCode {
static final String BS = "BS";
static final String BY = "BY";
static final String CA = "CA";
static final String CH = "CH";
static final String CL = "CL";
static final String CC = "CC";
static final String CN = "CN";
static final String CO = "CO";
static final String CS = "CS";
static final String CX = "CX";
static final String DE = "DE";
static final String FR = "FR";
static final String GB = "GB";
static final String HU = "HU";
static final String GG = "GG";
static final String IT = "IT";
static final String JP = "JP";
static final String KR = "KR";
static final String MX = "MX";
static final String NZ = "NZ";
static final String PG = "PG";
static final String PL = "PL";
static final String RE = "RE";
static final String RU = "RU";
static final String SE = "SE";
static final String SG = "SG";
static final String TA = "TA";
static final String US = "US";
static final String UZ = "UZ";
static final String YT = "YT";
static final String ZW = "ZW";
// Official code for the unknown region.
static final String ZZ = "ZZ";
}

+ 11
- 0
tools/java/common/src/com/google/i18n/phonenumbers/BuildMetadataFromXml.java View File

@ -162,6 +162,17 @@ public class BuildMetadataFromXml {
return countryCodeToRegionCodeMap;
}
// Build an un list of region codes from the metadata
public static List<String> buildRegionCodeList(
PhoneMetadataCollection metadataCollection) {
List<String> regionCodeList = new ArrayList<String>();
for (PhoneMetadata metadata : metadataCollection.getMetadataList()) {
String regionCode = metadata.getId();
regionCodeList.add(regionCode);
}
return regionCodeList;
}
private static String validateRE(String regex) {
return validateRE(regex, false);
}


+ 47
- 1
tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java View File

@ -26,6 +26,8 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.List;
import java.util.Map;
@ -56,6 +58,7 @@ public class BuildMetadataProtoFromXml extends Command {
private static final String COPYRIGHT = "copyright";
private static final String SINGLE_FILE = "single-file";
private static final String LITE_BUILD = "lite-build";
private static final String BUILD_REGIONCODE = "build-regioncode";
// Only supported for clients who have consulted with the libphonenumber team, and the behavior is
// subject to change without notice.
private static final String SPECIAL_BUILD = "special-build";
@ -81,6 +84,8 @@ public class BuildMetadataProtoFromXml extends Command {
" [--" + LITE_BUILD + "=<true|false>] Optional (default: false). In a lite build,\n" +
" certain metadata will be omitted. At this\n" +
" moment, example numbers information is omitted.\n" +
" [--" + BUILD_REGIONCODE + "=<true|false>] Optional (default: false). Generate\n" +
" RegionCode class with constants for all region codes.\n" +
"\n" +
"Example command line invocation:\n" +
CLASS_NAME + " \\\n" +
@ -90,7 +95,8 @@ public class BuildMetadataProtoFromXml extends Command {
" --" + MAPPING_CLASS + "=CountryCodeToRegionCodeMap \\\n" +
" --" + COPYRIGHT + "=2010 \\\n" +
" --" + SINGLE_FILE + "=false \\\n" +
" --" + LITE_BUILD + "=false\n";
" --" + LITE_BUILD + "=false\n" +
" --" + BUILD_REGIONCODE + "=true\n";
private static final String GENERATION_COMMENT =
"/* This file is automatically generated by {@link " + CLASS_NAME + "}.\n" +
@ -115,6 +121,7 @@ public class BuildMetadataProtoFromXml extends Command {
boolean singleFile = false;
boolean liteBuild = false;
boolean specialBuild = false;
boolean buildRegioncode = false;
for (int i = 1; i < getArgs().length; i++) {
String key = null;
@ -144,6 +151,9 @@ public class BuildMetadataProtoFromXml extends Command {
} else if (SPECIAL_BUILD.equals(key) &&
("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value))) {
specialBuild = "true".equalsIgnoreCase(value);
} else if (BUILD_REGIONCODE.equals(key) &&
("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value))) {
buildRegioncode = "true".equalsIgnoreCase(value);
} else {
System.err.println(HELP_MESSAGE);
System.err.println("Illegal command line parameter: " + getArgs()[i]);
@ -195,6 +205,16 @@ public class BuildMetadataProtoFromXml extends Command {
writeCountryCallingCodeMappingToJavaFile(
countryCodeToRegionCodeMap, outputDir, mappingClass, copyright);
if (buildRegioncode) {
SortedSet<String> regionCodeSet = new TreeSet<String>();
regionCodeSet.addAll(getDefaultRegionCodeList());
regionCodeSet.addAll(BuildMetadataFromXml.buildRegionCodeList(metadataCollection));
List<String> regionCodeList = new ArrayList<String>(regionCodeSet);
System.out.println("Found " + regionCodeList.size() + " region codes");
writeRegionCodeConstantsToJavaFile(regionCodeList, outputDir, copyright);
}
} catch (Exception e) {
e.printStackTrace();
return false;
@ -339,6 +359,32 @@ public class BuildMetadataProtoFromXml extends Command {
writer.addToBody(" }\n");
}
private static final String REGION_CODE_CONSTS_COMMENT =
" // Class containing string constants of region codes for easier testing.\n";
private static void writeRegionCodeConstantsToJavaFile(List<String> regionCodeList, String outputDir, String copyright) throws IOException {
ClassWriter writer = new ClassWriter(outputDir, "RegionCode", copyright);
writer.addToBody(REGION_CODE_CONSTS_COMMENT);
for (String regionCode : regionCodeList) {
String variableName = regionCode.toUpperCase();
if (variableName.equals("001")) {
writer.addToBody(" // Region code for global networks (e.g. +800 numbers).\n");
variableName = "UN001";
} else if (variableName.equals("ZZ")) {
writer.addToBody(" // Official code for the unknown region.\n");
}
writer.addToBody(" static final String " + variableName + " = \"" + regionCode + "\";\n");
}
writer.writeToFile();
}
private static List<String> getDefaultRegionCodeList() {
// hard-coded list of region codes to include in the RegionCode class
return Arrays.asList("ZZ");
}
private static final class ClassWriter {
private final String name;
private final String copyright;


Loading…
Cancel
Save