String extension = maybeStripExtension(nationalNumber);
if (extension.length() > 0) {
phoneNumber.setExtension(extension);
}
PhoneMetadata countryMetadata = getMetadataForRegion(defaultCountry);
// Check to see if the number is given in international format so we know whether this number is
// from the default country or not.
StringBuffer normalizedNationalNumber = new StringBuffer();
int countryCode = maybeExtractCountryCode(nationalNumber.toString(), countryMetadata,
normalizedNationalNumber, keepRawInput, phoneNumber);
if (countryCode != 0) {
String phoneNumberRegion = getRegionCodeForCountryCode(countryCode);
if (!phoneNumberRegion.equals(defaultCountry)) {
countryMetadata = getMetadataForRegion(phoneNumberRegion);
}
} else {
// If no extracted country code, use the region supplied instead. The national number is just
// the normalized version of the number we were given to parse.
normalize(nationalNumber);
normalizedNationalNumber.append(nationalNumber);
if (defaultCountry != null) {
countryCode = countryMetadata.getCountryCode();
phoneNumber.setCountryCode(countryCode);
} else if (keepRawInput) {
phoneNumber.clearCountryCodeSource();
}
}
if (normalizedNationalNumber.length() < MIN_LENGTH_FOR_NSN) {
throw new NumberParseException(NumberParseException.ErrorType.TOO_SHORT_NSN,
"The string supplied is too short to be a phone number.");
}
if (countryMetadata != null) {
Pattern validNumberPattern =
regexCache.getPatternForRegex(countryMetadata.getGeneralDesc()
.getNationalNumberPattern());
maybeStripNationalPrefix(normalizedNationalNumber,
countryMetadata.getNationalPrefixForParsing(),
countryMetadata.getNationalPrefixTransformRule(),
validNumberPattern);
}
int lengthOfNationalNumber = normalizedNationalNumber.length();
if (lengthOfNationalNumber < MIN_LENGTH_FOR_NSN) {
throw new NumberParseException(NumberParseException.ErrorType.TOO_SHORT_NSN,