// Build a mapping from a country calling code to the region codes which denote the country/region // represented by that country code. In the case of multiple countries sharing a calling code, // such as the NANPA countries, the one indicated with "isMainCountryForCode" in the metadata // should be first. public static Dictionary <int, List <String> > buildCountryCodeToRegionCodeMap( PhoneMetadataCollection metadataCollection) { var countryCodeToRegionCodeMap = new Dictionary <int, List <String> >(); foreach (PhoneMetadata metadata in metadataCollection.getMetadataList()) { String regionCode = metadata.getId(); int countryCode = metadata.getCountryCode(); if (countryCodeToRegionCodeMap.ContainsKey(countryCode)) { if (metadata.getMainCountryForCode()) { countryCodeToRegionCodeMap[countryCode].Insert(0, regionCode); } else { countryCodeToRegionCodeMap[countryCode].Add(regionCode); } } else { // For most countries, there will be only one region code for the country calling code. List <String> listWithRegionCode = new List <String>(1); if (regionCode.Length != 0) // For alternate formats, there are no region codes at all. { listWithRegionCode.Add(regionCode); } countryCodeToRegionCodeMap.Add(countryCode, listWithRegionCode); } } return(countryCodeToRegionCodeMap); }
// Build the PhoneMetadataCollection from the input XML file. public static PhoneMetadataCollection buildPhoneMetadataCollection(String inputXmlFile, bool liteBuild) { // DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); // DocumentBuilder builder = builderFactory.newDocumentBuilder(); var document = new XmlDocument(); // File xmlFile = new File(inputXmlFile); // Document document = builder.parse(xmlFile); document.Load(inputXmlFile); document.Normalize(); var territory = document.GetElementsByTagName("territory"); PhoneMetadataCollection.Builder metadataCollection = PhoneMetadataCollection.newBuilder(); int numOfTerritories = territory.Count; // TODO: Look for other uses of these constants and possibly pull them out into // a separate constants file. bool isShortNumberMetadata = inputXmlFile.Contains("ShortNumberMetadata"); bool isAlternateFormatsMetadata = inputXmlFile.Contains("PhoneNumberAlternateFormats"); for (int i = 0; i < numOfTerritories; i++) { XmlElement territoryXmlElement = (XmlElement)territory.Item(i); String regionCode = ""; // For the main metadata file this should always be set, but for other supplementary data // files the country calling code may be all that is needed. if (territoryXmlElement.HasAttribute("id")) { regionCode = territoryXmlElement.GetAttribute("id"); } PhoneMetadata metadata = loadCountryMetadata(regionCode, territoryXmlElement, liteBuild, isShortNumberMetadata, isAlternateFormatsMetadata); metadataCollection.addMetadata(metadata); } return(metadataCollection.build()); }
private static void GenerateCountryCodeToRegionCodeMap(string inputFileName, string outputDirectory, string filePrefix) { Directory.CreateDirectory(outputDirectory); var file = File.OpenRead(inputFileName); var meta = BuildMetadataFromXml.BuildPhoneMetadataCollection(file, false); for (var i = 0; i < meta.MetadataCount; ++i) { var data = meta.GetMetadata(i); var regionCode = data.Id; if (string.IsNullOrEmpty(regionCode) || regionCode.Equals("001")) { regionCode = data.CountryCode.ToString(CultureInfo.InvariantCulture); } var outMetadataCollection = new PhoneMetadataCollection(); outMetadataCollection.MetadataList.Add(data); var outputFileName = Path.Combine(outputDirectory, filePrefix + "_" + regionCode); var outputForRegion = File.OpenWrite(outputFileName); outMetadataCollection.WriteTo(outputForRegion); } }
public static void Main(string[] args) { // The format of a well-formed command line parameter. var pattern = new Regex("--(.+?)=(.*)"); String inputFile = null; String outputDir = null; String dataPrefix = null; String mappingClass = null; String copyright = null; bool liteBuild = false; for (int i = 0; i < args.Length; i++) { String key = null; String value = null; var matcher = pattern.Match(args[i]); if (matcher.Success) { key = matcher.Groups[1].Value; value = matcher.Groups[2].Value; } if (INPUT_FILE.Equals(key)) { inputFile = value; } else if (OUTPUT_DIR.Equals(key)) { outputDir = value; } else if (DATA_PREFIX.Equals(key)) { dataPrefix = value; } else if (MAPPING_CLASS.Equals(key)) { mappingClass = value; } else if (COPYRIGHT.Equals(key)) { copyright = value; } else if (LITE_BUILD.Equals(key) && ("true".Equals(value, StringComparison.OrdinalIgnoreCase) || "false".Equals(value, StringComparison.OrdinalIgnoreCase))) { liteBuild = "true".Equals(value, StringComparison.OrdinalIgnoreCase); } else { Console.WriteLine(HELP_MESSAGE); Console.WriteLine("Illegal command line parameter: " + args[i]); return; } } if (inputFile == null || outputDir == null || dataPrefix == null || mappingClass == null || copyright == null) { Console.WriteLine(HELP_MESSAGE); return; } String filePrefix = Path.Combine(outputDir, dataPrefix); try { PhoneMetadataCollection metadataCollection = BuildMetadataFromXml.buildPhoneMetadataCollection(inputFile, liteBuild); foreach (PhoneMetadata metadata in metadataCollection.getMetadataList()) { String regionCode = metadata.getId(); // 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") || String.IsNullOrEmpty(regionCode)) { regionCode = metadata.getCountryCode().ToString(); } PhoneMetadataCollection outMetadataCollection = new PhoneMetadataCollection(); outMetadataCollection.addMetadata(metadata); using (var outputForRegion = new FileStream(filePrefix + "_" + regionCode, FileMode.OpenOrCreate)) { outputForRegion.SetLength(0); using (var writer = new BinaryWriter(outputForRegion)) { outMetadataCollection.writeExternal(writer); } } } Dictionary <int, List <String> > countryCodeToRegionCodeMap = BuildMetadataFromXml.buildCountryCodeToRegionCodeMap(metadataCollection); // writeCountryCallingCodeMappingToCSharpFile(countryCodeToRegionCodeMap, outputDir, mappingClass, copyright); } catch (Exception e) { Console.WriteLine(e.ToString()); return; } Console.WriteLine("Metadata code successfully generated."); }