Package com.google.i18n.phonenumbers.Phonemetadata

Examples of com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata


                 metadata.getIntlNumberFormat(3).getPattern());
    assertEquals("$1 $2 $3 $4", metadata.getIntlNumberFormat(3).getFormat());
  }

  public void testGetInstanceLoadInternationalTollFreeMetadata() {
    PhoneMetadata metadata = phoneUtil.getMetadataForNonGeographicalRegion(800);
    assertEquals("001", metadata.getId());
    assertEquals(800, metadata.getCountryCode());
    assertEquals("$1 $2", metadata.getNumberFormat(0).getFormat());
    assertEquals("(\\d{4})(\\d{4})", metadata.getNumberFormat(0).getPattern());
    assertEquals("12345678", metadata.getGeneralDesc().getExampleNumber());
    assertEquals("12345678", metadata.getTollFree().getExampleNumber());
  }
View Full Code Here


    // This case has a trailing RTL char.
    assertEquals("650) 253-0000", PhoneNumberUtil.extractPossibleNumber("(650) 253-0000\u200F"));
  }

  public void testMaybeStripNationalPrefix() {
    PhoneMetadata metadata = new PhoneMetadata();
    metadata.setNationalPrefixForParsing("34");
    metadata.setGeneralDesc(new PhoneNumberDesc().setNationalNumberPattern("\\d{4,8}"));
    StringBuilder numberToStrip = new StringBuilder("34356778");
    String strippedNumber = "356778";
    assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null));
    assertEquals("Should have had national prefix stripped.",
                 strippedNumber, numberToStrip.toString());
    // Retry stripping - now the number should not start with the national prefix, so no more
    // stripping should occur.
    assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null));
    assertEquals("Should have had no change - no national prefix present.",
                 strippedNumber, numberToStrip.toString());
    // Some countries have no national prefix. Repeat test with none specified.
    metadata.setNationalPrefixForParsing("");
    assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null));
    assertEquals("Should not strip anything with empty national prefix.",
                 strippedNumber, numberToStrip.toString());
    // If the resultant number doesn't match the national rule, it shouldn't be stripped.
    metadata.setNationalPrefixForParsing("3");
    numberToStrip = new StringBuilder("3123");
    strippedNumber = "3123";
    assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null));
    assertEquals("Should have had no change - after stripping, it wouldn't have matched " +
                 "the national rule.",
                 strippedNumber, numberToStrip.toString());
    // Test extracting carrier selection code.
    metadata.setNationalPrefixForParsing("0(81)?");
    numberToStrip = new StringBuilder("08122123456");
    strippedNumber = "22123456";
    StringBuilder carrierCode = new StringBuilder();
    assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(
        numberToStrip, metadata, carrierCode));
    assertEquals("81", carrierCode.toString());
    assertEquals("Should have had national prefix and carrier code stripped.",
                 strippedNumber, numberToStrip.toString());
    // If there was a transform rule, check it was applied.
    metadata.setNationalPrefixTransformRule("5$15");
    // Note that a capturing group is present here.
    metadata.setNationalPrefixForParsing("0(\\d{2})");
    numberToStrip = new StringBuilder("031123");
    String transformedNumber = "5315123";
    assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata, null));
    assertEquals("Should transform the 031 to a 5315.",
                 transformedNumber, numberToStrip.toString());
