From 725b617facb7c92eba6378b617a076f45396240f Mon Sep 17 00:00:00 2001 From: silviobraendle Date: Mon, 9 Sep 2024 08:20:36 +0000 Subject: [PATCH] Generate RegionCode constants for testing. This generates the RegionCode class that contains string constants for testing using the build tools. Context: b/237181793 --- java/build.xml | 3 +- .../CountryCodeToRegionCodeMapForTesting.java | 2 +- .../google/i18n/phonenumbers/RegionCode.java | 20 ++++---- .../phonenumbers/BuildMetadataFromXml.java | 11 +++++ .../BuildMetadataProtoFromXml.java | 48 ++++++++++++++++++- 5 files changed, 70 insertions(+), 14 deletions(-) diff --git a/java/build.xml b/java/build.xml index f010e7cf5..cd6823821 100644 --- a/java/build.xml +++ b/java/build.xml @@ -77,8 +77,9 @@ - + + diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java index a68d45fea..3a8c65c02 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java @@ -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. diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java index 20fc1212c..9ee56c8b7 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java @@ -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"; } diff --git a/tools/java/common/src/com/google/i18n/phonenumbers/BuildMetadataFromXml.java b/tools/java/common/src/com/google/i18n/phonenumbers/BuildMetadataFromXml.java index d38927390..83705b80a 100644 --- a/tools/java/common/src/com/google/i18n/phonenumbers/BuildMetadataFromXml.java +++ b/tools/java/common/src/com/google/i18n/phonenumbers/BuildMetadataFromXml.java @@ -162,6 +162,17 @@ public class BuildMetadataFromXml { return countryCodeToRegionCodeMap; } + // Build an un list of region codes from the metadata + public static List buildRegionCodeList( + PhoneMetadataCollection metadataCollection) { + List regionCodeList = new ArrayList(); + for (PhoneMetadata metadata : metadataCollection.getMetadataList()) { + String regionCode = metadata.getId(); + regionCodeList.add(regionCode); + } + return regionCodeList; + } + private static String validateRE(String regex) { return validateRE(regex, false); } 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 282827106..7532ade87 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 @@ -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 + "=] 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 + "=] 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 regionCodeSet = new TreeSet(); + regionCodeSet.addAll(getDefaultRegionCodeList()); + regionCodeSet.addAll(BuildMetadataFromXml.buildRegionCodeList(metadataCollection)); + List regionCodeList = new ArrayList(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 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 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;