private boolean createFormattingTemplate(NumberFormat format) { String numberPattern = format.getPattern(); // The formatter doesn't format numbers when numberPattern contains "|", e.g. // (20|3)\d{4}. In those cases we quickly return. if (numberPattern.indexOf('|') != -1) { return(false); } // Replace anything in the form of [..] with \d numberPattern = CHARACTER_CLASS_PATTERN.matcher(numberPattern).replaceAll("\\\\d"); // 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, format.getFormat()); if (tempTemplate.length() > 0) { formattingTemplate.append(tempTemplate); return(true); } return(false); }
/** * Helper method to get the national-number part of a number, formatted without any national * prefix, and return it as a set of digit blocks that would be formatted together. */ private static String[] getNationalNumberGroups(PhoneNumberUtil util, PhoneNumber number, NumberFormat formattingPattern) { if (formattingPattern == null) { // This will be in the format +CC-DG;ext=EXT where DG represents groups of digits. String rfc3966Format = util.format(number, PhoneNumberFormat.RFC3966); // We remove the extension part from the formatted string before splitting it into different // groups. int endIndex = rfc3966Format.indexOf(';'); if (endIndex < 0) { endIndex = rfc3966Format.length(); } // The country-code will have a '-' following it. int startIndex = rfc3966Format.indexOf('-') + 1; return(rfc3966Format.substring(startIndex, endIndex).split("-")); } else { // We format the NSN only, and split that according to the separator. String nationalSignificantNumber = util.getNationalSignificantNumber(number); return(util.formatNsnUsingPattern(nationalSignificantNumber, formattingPattern, PhoneNumberFormat.RFC3966).split("-")); } }
// Returns true if a new template is created as opposed to reusing the existing template. private boolean maybeCreateNewTemplate() { // When there are multiple available formats, the formatter uses the first format where a // formatting template could be created. Iterator <NumberFormat> it = possibleFormats.iterator(); while (it.hasNext()) { NumberFormat numberFormat = it.next(); String pattern = numberFormat.getPattern(); if (currentFormattingPattern.equals(pattern)) { return(false); } if (createFormattingTemplate(numberFormat)) { currentFormattingPattern = pattern; shouldAddSpaceAfterNationalPrefix = NATIONAL_PREFIX_SEPARATORS_PATTERN.matcher( numberFormat.getNationalPrefixFormattingRule()).find(); // With a new formatting template, the matched position using the old template needs to be // reset. lastMatchPosition = 0; return(true); } else // Remove the current number format from possibleFormats. { it.remove(); } } ableToFormat = false; return(false); }
internal static boolean isNationalPrefixPresentIfRequired(PhoneNumber number, PhoneNumberUtil util) { // First, check how we deduced the country code. If it was written in international format, then // the national prefix is not required. if (number.getCountryCodeSource() != CountryCodeSource.FROM_DEFAULT_COUNTRY) { return(true); } String phoneNumberRegion = util.getRegionCodeForCountryCode(number.getCountryCode()); PhoneMetadata metadata = util.getMetadataForRegion(phoneNumberRegion); if (metadata == null) { return(true); } // Check if a national prefix should be present when formatting this number. String nationalNumber = util.getNationalSignificantNumber(number); NumberFormat formatRule = util.chooseFormattingPatternForNumber(metadata.numberFormats(), nationalNumber); // To do this, we check that a national prefix formatting rule was present and that it wasn't // just the first-group symbol ($1) with punctuation. if ((formatRule != null) && formatRule.getNationalPrefixFormattingRule().length() > 0) { if (formatRule.isNationalPrefixOptionalWhenFormatting()) { // The national-prefix is optional in these cases, so we don't need to check if it was // present. return(true); } if (PhoneNumberUtil.formattingRuleHasFirstGroupOnly( formatRule.getNationalPrefixFormattingRule())) { // National Prefix not needed for this number. return(true); } // Normalize the remainder. String rawInputCopy = PhoneNumberUtil.normalizeDigitsOnly(number.getRawInput()); StringBuilder rawInput = new StringBuilder(rawInputCopy); // Check if we found a national prefix and/or carrier code at the start of the raw input, and // return the result. return(util.maybeStripNationalPrefixAndCarrierCode(rawInput, metadata, null)); } return(true); }
private void narrowDownPossibleFormats(String leadingDigits) { int indexOfLeadingDigitsPattern = leadingDigits.length() - MIN_LEADING_DIGITS_LENGTH; Iterator <NumberFormat> it = possibleFormats.iterator(); while (it.hasNext()) { NumberFormat format = it.next(); if (format.leadingDigitsPatternSize() > indexOfLeadingDigitsPattern) { Pattern leadingDigitsPattern = regexCache.getPatternForRegex( format.getLeadingDigitsPattern(indexOfLeadingDigitsPattern)); Matcher m = leadingDigitsPattern.matcher(leadingDigits); if (!m.lookingAt()) { it.remove(); } } // else the particular format has no more specific leadingDigitsPattern, and it should be // retained. } }
private boolean createFormattingTemplate(NumberFormat format) { String numberPattern = format.getPattern(); // The formatter doesn't format numbers when numberPattern contains "|", e.g. // (20|3)\d{4}. In those cases we quickly return. if (numberPattern.indexOf('|') != -1) { return false; } // Replace anything in the form of [..] with \d numberPattern = CHARACTER_CLASS_PATTERN.matcher(numberPattern).replaceAll("\\\\d"); // 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, format.getFormat()); if (tempTemplate.length() > 0) { formattingTemplate.append(tempTemplate); return true; } return false; }
internal NumberFormat(_NumberFormat numberFormat) { this._inner = numberFormat; }
public NumberFormat() { this._inner = new _NumberFormat(); }
public void testFormatByPattern() { NumberFormat newNumFormat = new NumberFormat(); newNumFormat.setPattern("(\\d{3})(\\d{3})(\\d{4})"); newNumFormat.setFormat("($1) $2-$3"); List<NumberFormat> newNumberFormats = new ArrayList<NumberFormat>(); newNumberFormats.add(newNumFormat); assertEquals("(650) 253-0000", phoneUtil.formatByPattern(US_NUMBER, PhoneNumberFormat.NATIONAL, newNumberFormats)); assertEquals("+1 (650) 253-0000", phoneUtil.formatByPattern(US_NUMBER, PhoneNumberFormat.INTERNATIONAL, newNumberFormats)); assertEquals("tel:+1-650-253-0000", phoneUtil.formatByPattern(US_NUMBER, PhoneNumberFormat.RFC3966, newNumberFormats)); // $NP is set to '1' for the US. Here we check that for other NANPA countries the US rules are // followed. newNumFormat.setNationalPrefixFormattingRule("$NP ($FG)"); newNumFormat.setFormat("$1 $2-$3"); assertEquals("1 (242) 365-1234", phoneUtil.formatByPattern(BS_NUMBER, PhoneNumberFormat.NATIONAL, newNumberFormats)); assertEquals("+1 242 365-1234", phoneUtil.formatByPattern(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL, newNumberFormats)); newNumFormat.setPattern("(\\d{2})(\\d{5})(\\d{3})"); newNumFormat.setFormat("$1-$2 $3"); newNumberFormats.set(0, newNumFormat); assertEquals("02-36618 300", phoneUtil.formatByPattern(IT_NUMBER, PhoneNumberFormat.NATIONAL, newNumberFormats)); assertEquals("+39 02-36618 300", phoneUtil.formatByPattern(IT_NUMBER, PhoneNumberFormat.INTERNATIONAL, newNumberFormats)); newNumFormat.setNationalPrefixFormattingRule("$NP$FG"); newNumFormat.setPattern("(\\d{2})(\\d{4})(\\d{4})"); newNumFormat.setFormat("$1 $2 $3"); newNumberFormats.set(0, newNumFormat); assertEquals("020 7031 3000", phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL, newNumberFormats)); newNumFormat.setNationalPrefixFormattingRule("($NP$FG)"); assertEquals("(020) 7031 3000", phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL, newNumberFormats)); newNumFormat.setNationalPrefixFormattingRule(""); assertEquals("20 7031 3000", phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.NATIONAL, newNumberFormats)); assertEquals("+44 20 7031 3000", phoneUtil.formatByPattern(GB_NUMBER, PhoneNumberFormat.INTERNATIONAL, newNumberFormats)); }
internal NumberFormat(_NumberFormat numberFormat) { this._inner = numberFormat; }