private void parseHelper(String numberToParse,String defaultRegion,boolean keepRawInput,boolean checkRegion,PhoneNumber phoneNumber) throws NumberParseException {
  if (numberToParse == null) {
    throw new NumberParseException(NumberParseException.ErrorType.NOT_A_NUMBER,"The phone number supplied was null.");
  }
 else   if (numberToParse.length() > MAX_INPUT_STRING_LENGTH) {
    throw new NumberParseException(NumberParseException.ErrorType.TOO_LONG,"The string supplied was too long to parse.");
  }
  StringBuilder nationalNumber=new StringBuilder();
  buildNationalNumberForParsing(numberToParse,nationalNumber);
  if (!isViablePhoneNumber(nationalNumber.toString())) {
    throw new NumberParseException(NumberParseException.ErrorType.NOT_A_NUMBER,"The string supplied did not seem to be a phone number.");
  }
  if (checkRegion && !checkRegionForParsing(nationalNumber.toString(),defaultRegion)) {
    throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE,"Missing or invalid default region.");
  }
  if (keepRawInput) {
    phoneNumber.setRawInput(numberToParse);
  }
  String extension=maybeStripExtension(nationalNumber);
  if (extension.length() > 0) {
    phoneNumber.setExtension(extension);
  }
  PhoneMetadata regionMetadata=getMetadataForRegion(defaultRegion);
  StringBuilder normalizedNationalNumber=new StringBuilder();
  int countryCode=0;
  try {
    countryCode=maybeExtractCountryCode(nationalNumber.toString(),regionMetadata,normalizedNationalNumber,keepRawInput,phoneNumber);
  }
 catch (  NumberParseException e) {
    Matcher matcher=PLUS_CHARS_PATTERN.matcher(nationalNumber.toString());
    if (e.getErrorType() == NumberParseException.ErrorType.INVALID_COUNTRY_CODE && matcher.lookingAt()) {
      countryCode=maybeExtractCountryCode(nationalNumber.substring(matcher.end()),regionMetadata,normalizedNationalNumber,keepRawInput,phoneNumber);
      if (countryCode == 0) {
        throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE,"Could not interpret numbers after plus-sign.");
      }
    }
 else {
      throw new NumberParseException(e.getErrorType(),e.getMessage());
    }
  }
  if (countryCode != 0) {
    String phoneNumberRegion=getRegionCodeForCountryCode(countryCode);
    if (!phoneNumberRegion.equals(defaultRegion)) {
      regionMetadata=getMetadataForRegionOrCallingCode(countryCode,phoneNumberRegion);
    }
  }
 else {
    normalize(nationalNumber);
    normalizedNationalNumber.append(nationalNumber);
    if (defaultRegion != null) {
      countryCode=regionMetadata.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 (regionMetadata != null) {
    StringBuilder carrierCode=new StringBuilder();
    StringBuilder potentialNationalNumber=new StringBuilder(normalizedNationalNumber);
    maybeStripNationalPrefixAndCarrierCode(potentialNationalNumber,regionMetadata,carrierCode);
    if (testNumberLength(potentialNationalNumber.toString(),regionMetadata.getGeneralDesc()) != ValidationResult.TOO_SHORT) {
      normalizedNationalNumber=potentialNationalNumber;
      if (keepRawInput && carrierCode.length() > 0) {
        phoneNumber.setPreferredDomesticCarrierCode(carrierCode.toString());
      }
    }
  }
  int lengthOfNationalNumber=normalizedNationalNumber.length();
  if (lengthOfNationalNumber < MIN_LENGTH_FOR_NSN) {
    throw new NumberParseException(NumberParseException.ErrorType.TOO_SHORT_NSN,"The string supplied is too short to be a phone number.");
  }
  if (lengthOfNationalNumber > MAX_LENGTH_FOR_NSN) {
    throw new NumberParseException(NumberParseException.ErrorType.TOO_LONG,"The string supplied is too long to be a phone number.");
  }
  setItalianLeadingZerosForPhoneNumber(normalizedNationalNumber.toString(),phoneNumber);
  phoneNumber.setNationalNumber(Long.parseLong(normalizedNationalNumber.toString()));
}

private void parseHelper ( String numberToParse , String defaultRegion , boolean keepRawInput , boolean checkRegion , PhoneNumber phoneNumber ) throws NumberParseException { if ( numberToParse == null ) { throw new NumberParseException ( NumberParseException . ErrorType . NOT_A_NUMBER , "The phone number supplied was null." ) ; } else if ( numberToParse . length ( ) > MAX_INPUT_STRING_LENGTH ) { throw new NumberParseException ( NumberParseException . ErrorType . TOO_LONG , "The string supplied was too long to parse." ) ; } StringBuilder nationalNumber = new StringBuilder ( ) ; buildNationalNumberForParsing ( numberToParse , nationalNumber ) ; if ( ! isViablePhoneNumber ( nationalNumber . toString ( ) ) ) { throw new NumberParseException ( NumberParseException . ErrorType . NOT_A_NUMBER , "The string supplied did not seem to be a phone number." ) ; } if ( checkRegion && ! checkRegionForParsing ( nationalNumber . toString ( ) , defaultRegion ) ) { throw new NumberParseException ( NumberParseException . ErrorType . INVALID_COUNTRY_CODE , "Missing or invalid default region." ) ; } if ( keepRawInput ) { phoneNumber . setRawInput ( numberToParse ) ; } String extension = maybeStripExtension ( nationalNumber ) ; if ( extension . length ( ) > 0 ) { phoneNumber . setExtension ( extension ) ; } PhoneMetadata regionMetadata = getMetadataForRegion ( defaultRegion ) ; StringBuilder normalizedNationalNumber = new StringBuilder ( ) ; int countryCode = 0 ; try { countryCode = maybeExtractCountryCode ( nationalNumber . toString ( ) , regionMetadata , normalizedNationalNumber , keepRawInput , phoneNumber ) ; } catch ( NumberParseException e ) { Matcher matcher = PLUS_CHARS_PATTERN . matcher ( nationalNumber . toString ( ) ) ; if ( e . getErrorType ( ) == NumberParseException . ErrorType . INVALID_COUNTRY_CODE && matcher . lookingAt ( ) ) { countryCode = maybeExtractCountryCode ( nationalNumber . substring ( matcher . end ( ) ) , regionMetadata , normalizedNationalNumber , keepRawInput , phoneNumber ) ; if ( countryCode == 0 ) { throw new NumberParseException ( NumberParseException . ErrorType . INVALID_COUNTRY_CODE , "Could not interpret numbers after plus-sign." ) ; } } else { throw new NumberParseException ( e . getErrorType ( ) , e . getMessage ( ) ) ; } } if ( countryCode != 0 ) { String phoneNumberRegion = getRegionCodeForCountryCode ( countryCode ) ; if ( ! phoneNumberRegion . equals ( defaultRegion ) ) { regionMetadata = getMetadataForRegionOrCallingCode ( countryCode , phoneNumberRegion ) ; } } else { normalize ( nationalNumber ) ; normalizedNationalNumber . append ( nationalNumber ) ; if ( defaultRegion != null ) { countryCode = regionMetadata . 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 ( regionMetadata != null ) { StringBuilder carrierCode = new StringBuilder ( ) ; StringBuilder potentialNationalNumber = new StringBuilder ( normalizedNationalNumber ) ; maybeStripNationalPrefixAndCarrierCode ( potentialNationalNumber , regionMetadata , carrierCode ) ; if ( testNumberLength ( potentialNationalNumber . toString ( ) , regionMetadata . getGeneralDesc ( ) ) != ValidationResult . TOO_SHORT ) { normalizedNationalNumber = potentialNationalNumber ; if ( keepRawInput && carrierCode . length ( ) > 0 ) { phoneNumber . setPreferredDomesticCarrierCode ( carrierCode . toString ( ) ) ; } } } int lengthOfNationalNumber = normalizedNationalNumber . length ( ) ; if ( lengthOfNationalNumber < MIN_LENGTH_FOR_NSN ) { throw new NumberParseException ( NumberParseException . ErrorType . TOO_SHORT_NSN , "The string supplied is too short to be a phone number." ) ; } if ( lengthOfNationalNumber > MAX_LENGTH_FOR_NSN ) { throw new NumberParseException ( NumberParseException . ErrorType . TOO_LONG , "The string supplied is too long to be a phone number." ) ; } setItalianLeadingZerosForPhoneNumber ( normalizedNationalNumber . toString ( ) , phoneNumber ) ; phoneNumber . setNationalNumber ( Long . parseLong ( normalizedNationalNumber . toString ( ) ) ) ; }
parses a string and fills up the phonenumber . this method is the same as the public parse ( ) method , with the exception that it allows the default region to be null , for use by isnumbermatch ( ) . checkregion should be set to false if it is permitted for the default region to be null or unknown ( " zz " ) .