View Full Code Here

                                                                     internationalPrefix));
  }

  public void testMaybeExtractCountryCode() {
    PhoneNumber number = new PhoneNumber();
    PhoneMetadata metadata = phoneUtil.getMetadataForRegion(RegionCode.US);
    // Note that for the US, the IDD is 011.
    try {
      String phoneNumber = "011112-3456789";
      String strippedNumber = "123456789";
      int countryCallingCode = 1;
View Full Code Here

*/
public class MetadataManagerTest extends TestCase {

  public void testAlternateFormatsContainsData() throws Exception {
    // We should have some data for Germany.
    PhoneMetadata germanyAlternateFormats = MetadataManager.getAlternateFormatsForCountry(49);
    assertNotNull(germanyAlternateFormats);
    assertTrue(germanyAlternateFormats.numberFormats().size() > 0);
  }
View Full Code Here

    assertNotNull(germanyAlternateFormats);
    assertTrue(germanyAlternateFormats.numberFormats().size() > 0);
  }

  public void testAlternateFormatsFailsGracefully() throws Exception {
    PhoneMetadata noAlternateFormats = MetadataManager.getAlternateFormatsForCountry(999);
    assertNull(noAlternateFormats);
  }
View Full Code Here

      // Returns false if the number starts with a plus sign. We don't believe dialing the country
      // code before emergency numbers (e.g. +1911) works, but later, if that proves to work, we can
      // add additional logic here to handle it.
      return false;
    }
    PhoneMetadata metadata = phoneUtil.getMetadataForRegion(regionCode);
    if (metadata == null || !metadata.hasEmergency()) {
      return false;
    }
    Pattern emergencyNumberPattern =
        Pattern.compile(metadata.getEmergency().getNationalNumberPattern());
    String normalizedNumber = PhoneNumberUtil.normalizeDigitsOnly(number);
    // In Brazil, it is impossible to append additional digits to an emergency number to dial the
    // number.
    return (!allowPrefixMatch || regionCode.equals("BR"))
        ? emergencyNumberPattern.matcher(normalizedNumber).matches()
View Full Code Here

  // The metadata needed by this class is the same for all regions sharing the same country calling
  // code. Therefore, we return the metadata for "main" region for this country calling code.
  private PhoneMetadata getMetadataForRegion(String regionCode) {
    int countryCallingCode = phoneUtil.getCountryCodeForRegion(regionCode);
    String mainCountry = phoneUtil.getRegionCodeForCountryCode(countryCallingCode);
    PhoneMetadata metadata = phoneUtil.getMetadataForRegion(mainCountry);
    if (metadata != null) {
      return metadata;
    }
    // Set to a default instance of the metadata. This allows us to function with an incorrect
    // region code, even if formatting only works for numbers specified with "+".
View Full Code Here

    String[] formattedNumberGroups = getNationalNumberGroups(util, number, null);
    if (checker.checkGroups(util, number, normalizedCandidate, formattedNumberGroups)) {
      return true;
    }
    // If this didn't pass, see if there are any alternate formats, and try them instead.
    PhoneMetadata alternateFormats =
        MetadataManager.getAlternateFormatsForCountry(number.getCountryCode());
    if (alternateFormats != null) {
      for (NumberFormat alternateFormat : alternateFormats.numberFormats()) {
        formattedNumberGroups = getNationalNumberGroups(util, number, alternateFormat);
        if (checker.checkGroups(util, number, normalizedCandidate, formattedNumberGroups)) {
          return true;
        }
      }
View Full Code Here

    if (number.getCountryCodeSource() != CountryCodeSource.FROM_DEFAULT_COUNTRY) {
      return true;
    }
    String phoneNumberRegion =
        util.getRegionCodeForCountryCode(number.getCountryCode());
    PhoneMetadata metadata = util.getMetadataForRegion(phoneNumberRegion);
    if (metadata == null) {
      return true;
    }
    // Check if a national prefix should be present when formatting this number.
    String nationalNumber = util.getNationalSignificantNumber(number);
    NumberFormat formatRule =
        util.chooseFormattingPatternForNumber(metadata.numberFormats(), nationalNumber);
    // To do this, we check that a national prefix formatting rule was present and that it wasn't
    // just the first-group symbol ($1) with punctuation.
    if ((formatRule != null) && formatRule.getNationalPrefixFormattingRule().length() > 0) {
      if (formatRule.isNationalPrefixOptionalWhenFormatting()) {
        // The national-prefix is optional in these cases, so we don't need to check if it was
View Full Code Here

    if (!isValidRegionCode(regionCode)) {
      // Note numbers belonging to non-geographical entities (e.g. +800 numbers) are always
      // internationally diallable, and will be caught here.
      return true;
    }
    PhoneMetadata metadata = getMetadataForRegion(regionCode);
    String nationalSignificantNumber = getNationalSignificantNumber(number);
    return !isNumberMatchingDesc(nationalSignificantNumber, metadata.getNoInternationalDialling());
  }
View Full Code Here

TOP

Related Classes of com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.