Browse Source

JAVA: libphonenumber 2011/04/11 release.

pull/567/head
Shaopeng Jia 15 years ago
committed by Mihaela Rosca
parent
commit
6dda1bb87f
49 changed files with 662 additions and 355 deletions
  1. +11
    -0
      java/release_notes.txt
  2. +66
    -66
      java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java
  3. +2
    -1
      java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java
  4. +9
    -2
      java/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java
  5. +49
    -39
      java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
  6. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AG
  7. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AI
  8. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AS
  9. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BB
  10. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BM
  11. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BS
  12. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CA
  13. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CK
  14. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_DM
  15. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_DO
  16. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_ET
  17. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_FK
  18. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GD
  19. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GE
  20. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GT
  21. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GU
  22. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN
  23. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IO
  24. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JM
  25. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KI
  26. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KN
  27. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KY
  28. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_LC
  29. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MN
  30. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MP
  31. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MR
  32. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MS
  33. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_NO
  34. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PR
  35. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SJ
  36. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SV
  37. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TC
  38. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TR
  39. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TT
  40. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_US
  41. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_VC
  42. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_VG
  43. BIN
      java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_VI
  44. +12
    -1
      java/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java
  45. +8
    -3
      java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
  46. BIN
      java/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG
  47. BIN
      java/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US
  48. +499
    -241
      resources/PhoneNumberMetaData.xml
  49. +6
    -2
      resources/PhoneNumberMetaDataForTesting.xml

+ 11
- 0
java/release_notes.txt View File

@ -1,3 +1,14 @@
Apr 11th, 2011
* Bug fixes:
- Adding date exclusion pattern in PhoneNumberMatcher.
- Fixing bug with selecting a suitable region for numbers with leading zeros where mutiple regions
for that country code are possible.
* Metadata changes:
- Updates: AG, AI, AS, BB, BM, BS, CA, DM, DO, ET, FO, GD, GE, GU, IN, JM, KN, KY, LC, MN, MP, MR,
MS, NO, PR, TC, TR, TT, US, VC, VG, VI
- New countries: CK, FK, GT, IO, KI, SJ, SV
* Documentation updates and some small refactoring to AsYouTypeFormatter
Mar 31st, 2011
* New functionality:
- Can parse and format numbers in RFC-3966 format.


+ 66
- 66
java/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java View File

