|
|
|
@ -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; |
|
|
|
|