Browse Source

Update libphonenumber to v4.3

Review URL: http://codereview.appspot.com/5434054
pull/567/head
Shaopeng Jia 14 years ago
committed by Mihaela Rosca
parent
commit
fa45d4a4a9
22 changed files with 358 additions and 112 deletions
  1. +64
    -8
      java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java
  2. +20
    -0
      java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java
  3. +3
    -4
      java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java
  4. +5
    -5
      java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java
  5. +58
    -26
      java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
  6. BIN
      java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR
  7. BIN
      java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GN
  8. BIN
      java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP
  9. BIN
      java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE
  10. BIN
      java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL
  11. BIN
      java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SG
  12. BIN
      java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SR
  13. BIN
      java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SX
  14. BIN
      java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH
  15. BIN
      java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TK
  16. +10
    -0
      java/libphonenumber/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java
  17. +7
    -0
      java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java
  18. +24
    -2
      java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
  19. BIN
      java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP
  20. +13
    -0
      java/release_notes.txt
  21. +146
    -63
      resources/PhoneNumberMetaData.xml
  22. +8
    -4
      resources/PhoneNumberMetaDataForTesting.xml

+ 64
- 8
java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java View File

@ -135,10 +135,12 @@ public class PhoneNumberOfflineGeocoder {
}
/**
* Returns a text description for the given language code for the given phone number. The
* description might consist of the name of the country where the phone number is from and/or the
* name of the geographical area the phone number is from. This method assumes the validity of the
* number passed in has already been checked.
* Returns a text description for the given phone number, in the language provided. The
* description might consist of the name of the country where the phone number is from, or the
* name of the geographical area the phone number is from if more detailed information is
* available.
*
* <p>This method assumes the validity of the number passed in has already been checked.
*
* @param number a valid phone number for which we want to get a text description
* @param languageCode the language code for which the description should be written
@ -156,10 +158,44 @@ public class PhoneNumberOfflineGeocoder {
}
/**
* Returns a text description for the given language code for the given phone number. The
* description might consist of the name of the country where the phone number is from and/or the
* name of the geographical area the phone number is from. This method explictly checkes the
* validity of the number passed in.
* As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale)} but also considers the
* region of the user. If the phone number is from the same region as the user, only a lower-level
* description will be returned, if one exists. Otherwise, the phone number's region will be
* returned, with optionally some more detailed information.
*
* <p>For example, for a user from the region "US" (United States), we would show "Mountain View,
* CA" for a particular number, omitting the United States from the description. For a user from
* the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United
* States" or even just "United States".
*
* <p>This method assumes the validity of the number passed in has already been checked.
*
* @param number the phone number for which we want to get a text description
* @param languageCode the language code for which the description should be written
* @param userRegion the region code for a given user. This region will be omitted from the
* description if the phone number comes from this region. It is a two-letter uppercase ISO
* country code as defined by ISO 3166-1.
* @return a text description for the given language code for the given phone number, or empty
* string if the number passed in is invalid
*/
public String getDescriptionForValidNumber(PhoneNumber number, Locale languageCode,
String userRegion) {
// If the user region matches the number's region, then we just show the lower-level
// description, if one exists - if no description exists, we will show the region(country) name
// for the number.
String regionCode = phoneUtil.getRegionCodeForNumber(number);
if (userRegion.equals(regionCode)) {
return getDescriptionForValidNumber(number, languageCode);
}
// Otherwise, we just show the region(country) name for now.
return getCountryNameForNumber(number, languageCode);
// TODO: Concatenate the lower-level and country-name information in an appropriate
// way for each language.
}
/**
* As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale)} but explicitly checks
* the validity of the number passed in.
*
* @param number the phone number for which we want to get a text description
* @param languageCode the language code for which the description should be written
@ -173,6 +209,26 @@ public class PhoneNumberOfflineGeocoder {
return getDescriptionForValidNumber(number, languageCode);
}
/**
* As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale, String)} but
* explicitly checks the validity of the number passed in.
*
* @param number the phone number for which we want to get a text description
* @param languageCode the language code for which the description should be written
* @param userRegion the region code for a given user. This region will be omitted from the
* description if the phone number comes from this region. It is a two-letter uppercase ISO
* country code as defined by ISO 3166-1.
* @return a text description for the given language code for the given phone number, or empty
* string if the number passed in is invalid
*/
public String getDescriptionForNumber(PhoneNumber number, Locale languageCode,
String userRegion) {
if (!phoneUtil.isValidNumber(number)) {
return "";
}
return getDescriptionForValidNumber(number, languageCode, userRegion);
}
/**
* Returns an area-level text description in the given language for the given phone number.
*


+ 20
- 0
java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java View File

@ -116,6 +116,26 @@ public class PhoneNumberOfflineGeocoderTest extends TestCase {
geocoder.getDescriptionForNumber(KO_NUMBER3, Locale.KOREAN));
}
public void testGetDescriptionForNumberWithUserRegion() {
// User in Italy, American number. We should just show United States, in German, and not more
// detailed information.
assertEquals("Vereinigte Staaten von Amerika",
geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN, "IT"));
// Unknown region - should just show country name.
assertEquals("Vereinigte Staaten von Amerika",
geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN, "ZZ"));
// User in the States, language German, should show detailed data.
assertEquals("Kalifornien",
geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN, "US"));
// User in the States, language French, no data for French, so we fallback to English detailed
// data.
assertEquals("CA",
geocoder.getDescriptionForNumber(US_NUMBER1, Locale.FRENCH, "US"));
// Invalid number - return an empty string.
assertEquals("", geocoder.getDescriptionForNumber(US_INVALID_NUMBER, Locale.ENGLISH,
"US"));
}
public void testGetDescriptionForInvalidNumber() {
assertEquals("", geocoder.getDescriptionForNumber(KO_INVALID_NUMBER, Locale.ENGLISH));
assertEquals("", geocoder.getDescriptionForNumber(US_INVALID_NUMBER, Locale.ENGLISH));


+ 3
- 4
java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java View File

@ -356,7 +356,7 @@ public class AsYouTypeFormatter {
}
return ableToFormat
? prefixBeforeNationalNumber + tempNationalNumber
: tempNationalNumber;
: accruedInput.toString();
} else {
return attemptToChooseFormattingPattern();
}
@ -428,8 +428,7 @@ public class AsYouTypeFormatter {
// number (excluding national prefix) have been entered.
if (nationalNumber.length() >= MIN_LEADING_DIGITS_LENGTH) {
getAvailableFormats(nationalNumber.substring(0, MIN_LEADING_DIGITS_LENGTH));
maybeCreateNewTemplate();
return inputAccruedNationalNumber();
return maybeCreateNewTemplate() ? inputAccruedNationalNumber() : accruedInput.toString();
} else {
return prefixBeforeNationalNumber + nationalNumber.toString();
}
@ -446,7 +445,7 @@ public class AsYouTypeFormatter {
}
return ableToFormat
? prefixBeforeNationalNumber + tempNationalNumber
: tempNationalNumber;
: accruedInput.toString();
} else {
return prefixBeforeNationalNumber.toString();
}


+ 5
- 5
java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java View File

@ -294,8 +294,8 @@ final class PhoneNumberMatcher implements Iterator<PhoneNumberMatch> {
block.equals(UnicodeBlock.COMBINING_DIACRITICAL_MARKS);
}
private static boolean isCurrencySymbol(char character) {
return Character.getType(character) == Character.CURRENCY_SYMBOL;
private static boolean isInvalidPunctuationSymbol(char character) {
return character == '%' || Character.getType(character) == Character.CURRENCY_SYMBOL;
}
/**
@ -407,15 +407,15 @@ final class PhoneNumberMatcher implements Iterator<PhoneNumberMatch> {
// punctuation, check the previous character.
if (offset > 0 && !LEAD_CLASS.matcher(candidate).lookingAt()) {
char previousChar = text.charAt(offset - 1);
// We return null if it is a latin letter or a currency symbol.
if (isCurrencySymbol(previousChar) || isLatinLetter(previousChar)) {
// We return null if it is a latin letter or an invalid punctuation symbol.
if (isInvalidPunctuationSymbol(previousChar) || isLatinLetter(previousChar)) {
return null;
}
}
int lastCharIndex = offset + candidate.length();
if (lastCharIndex < text.length()) {
char nextChar = text.charAt(lastCharIndex);
if (isCurrencySymbol(nextChar) || isLatinLetter(nextChar)) {
if (isInvalidPunctuationSymbol(nextChar) || isLatinLetter(nextChar)) {
return null;
}
}


+ 58
- 26
java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java View File

@ -97,6 +97,11 @@ public class PhoneNumberUtil {
private static final String RFC3966_EXTN_PREFIX = ";ext=";
// A map that contains characters that are essential when dialling. That means any of the
// characters in this map must not be removed from a number when dialing, otherwise the call will
// not reach the intended destination.
private static final Map<Character, Character> DIALLABLE_CHAR_MAPPINGS;
// Only upper-case variants of alpha characters are stored.
private static final Map<Character, Character> ALPHA_MAPPINGS;
@ -156,6 +161,12 @@ public class PhoneNumberUtil {
combinedMap.putAll(asciiDigitMappings);
ALPHA_PHONE_MAPPINGS = Collections.unmodifiableMap(combinedMap);
HashMap<Character, Character> diallableCharMap = new HashMap<Character, Character>();
diallableCharMap.put('+', '+');
diallableCharMap.put('*', '*');
diallableCharMap.putAll(asciiDigitMappings);
DIALLABLE_CHAR_MAPPINGS = Collections.unmodifiableMap(diallableCharMap);
HashMap<Character, Character> allPlusNumberGroupings = new HashMap<Character, Character>();
// Put (lower letter -> upper letter) and (upper letter -> upper letter) mappings.
for (char c : ALPHA_MAPPINGS.keySet()) {
@ -407,9 +418,8 @@ public class PhoneNumberUtil {
*/
public enum Leniency {
/**
* Phone numbers accepted are
* {@linkplain PhoneNumberUtil#isPossibleNumber(Phonenumber.PhoneNumber) possible}, but not
* necessarily {@linkplain PhoneNumberUtil#isValidNumber(Phonenumber.PhoneNumber) valid}.
* Phone numbers accepted are {@linkplain PhoneNumberUtil#isPossibleNumber(PhoneNumber)
* possible}, but not necessarily {@linkplain PhoneNumberUtil#isValidNumber(PhoneNumber) valid}.
*/
POSSIBLE {
@Override
@ -418,9 +428,8 @@ public class PhoneNumberUtil {
}
},
/**
* Phone numbers accepted are
* {@linkplain PhoneNumberUtil#isPossibleNumber(Phonenumber.PhoneNumber) possible} and
* {@linkplain PhoneNumberUtil#isValidNumber(Phonenumber.PhoneNumber) valid}. Numbers written
* Phone numbers accepted are {@linkplain PhoneNumberUtil#isPossibleNumber(PhoneNumber)
* possible} and {@linkplain PhoneNumberUtil#isValidNumber(PhoneNumber) valid}. Numbers written
* in national format must have their national-prefix present if it is usually written for a
* number of this type.
*/
@ -1059,9 +1068,8 @@ public class PhoneNumberUtil {
}
/**
* Same as {@link #format(Phonenumber.PhoneNumber, PhoneNumberUtil.PhoneNumberFormat)}, but
* accepts a mutable StringBuilder as a parameter to decrease object creation when invoked many
* times.
* Same as {@link #format(PhoneNumber, PhoneNumberFormat)}, but accepts a mutable StringBuilder as
* a parameter to decrease object creation when invoked many times.
*/
public void format(PhoneNumber number, PhoneNumberFormat numberFormat,
StringBuilder formattedNumber) {
@ -1222,7 +1230,7 @@ public class PhoneNumberUtil {
*/
public String formatNumberForMobileDialing(PhoneNumber number, String regionCallingFrom,
boolean withFormatting) {
String regionCode = getRegionCodeForNumber(number);
String regionCode = getRegionCodeForCountryCode(number.getCountryCode());
if (!isValidRegionCode(regionCode)) {
return number.hasRawInput() ? number.getRawInput() : "";
}
@ -1231,11 +1239,19 @@ public class PhoneNumberUtil {
// Clear the extension, as that part cannot normally be dialed together with the main number.
PhoneNumber numberNoExt = new PhoneNumber().mergeFrom(number).clearExtension();
PhoneNumberType numberType = getNumberType(numberNoExt);
if ((regionCode.equals("CO")) && (regionCallingFrom.equals("CO")) &&
(numberType == PhoneNumberType.FIXED_LINE)) {
formattedNumber =
formatNationalNumberWithCarrierCode(numberNoExt, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX);
} else if ((regionCode.equals("BR")) && (regionCallingFrom.equals("BR")) &&
if (regionCode.equals("CO") && regionCallingFrom.equals("CO")) {
if (numberType == PhoneNumberType.FIXED_LINE) {
formattedNumber =
formatNationalNumberWithCarrierCode(numberNoExt, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX);
} else {
// E164 doesn't work at all when dialing within Colombia.
formattedNumber = format(numberNoExt, PhoneNumberFormat.NATIONAL);
}
} else if (regionCode.equals("PE") && regionCallingFrom.equals("PE")) {
// In Peru, numbers cannot be dialled using E164 format from a mobile phone for Movistar.
// Instead they must be dialled in national format.
formattedNumber = format(numberNoExt, PhoneNumberFormat.NATIONAL);
} else if (regionCode.equals("BR") && regionCallingFrom.equals("BR") &&
((numberType == PhoneNumberType.FIXED_LINE) || (numberType == PhoneNumberType.MOBILE) ||
(numberType == PhoneNumberType.FIXED_LINE_OR_MOBILE))) {
formattedNumber = numberNoExt.hasPreferredDomesticCarrierCode()
@ -1251,7 +1267,9 @@ public class PhoneNumberUtil {
formattedNumber = (regionCallingFrom.equals(regionCode))
? format(numberNoExt, PhoneNumberFormat.NATIONAL) : "";
}
return withFormatting ? formattedNumber : normalizeDigitsOnly(formattedNumber);
return withFormatting ? formattedNumber
: normalizeHelper(formattedNumber, DIALLABLE_CHAR_MAPPINGS,
true /* remove non matches */);
}
/**
@ -1339,7 +1357,11 @@ public class PhoneNumberUtil {
* @return the formatted phone number in its original number format
*/
public String formatInOriginalFormat(PhoneNumber number, String regionCallingFrom) {
if (number.hasRawInput() && !isValidNumber(number)) {
if (number.hasRawInput() &&
(!hasFormattingPatternForNumber(number) || !isValidNumber(number))) {
// We check if we have the formatting pattern because without that, we might format the number
// as a group without national prefix. We also want to check the validity of the number
// because we don't want to risk formatting the number if we don't really understand it.
return number.getRawInput();
}
if (!number.hasCountryCodeSource()) {
@ -1358,6 +1380,18 @@ public class PhoneNumberUtil {
}
}
private boolean hasFormattingPatternForNumber(PhoneNumber number) {
String phoneNumberRegion = getRegionCodeForCountryCode(number.getCountryCode());
PhoneMetadata metadata = getMetadataForRegion(phoneNumberRegion);
if (metadata == null) {
return false;
}
String nationalNumber = getNationalSignificantNumber(number);
NumberFormat formatRule =
chooseFormattingPatternForNumber(metadata.numberFormats(), nationalNumber);
return formatRule != null;
}
/**
* Formats a phone number for out-of-country dialing purposes.
*
@ -2028,10 +2062,10 @@ public class PhoneNumberUtil {
/**
* Check whether a phone number is a possible number given a number in the form of a string, and
* the region where the number could be dialed from. It provides a more lenient check than
* {@link #isValidNumber}. See {@link #isPossibleNumber(Phonenumber.PhoneNumber)} for details.
* {@link #isValidNumber}. See {@link #isPossibleNumber(PhoneNumber)} for details.
*
* <p>This method first parses the number, then invokes
* {@link #isPossibleNumber(Phonenumber.PhoneNumber)} with the resultant PhoneNumber object.
* <p>This method first parses the number, then invokes {@link #isPossibleNumber(PhoneNumber)}
* with the resultant PhoneNumber object.
*
* @param number the number that needs to be checked, in the form of a string
* @param regionDialingFrom the region that we are expecting the number to be dialed from.
@ -2683,15 +2717,14 @@ public class PhoneNumberUtil {
/**
* Takes two phone numbers as strings and compares them for equality. This is a convenience
* wrapper for {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)}. No
* default region is known.
* wrapper for {@link #isNumberMatch(PhoneNumber, PhoneNumber)}. No default region is known.
*
* @param firstNumber first number to compare. Can contain formatting, and can have country
* calling code specified with + at the start.
* @param secondNumber second number to compare. Can contain formatting, and can have country
* calling code specified with + at the start.
* @return NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See
* {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)} for more details.
* {@link #isNumberMatch(PhoneNumber, PhoneNumber)} for more details.
*/
public MatchType isNumberMatch(String firstNumber, String secondNumber) {
try {
@ -2723,14 +2756,13 @@ public class PhoneNumberUtil {
/**
* Takes two phone numbers and compares them for equality. This is a convenience wrapper for
* {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)}. No default region is
* known.
* {@link #isNumberMatch(PhoneNumber, PhoneNumber)}. No default region is known.
*
* @param firstNumber first number to compare in proto buffer format.
* @param secondNumber second number to compare. Can contain formatting, and can have country
* calling code specified with + at the start.
* @return NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See
* {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)} for more details.
* {@link #isNumberMatch(PhoneNumber, PhoneNumber)} for more details.
*/
public MatchType isNumberMatch(PhoneNumber firstNumber, String secondNumber) {
// First see if the second number has an implicit country calling code, by attempting to parse


BIN
java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR View File


BIN
java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GN View File


BIN
java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP View File


BIN
java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE View File


BIN
java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL View File


BIN
java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SG View File


BIN
java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SR View File


BIN
java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SX View File


BIN
java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH View File


BIN
java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TK View File


+ 10
- 0
java/libphonenumber/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java View File

@ -93,6 +93,7 @@ public class AsYouTypeFormatterTest extends TestCase {
assertEquals("+81 90 1234 5678", formatter.inputDigit('8'));
assertEquals("+81 90 12 345 6789", formatter.inputDigit('9'));
assertEquals("+81901234567890", formatter.inputDigit('0'));
assertEquals("+819012345678901", formatter.inputDigit('1'));
}
public void testAYTFUS() {
@ -716,6 +717,15 @@ public class AsYouTypeFormatterTest extends TestCase {
assertEquals("+81 222 12 567", formatter.inputDigit('7'));
assertEquals("+81 222 12 5678", formatter.inputDigit('8'));
// 011113
formatter.clear();
assertEquals("0", formatter.inputDigit('0'));
assertEquals("01", formatter.inputDigit('1'));
assertEquals("011", formatter.inputDigit('1'));
assertEquals("011 1", formatter.inputDigit('1'));
assertEquals("011 11", formatter.inputDigit('1'));
assertEquals("011113", formatter.inputDigit('3'));
// +81 3332 2 5678
formatter.clear();
assertEquals("+", formatter.inputDigit('+'));


+ 7
- 0
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java View File

@ -266,6 +266,13 @@ public class PhoneNumberMatcherTest extends TestCase {
findMatchesInContexts(possibleOnlyContexts, false, true);
}
public void testPercentageNotSeenAsPhoneNumber() throws Exception {
ArrayList<NumberContext> possibleOnlyContexts = new ArrayList<NumberContext>();
possibleOnlyContexts.add(new NumberContext("", "%"));
// Numbers followed by % should be dropped.
findMatchesInContexts(possibleOnlyContexts, false, true);
}
public void testPhoneNumberWithLeadingOrTrailingMoneyMatches() throws Exception {
// Because of the space after the 20 (or before the 100) these dollar amounts should not stop
// the actual number from being found.


+ 24
- 2
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File

@ -71,6 +71,8 @@ public class PhoneNumberUtilTest extends TestCase {
private static final PhoneNumber IT_NUMBER =
new PhoneNumber().setCountryCode(39).setNationalNumber(236618300L).
setItalianLeadingZero(true);
private static final PhoneNumber JP_STAR_NUMBER =
new PhoneNumber().setCountryCode(81).setNationalNumber(2345);
// Numbers to test the formatting rules from Mexico.
private static final PhoneNumber MX_MOBILE1 =
new PhoneNumber().setCountryCode(52).setNationalNumber(12345678900L);
@ -614,7 +616,8 @@ public class PhoneNumberUtilTest extends TestCase {
// US toll free numbers are marked as noInternationalDialling in the test metadata for testing
// purposes.
assertEquals("800 253 0000",
phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US, true));
phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US,
true /* keep formatting */));
assertEquals("", phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, true));
assertEquals("+1 650 253 0000",
phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, true));
@ -623,12 +626,26 @@ public class PhoneNumberUtilTest extends TestCase {
phoneUtil.formatNumberForMobileDialing(usNumberWithExtn, RegionCode.US, true));
assertEquals("8002530000",
phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US, false));
phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US,
false /* remove formatting */));
assertEquals("", phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, false));
assertEquals("+16502530000",
phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, false));
assertEquals("+16502530000",
phoneUtil.formatNumberForMobileDialing(usNumberWithExtn, RegionCode.US, false));
// An invalid US number, which is one digit too long.
assertEquals("+165025300001",
phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER, RegionCode.US, false));
assertEquals("+1 65025300001",
phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER, RegionCode.US, true));
// Star numbers. In real life they appear in Israel, but we have them in JP in our test
// metadata.
assertEquals("*2345",
phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER, RegionCode.JP, false));
assertEquals("*2345",
phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER, RegionCode.JP, true));
}
public void testFormatByPattern() {
@ -730,6 +747,11 @@ public class PhoneNumberUtilTest extends TestCase {
// When the raw input is unavailable, format as usual.
PhoneNumber number7 = phoneUtil.parse("7345678901", RegionCode.US);
assertEquals("734 567 8901", phoneUtil.formatInOriginalFormat(number7, RegionCode.US));
// This number is valid, but we don't have a formatting pattern for it. Fall back to the raw
// input.
PhoneNumber number8 = phoneUtil.parseAndKeepRawInput("02-4567-8900", RegionCode.KR);
assertEquals("02-4567-8900", phoneUtil.formatInOriginalFormat(number8, RegionCode.KR));
}
public void testIsPremiumRate() {


BIN
java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_JP View File


+ 13
- 0
java/release_notes.txt View File

@ -1,3 +1,16 @@
November 24th, 2011: libphonenumber-4.3
* Code changes
- Fix the problems with AYTF crashing for longer numbers entered with +CountryCode, and incorrectly
removing national prefix for some numbers.
- Improve PhoneNumberMatcher to not match numbers ending with '%'.
- Fix formatNumberForMobileDialing to handle Israeli star numbers, Peruvian and Colombian numbers.
- Modify formatInOriginalFormat to use the raw input if we don't have a formatting pattern for a
number.
- Simple offline geocoding function which takes into account the user's region.
* Metadata changes
- Updates for CR, GN, JP, KE, PL, SG, SR, SX, TH, TK
November 10th, 2011: libphonenumber-4.2
* Code changes
- Providing an "exact match" isEmergencyNumber method


+ 146
- 63
resources/PhoneNumberMetaData.xml View File

@ -4436,7 +4436,7 @@
<!-- Costa Rica -->
<!-- http://www.itu.int/oth/T0202000030/en -->
<territory id="CR" countryCode="506" internationalPrefix="00"
nationalPrefixForParsing="(1900)" carrierCodeFormattingRule="$CC $FG">
nationalPrefixForParsing="(19(?:0[0-2]|19|77))" carrierCodeFormattingRule="$CC $FG">
<availableFormats>
<numberFormat pattern="(\d{4})(\d{4})">
<leadingDigits>
@ -4463,8 +4463,11 @@
<!-- Adding 8[67] prefix after numbers were found online where these prefixes have been
assigned. -->
<nationalNumberPattern>
57[01]\d[01]\d{3}|
8[36789]\d{6}
5(?:
0[0-4]\d{5}|
7[01]\d[01]\d{3}
)|
8[36-9]\d{6}
</nationalNumberPattern>
<possibleNumberPattern>\d{8}</possibleNumberPattern>
<exampleNumber>83123456</exampleNumber>
@ -4488,7 +4491,8 @@
2(?:
00\d|
900
)\d{2}
)\d{2}|
5\d{5}
)
</nationalNumberPattern>
<possibleNumberPattern>\d{8}</possibleNumberPattern>
@ -4512,14 +4516,17 @@
)|
2(?:
12|
22|
34
)|
333|
400|
55[15]|
7(?:
00|
1[78]|
77
)|
)
)
</nationalNumberPattern>
<possibleNumberPattern>\d{4}</possibleNumberPattern>
@ -7900,7 +7907,7 @@
55\d{6}|
6(?:
0(?:
2\d|
2[0-35-9]|
3[3467]|
5[2457-9]
)|
@ -7913,6 +7920,10 @@
</nationalNumberPattern>
<exampleNumber>60201234</exampleNumber>
</mobile>
<voip>
<nationalNumberPattern>78\d{6}</nationalNumberPattern>
<exampleNumber>78123456</exampleNumber>
</voip>
<!-- No national emergency numbers were located for Guinea. -->
</territory>
@ -11033,10 +11044,14 @@
</numberFormat>
<!-- National-only toll-free numbers (0077 and 0088, where the leading "0" is considered the
national prefix) -->
<numberFormat pattern="(\d{3})(\d{4,5})">
<numberFormat pattern="(\d{3})(\d{4})">
<leadingDigits>077</leadingDigits>
<format>$1-$2</format>
</numberFormat>
<numberFormat pattern="(\d{3})(\d{2})(\d{3,4})">
<leadingDigits>077</leadingDigits>
<format>$1-$2-$3</format>
</numberFormat>
<numberFormat pattern="(\d{3})(\d{2})(\d{4})">
<leadingDigits>088</leadingDigits>
<format>$1-$2-$3</format>
@ -11809,7 +11824,7 @@
<nationalNumberPattern>
[1-9]\d{8,9}|
0(?:
7\d{5,6}|
7\d{5,7}|
8\d{7}
)
</nationalNumberPattern>
@ -11821,12 +11836,12 @@
0(?:
777(?:
[01]\d{2}|
5\d{3}
5\d{3}|
8\d{4}
)|
882[1245]\d{4}
)
</nationalNumberPattern>
<possibleNumberPattern>\d{7,9}</possibleNumberPattern>
<exampleNumber>0777012</exampleNumber>
</noInternationalDialling>
<fixedLine>
@ -11899,12 +11914,12 @@
0(?:
777(?:
[01]\d{2}|
5\d{3}
5\d{3}|
8\d{4}
)|
882[1245]\d{4}
)
</nationalNumberPattern>
<possibleNumberPattern>\d{7,10}</possibleNumberPattern>
<exampleNumber>120123456</exampleNumber>
</tollFree>
<premiumRate>
@ -11941,22 +11956,23 @@
nationalPrefix="0" nationalPrefixFormattingRule="$NP$FG">
<availableFormats>
<numberFormat pattern="(\d{2})(\d{4,7})">
<leadingDigits>
[2-6]|
91
</leadingDigits>
<leadingDigits>[24-6]</leadingDigits>
<format>$1 $2</format>
</numberFormat>
<numberFormat pattern="(\d{3})(\d{6,7})">
<leadingDigits>
[78]|
90
</leadingDigits>
<leadingDigits>7</leadingDigits>
<format>$1 $2</format>
</numberFormat>
<numberFormat pattern="(\d{3})(\d{3})(\d{3,4})">
<leadingDigits>[89]</leadingDigits>
<format>$1 $2 $3</format>
</numberFormat>
</availableFormats>
<generalDesc>
<nationalNumberPattern>\d{6,10}</nationalNumberPattern>
<nationalNumberPattern>
20\d{4,7}|
[4-9]\d{5,9}
</nationalNumberPattern>
<possibleNumberPattern>\d{4,10}</possibleNumberPattern>
</generalDesc>
<fixedLine>
@ -11972,41 +11988,52 @@
<exampleNumber>202012345</exampleNumber>
</fixedLine>
<mobile>
<!-- Adding 70[0-5], 71[6-9], 73[019], 753 and 77[145] after successful delivery of SMSs.
-->
<!-- Adding 70[3-5], and 775 after successful delivery of SMSs. -->
<nationalNumberPattern>
7(?:
0[0-5]|
[123]\d|
5[0-3]|
7[0-5]
7[0-5]|
8[6-9]
)\d{6}
</nationalNumberPattern>
<possibleNumberPattern>\d{9}</possibleNumberPattern>
<exampleNumber>712123456</exampleNumber>
</mobile>
<!-- There is no document that has the information on the actual length of premium rates and
tollfree numbers. The information below comes from research on existing numbers. -->
<tollFree>
<nationalNumberPattern>
8(?:
00|
88
)\d{6,7}
</nationalNumberPattern>
<!-- Longer numbers have been found than the plan suggests, so we support them here too. -->
<nationalNumberPattern>800[245-8]\d{5,6}</nationalNumberPattern>
<possibleNumberPattern>\d{9,10}</possibleNumberPattern>
<exampleNumber>800123456</exampleNumber>
<exampleNumber>800223456</exampleNumber>
</tollFree>
<premiumRate>
<nationalNumberPattern>
9(?:
00|
1
)\d{6,7}
</nationalNumberPattern>
<possibleNumberPattern>\d{8,10}</possibleNumberPattern>
<exampleNumber>900123456</exampleNumber>
<nationalNumberPattern>
9(?:
00[2-578]|
11\d
)\d{5}
</nationalNumberPattern>
<possibleNumberPattern>\d{9}</possibleNumberPattern>
<exampleNumber>900223456</exampleNumber>
</premiumRate>
<shortCode>
<nationalNumberPattern>
1(?:
0[089]|
1(?:
[06]|
99
)|
2[123]|
3[013]|
4[14]|
501
)
</nationalNumberPattern>
<possibleNumberPattern>\d{3,4}</possibleNumberPattern>
<exampleNumber>116</exampleNumber>
</shortCode>
<emergency>
<!-- http://www.kenyapolice.go.ke/report_a_crime.asp -->
<nationalNumberPattern>
@ -17750,24 +17777,50 @@
</leadingDigits>
<format>$1 $2 $3 $4</format>
</numberFormat>
<numberFormat pattern="(\d{2})(\d{4,6})">
<!-- Pattern for shorter fixed-line numbers. -->
<leadingDigits>
[124]|
3[2-4]|
5[24-689]|
6[1-3578]|
7[14-7]|
8[1-7]
</leadingDigits>
<format>$1 $2</format>
</numberFormat>
<!-- We are formatting 70 numbers as per mobile numbers, based on information from some
Poles that this is more usual. -->
<numberFormat pattern="(\d{3})(\d{3})(\d{3})">
<leadingDigits>
39|
5[013]|
6[069]|
6[0469]|
7[0289]|
8[08]
</leadingDigits>
<format>$1 $2 $3</format>
</numberFormat>
<!-- Additional patterns for shorter pager numbers. -->
<numberFormat pattern="(\d{3})(\d{2})(\d{2,3})">
<leadingDigits>64</leadingDigits>
<format>$1 $2 $3</format>
</numberFormat>
<numberFormat pattern="(\d{3})(\d{3})">
<leadingDigits>64</leadingDigits>
<format>$1 $2</format>
</numberFormat>
</availableFormats>
<generalDesc>
<nationalNumberPattern>[1-9]\d{8}</nationalNumberPattern>
<possibleNumberPattern>\d{9}</possibleNumberPattern>
<nationalNumberPattern>
[1-58]\d{6,8}|
9\d{8}|
[67]\d{5,8}
</nationalNumberPattern>
<possibleNumberPattern>\d{6,9}</possibleNumberPattern>
</generalDesc>
<fixedLine>
<!-- Grouped by prefix-length. -->
<nationalNumberPattern>
(?:
1[2-8]|
@ -17776,8 +17829,12 @@
4[1-468]|
5[24-689]|
6[1-3578]|
7[14-7]|
8[1-79]|
7[14-6]|
8[1-7]
)\d{5,7}|
77\d{4,6}|
(?:
89|
9[145]
)\d{7}
</nationalNumberPattern>
@ -17792,22 +17849,31 @@
88
)\d{7}
</nationalNumberPattern>
<possibleNumberPattern>\d{9}</possibleNumberPattern>
<exampleNumber>512345678</exampleNumber>
</mobile>
<pager>
<nationalNumberPattern>642\d{3,6}</nationalNumberPattern>
<exampleNumber>642123456</exampleNumber>
</pager>
<tollFree>
<nationalNumberPattern>800\d{6}</nationalNumberPattern>
<possibleNumberPattern>\d{9}</possibleNumberPattern>
<exampleNumber>800123456</exampleNumber>
</tollFree>
<premiumRate>
<nationalNumberPattern>70\d{7}</nationalNumberPattern>
<possibleNumberPattern>\d{9}</possibleNumberPattern>
<exampleNumber>701234567</exampleNumber>
</premiumRate>
<sharedCost>
<nationalNumberPattern>801\d{6}</nationalNumberPattern>
<possibleNumberPattern>\d{9}</possibleNumberPattern>
<exampleNumber>801234567</exampleNumber>
</sharedCost>
<voip>
<nationalNumberPattern>39\d{7}</nationalNumberPattern>
<possibleNumberPattern>\d{9}</possibleNumberPattern>
<exampleNumber>391234567</exampleNumber>
</voip>
<emergency>
@ -19312,13 +19378,7 @@
<exampleNumber>1312</exampleNumber>
</shortCode>
<emergency>
<nationalNumberPattern>
112|
9(?:
11|
9[59]
)
</nationalNumberPattern>
<nationalNumberPattern>99[59]</nationalNumberPattern>
<possibleNumberPattern>\d{3}</possibleNumberPattern>
<exampleNumber>999</exampleNumber>
</emergency>
@ -19914,7 +19974,7 @@
<!-- Adding 74 from numbers found online. -->
<nationalNumberPattern>
(?:
7[1245]|
7[1-5]|
8[1-9]
)\d{5}
</nationalNumberPattern>
@ -20044,7 +20104,12 @@
<possibleNumberPattern>\d{7}(?:\d{3})?</possibleNumberPattern>
</generalDesc>
<fixedLine>
<nationalNumberPattern>72154[2-8]\d{4}</nationalNumberPattern>
<nationalNumberPattern>
7215(?:
4[2-8]|
8[239]
)\d{4}
</nationalNumberPattern>
<exampleNumber>7215425678</exampleNumber>
</fixedLine>
<mobile>
@ -20054,7 +20119,7 @@
7215(?:
1[02]|
2\d|
5[0346]|
5[03469]|
8[01678]
)\d{4}
</nationalNumberPattern>
@ -20411,6 +20476,7 @@
<!-- Thailand -->
<!-- http://www.itu.int/oth/T02020000CD/en -->
<!-- http://www.barascientific.com/bscnews/variety/emergency/Tel-4Digi.pdf -->
<territory id="TH" countryCode="66" internationalPrefix="00"
nationalPrefix="0" nationalPrefixFormattingRule="$NP$FG">
<availableFormats>
@ -20437,10 +20503,15 @@
<generalDesc>
<nationalNumberPattern>
[2-9]\d{7,8}|
1\d{9}
1\d{3}(?:\d{6})?
</nationalNumberPattern>
<possibleNumberPattern>\d{8,10}</possibleNumberPattern>
<possibleNumberPattern>\d{4}|\d{8,10}</possibleNumberPattern>
</generalDesc>
<noInternationalDialling>
<nationalNumberPattern>1\d{3}</nationalNumberPattern>
<possibleNumberPattern>\d{4}</possibleNumberPattern>
<exampleNumber>1100</exampleNumber>
</noInternationalDialling>
<fixedLine>
<nationalNumberPattern>
(?:
@ -20477,6 +20548,13 @@
<possibleNumberPattern>\d{9}</possibleNumberPattern>
<exampleNumber>601234567</exampleNumber>
</voip>
<uan>
<!-- 4-digit numbers are classified under UAN. Most of these are commercial numbers, and
their costs range from free to 6 Baht/min. -->
<nationalNumberPattern>1\d{3}</nationalNumberPattern>
<possibleNumberPattern>\d{4}</possibleNumberPattern>
<exampleNumber>1100</exampleNumber>
</uan>
<emergency>
<nationalNumberPattern>
1(?:
@ -20587,14 +20665,19 @@
<!-- Tokelau -->
<!-- http://www.itu.int/oth/T02020000D2/en -->
<territory id="TK" countryCode="690" internationalPrefix="00">
<!-- No more information beyond leading digit and number length can be found. Numbers are
formatted as a block. The example number is the test number provided in the ITU document.
-->
<generalDesc>
<nationalNumberPattern>[1-9]\d{3}</nationalNumberPattern>
<nationalNumberPattern>[2-5]\d{3}</nationalNumberPattern>
<possibleNumberPattern>\d{4}</possibleNumberPattern>
<exampleNumber>3190</exampleNumber>
</generalDesc>
<fixedLine>
<nationalNumberPattern>[2-4]\d{3}</nationalNumberPattern>
<!-- The example number is the contact number from the ITU document. -->
<exampleNumber>3010</exampleNumber>
</fixedLine>
<mobile>
<nationalNumberPattern>5\d{3}</nationalNumberPattern>
<exampleNumber>5190</exampleNumber>
</mobile>
<!-- No evidence that any emergency numbers exist for this tiny island has been found. -->
</territory>


+ 8
- 4
resources/PhoneNumberMetaDataForTesting.xml View File

@ -346,9 +346,9 @@
<format>$1 $2 $3 $4</format>
</numberFormat>
<numberFormat pattern="(\d{2})(\d{3})(\d{4})">
<leadingDigits>222|333</leadingDigits>
<leadingDigits>(?:222|333)1</leadingDigits>
<leadingDigits>(?:222|333)11</leadingDigits>
<leadingDigits>111|222|333</leadingDigits>
<leadingDigits>(?:111|222|333)1</leadingDigits>
<leadingDigits>(?:111|222|333)11</leadingDigits>
<format>$1 $2 $3</format>
</numberFormat>
<numberFormat pattern="(\d{4})(\d)(\d{4})">
@ -364,11 +364,15 @@
</numberFormat>
<!-- The following numberFormat is added to test that the format containing the star sign is
not used by the AYTF. -->
<numberFormat pattern="(\d{4})">
<numberFormat nationalPrefixFormattingRule="$FG" pattern="(\d{4})">
<leadingDigits>[23]</leadingDigits>
<format>*$1</format>
</numberFormat>
</availableFormats>
<noInternationalDialling>
<nationalNumberPattern>[23]\d{3}</nationalNumberPattern>
<possibleNumberPattern>\d{4}</possibleNumberPattern>
</noInternationalDialling>
</territory>
<!-- Korea (Rep. of) -->


Loading…
Cancel
Save