@ -28,13 +28,13 @@ import java.util.regex.Pattern;
/**
* A formatter which formats phone numbers as they are entered.
*
* An AsYouTypeFormatter could be created by invoking the getAsYouTypeFormatter method of the
* PhoneNumberUtil. After that digits could be added by invoking the inputDigit method on the
* formatter instance, and the partially formatted phone number will be returned each time a digit
* is added. The clear method could be invoked before a new number needs to be formatted.
* <p>An AsYouTypeFormatter could be created by invoking
* {@link PhoneNumberUtil#getAsYouTypeFormatter}. After that digits could be added by invoking
* {@link #inputDigit} on the formatter instance, and the partially formatted phone number will be
* returned each time a digit is added. {@link #clear} could be invoked before a new number needs to
* be formatted.
*
* See testAsYouTypeFormatterUS(), testAsYouTestFormatterGB() and testAsYouTypeFormatterDE() in
* PhoneNumberUtilTest.java for more details on how the formatter is to be used.
* <p>See the unittests for more details on how the formatter is to be used.
*
* @author Shaopeng Jia
*/
@ -47,9 +47,12 @@ public class AsYouTypeFormatter {
private StringBuffer accruedInputWithoutFormatting = new StringBuffer();
private boolean ableToFormat = true;
private boolean isInternationalFormatting = false;
private boolean isExpectingCountryCode = false;
private boolean isExpectingCountryCallingCode = false;
private final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
private String defaultCountry;
private static final PhoneMetadata EMPTY_METADATA =
new PhoneMetadata().setInternationalPrefix("NA");
private PhoneMetadata defaultMetaData;
private PhoneMetadata currentMetaData;
@ -79,8 +82,6 @@ public class AsYouTypeFormatter {
// The position of a digit upon which inputDigitAndRememberPosition is most recently invoked, as
// found in accruedInputWithoutFormatting.
private int positionToRemember = 0;
private Pattern nationalPrefixForParsing;
private Pattern internationalPrefix;
private StringBuffer prefixBeforeNationalNumber = new StringBuffer();
private StringBuffer nationalNumber = new StringBuffer();
private List<NumberFormat> possibleFormats = new ArrayList<NumberFormat>();
@ -96,21 +97,18 @@ public class AsYouTypeFormatter {
*/
AsYouTypeFormatter(String regionCode) {
defaultCountry = regionCode;
initializeCountrySpecificInfo(defaultCountry);
currentMetaData = getMetadataForRegion(defaultCountry);
defaultMetaData = currentMetaData;
}
private void initializeCountrySpecificInfo(String regionCode) {
currentMetaData = phoneUtil.getMetadataForRegion(regionCode);
if (currentMetaData == null) {
// 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 "+".
currentMetaData = new PhoneMetadata().setInternationalPrefix("NA");
private PhoneMetadata getMetadataForRegion(String regionCode) {
PhoneMetadata metadata = phoneUtil.getMetadataForRegion(regionCode);
if (metadata != null) {
return metadata;
}
nationalPrefixForParsing =
regexCache.getPatternForRegex(currentMetaData.getNationalPrefixForParsing());
internationalPrefix =
regexCache.getPatternForRegex("\\+|" + currentMetaData.getInternationalPrefix());
// 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 "+".
return EMPTY_METADATA;
}
// Returns true if a new template is created as opposed to reusing the existing template.
@ -141,8 +139,7 @@ public class AsYouTypeFormatter {
}
private void narrowDownPossibleFormats(String leadingDigits) {
int lengthOfLeadingDigits = leadingDigits.length();
int indexOfLeadingDigitsPattern = lengthOfLeadingDigits - MIN_LEADING_DIGITS_LENGTH;
int indexOfLeadingDigitsPattern = leadingDigits.length() - MIN_LEADING_DIGITS_LENGTH;
Iterator<NumberFormat> it = possibleFormats.iterator();
while (it.hasNext()) {
NumberFormat format = it.next();
@ -160,7 +157,6 @@ public class AsYouTypeFormatter {
}
private boolean createFormattingTemplate(NumberFormat format) {
String numberFormat = format.getFormat();
String numberPattern = format.getPattern();
// The formatter doesn't format numbers when numberPattern contains "|", e.g.
@ -175,7 +171,7 @@ public class AsYouTypeFormatter {
// Replace any standalone digit (not the one in d{}) with \d
numberPattern = STANDALONE_DIGIT_PATTERN.matcher(numberPattern).replaceAll("\\\\d");
formattingTemplate.setLength(0);
String tempTemplate = getFormattingTemplate(numberPattern, numberFormat);
String tempTemplate = getFormattingTemplate(numberPattern, format.getFormat());
if (tempTemplate.length() > nationalNumber.length()) {
formattingTemplate.append(tempTemplate);
return true;
@ -215,10 +211,10 @@ public class AsYouTypeFormatter {
positionToRemember = 0;
originalPosition = 0;
isInternationalFormatting = false;
isExpectingCountryCode = false;
isExpectingCountryCallingCode = false;
possibleFormats.clear();
if (!currentMetaData.equals(defaultMetaData)) {
initializeCountrySpecificInfo(defaultCountry);
currentMetaData = getMetadataForRegion(defaultCountry);
}
}
@ -226,8 +222,9 @@ public class AsYouTypeFormatter {
* Formats a phone number on-the-fly as each digit is entered.
*
* @param nextChar the most recently entered digit of a phone number. Formatting characters are
* allowed, but they are removed from the result. Full width digits and Arabic-indic digits
* are allowed, and will be shown as they are.
* allowed, but as soon as they are encountered this method formats the number as entered and
* not "as you type" anymore. Full width digits and Arabic-indic digits are allowed, and will
* be shown as they are.
* @return the partially formatted phone number.
*/
public String inputDigit(char nextChar) {
@ -236,9 +233,10 @@ public class AsYouTypeFormatter {
}
/**
* Same as inputDigit, but remembers the position where nextChar is inserted, so that it could be
* retrieved later by using getRememberedPosition(). The remembered position will be automatically
* adjusted if additional formatting characters are later inserted/removed in front of nextChar.
* Same as {@link #inputDigit}, but remembers the position where {@code nextChar} is inserted, so
* that it could be retrieved later by using {@link #getRememberedPosition}. The remembered
* position will be automatically adjusted if additional formatting characters are later
* inserted/removed in front of {@code nextChar}.
*/
public String inputDigitAndRememberPosition(char nextChar) {
currentOutput = inputDigitWithOptionToRememberPosition(nextChar, true);
@ -265,29 +263,29 @@ public class AsYouTypeFormatter {
// We start to attempt to format only when at least MIN_LEADING_DIGITS_LENGTH digits (the plus
// sign is counted as a digit as well for this purpose) have been entered.
switch (accruedInputWithoutFormatting.length()) {
case 0: // this is the case where the first few inputs are neither digits nor the plus sign.
case 0:
case 1:
case 2:
return accruedInput.toString();
case 3:
if (attemptToExtractIdd()) {
isExpectingCountryCode = true;
isExpectingCountryCallingCode = true;
} else { // No IDD or plus sign is found, must be entering in national format.
removeNationalPrefixFromNationalNumber();
return attemptToChooseFormattingPattern();
}
case 4:
case 5:
if (isExpectingCountryCode) {
if (attemptToExtractCountryCode()) {
isExpectingCountryCode = false;
if (isExpectingCountryCallingCode) {
if (attemptToExtractCountryCallingCode()) {
isExpectingCountryCallingCode = false;
}
return prefixBeforeNationalNumber + nationalNumber.toString();
}
// We make a last attempt to extract a country code at the 6th digit because the maximum
// length of IDD and country code are both 3.
// We make a last attempt to extract a country calling code at the 6th digit because the
// maximum length of IDD and country calling code are both 3.
case 6:
if (isExpectingCountryCode && !attemptToExtractCountryCode()) {
if (isExpectingCountryCallingCode && !attemptToExtractCountryCallingCode()) {
ableToFormat = false;
return accruedInput.toString();
}
@ -326,22 +324,19 @@ public class AsYouTypeFormatter {
/**
* Returns the current position in the partially formatted phone number of the character which was
* previously passed in as the parameter of inputDigitAndRememberPosition().
* previously passed in as the parameter of {@link #inputDigitAndRememberPosition}.
*/
public int getRememberedPosition() {
if (!ableToFormat) {
return originalPosition;
}
int accruedInputIndex = 0, currentOutputIndex = 0;
int currentOutputLength = currentOutput.length();
while (accruedInputIndex < positionToRemember && currentOutputIndex < currentOutputLength) {
while (accruedInputIndex < positionToRemember && currentOutputIndex < currentOutput.length()) {
if (accruedInputWithoutFormatting.charAt(accruedInputIndex) ==
currentOutput.charAt(currentOutputIndex)) {
accruedInputIndex++;
currentOutputIndex++;
} else {
currentOutputIndex++;
}
currentOutputIndex++;
}
return currentOutputIndex;
}
@ -384,6 +379,8 @@ public class AsYouTypeFormatter {
prefixBeforeNationalNumber.append("1 ");
isInternationalFormatting = true;
} else if (currentMetaData.hasNationalPrefix()) {
Pattern nationalPrefixForParsing =
regexCache.getPatternForRegex(currentMetaData.getNationalPrefixForParsing());
Matcher m = nationalPrefixForParsing.matcher(nationalNumber);
if (m.lookingAt()) {
// When the national prefix is detected, we use international formatting rules instead of
@ -405,14 +402,17 @@ public class AsYouTypeFormatter {
* defaultCountry.
*/
private boolean attemptToExtractIdd() {
Pattern internationalPrefix =
regexCache.getPatternForRegex("\\" + PhoneNumberUtil.PLUS_SIGN + "|" +
currentMetaData.getInternationalPrefix());
Matcher iddMatcher = internationalPrefix.matcher(accruedInputWithoutFormatting);
if (iddMatcher.lookingAt()) {
isInternationalFormatting = true;
int startOfCountryCode = iddMatcher.end();
int startOfCountryCallingCode = iddMatcher.end();
nationalNumber.setLength(0);
nationalNumber.append(accruedInputWithoutFormatting.substring(startOfCountryCode));
nationalNumber.append(accruedInputWithoutFormatting.substring(startOfCountryCallingCode));
prefixBeforeNationalNumber.append(
accruedInputWithoutFormatting.substring(0, startOfCountryCode));
accruedInputWithoutFormatting.substring(0, startOfCountryCallingCode));
if (accruedInputWithoutFormatting.charAt(0) != PhoneNumberUtil.PLUS_SIGN) {
prefixBeforeNationalNumber.append(" ");
}
@ -422,41 +422,41 @@ public class AsYouTypeFormatter {
}
/**
* Extracts country code from the beginning of nationalNumber to prefixBeforeNationalNumber when
* they are available, and places the remaining input into nationalNumber.
* Extracts the country calling code from the beginning of nationalNumber to
* prefixBeforeNationalNumber when they are available, and places the remaining input into
* nationalNumber.
*
* @return true when a valid country code can be found.
* @return true when a valid country calling code can be found.
*/
private boolean attemptToExtractCountryCode() {
private boolean attemptToExtractCountryCallingCode() {
if (nationalNumber.length() == 0) {
return false;
}
StringBuffer numberWithoutCountryCode = new StringBuffer();
int countryCode = phoneUtil.extractCountryCode(nationalNumber, numberWithoutCountryCode);
StringBuffer numberWithoutCountryCallingCode = new StringBuffer();
int countryCode = phoneUtil.extractCountryCode(nationalNumber, numberWithoutCountryCallingCode);
if (countryCode == 0) {
return false;
} else {
nationalNumber.setLength(0);
nationalNumber.append(numberWithoutCountryCode);
String newRegionCode = phoneUtil.getRegionCodeForCountryCode(countryCode);
if (!newRegionCode.equals(defaultCountry)) {
initializeCountrySpecificInfo(newRegionCode);
}
String countryCodeString = Integer.toString(countryCode);
prefixBeforeNationalNumber.append(countryCodeString).append(" ");
}
nationalNumber.setLength(0);
nationalNumber.append(numberWithoutCountryCallingCode);
String newRegionCode = phoneUtil.getRegionCodeForCountryCode(countryCode);
if (newRegionCode != defaultCountry) {
currentMetaData = getMetadataForRegion(newRegionCode);
}
String countryCodeString = Integer.toString(countryCode);
prefixBeforeNationalNumber.append(countryCodeString).append(" ");
return true;
}
// Accrues digits and the plus sign to accruedInputWithoutFormatting for later use. If nextChar
// contains a digit in non-ASCII format (e.g. the full-width version of digits), it is first
// normalized to the ASCII version. The return value is nextChar itself, or its normalized
// version, if nextChar is a digit in non-ASCII format.
// version, if nextChar is a digit in non-ASCII format. This method assumes its input is either a
// digit or the plus sign.
private char normalizeAndAccrueDigitsAndPlusSign(char nextChar, boolean rememberPosition) {
if (nextChar == PhoneNumberUtil.PLUS_SIGN) {
accruedInputWithoutFormatting.append(nextChar);
}
if (PhoneNumberUtil.DIGIT_MAPPINGS.containsKey(nextChar)) {
} else {
nextChar = PhoneNumberUtil.DIGIT_MAPPINGS.get(nextChar);
accruedInputWithoutFormatting.append(nextChar);
nationalNumber.append(nextChar);


+ 2
- 1
java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java View File

@ -132,8 +132,9 @@ public class CountryCodeToRegionCodeMap {
listWithRegionCode.add("SE");
countryCodeToRegionCodeMap.put(46, listWithRegionCode);
listWithRegionCode = new ArrayList<String>(1);
listWithRegionCode = new ArrayList<String>(2);
listWithRegionCode.add("NO");
listWithRegionCode.add("SJ");
countryCodeToRegionCodeMap.put(47, listWithRegionCode);
listWithRegionCode = new ArrayList<String>(1);


+ 9
- 2
java/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java View File

@ -68,6 +68,13 @@ final class PhoneNumberMatcher implements Iterator<PhoneNumberMatch> {
*/
private static final Pattern PUB_PAGES = Pattern.compile("\\d{1,5}-+\\d{1,5}\\s{0,4}\\(\\d{1,4}");
/**
* Matches strings that look like dates using "/" as a separator. Examples: 3/10/2011, 31/10/96 or
* 08/31/95.
*/
private static final Pattern SLASH_SEPARATED_DATES =
Pattern.compile("(?:(?:[0-3]?\\d/[01]?\\d)|(?:[01]?\\d/[0-3]?\\d))/(?:[12]\\d)?\\d{2}");
static {
/* Builds the PATTERN and INNER regular expression patterns. The building blocks below
* exist to make the patterns more easily understood. */
@ -268,8 +275,8 @@ final class PhoneNumberMatcher implements Iterator<PhoneNumberMatch> {
* @return the match found, null if none can be found
*/
private PhoneNumberMatch extractMatch(CharSequence candidate, int offset) {
// Skip a match that is more likely a publication page reference.
if (PUB_PAGES.matcher(candidate).find()) {
// Skip a match that is more likely a publication page reference or a date.
if (PUB_PAGES.matcher(candidate).find() || SLASH_SEPARATED_DATES.matcher(candidate).find()) {
return null;
}


+ 49
- 39
java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java View File

@ -1527,7 +1527,7 @@ public class PhoneNumberUtil {
private String getRegionCodeForNumberFromRegionList(PhoneNumber number,
List<String> regionCodes) {
String nationalNumber = String.valueOf(number.getNationalNumber());
String nationalNumber = getNationalSignificantNumber(number);
for (String regionCode : regionCodes) {
// If leadingDigits is present, use this. Otherwise, do full validation.
PhoneMetadata metadata = getMetadataForRegion(regionCode);
@ -1663,6 +1663,24 @@ public class PhoneNumberUtil {
return isPossibleNumberWithReason(number) == ValidationResult.IS_POSSIBLE;
}
/**
* Helper method to check a number against a particular pattern and determine whether it matches,
* or is too short or too long. Currently, if a number pattern suggests that numbers of length 7
* and 10 are possible, and a number in between these possible lengths is entered, such as of
* length 8, this will return TOO_LONG.
*/
private ValidationResult testNumberLengthAgainstPattern(Pattern numberPattern, String number) {
Matcher numberMatcher = numberPattern.matcher(number);
if (numberMatcher.matches()) {
return ValidationResult.IS_POSSIBLE;
}
if (numberMatcher.lookingAt()) {
return ValidationResult.TOO_LONG;
} else {
return ValidationResult.TOO_SHORT;
}
}
/**
* Check whether a phone number is a possible number. It provides a more lenient check than
* {@link #isValidNumber} in the following sense:
@ -1708,14 +1726,9 @@ public class PhoneNumberUtil {
return ValidationResult.IS_POSSIBLE;
}
}
String possibleNumberPattern = generalNumDesc.getPossibleNumberPattern();
Matcher m = regexCache.getPatternForRegex(possibleNumberPattern).matcher(nationalNumber);
if (m.lookingAt()) {
return (m.end() == nationalNumber.length()) ? ValidationResult.IS_POSSIBLE
: ValidationResult.TOO_LONG;
} else {
return ValidationResult.TOO_SHORT;
}
Pattern possibleNumberPattern =
regexCache.getPatternForRegex(generalNumDesc.getPossibleNumberPattern());
return testNumberLengthAgainstPattern(possibleNumberPattern, nationalNumber);
}
/**
@ -1869,37 +1882,34 @@ public class PhoneNumberUtil {
throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
"Country calling code supplied was not recognised.");
} else if (defaultRegionMetadata != null) {
// Check to see if the number is valid for the default region already. If not, we check to
// see if the country calling code for the default region is present at the start of the
// number.
PhoneNumberDesc generalDesc = defaultRegionMetadata.getGeneralDesc();
Pattern validNumberPattern =
regexCache.getPatternForRegex(generalDesc.getNationalNumberPattern());
if (!validNumberPattern.matcher(fullNumber).matches()) {
int defaultCountryCode = defaultRegionMetadata.getCountryCode();
String defaultCountryCodeString = String.valueOf(defaultCountryCode);
String normalizedNumber = fullNumber.toString();
if (normalizedNumber.startsWith(defaultCountryCodeString)) {
// If so, strip this, and see if the resultant number is valid.
StringBuffer potentialNationalNumber =
new StringBuffer(normalizedNumber.substring(defaultCountryCodeString.length()));
maybeStripNationalPrefixAndCarrierCode(potentialNationalNumber, defaultRegionMetadata);
Matcher possibleNumberMatcher =
regexCache.getPatternForRegex(generalDesc.getPossibleNumberPattern()).matcher(
potentialNationalNumber);
// If the resultant number is either valid, or still too long even with the country
// calling code stripped, we consider this a better result and keep the potential national
// number.
if (validNumberPattern.matcher(potentialNationalNumber).matches() ||
(possibleNumberMatcher.lookingAt() &&
possibleNumberMatcher.end() != potentialNationalNumber.length())) {
nationalNumber.append(potentialNationalNumber);
if (keepRawInput) {
phoneNumber.setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN);
}
phoneNumber.setCountryCode(defaultCountryCode);
return defaultCountryCode;
// Check to see if the number starts with the country calling code for the default region. If
// so, we remove the country calling code, and do some checks on the validity of the number
// before and after.
int defaultCountryCode = defaultRegionMetadata.getCountryCode();
String defaultCountryCodeString = String.valueOf(defaultCountryCode);
String normalizedNumber = fullNumber.toString();
if (normalizedNumber.startsWith(defaultCountryCodeString)) {
StringBuffer potentialNationalNumber =
new StringBuffer(normalizedNumber.substring(defaultCountryCodeString.length()));
PhoneNumberDesc generalDesc = defaultRegionMetadata.getGeneralDesc();
Pattern validNumberPattern =
regexCache.getPatternForRegex(generalDesc.getNationalNumberPattern());
maybeStripNationalPrefixAndCarrierCode(potentialNationalNumber, defaultRegionMetadata);
Pattern possibleNumberPattern =
regexCache.getPatternForRegex(generalDesc.getPossibleNumberPattern());
// If the number was not valid before but is valid now, or if it was too long before, we
// consider the number with the country code stripped to be a better result and keep that
// instead.
if ((!validNumberPattern.matcher(fullNumber).matches() &&
validNumberPattern.matcher(potentialNationalNumber).matches()) ||
testNumberLengthAgainstPattern(possibleNumberPattern, fullNumber.toString())
== ValidationResult.TOO_LONG) {
nationalNumber.append(potentialNationalNumber);
if (keepRawInput) {
phoneNumber.setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN);
}
phoneNumber.setCountryCode(defaultCountryCode);
return defaultCountryCode;
}
}
}


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AG View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AI View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AS View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BB View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BM View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BS View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CA View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CK View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_DM View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_DO View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_ET View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_FK View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GD View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GE View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GT View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GU View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IO View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JM View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KI View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KN View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KY View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_LC View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MN View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MP View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MR View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MS View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_NO View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PR View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SJ View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SV View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TC View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TR View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TT View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_US View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_VC View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_VG View File


BIN
java/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_VI View File


+ 12
- 1
java/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java View File

@ -453,7 +453,18 @@ public class PhoneNumberMatcherTest extends TestCase {
"As quoted by Alfonso 12-15 (2009), you may call me at ", ""));
contextPairs.add(new NumberContext(
"As quoted by Alfonso et al. 12-15 (2009), you may call me at ", ""));
// with a postfix stripped off as it looks like the start of another number
// With dates, written in the American style.
contextPairs.add(new NumberContext(
"As I said on 03/10/2011, you may call me at ", ""));
contextPairs.add(new NumberContext(
"As I said on 03/27/2011, you may call me at ", ""));
contextPairs.add(new NumberContext(
"As I said on 31/8/2011, you may call me at ", ""));
contextPairs.add(new NumberContext(
"As I said on 1/12/2011, you may call me at ", ""));
contextPairs.add(new NumberContext(
"I was born on 10/12/82. Please call me at ", ""));
// With a postfix stripped off as it looks like the start of another number
contextPairs.add(new NumberContext("Call ", "/x12 more"));
doTestInContext(number, defaultCountry, contextPairs, Leniency.POSSIBLE);


+ 8
- 3
java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File

@ -140,7 +140,7 @@ public class PhoneNumberUtilTest extends TestCase {
assertEquals("$1 $2 $3", metadata.getNumberFormat(0).getFormat());
assertEquals("[13-9]\\d{9}|2[0-35-9]\\d{8}",
metadata.getGeneralDesc().getNationalNumberPattern());
assertEquals("\\d{7,10}", metadata.getGeneralDesc().getPossibleNumberPattern());
assertEquals("\\d{7}(?:\\d{3})?", metadata.getGeneralDesc().getPossibleNumberPattern());
assertTrue(metadata.getGeneralDesc().exactlySameAs(metadata.getFixedLine()));
assertEquals("\\d{10}", metadata.getTollFree().getPossibleNumberPattern());
assertEquals("900\\d{7}", metadata.getPremiumRate().getNationalNumberPattern());
@ -913,6 +913,11 @@ public class PhoneNumberUtilTest extends TestCase {
assertEquals(PhoneNumberUtil.ValidationResult.TOO_SHORT,
phoneUtil.isPossibleNumberWithReason(number));
number.setCountryCode(65);
number.setNationalNumber(1234567890L);
assertEquals(PhoneNumberUtil.ValidationResult.IS_POSSIBLE,
phoneUtil.isPossibleNumberWithReason(number));
// Try with number that we don't have metadata for.
PhoneNumber adNumber = new PhoneNumber();
adNumber.setCountryCode(376).setNationalNumber(12345L);
@ -1242,9 +1247,9 @@ public class PhoneNumberUtilTest extends TestCase {
}
number.clear();
try {
String phoneNumber = "(1 610) 619 43";
String phoneNumber = "(1 610) 619";
StringBuffer numberToFill = new StringBuffer();
assertEquals("Should not have extracted a country calling code - invalid number both " +
assertEquals("Should not have extracted a country calling code - too short number both " +
"before and after extraction of uncertain country calling code.",
0,
phoneUtil.maybeExtractCountryCode(phoneNumber, metadata, numberToFill, true,


BIN
java/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_SG View File


BIN
java/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_US View File


+ 499
- 241
resources/PhoneNumberMetaData.xml
File diff suppressed because it is too large
View File


+ 6
- 2
resources/PhoneNumberMetaDataForTesting.xml View File

@ -670,7 +670,11 @@
</availableFormats>
<generalDesc>
<nationalNumberPattern>[13689]\d{7,10}</nationalNumberPattern>
<possibleNumberPattern>\d{8,11}</possibleNumberPattern>
<!-- This specific pattern with the | is used to unit-test IsPossibleNumberWithReason. -->
<possibleNumberPattern>
\d{8}|
\d{10,11}
</possibleNumberPattern>
</generalDesc>
<fixedLine>
<nationalNumberPattern>[36]\d{7}</nationalNumberPattern>
@ -710,7 +714,7 @@
</availableFormats>
<generalDesc>
<nationalNumberPattern>[13-9]\d{9}|2[0-35-9]\d{8}</nationalNumberPattern>
<possibleNumberPattern>\d{7,10}</possibleNumberPattern>
<possibleNumberPattern>\d{7}(?:\d{3})?</possibleNumberPattern>
<exampleNumber>1234567890</exampleNumber>
</generalDesc>
<noInternationalDialling>


Loading…
Cancel
Save