|
|
@ -769,7 +769,7 @@ public class PhoneNumberUtil { |
|
|
} |
|
|
} |
|
|
// If a country doesn't use a national prefix, and this number doesn't have an Italian leading |
|
|
// If a country doesn't use a national prefix, and this number doesn't have an Italian leading |
|
|
// zero, we assume it is a closed dialling plan with no area codes. |
|
|
// zero, we assume it is a closed dialling plan with no area codes. |
|
|
if (!metadata.hasNationalPrefix() && !number.isItalianLeadingZero()) { |
|
|
|
|
|
|
|
|
if (metadata.nationalPrefix.equals("") && !number.isItalianLeadingZero()) { |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -1123,19 +1123,19 @@ public class PhoneNumberUtil { |
|
|
// need to copy the rule so that subsequent replacements for different numbers have the |
|
|
// need to copy the rule so that subsequent replacements for different numbers have the |
|
|
// appropriate national prefix. |
|
|
// appropriate national prefix. |
|
|
numFormatCopy.mergeFrom(formattingPattern); |
|
|
numFormatCopy.mergeFrom(formattingPattern); |
|
|
String nationalPrefixFormattingRule = formattingPattern.getNationalPrefixFormattingRule(); |
|
|
|
|
|
|
|
|
String nationalPrefixFormattingRule = formattingPattern.nationalPrefixFormattingRule; |
|
|
if (nationalPrefixFormattingRule.length() > 0) { |
|
|
if (nationalPrefixFormattingRule.length() > 0) { |
|
|
String nationalPrefix = metadata.getNationalPrefix(); |
|
|
|
|
|
|
|
|
String nationalPrefix = metadata.nationalPrefix; |
|
|
if (nationalPrefix.length() > 0) { |
|
|
if (nationalPrefix.length() > 0) { |
|
|
// Replace $NP with national prefix and $FG with the first group ($1). |
|
|
// Replace $NP with national prefix and $FG with the first group ($1). |
|
|
nationalPrefixFormattingRule = |
|
|
nationalPrefixFormattingRule = |
|
|
NP_PATTERN.matcher(nationalPrefixFormattingRule).replaceFirst(nationalPrefix); |
|
|
NP_PATTERN.matcher(nationalPrefixFormattingRule).replaceFirst(nationalPrefix); |
|
|
nationalPrefixFormattingRule = |
|
|
nationalPrefixFormattingRule = |
|
|
FG_PATTERN.matcher(nationalPrefixFormattingRule).replaceFirst("\\$1"); |
|
|
FG_PATTERN.matcher(nationalPrefixFormattingRule).replaceFirst("\\$1"); |
|
|
numFormatCopy.setNationalPrefixFormattingRule(nationalPrefixFormattingRule); |
|
|
|
|
|
|
|
|
numFormatCopy.nationalPrefixFormattingRule = nationalPrefixFormattingRule; |
|
|
} else { |
|
|
} else { |
|
|
// We don't want to have a rule for how to format the national prefix if there isn't one. |
|
|
// We don't want to have a rule for how to format the national prefix if there isn't one. |
|
|
numFormatCopy.clearNationalPrefixFormattingRule(); |
|
|
|
|
|
|
|
|
numFormatCopy.nationalPrefixFormattingRule = ""; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
formattedNumber.append( |
|
|
formattedNumber.append( |
|
|
@ -1351,16 +1351,16 @@ public class PhoneNumberUtil { |
|
|
} |
|
|
} |
|
|
// Metadata cannot be null because we checked 'isValidRegionCode()' above. |
|
|
// Metadata cannot be null because we checked 'isValidRegionCode()' above. |
|
|
PhoneMetadata metadataForRegionCallingFrom = getMetadataForRegion(regionCallingFrom); |
|
|
PhoneMetadata metadataForRegionCallingFrom = getMetadataForRegion(regionCallingFrom); |
|
|
String internationalPrefix = metadataForRegionCallingFrom.getInternationalPrefix(); |
|
|
|
|
|
|
|
|
String internationalPrefix = metadataForRegionCallingFrom.internationalPrefix; |
|
|
|
|
|
|
|
|
// For regions that have multiple international prefixes, the international format of the |
|
|
// For regions that have multiple international prefixes, the international format of the |
|
|
// number is returned, unless there is a preferred international prefix. |
|
|
// number is returned, unless there is a preferred international prefix. |
|
|
String internationalPrefixForFormatting = ""; |
|
|
String internationalPrefixForFormatting = ""; |
|
|
if (UNIQUE_INTERNATIONAL_PREFIX.matcher(internationalPrefix).matches()) { |
|
|
if (UNIQUE_INTERNATIONAL_PREFIX.matcher(internationalPrefix).matches()) { |
|
|
internationalPrefixForFormatting = internationalPrefix; |
|
|
internationalPrefixForFormatting = internationalPrefix; |
|
|
} else if (metadataForRegionCallingFrom.hasPreferredInternationalPrefix()) { |
|
|
|
|
|
|
|
|
} else if (!metadataForRegionCallingFrom.preferredInternationalPrefix.equals("")) { |
|
|
internationalPrefixForFormatting = |
|
|
internationalPrefixForFormatting = |
|
|
metadataForRegionCallingFrom.getPreferredInternationalPrefix(); |
|
|
|
|
|
|
|
|
metadataForRegionCallingFrom.preferredInternationalPrefix; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
String regionCode = getRegionCodeForCountryCode(countryCallingCode); |
|
|
String regionCode = getRegionCodeForCountryCode(countryCallingCode); |
|
|
@ -1458,7 +1458,7 @@ public class PhoneNumberUtil { |
|
|
// return the national format. |
|
|
// return the national format. |
|
|
// TODO: Refactor the code below with the code in |
|
|
// TODO: Refactor the code below with the code in |
|
|
// isNationalPrefixPresentIfRequired. |
|
|
// isNationalPrefixPresentIfRequired. |
|
|
String candidateNationalPrefixRule = formatRule.getNationalPrefixFormattingRule(); |
|
|
|
|
|
|
|
|
String candidateNationalPrefixRule = formatRule.nationalPrefixFormattingRule; |
|
|
// We assume that the first-group symbol will never be _before_ the national prefix. |
|
|
// We assume that the first-group symbol will never be _before_ the national prefix. |
|
|
int indexOfFirstGroup = candidateNationalPrefixRule.indexOf("$1"); |
|
|
int indexOfFirstGroup = candidateNationalPrefixRule.indexOf("$1"); |
|
|
if (indexOfFirstGroup <= 0) { |
|
|
if (indexOfFirstGroup <= 0) { |
|
|
@ -1476,7 +1476,7 @@ public class PhoneNumberUtil { |
|
|
// Otherwise, we need to remove the national prefix from our output. |
|
|
// Otherwise, we need to remove the national prefix from our output. |
|
|
NumberFormat numFormatCopy = new NumberFormat(); |
|
|
NumberFormat numFormatCopy = new NumberFormat(); |
|
|
numFormatCopy.mergeFrom(formatRule); |
|
|
numFormatCopy.mergeFrom(formatRule); |
|
|
numFormatCopy.clearNationalPrefixFormattingRule(); |
|
|
|
|
|
|
|
|
numFormatCopy.nationalPrefixFormattingRule = ""; |
|
|
List<NumberFormat> numberFormats = new ArrayList<NumberFormat>(1); |
|
|
List<NumberFormat> numberFormats = new ArrayList<NumberFormat>(1); |
|
|
numberFormats.add(numFormatCopy); |
|
|
numberFormats.add(numFormatCopy); |
|
|
formattedNumber = formatByPattern(number, PhoneNumberFormat.NATIONAL, numberFormats); |
|
|
formattedNumber = formatByPattern(number, PhoneNumberFormat.NATIONAL, numberFormats); |
|
|
@ -1606,9 +1606,9 @@ public class PhoneNumberUtil { |
|
|
NumberFormat newFormat = new NumberFormat(); |
|
|
NumberFormat newFormat = new NumberFormat(); |
|
|
newFormat.mergeFrom(formattingPattern); |
|
|
newFormat.mergeFrom(formattingPattern); |
|
|
// The first group is the first group of digits that the user wrote together. |
|
|
// The first group is the first group of digits that the user wrote together. |
|
|
newFormat.setPattern("(\\d+)(.*)"); |
|
|
|
|
|
|
|
|
newFormat.pattern = "(\\d+)(.*)"; |
|
|
// Here we just concatenate them back together after the national prefix has been fixed. |
|
|
// Here we just concatenate them back together after the national prefix has been fixed. |
|
|
newFormat.setFormat("$1$2"); |
|
|
|
|
|
|
|
|
newFormat.format = "$1$2"; |
|
|
// Now we format using this pattern instead of the default pattern, but with the national |
|
|
// Now we format using this pattern instead of the default pattern, but with the national |
|
|
// prefix prefixed if necessary. |
|
|
// prefix prefixed if necessary. |
|
|
// This will not work in the cases where the pattern (and not the leading digits) decide |
|
|
// This will not work in the cases where the pattern (and not the leading digits) decide |
|
|
@ -1621,11 +1621,11 @@ public class PhoneNumberUtil { |
|
|
// prefixes, the international format of the number is returned, unless there is a preferred |
|
|
// prefixes, the international format of the number is returned, unless there is a preferred |
|
|
// international prefix. |
|
|
// international prefix. |
|
|
if (metadataForRegionCallingFrom != null) { |
|
|
if (metadataForRegionCallingFrom != null) { |
|
|
String internationalPrefix = metadataForRegionCallingFrom.getInternationalPrefix(); |
|
|
|
|
|
|
|
|
String internationalPrefix = metadataForRegionCallingFrom.internationalPrefix; |
|
|
internationalPrefixForFormatting = |
|
|
internationalPrefixForFormatting = |
|
|
UNIQUE_INTERNATIONAL_PREFIX.matcher(internationalPrefix).matches() |
|
|
UNIQUE_INTERNATIONAL_PREFIX.matcher(internationalPrefix).matches() |
|
|
? internationalPrefix |
|
|
? internationalPrefix |
|
|
: metadataForRegionCallingFrom.getPreferredInternationalPrefix(); |
|
|
|
|
|
|
|
|
: metadataForRegionCallingFrom.preferredInternationalPrefix; |
|
|
} |
|
|
} |
|
|
StringBuilder formattedNumber = new StringBuilder(rawInput); |
|
|
StringBuilder formattedNumber = new StringBuilder(rawInput); |
|
|
String regionCode = getRegionCodeForCountryCode(countryCode); |
|
|
String regionCode = getRegionCodeForCountryCode(countryCode); |
|
|
@ -1727,7 +1727,7 @@ public class PhoneNumberUtil { |
|
|
if (size == 0 || regexCache.getPatternForRegex( |
|
|
if (size == 0 || regexCache.getPatternForRegex( |
|
|
// We always use the last leading_digits_pattern, as it is the most detailed. |
|
|
// We always use the last leading_digits_pattern, as it is the most detailed. |
|
|
numFormat.getLeadingDigitsPattern(size - 1)).matcher(nationalNumber).lookingAt()) { |
|
|
numFormat.getLeadingDigitsPattern(size - 1)).matcher(nationalNumber).lookingAt()) { |
|
|
Matcher m = regexCache.getPatternForRegex(numFormat.getPattern()).matcher(nationalNumber); |
|
|
|
|
|
|
|
|
Matcher m = regexCache.getPatternForRegex(numFormat.pattern).matcher(nationalNumber); |
|
|
if (m.matches()) { |
|
|
if (m.matches()) { |
|
|
return numFormat; |
|
|
return numFormat; |
|
|
} |
|
|
} |
|
|
@ -1749,15 +1749,15 @@ public class PhoneNumberUtil { |
|
|
NumberFormat formattingPattern, |
|
|
NumberFormat formattingPattern, |
|
|
PhoneNumberFormat numberFormat, |
|
|
PhoneNumberFormat numberFormat, |
|
|
String carrierCode) { |
|
|
String carrierCode) { |
|
|
String numberFormatRule = formattingPattern.getFormat(); |
|
|
|
|
|
|
|
|
String numberFormatRule = formattingPattern.format; |
|
|
Matcher m = |
|
|
Matcher m = |
|
|
regexCache.getPatternForRegex(formattingPattern.getPattern()).matcher(nationalNumber); |
|
|
|
|
|
|
|
|
regexCache.getPatternForRegex(formattingPattern.pattern).matcher(nationalNumber); |
|
|
String formattedNationalNumber = ""; |
|
|
String formattedNationalNumber = ""; |
|
|
if (numberFormat == PhoneNumberFormat.NATIONAL && |
|
|
if (numberFormat == PhoneNumberFormat.NATIONAL && |
|
|
carrierCode != null && carrierCode.length() > 0 && |
|
|
carrierCode != null && carrierCode.length() > 0 && |
|
|
formattingPattern.getDomesticCarrierCodeFormattingRule().length() > 0) { |
|
|
|
|
|
|
|
|
formattingPattern.domesticCarrierCodeFormattingRule.length() > 0) { |
|
|
// Replace the $CC in the formatting rule with the desired carrier code. |
|
|
// Replace the $CC in the formatting rule with the desired carrier code. |
|
|
String carrierCodeFormattingRule = formattingPattern.getDomesticCarrierCodeFormattingRule(); |
|
|
|
|
|
|
|
|
String carrierCodeFormattingRule = formattingPattern.domesticCarrierCodeFormattingRule; |
|
|
carrierCodeFormattingRule = |
|
|
carrierCodeFormattingRule = |
|
|
CC_PATTERN.matcher(carrierCodeFormattingRule).replaceFirst(carrierCode); |
|
|
CC_PATTERN.matcher(carrierCodeFormattingRule).replaceFirst(carrierCode); |
|
|
// Now replace the $FG in the formatting rule with the first group and the carrier code |
|
|
// Now replace the $FG in the formatting rule with the first group and the carrier code |
|
|
@ -1767,7 +1767,7 @@ public class PhoneNumberUtil { |
|
|
formattedNationalNumber = m.replaceAll(numberFormatRule); |
|
|
formattedNationalNumber = m.replaceAll(numberFormatRule); |
|
|
} else { |
|
|
} else { |
|
|
// Use the national prefix formatting rule instead. |
|
|
// Use the national prefix formatting rule instead. |
|
|
String nationalPrefixFormattingRule = formattingPattern.getNationalPrefixFormattingRule(); |
|
|
|
|
|
|
|
|
String nationalPrefixFormattingRule = formattingPattern.nationalPrefixFormattingRule; |
|
|
if (numberFormat == PhoneNumberFormat.NATIONAL && |
|
|
if (numberFormat == PhoneNumberFormat.NATIONAL && |
|
|
nationalPrefixFormattingRule != null && |
|
|
nationalPrefixFormattingRule != null && |
|
|
nationalPrefixFormattingRule.length() > 0) { |
|
|
nationalPrefixFormattingRule.length() > 0) { |
|
|
@ -1820,8 +1820,8 @@ public class PhoneNumberUtil { |
|
|
} |
|
|
} |
|
|
PhoneNumberDesc desc = getNumberDescByType(getMetadataForRegion(regionCode), type); |
|
|
PhoneNumberDesc desc = getNumberDescByType(getMetadataForRegion(regionCode), type); |
|
|
try { |
|
|
try { |
|
|
if (desc.hasExampleNumber()) { |
|
|
|
|
|
return parse(desc.getExampleNumber(), regionCode); |
|
|
|
|
|
|
|
|
if (!desc.exampleNumber.equals("")) { |
|
|
|
|
|
return parse(desc.exampleNumber, regionCode); |
|
|
} |
|
|
} |
|
|
} catch (NumberParseException e) { |
|
|
} catch (NumberParseException e) { |
|
|
logger.log(Level.SEVERE, e.toString()); |
|
|
logger.log(Level.SEVERE, e.toString()); |
|
|
@ -1840,10 +1840,10 @@ public class PhoneNumberUtil { |
|
|
public PhoneNumber getExampleNumberForNonGeoEntity(int countryCallingCode) { |
|
|
public PhoneNumber getExampleNumberForNonGeoEntity(int countryCallingCode) { |
|
|
PhoneMetadata metadata = getMetadataForNonGeographicalRegion(countryCallingCode); |
|
|
PhoneMetadata metadata = getMetadataForNonGeographicalRegion(countryCallingCode); |
|
|
if (metadata != null) { |
|
|
if (metadata != null) { |
|
|
PhoneNumberDesc desc = metadata.getGeneralDesc(); |
|
|
|
|
|
|
|
|
PhoneNumberDesc desc = metadata.generalDesc; |
|
|
try { |
|
|
try { |
|
|
if (desc.hasExampleNumber()) { |
|
|
|
|
|
return parse("+" + countryCallingCode + desc.getExampleNumber(), "ZZ"); |
|
|
|
|
|
|
|
|
if (!desc.exampleNumber.equals("")) { |
|
|
|
|
|
return parse("+" + countryCallingCode + desc.exampleNumber, "ZZ"); |
|
|
} |
|
|
} |
|
|
} catch (NumberParseException e) { |
|
|
} catch (NumberParseException e) { |
|
|
logger.log(Level.SEVERE, e.toString()); |
|
|
logger.log(Level.SEVERE, e.toString()); |
|
|
@ -1866,8 +1866,8 @@ public class PhoneNumberUtil { |
|
|
if (numberFormat == PhoneNumberFormat.RFC3966) { |
|
|
if (numberFormat == PhoneNumberFormat.RFC3966) { |
|
|
formattedNumber.append(RFC3966_EXTN_PREFIX).append(number.getExtension()); |
|
|
formattedNumber.append(RFC3966_EXTN_PREFIX).append(number.getExtension()); |
|
|
} else { |
|
|
} else { |
|
|
if (metadata.hasPreferredExtnPrefix()) { |
|
|
|
|
|
formattedNumber.append(metadata.getPreferredExtnPrefix()).append(number.getExtension()); |
|
|
|
|
|
|
|
|
if (metadata.preferredExtnPrefix.length() != 0) { |
|
|
|
|
|
formattedNumber.append(metadata.preferredExtnPrefix).append(number.getExtension()); |
|
|
} else { |
|
|
} else { |
|
|
formattedNumber.append(DEFAULT_EXTN_PREFIX).append(number.getExtension()); |
|
|
formattedNumber.append(DEFAULT_EXTN_PREFIX).append(number.getExtension()); |
|
|
} |
|
|
} |
|
|
@ -1878,28 +1878,28 @@ public class PhoneNumberUtil { |
|
|
PhoneNumberDesc getNumberDescByType(PhoneMetadata metadata, PhoneNumberType type) { |
|
|
PhoneNumberDesc getNumberDescByType(PhoneMetadata metadata, PhoneNumberType type) { |
|
|
switch (type) { |
|
|
switch (type) { |
|
|
case PREMIUM_RATE: |
|
|
case PREMIUM_RATE: |
|
|
return metadata.getPremiumRate(); |
|
|
|
|
|
|
|
|
return metadata.premiumRate; |
|
|
case TOLL_FREE: |
|
|
case TOLL_FREE: |
|
|
return metadata.getTollFree(); |
|
|
|
|
|
|
|
|
return metadata.tollFree; |
|
|
case MOBILE: |
|
|
case MOBILE: |
|
|
return metadata.getMobile(); |
|
|
|
|
|
|
|
|
return metadata.mobile; |
|
|
case FIXED_LINE: |
|
|
case FIXED_LINE: |
|
|
case FIXED_LINE_OR_MOBILE: |
|
|
case FIXED_LINE_OR_MOBILE: |
|
|
return metadata.getFixedLine(); |
|
|
|
|
|
|
|
|
return metadata.fixedLine; |
|
|
case SHARED_COST: |
|
|
case SHARED_COST: |
|
|
return metadata.getSharedCost(); |
|
|
|
|
|
|
|
|
return metadata.sharedCost; |
|
|
case VOIP: |
|
|
case VOIP: |
|
|
return metadata.getVoip(); |
|
|
|
|
|
|
|
|
return metadata.voip; |
|
|
case PERSONAL_NUMBER: |
|
|
case PERSONAL_NUMBER: |
|
|
return metadata.getPersonalNumber(); |
|
|
|
|
|
|
|
|
return metadata.personalNumber; |
|
|
case PAGER: |
|
|
case PAGER: |
|
|
return metadata.getPager(); |
|
|
|
|
|
|
|
|
return metadata.pager; |
|
|
case UAN: |
|
|
case UAN: |
|
|
return metadata.getUan(); |
|
|
|
|
|
|
|
|
return metadata.uan; |
|
|
case VOICEMAIL: |
|
|
case VOICEMAIL: |
|
|
return metadata.getVoicemail(); |
|
|
|
|
|
|
|
|
return metadata.voicemail; |
|
|
default: |
|
|
default: |
|
|
return metadata.getGeneralDesc(); |
|
|
|
|
|
|
|
|
return metadata.generalDesc; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -1920,48 +1920,48 @@ public class PhoneNumberUtil { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private PhoneNumberType getNumberTypeHelper(String nationalNumber, PhoneMetadata metadata) { |
|
|
private PhoneNumberType getNumberTypeHelper(String nationalNumber, PhoneMetadata metadata) { |
|
|
if (!isNumberMatchingDesc(nationalNumber, metadata.getGeneralDesc())) { |
|
|
|
|
|
|
|
|
if (!isNumberMatchingDesc(nationalNumber, metadata.generalDesc)) { |
|
|
return PhoneNumberType.UNKNOWN; |
|
|
return PhoneNumberType.UNKNOWN; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.getPremiumRate())) { |
|
|
|
|
|
|
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.premiumRate)) { |
|
|
return PhoneNumberType.PREMIUM_RATE; |
|
|
return PhoneNumberType.PREMIUM_RATE; |
|
|
} |
|
|
} |
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.getTollFree())) { |
|
|
|
|
|
|
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.tollFree)) { |
|
|
return PhoneNumberType.TOLL_FREE; |
|
|
return PhoneNumberType.TOLL_FREE; |
|
|
} |
|
|
} |
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.getSharedCost())) { |
|
|
|
|
|
|
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.sharedCost)) { |
|
|
return PhoneNumberType.SHARED_COST; |
|
|
return PhoneNumberType.SHARED_COST; |
|
|
} |
|
|
} |
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.getVoip())) { |
|
|
|
|
|
|
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.voip)) { |
|
|
return PhoneNumberType.VOIP; |
|
|
return PhoneNumberType.VOIP; |
|
|
} |
|
|
} |
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.getPersonalNumber())) { |
|
|
|
|
|
|
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.personalNumber)) { |
|
|
return PhoneNumberType.PERSONAL_NUMBER; |
|
|
return PhoneNumberType.PERSONAL_NUMBER; |
|
|
} |
|
|
} |
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.getPager())) { |
|
|
|
|
|
|
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.pager)) { |
|
|
return PhoneNumberType.PAGER; |
|
|
return PhoneNumberType.PAGER; |
|
|
} |
|
|
} |
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.getUan())) { |
|
|
|
|
|
|
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.uan)) { |
|
|
return PhoneNumberType.UAN; |
|
|
return PhoneNumberType.UAN; |
|
|
} |
|
|
} |
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.getVoicemail())) { |
|
|
|
|
|
|
|
|
if (isNumberMatchingDesc(nationalNumber, metadata.voicemail)) { |
|
|
return PhoneNumberType.VOICEMAIL; |
|
|
return PhoneNumberType.VOICEMAIL; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
boolean isFixedLine = isNumberMatchingDesc(nationalNumber, metadata.getFixedLine()); |
|
|
|
|
|
|
|
|
boolean isFixedLine = isNumberMatchingDesc(nationalNumber, metadata.fixedLine); |
|
|
if (isFixedLine) { |
|
|
if (isFixedLine) { |
|
|
if (metadata.isSameMobileAndFixedLinePattern()) { |
|
|
|
|
|
|
|
|
if (metadata.sameMobileAndFixedLinePattern) { |
|
|
return PhoneNumberType.FIXED_LINE_OR_MOBILE; |
|
|
return PhoneNumberType.FIXED_LINE_OR_MOBILE; |
|
|
} else if (isNumberMatchingDesc(nationalNumber, metadata.getMobile())) { |
|
|
|
|
|
|
|
|
} else if (isNumberMatchingDesc(nationalNumber, metadata.mobile)) { |
|
|
return PhoneNumberType.FIXED_LINE_OR_MOBILE; |
|
|
return PhoneNumberType.FIXED_LINE_OR_MOBILE; |
|
|
} |
|
|
} |
|
|
return PhoneNumberType.FIXED_LINE; |
|
|
return PhoneNumberType.FIXED_LINE; |
|
|
} |
|
|
} |
|
|
// Otherwise, test to see if the number is mobile. Only do this if certain that the patterns for |
|
|
// Otherwise, test to see if the number is mobile. Only do this if certain that the patterns for |
|
|
// mobile and fixed line aren't the same. |
|
|
// mobile and fixed line aren't the same. |
|
|
if (!metadata.isSameMobileAndFixedLinePattern() && |
|
|
|
|
|
isNumberMatchingDesc(nationalNumber, metadata.getMobile())) { |
|
|
|
|
|
|
|
|
if (!metadata.sameMobileAndFixedLinePattern && |
|
|
|
|
|
isNumberMatchingDesc(nationalNumber, metadata.mobile)) { |
|
|
return PhoneNumberType.MOBILE; |
|
|
return PhoneNumberType.MOBILE; |
|
|
} |
|
|
} |
|
|
return PhoneNumberType.UNKNOWN; |
|
|
return PhoneNumberType.UNKNOWN; |
|
|
@ -1987,14 +1987,14 @@ public class PhoneNumberUtil { |
|
|
|
|
|
|
|
|
boolean isNumberPossibleForDesc(String nationalNumber, PhoneNumberDesc numberDesc) { |
|
|
boolean isNumberPossibleForDesc(String nationalNumber, PhoneNumberDesc numberDesc) { |
|
|
Matcher possibleNumberPatternMatcher = |
|
|
Matcher possibleNumberPatternMatcher = |
|
|
regexCache.getPatternForRegex(numberDesc.getPossibleNumberPattern()) |
|
|
|
|
|
|
|
|
regexCache.getPatternForRegex(numberDesc.possibleNumberPattern) |
|
|
.matcher(nationalNumber); |
|
|
.matcher(nationalNumber); |
|
|
return possibleNumberPatternMatcher.matches(); |
|
|
return possibleNumberPatternMatcher.matches(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
boolean isNumberMatchingDesc(String nationalNumber, PhoneNumberDesc numberDesc) { |
|
|
boolean isNumberMatchingDesc(String nationalNumber, PhoneNumberDesc numberDesc) { |
|
|
Matcher nationalNumberPatternMatcher = |
|
|
Matcher nationalNumberPatternMatcher = |
|
|
regexCache.getPatternForRegex(numberDesc.getNationalNumberPattern()) |
|
|
|
|
|
|
|
|
regexCache.getPatternForRegex(numberDesc.nationalNumberPattern) |
|
|
.matcher(nationalNumber); |
|
|
.matcher(nationalNumber); |
|
|
return isNumberPossibleForDesc(nationalNumber, numberDesc) && |
|
|
return isNumberPossibleForDesc(nationalNumber, numberDesc) && |
|
|
nationalNumberPatternMatcher.matches(); |
|
|
nationalNumberPatternMatcher.matches(); |
|
|
@ -2073,8 +2073,8 @@ public class PhoneNumberUtil { |
|
|
// If leadingDigits is present, use this. Otherwise, do full validation. |
|
|
// If leadingDigits is present, use this. Otherwise, do full validation. |
|
|
// Metadata cannot be null because the region codes come from the country calling code map. |
|
|
// Metadata cannot be null because the region codes come from the country calling code map. |
|
|
PhoneMetadata metadata = getMetadataForRegion(regionCode); |
|
|
PhoneMetadata metadata = getMetadataForRegion(regionCode); |
|
|
if (metadata.hasLeadingDigits()) { |
|
|
|
|
|
if (regexCache.getPatternForRegex(metadata.getLeadingDigits()) |
|
|
|
|
|
|
|
|
if (metadata.leadingDigits.length() != 0) { |
|
|
|
|
|
if (regexCache.getPatternForRegex(metadata.leadingDigits) |
|
|
.matcher(nationalNumber).lookingAt()) { |
|
|
.matcher(nationalNumber).lookingAt()) { |
|
|
return regionCode; |
|
|
return regionCode; |
|
|
} |
|
|
} |
|
|
@ -2140,7 +2140,7 @@ public class PhoneNumberUtil { |
|
|
if (metadata == null) { |
|
|
if (metadata == null) { |
|
|
throw new IllegalArgumentException("Invalid region code: " + regionCode); |
|
|
throw new IllegalArgumentException("Invalid region code: " + regionCode); |
|
|
} |
|
|
} |
|
|
return metadata.getCountryCode(); |
|
|
|
|
|
|
|
|
return metadata.countryCode; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -2166,7 +2166,7 @@ public class PhoneNumberUtil { |
|
|
+ ") provided."); |
|
|
+ ") provided."); |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
String nationalPrefix = metadata.getNationalPrefix(); |
|
|
|
|
|
|
|
|
String nationalPrefix = metadata.nationalPrefix; |
|
|
// If no national prefix was found, we return null. |
|
|
// If no national prefix was found, we return null. |
|
|
if (nationalPrefix.length() == 0) { |
|
|
if (nationalPrefix.length() == 0) { |
|
|
return null; |
|
|
return null; |
|
|
@ -2200,7 +2200,7 @@ public class PhoneNumberUtil { |
|
|
if (mainMetadataForCallingCode == null) { |
|
|
if (mainMetadataForCallingCode == null) { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
return mainMetadataForCallingCode.isLeadingZeroPossible(); |
|
|
|
|
|
|
|
|
return mainMetadataForCallingCode.leadingZeroPossible; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -2257,7 +2257,7 @@ public class PhoneNumberUtil { |
|
|
*/ |
|
|
*/ |
|
|
private boolean isShorterThanPossibleNormalNumber(PhoneMetadata regionMetadata, String number) { |
|
|
private boolean isShorterThanPossibleNormalNumber(PhoneMetadata regionMetadata, String number) { |
|
|
Pattern possibleNumberPattern = regexCache.getPatternForRegex( |
|
|
Pattern possibleNumberPattern = regexCache.getPatternForRegex( |
|
|
regionMetadata.getGeneralDesc().getPossibleNumberPattern()); |
|
|
|
|
|
|
|
|
regionMetadata.generalDesc.possibleNumberPattern); |
|
|
return testNumberLengthAgainstPattern(possibleNumberPattern, number) == |
|
|
return testNumberLengthAgainstPattern(possibleNumberPattern, number) == |
|
|
ValidationResult.TOO_SHORT; |
|
|
ValidationResult.TOO_SHORT; |
|
|
} |
|
|
} |
|
|
@ -2297,7 +2297,7 @@ public class PhoneNumberUtil { |
|
|
// Metadata cannot be null because the country calling code is valid. |
|
|
// Metadata cannot be null because the country calling code is valid. |
|
|
PhoneMetadata metadata = getMetadataForRegionOrCallingCode(countryCode, regionCode); |
|
|
PhoneMetadata metadata = getMetadataForRegionOrCallingCode(countryCode, regionCode); |
|
|
Pattern possibleNumberPattern = |
|
|
Pattern possibleNumberPattern = |
|
|
regexCache.getPatternForRegex(metadata.getGeneralDesc().getPossibleNumberPattern()); |
|
|
|
|
|
|
|
|
regexCache.getPatternForRegex(metadata.generalDesc.possibleNumberPattern); |
|
|
return testNumberLengthAgainstPattern(possibleNumberPattern, nationalNumber); |
|
|
return testNumberLengthAgainstPattern(possibleNumberPattern, nationalNumber); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -2429,7 +2429,7 @@ public class PhoneNumberUtil { |
|
|
// Set the default prefix to be something that will never match. |
|
|
// Set the default prefix to be something that will never match. |
|
|
String possibleCountryIddPrefix = "NonMatch"; |
|
|
String possibleCountryIddPrefix = "NonMatch"; |
|
|
if (defaultRegionMetadata != null) { |
|
|
if (defaultRegionMetadata != null) { |
|
|
possibleCountryIddPrefix = defaultRegionMetadata.getInternationalPrefix(); |
|
|
|
|
|
|
|
|
possibleCountryIddPrefix = defaultRegionMetadata.internationalPrefix; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
CountryCodeSource countryCodeSource = |
|
|
CountryCodeSource countryCodeSource = |
|
|
@ -2457,19 +2457,19 @@ public class PhoneNumberUtil { |
|
|
// Check to see if the number starts with the country calling code for the default region. If |
|
|
// 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 |
|
|
// so, we remove the country calling code, and do some checks on the validity of the number |
|
|
// before and after. |
|
|
// before and after. |
|
|
int defaultCountryCode = defaultRegionMetadata.getCountryCode(); |
|
|
|
|
|
|
|
|
int defaultCountryCode = defaultRegionMetadata.countryCode; |
|
|
String defaultCountryCodeString = String.valueOf(defaultCountryCode); |
|
|
String defaultCountryCodeString = String.valueOf(defaultCountryCode); |
|
|
String normalizedNumber = fullNumber.toString(); |
|
|
String normalizedNumber = fullNumber.toString(); |
|
|
if (normalizedNumber.startsWith(defaultCountryCodeString)) { |
|
|
if (normalizedNumber.startsWith(defaultCountryCodeString)) { |
|
|
StringBuilder potentialNationalNumber = |
|
|
StringBuilder potentialNationalNumber = |
|
|
new StringBuilder(normalizedNumber.substring(defaultCountryCodeString.length())); |
|
|
new StringBuilder(normalizedNumber.substring(defaultCountryCodeString.length())); |
|
|
PhoneNumberDesc generalDesc = defaultRegionMetadata.getGeneralDesc(); |
|
|
|
|
|
|
|
|
PhoneNumberDesc generalDesc = defaultRegionMetadata.generalDesc; |
|
|
Pattern validNumberPattern = |
|
|
Pattern validNumberPattern = |
|
|
regexCache.getPatternForRegex(generalDesc.getNationalNumberPattern()); |
|
|
|
|
|
|
|
|
regexCache.getPatternForRegex(generalDesc.nationalNumberPattern); |
|
|
maybeStripNationalPrefixAndCarrierCode( |
|
|
maybeStripNationalPrefixAndCarrierCode( |
|
|
potentialNationalNumber, defaultRegionMetadata, null /* Don't need the carrier code */); |
|
|
potentialNationalNumber, defaultRegionMetadata, null /* Don't need the carrier code */); |
|
|
Pattern possibleNumberPattern = |
|
|
Pattern possibleNumberPattern = |
|
|
regexCache.getPatternForRegex(generalDesc.getPossibleNumberPattern()); |
|
|
|
|
|
|
|
|
regexCache.getPatternForRegex(generalDesc.possibleNumberPattern); |
|
|
// If the number was not valid before but is valid now, or if it was too long before, we |
|
|
// 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 calling code stripped to be a better result and |
|
|
// consider the number with the country calling code stripped to be a better result and |
|
|
// keep that instead. |
|
|
// keep that instead. |
|
|
@ -2562,7 +2562,7 @@ public class PhoneNumberUtil { |
|
|
boolean maybeStripNationalPrefixAndCarrierCode( |
|
|
boolean maybeStripNationalPrefixAndCarrierCode( |
|
|
StringBuilder number, PhoneMetadata metadata, StringBuilder carrierCode) { |
|
|
StringBuilder number, PhoneMetadata metadata, StringBuilder carrierCode) { |
|
|
int numberLength = number.length(); |
|
|
int numberLength = number.length(); |
|
|
String possibleNationalPrefix = metadata.getNationalPrefixForParsing(); |
|
|
|
|
|
|
|
|
String possibleNationalPrefix = metadata.nationalPrefixForParsing; |
|
|
if (numberLength == 0 || possibleNationalPrefix.length() == 0) { |
|
|
if (numberLength == 0 || possibleNationalPrefix.length() == 0) { |
|
|
// Early return for numbers of zero length. |
|
|
// Early return for numbers of zero length. |
|
|
return false; |
|
|
return false; |
|
|
@ -2571,14 +2571,14 @@ public class PhoneNumberUtil { |
|
|
Matcher prefixMatcher = regexCache.getPatternForRegex(possibleNationalPrefix).matcher(number); |
|
|
Matcher prefixMatcher = regexCache.getPatternForRegex(possibleNationalPrefix).matcher(number); |
|
|
if (prefixMatcher.lookingAt()) { |
|
|
if (prefixMatcher.lookingAt()) { |
|
|
Pattern nationalNumberRule = |
|
|
Pattern nationalNumberRule = |
|
|
regexCache.getPatternForRegex(metadata.getGeneralDesc().getNationalNumberPattern()); |
|
|
|
|
|
|
|
|
regexCache.getPatternForRegex(metadata.generalDesc.nationalNumberPattern); |
|
|
// Check if the original number is viable. |
|
|
// Check if the original number is viable. |
|
|
boolean isViableOriginalNumber = nationalNumberRule.matcher(number).matches(); |
|
|
boolean isViableOriginalNumber = nationalNumberRule.matcher(number).matches(); |
|
|
// prefixMatcher.group(numOfGroups) == null implies nothing was captured by the capturing |
|
|
// prefixMatcher.group(numOfGroups) == null implies nothing was captured by the capturing |
|
|
// groups in possibleNationalPrefix; therefore, no transformation is necessary, and we just |
|
|
// groups in possibleNationalPrefix; therefore, no transformation is necessary, and we just |
|
|
// remove the national prefix. |
|
|
// remove the national prefix. |
|
|
int numOfGroups = prefixMatcher.groupCount(); |
|
|
int numOfGroups = prefixMatcher.groupCount(); |
|
|
String transformRule = metadata.getNationalPrefixTransformRule(); |
|
|
|
|
|
|
|
|
String transformRule = metadata.nationalPrefixTransformRule; |
|
|
if (transformRule == null || transformRule.length() == 0 || |
|
|
if (transformRule == null || transformRule.length() == 0 || |
|
|
prefixMatcher.group(numOfGroups) == null) { |
|
|
prefixMatcher.group(numOfGroups) == null) { |
|
|
// If the original number was viable, and the resultant number is not, we return. |
|
|
// If the original number was viable, and the resultant number is not, we return. |
|
|
@ -2864,7 +2864,7 @@ public class PhoneNumberUtil { |
|
|
normalize(nationalNumber); |
|
|
normalize(nationalNumber); |
|
|
normalizedNationalNumber.append(nationalNumber); |
|
|
normalizedNationalNumber.append(nationalNumber); |
|
|
if (defaultRegion != null) { |
|
|
if (defaultRegion != null) { |
|
|
countryCode = regionMetadata.getCountryCode(); |
|
|
|
|
|
|
|
|
countryCode = regionMetadata.countryCode; |
|
|
phoneNumber.setCountryCode(countryCode); |
|
|
phoneNumber.setCountryCode(countryCode); |
|
|
} else if (keepRawInput) { |
|
|
} else if (keepRawInput) { |
|
|
phoneNumber.clearCountryCodeSource(); |
|
|
phoneNumber.clearCountryCodeSource(); |
|
|
@ -3138,7 +3138,7 @@ public class PhoneNumberUtil { |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
String nationalSignificantNumber = getNationalSignificantNumber(number); |
|
|
String nationalSignificantNumber = getNationalSignificantNumber(number); |
|
|
return !isNumberMatchingDesc(nationalSignificantNumber, metadata.getNoInternationalDialling()); |
|
|
|
|
|
|
|
|
return !isNumberMatchingDesc(nationalSignificantNumber, metadata.noInternationalDialling); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -3154,6 +3154,6 @@ public class PhoneNumberUtil { |
|
|
logger.log(Level.WARNING, "Invalid or unknown region code provided: " + regionCode); |
|
|
logger.log(Level.WARNING, "Invalid or unknown region code provided: " + regionCode); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
return metadata.isMobileNumberPortableRegion(); |
|
|
|
|
|
|
|
|
return metadata.mobileNumberPortableRegion; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |