// Tests loadTerritoryTagMetadata(). [TestMethod] public void testLoadTerritoryTagMetadata() { String xmlInput = "<territory countryCode='33' leadingDigits='2' internationalPrefix='00'" + " preferredInternationalPrefix='0011' nationalPrefixForParsing='0'" + " nationalPrefixTransformRule='9$1'" + // nationalPrefix manually injected. " preferredExtnPrefix=' x' mainCountryForCode='true'" + " leadingZeroPossible='true' mobileNumberPortableRegion='true'>" + "</territory>"; XmlElement territoryElement = parseXmlString(xmlInput); PhoneMetadata.Builder phoneMetadata = BuildMetadataFromXml.loadTerritoryTagMetadata("33", territoryElement, "0"); Assert.AreEqual(33, phoneMetadata.getCountryCode()); Assert.AreEqual("2", phoneMetadata.getLeadingDigits()); Assert.AreEqual("00", phoneMetadata.getInternationalPrefix()); Assert.AreEqual("0011", phoneMetadata.getPreferredInternationalPrefix()); Assert.AreEqual("0", phoneMetadata.getNationalPrefixForParsing()); Assert.AreEqual("9$1", phoneMetadata.getNationalPrefixTransformRule()); Assert.AreEqual("0", phoneMetadata.getNationalPrefix()); Assert.AreEqual(" x", phoneMetadata.getPreferredExtnPrefix()); Assert.IsTrue(phoneMetadata.getMainCountryForCode()); Assert.IsTrue(phoneMetadata.isLeadingZeroPossible()); Assert.IsTrue(phoneMetadata.isMobileNumberPortableRegion()); }
/** * Extracts the pattern for the national format. * * @throws RuntimeException if multiple or no formats have been encountered. */ // @VisibleForTesting internal static void loadNationalFormat(PhoneMetadata.Builder metadata, XmlElement numberFormatXmlElement, NumberFormat.Builder format) { setLeadingDigitsPatterns(numberFormatXmlElement, format); format.setPattern(validateRE(numberFormatXmlElement.GetAttribute(PATTERN))); var formatPattern = numberFormatXmlElement.GetElementsByTagName(FORMAT); int numFormatPatterns = formatPattern.Count; if (numFormatPatterns != 1) { // LOGGER.log(Level.SEVERE, "One format pattern for a numberFormat element should be defined."); String countryId = metadata.getId().Length > 0 ? metadata.getId() : metadata.getCountryCode().ToString(); throw new Exception("Invalid number of format patterns (" + numFormatPatterns + ") for country: " + countryId); } format.setFormat(formatPattern.Item(0).FirstChild.Value); }
/** * Extracts the pattern for international format. If there is no intlFormat, default to using the * national format. If the intlFormat is set to "NA" the intlFormat should be ignored. * * @throws RuntimeException if multiple intlFormats have been encountered. * @return whether an international number format is defined. */ // @VisibleForTesting internal static bool loadInternationalFormat(PhoneMetadata.Builder metadata, XmlElement numberFormatXmlElement, NumberFormat nationalFormat) { NumberFormat.Builder intlFormat = NumberFormat.newBuilder(); var intlFormatPattern = numberFormatXmlElement.GetElementsByTagName(INTL_FORMAT); var hasExplicitIntlFormatDefined = false; if (intlFormatPattern.Count > 1) { // LOGGER.log(Level.SEVERE, // "A maximum of one intlFormat pattern for a numberFormat element should be " + // "defined."); String countryId = metadata.getId().Length > 0 ? metadata.getId() : metadata.getCountryCode().ToString(); throw new Exception("Invalid number of intlFormat patterns for country: " + countryId); } else if (intlFormatPattern.Count == 0) { // Default to use the same as the national pattern if none is defined. intlFormat.mergeFrom(nationalFormat); } else { intlFormat.setPattern(numberFormatXmlElement.GetAttribute(PATTERN)); setLeadingDigitsPatterns(numberFormatXmlElement, intlFormat); String intlFormatPatternValue = intlFormatPattern.Item(0).FirstChild.Value; if (!intlFormatPatternValue.Equals("NA")) { intlFormat.setFormat(intlFormatPatternValue); } hasExplicitIntlFormatDefined = true; } if (intlFormat.HasFormat()) { metadata.addIntlNumberFormat(intlFormat); } return(hasExplicitIntlFormatDefined); }