1 /* 2 * Copyright (C) 2011 The Libphonenumber Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.i18n.phonenumbers; 18 19 import com.google.i18n.phonenumbers.PhoneNumberUtil.Leniency; 20 import com.google.i18n.phonenumbers.PhoneNumberUtil.MatchType; 21 import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; 22 import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat; 23 import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; 24 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; 25 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; 26 27 import java.lang.Character.UnicodeBlock; 28 import java.util.Iterator; 29 import java.util.NoSuchElementException; 30 import java.util.regex.Matcher; 31 import java.util.regex.Pattern; 32 33 /** 34 * A stateful class that finds and extracts telephone numbers from {@linkplain CharSequence text}. 35 * Instances can be created using the {@linkplain PhoneNumberUtil#findNumbers factory methods} in 36 * {@link PhoneNumberUtil}. 37 * 38 * <p>Vanity numbers (phone numbers using alphabetic digits such as <tt>1-800-SIX-FLAGS</tt> are 39 * not found. 40 * 41 * <p>This class is not thread-safe. 42 */ 43 final class PhoneNumberMatcher implements Iterator<PhoneNumberMatch> { 44 /** 45 * The phone number pattern used by {@link #find}, similar to 46 * {@code PhoneNumberUtil.VALID_PHONE_NUMBER}, but with the following differences: 47 * <ul> 48 * <li>All captures are limited in order to place an upper bound to the text matched by the 49 * pattern. 50 * <ul> 51 * <li>Leading punctuation / plus signs are limited. 52 * <li>Consecutive occurrences of punctuation are limited. 53 * <li>Number of digits is limited. 54 * </ul> 55 * <li>No whitespace is allowed at the start or end. 56 * <li>No alpha digits (vanity numbers such as 1-800-SIX-FLAGS) are currently supported. 57 * </ul> 58 */ 59 private static final Pattern PATTERN; 60 /** 61 * Matches strings that look like publication pages. Example: 62 * <pre>Computing Complete Answers to Queries in the Presence of Limited Access Patterns. 63 * Chen Li. VLDB J. 12(3): 211-227 (2003).</pre> 64 * 65 * The string "211-227 (2003)" is not a telephone number. 66 */ 67 private static final Pattern PUB_PAGES = Pattern.compile("\\d{1,5}-+\\d{1,5}\\s{0,4}\\(\\d{1,4}"); 68 69 /** 70 * Matches strings that look like dates using "/" as a separator. Examples: 3/10/2011, 31/10/96 or 71 * 08/31/95. 72 */ 73 private static final Pattern SLASH_SEPARATED_DATES = 74 Pattern.compile("(?:(?:[0-3]?\\d/[01]?\\d)|(?:[01]?\\d/[0-3]?\\d))/(?:[12]\\d)?\\d{2}"); 75 76 /** 77 * Matches timestamps. Examples: "2012-01-02 08:00". Note that the reg-ex does not include the 78 * trailing ":\d\d" -- that is covered by TIME_STAMPS_SUFFIX. 79 */ 80 private static final Pattern TIME_STAMPS = 81 Pattern.compile("[12]\\d{3}[-/]?[01]\\d[-/]?[0-3]\\d +[0-2]\\d$"); 82 private static final Pattern TIME_STAMPS_SUFFIX = Pattern.compile(":[0-5]\\d"); 83 84 /** 85 * Pattern to check that brackets match. Opening brackets should be closed within a phone number. 86 * This also checks that there is something inside the brackets. Having no brackets at all is also 87 * fine. 88 */ 89 private static final Pattern MATCHING_BRACKETS; 90 91 /** 92 * Patterns used to extract phone numbers from a larger phone-number-like pattern. These are 93 * ordered according to specificity. For example, white-space is last since that is frequently 94 * used in numbers, not just to separate two numbers. We have separate patterns since we don't 95 * want to break up the phone-number-like text on more than one different kind of symbol at one 96 * time, although symbols of the same type (e.g. space) can be safely grouped together. 97 * 98 * Note that if there is a match, we will always check any text found up to the first match as 99 * well. 100 */ 101 private static final Pattern[] INNER_MATCHES = { 102 // Breaks on the slash - e.g. "651-234-2345/332-445-1234" 103 Pattern.compile("/+(.*)"), 104 // Note that the bracket here is inside the capturing group, since we consider it part of the 105 // phone number. Will match a pattern like "(650) 223 3345 (754) 223 3321". 106 Pattern.compile("(\\([^(]*)"), 107 // Breaks on a hyphen - e.g. "12345 - 332-445-1234 is my number." 108 // We require a space on either side of the hyphen for it to be considered a separator. 109 Pattern.compile("(?:\\p{Z}-|-\\p{Z})\\p{Z}*(.+)"), 110 // Various types of wide hyphens. Note we have decided not to enforce a space here, since it's 111 // possible that it's supposed to be used to break two numbers without spaces, and we haven't 112 // seen many instances of it used within a number. 113 Pattern.compile("[\u2012-\u2015\uFF0D]\\p{Z}*(.+)"), 114 // Breaks on a full stop - e.g. "12345. 332-445-1234 is my number." 115 Pattern.compile("\\.+\\p{Z}*([^.]+)"), 116 // Breaks on space - e.g. "3324451234 8002341234" 117 Pattern.compile("\\p{Z}+(\\P{Z}+)") 118 }; 119 120 /** 121 * Punctuation that may be at the start of a phone number - brackets and plus signs. 122 */ 123 private static final Pattern LEAD_CLASS; 124 125 static { 126 /* Builds the MATCHING_BRACKETS and PATTERN regular expressions. The building blocks below exist 127 * to make the pattern more easily understood. */ 128 129 String openingParens = "(\\[\uFF08\uFF3B"; 130 String closingParens = ")\\]\uFF09\uFF3D"; 131 String nonParens = "[^" + openingParens + closingParens + "]"; 132 133 /* Limit on the number of pairs of brackets in a phone number. */ 134 String bracketPairLimit = limit(0, 3); 135 /* 136 * An opening bracket at the beginning may not be closed, but subsequent ones should be. It's 137 * also possible that the leading bracket was dropped, so we shouldn't be surprised if we see a 138 * closing bracket first. We limit the sets of brackets in a phone number to four. 139 */ 140 MATCHING_BRACKETS = Pattern.compile( 141 "(?:[" + openingParens + "])?" + "(?:" + nonParens + "+" + "[" + closingParens + "])?" 142 + nonParens + "+" 143 + "(?:[" + openingParens + "]" + nonParens + "+[" + closingParens + "])" + bracketPairLimit 144 + nonParens + "*"); 145 146 /* Limit on the number of leading (plus) characters. */ 147 String leadLimit = limit(0, 2); 148 /* Limit on the number of consecutive punctuation characters. */ 149 String punctuationLimit = limit(0, 4); 150 /* The maximum number of digits allowed in a digit-separated block. As we allow all digits in a 151 * single block, set high enough to accommodate the entire national number and the international 152 * country code. */ 153 int digitBlockLimit = 154 PhoneNumberUtil.MAX_LENGTH_FOR_NSN + PhoneNumberUtil.MAX_LENGTH_COUNTRY_CODE; 155 /* Limit on the number of blocks separated by punctuation. Uses digitBlockLimit since some 156 * formats use spaces to separate each digit. */ 157 String blockLimit = limit(0, digitBlockLimit); 158 159 /* A punctuation sequence allowing white space. */ 160 String punctuation = "[" + PhoneNumberUtil.VALID_PUNCTUATION + "]" + punctuationLimit; 161 /* A digits block without punctuation. */ 162 String digitSequence = "\\p{Nd}" + limit(1, digitBlockLimit); 163 164 String leadClassChars = openingParens + PhoneNumberUtil.PLUS_CHARS; 165 String leadClass = "[" + leadClassChars + "]"; 166 LEAD_CLASS = Pattern.compile(leadClass); 167 168 /* Phone number pattern allowing optional punctuation. */ 169 PATTERN = Pattern.compile( 170 "(?:" + leadClass + punctuation + ")" + leadLimit 171 + digitSequence + "(?:" + punctuation + digitSequence + ")" + blockLimit 172 + "(?:" + PhoneNumberUtil.EXTN_PATTERNS_FOR_MATCHING + ")?", 173 PhoneNumberUtil.REGEX_FLAGS); 174 } 175 176 /** Returns a regular expression quantifier with an upper and lower limit. */ limit(int lower, int upper)177 private static String limit(int lower, int upper) { 178 if ((lower < 0) || (upper <= 0) || (upper < lower)) { 179 throw new IllegalArgumentException(); 180 } 181 return "{" + lower + "," + upper + "}"; 182 } 183 184 /** The potential states of a PhoneNumberMatcher. */ 185 private enum State { 186 NOT_READY, READY, DONE 187 } 188 189 /** The phone number utility. */ 190 private final PhoneNumberUtil phoneUtil; 191 /** The text searched for phone numbers. */ 192 private final CharSequence text; 193 /** 194 * The region (country) to assume for phone numbers without an international prefix, possibly 195 * null. 196 */ 197 private final String preferredRegion; 198 /** The degree of validation requested. */ 199 private final Leniency leniency; 200 /** The maximum number of retries after matching an invalid number. */ 201 private long maxTries; 202 203 /** The iteration tristate. */ 204 private State state = State.NOT_READY; 205 /** The last successful match, null unless in {@link State#READY}. */ 206 private PhoneNumberMatch lastMatch = null; 207 /** The next index to start searching at. Undefined in {@link State#DONE}. */ 208 private int searchIndex = 0; 209 210 /** 211 * Creates a new instance. See the factory methods in {@link PhoneNumberUtil} on how to obtain a 212 * new instance. 213 * 214 * @param util the phone number util to use 215 * @param text the character sequence that we will search, null for no text 216 * @param country the country to assume for phone numbers not written in international format 217 * (with a leading plus, or with the international dialing prefix of the specified region). 218 * May be null or "ZZ" if only numbers with a leading plus should be 219 * considered. 220 * @param leniency the leniency to use when evaluating candidate phone numbers 221 * @param maxTries the maximum number of invalid numbers to try before giving up on the text. 222 * This is to cover degenerate cases where the text has a lot of false positives in it. Must 223 * be {@code >= 0}. 224 */ PhoneNumberMatcher(PhoneNumberUtil util, CharSequence text, String country, Leniency leniency, long maxTries)225 PhoneNumberMatcher(PhoneNumberUtil util, CharSequence text, String country, Leniency leniency, 226 long maxTries) { 227 228 if ((util == null) || (leniency == null)) { 229 throw new NullPointerException(); 230 } 231 if (maxTries < 0) { 232 throw new IllegalArgumentException(); 233 } 234 this.phoneUtil = util; 235 this.text = (text != null) ? text : ""; 236 this.preferredRegion = country; 237 this.leniency = leniency; 238 this.maxTries = maxTries; 239 } 240 241 /** 242 * Attempts to find the next subsequence in the searched sequence on or after {@code searchIndex} 243 * that represents a phone number. Returns the next match, null if none was found. 244 * 245 * @param index the search index to start searching at 246 * @return the phone number match found, null if none can be found 247 */ find(int index)248 private PhoneNumberMatch find(int index) { 249 Matcher matcher = PATTERN.matcher(text); 250 while ((maxTries > 0) && matcher.find(index)) { 251 int start = matcher.start(); 252 CharSequence candidate = text.subSequence(start, matcher.end()); 253 254 // Check for extra numbers at the end. 255 // TODO: This is the place to start when trying to support extraction of multiple phone number 256 // from split notations (+41 79 123 45 67 / 68). 257 candidate = trimAfterFirstMatch(PhoneNumberUtil.SECOND_NUMBER_START_PATTERN, candidate); 258 259 PhoneNumberMatch match = extractMatch(candidate, start); 260 if (match != null) { 261 return match; 262 } 263 264 index = start + candidate.length(); 265 maxTries--; 266 } 267 268 return null; 269 } 270 271 /** 272 * Trims away any characters after the first match of {@code pattern} in {@code candidate}, 273 * returning the trimmed version. 274 */ trimAfterFirstMatch(Pattern pattern, CharSequence candidate)275 private static CharSequence trimAfterFirstMatch(Pattern pattern, CharSequence candidate) { 276 Matcher trailingCharsMatcher = pattern.matcher(candidate); 277 if (trailingCharsMatcher.find()) { 278 candidate = candidate.subSequence(0, trailingCharsMatcher.start()); 279 } 280 return candidate; 281 } 282 283 /** 284 * Helper method to determine if a character is a Latin-script letter or not. For our purposes, 285 * combining marks should also return true since we assume they have been added to a preceding 286 * Latin character. 287 */ 288 // @VisibleForTesting isLatinLetter(char letter)289 static boolean isLatinLetter(char letter) { 290 // Combining marks are a subset of non-spacing-mark. 291 if (!Character.isLetter(letter) && Character.getType(letter) != Character.NON_SPACING_MARK) { 292 return false; 293 } 294 UnicodeBlock block = UnicodeBlock.of(letter); 295 return block.equals(UnicodeBlock.BASIC_LATIN) 296 || block.equals(UnicodeBlock.LATIN_1_SUPPLEMENT) 297 || block.equals(UnicodeBlock.LATIN_EXTENDED_A) 298 || block.equals(UnicodeBlock.LATIN_EXTENDED_ADDITIONAL) 299 || block.equals(UnicodeBlock.LATIN_EXTENDED_B) 300 || block.equals(UnicodeBlock.COMBINING_DIACRITICAL_MARKS); 301 } 302 isInvalidPunctuationSymbol(char character)303 private static boolean isInvalidPunctuationSymbol(char character) { 304 return character == '%' || Character.getType(character) == Character.CURRENCY_SYMBOL; 305 } 306 307 /** 308 * Attempts to extract a match from a {@code candidate} character sequence. 309 * 310 * @param candidate the candidate text that might contain a phone number 311 * @param offset the offset of {@code candidate} within {@link #text} 312 * @return the match found, null if none can be found 313 */ extractMatch(CharSequence candidate, int offset)314 private PhoneNumberMatch extractMatch(CharSequence candidate, int offset) { 315 // Skip a match that is more likely to be a date. 316 if (SLASH_SEPARATED_DATES.matcher(candidate).find()) { 317 return null; 318 } 319 320 // Skip potential time-stamps. 321 if (TIME_STAMPS.matcher(candidate).find()) { 322 String followingText = text.toString().substring(offset + candidate.length()); 323 if (TIME_STAMPS_SUFFIX.matcher(followingText).lookingAt()) { 324 return null; 325 } 326 } 327 328 // Try to come up with a valid match given the entire candidate. 329 PhoneNumberMatch match = parseAndVerify(candidate, offset); 330 if (match != null) { 331 return match; 332 } 333 334 // If that failed, try to find an "inner match" - there might be a phone number within this 335 // candidate. 336 return extractInnerMatch(candidate, offset); 337 } 338 339 /** 340 * Attempts to extract a match from {@code candidate} if the whole candidate does not qualify as a 341 * match. 342 * 343 * @param candidate the candidate text that might contain a phone number 344 * @param offset the current offset of {@code candidate} within {@link #text} 345 * @return the match found, null if none can be found 346 */ extractInnerMatch(CharSequence candidate, int offset)347 private PhoneNumberMatch extractInnerMatch(CharSequence candidate, int offset) { 348 for (Pattern possibleInnerMatch : INNER_MATCHES) { 349 Matcher groupMatcher = possibleInnerMatch.matcher(candidate); 350 boolean isFirstMatch = true; 351 while (groupMatcher.find() && maxTries > 0) { 352 if (isFirstMatch) { 353 // We should handle any group before this one too. 354 CharSequence group = trimAfterFirstMatch( 355 PhoneNumberUtil.UNWANTED_END_CHAR_PATTERN, 356 candidate.subSequence(0, groupMatcher.start())); 357 PhoneNumberMatch match = parseAndVerify(group, offset); 358 if (match != null) { 359 return match; 360 } 361 maxTries--; 362 isFirstMatch = false; 363 } 364 CharSequence group = trimAfterFirstMatch( 365 PhoneNumberUtil.UNWANTED_END_CHAR_PATTERN, groupMatcher.group(1)); 366 PhoneNumberMatch match = parseAndVerify(group, offset + groupMatcher.start(1)); 367 if (match != null) { 368 return match; 369 } 370 maxTries--; 371 } 372 } 373 return null; 374 } 375 376 /** 377 * Parses a phone number from the {@code candidate} using {@link PhoneNumberUtil#parse} and 378 * verifies it matches the requested {@link #leniency}. If parsing and verification succeed, a 379 * corresponding {@link PhoneNumberMatch} is returned, otherwise this method returns null. 380 * 381 * @param candidate the candidate match 382 * @param offset the offset of {@code candidate} within {@link #text} 383 * @return the parsed and validated phone number match, or null 384 */ parseAndVerify(CharSequence candidate, int offset)385 private PhoneNumberMatch parseAndVerify(CharSequence candidate, int offset) { 386 try { 387 // Check the candidate doesn't contain any formatting which would indicate that it really 388 // isn't a phone number. 389 if (!MATCHING_BRACKETS.matcher(candidate).matches() || PUB_PAGES.matcher(candidate).find()) { 390 return null; 391 } 392 393 // If leniency is set to VALID or stricter, we also want to skip numbers that are surrounded 394 // by Latin alphabetic characters, to skip cases like abc8005001234 or 8005001234def. 395 if (leniency.compareTo(Leniency.VALID) >= 0) { 396 // If the candidate is not at the start of the text, and does not start with phone-number 397 // punctuation, check the previous character. 398 if (offset > 0 && !LEAD_CLASS.matcher(candidate).lookingAt()) { 399 char previousChar = text.charAt(offset - 1); 400 // We return null if it is a latin letter or an invalid punctuation symbol. 401 if (isInvalidPunctuationSymbol(previousChar) || isLatinLetter(previousChar)) { 402 return null; 403 } 404 } 405 int lastCharIndex = offset + candidate.length(); 406 if (lastCharIndex < text.length()) { 407 char nextChar = text.charAt(lastCharIndex); 408 if (isInvalidPunctuationSymbol(nextChar) || isLatinLetter(nextChar)) { 409 return null; 410 } 411 } 412 } 413 414 PhoneNumber number = phoneUtil.parseAndKeepRawInput(candidate, preferredRegion); 415 416 if (leniency.verify(number, candidate, phoneUtil)) { 417 // We used parseAndKeepRawInput to create this number, but for now we don't return the extra 418 // values parsed. TODO: stop clearing all values here and switch all users over 419 // to using rawInput() rather than the rawString() of PhoneNumberMatch. 420 number.clearCountryCodeSource(); 421 number.clearRawInput(); 422 number.clearPreferredDomesticCarrierCode(); 423 return new PhoneNumberMatch(offset, candidate.toString(), number); 424 } 425 } catch (NumberParseException e) { 426 // ignore and continue 427 } 428 return null; 429 } 430 431 /** 432 * Small helper interface such that the number groups can be checked according to different 433 * criteria, both for our default way of performing formatting and for any alternate formats we 434 * may want to check. 435 */ 436 interface NumberGroupingChecker { 437 /** 438 * Returns true if the groups of digits found in our candidate phone number match our 439 * expectations. 440 * 441 * @param number the original number we found when parsing 442 * @param normalizedCandidate the candidate number, normalized to only contain ASCII digits, 443 * but with non-digits (spaces etc) retained 444 * @param expectedNumberGroups the groups of digits that we would expect to see if we 445 * formatted this number 446 */ checkGroups(PhoneNumberUtil util, PhoneNumber number, StringBuilder normalizedCandidate, String[] expectedNumberGroups)447 boolean checkGroups(PhoneNumberUtil util, PhoneNumber number, 448 StringBuilder normalizedCandidate, String[] expectedNumberGroups); 449 } 450 allNumberGroupsRemainGrouped(PhoneNumberUtil util, PhoneNumber number, StringBuilder normalizedCandidate, String[] formattedNumberGroups)451 static boolean allNumberGroupsRemainGrouped(PhoneNumberUtil util, 452 PhoneNumber number, 453 StringBuilder normalizedCandidate, 454 String[] formattedNumberGroups) { 455 int fromIndex = 0; 456 if (number.getCountryCodeSource() != CountryCodeSource.FROM_DEFAULT_COUNTRY) { 457 // First skip the country code if the normalized candidate contained it. 458 String countryCode = Integer.toString(number.getCountryCode()); 459 fromIndex = normalizedCandidate.indexOf(countryCode) + countryCode.length(); 460 } 461 // Check each group of consecutive digits are not broken into separate groupings in the 462 // {@code normalizedCandidate} string. 463 for (int i = 0; i < formattedNumberGroups.length; i++) { 464 // Fails if the substring of {@code normalizedCandidate} starting from {@code fromIndex} 465 // doesn't contain the consecutive digits in formattedNumberGroups[i]. 466 fromIndex = normalizedCandidate.indexOf(formattedNumberGroups[i], fromIndex); 467 if (fromIndex < 0) { 468 return false; 469 } 470 // Moves {@code fromIndex} forward. 471 fromIndex += formattedNumberGroups[i].length(); 472 if (i == 0 && fromIndex < normalizedCandidate.length()) { 473 // We are at the position right after the NDC. We get the region used for formatting 474 // information based on the country code in the phone number, rather than the number itself, 475 // as we do not need to distinguish between different countries with the same country 476 // calling code and this is faster. 477 String region = util.getRegionCodeForCountryCode(number.getCountryCode()); 478 if (util.getNddPrefixForRegion(region, true) != null 479 && Character.isDigit(normalizedCandidate.charAt(fromIndex))) { 480 // This means there is no formatting symbol after the NDC. In this case, we only 481 // accept the number if there is no formatting symbol at all in the number, except 482 // for extensions. This is only important for countries with national prefixes. 483 String nationalSignificantNumber = util.getNationalSignificantNumber(number); 484 return normalizedCandidate.substring(fromIndex - formattedNumberGroups[i].length()) 485 .startsWith(nationalSignificantNumber); 486 } 487 } 488 } 489 // The check here makes sure that we haven't mistakenly already used the extension to 490 // match the last group of the subscriber number. Note the extension cannot have 491 // formatting in-between digits. 492 return normalizedCandidate.substring(fromIndex).contains(number.getExtension()); 493 } 494 allNumberGroupsAreExactlyPresent(PhoneNumberUtil util, PhoneNumber number, StringBuilder normalizedCandidate, String[] formattedNumberGroups)495 static boolean allNumberGroupsAreExactlyPresent(PhoneNumberUtil util, 496 PhoneNumber number, 497 StringBuilder normalizedCandidate, 498 String[] formattedNumberGroups) { 499 String[] candidateGroups = 500 PhoneNumberUtil.NON_DIGITS_PATTERN.split(normalizedCandidate.toString()); 501 // Set this to the last group, skipping it if the number has an extension. 502 int candidateNumberGroupIndex = 503 number.hasExtension() ? candidateGroups.length - 2 : candidateGroups.length - 1; 504 // First we check if the national significant number is formatted as a block. 505 // We use contains and not equals, since the national significant number may be present with 506 // a prefix such as a national number prefix, or the country code itself. 507 if (candidateGroups.length == 1 508 || candidateGroups[candidateNumberGroupIndex].contains( 509 util.getNationalSignificantNumber(number))) { 510 return true; 511 } 512 // Starting from the end, go through in reverse, excluding the first group, and check the 513 // candidate and number groups are the same. 514 for (int formattedNumberGroupIndex = (formattedNumberGroups.length - 1); 515 formattedNumberGroupIndex > 0 && candidateNumberGroupIndex >= 0; 516 formattedNumberGroupIndex--, candidateNumberGroupIndex--) { 517 if (!candidateGroups[candidateNumberGroupIndex].equals( 518 formattedNumberGroups[formattedNumberGroupIndex])) { 519 return false; 520 } 521 } 522 // Now check the first group. There may be a national prefix at the start, so we only check 523 // that the candidate group ends with the formatted number group. 524 return (candidateNumberGroupIndex >= 0 525 && candidateGroups[candidateNumberGroupIndex].endsWith(formattedNumberGroups[0])); 526 } 527 528 /** 529 * Helper method to get the national-number part of a number, formatted without any national 530 * prefix, and return it as a set of digit blocks that would be formatted together. 531 */ getNationalNumberGroups(PhoneNumberUtil util, PhoneNumber number, NumberFormat formattingPattern)532 private static String[] getNationalNumberGroups(PhoneNumberUtil util, PhoneNumber number, 533 NumberFormat formattingPattern) { 534 if (formattingPattern == null) { 535 // This will be in the format +CC-DG;ext=EXT where DG represents groups of digits. 536 String rfc3966Format = util.format(number, PhoneNumberFormat.RFC3966); 537 // We remove the extension part from the formatted string before splitting it into different 538 // groups. 539 int endIndex = rfc3966Format.indexOf(';'); 540 if (endIndex < 0) { 541 endIndex = rfc3966Format.length(); 542 } 543 // The country-code will have a '-' following it. 544 int startIndex = rfc3966Format.indexOf('-') + 1; 545 return rfc3966Format.substring(startIndex, endIndex).split("-"); 546 } else { 547 // We format the NSN only, and split that according to the separator. 548 String nationalSignificantNumber = util.getNationalSignificantNumber(number); 549 return util.formatNsnUsingPattern(nationalSignificantNumber, 550 formattingPattern, PhoneNumberFormat.RFC3966).split("-"); 551 } 552 } 553 checkNumberGroupingIsValid( PhoneNumber number, CharSequence candidate, PhoneNumberUtil util, NumberGroupingChecker checker)554 static boolean checkNumberGroupingIsValid( 555 PhoneNumber number, CharSequence candidate, PhoneNumberUtil util, 556 NumberGroupingChecker checker) { 557 // TODO: Evaluate how this works for other locales (testing has been limited to NANPA regions) 558 // and optimise if necessary. 559 StringBuilder normalizedCandidate = 560 PhoneNumberUtil.normalizeDigits(candidate, true /* keep non-digits */); 561 String[] formattedNumberGroups = getNationalNumberGroups(util, number, null); 562 if (checker.checkGroups(util, number, normalizedCandidate, formattedNumberGroups)) { 563 return true; 564 } 565 // If this didn't pass, see if there are any alternate formats, and try them instead. 566 PhoneMetadata alternateFormats = 567 MetadataManager.getAlternateFormatsForCountry(number.getCountryCode()); 568 if (alternateFormats != null) { 569 for (NumberFormat alternateFormat : alternateFormats.numberFormats()) { 570 formattedNumberGroups = getNationalNumberGroups(util, number, alternateFormat); 571 if (checker.checkGroups(util, number, normalizedCandidate, formattedNumberGroups)) { 572 return true; 573 } 574 } 575 } 576 return false; 577 } 578 containsMoreThanOneSlashInNationalNumber(PhoneNumber number, String candidate)579 static boolean containsMoreThanOneSlashInNationalNumber(PhoneNumber number, String candidate) { 580 int firstSlashInBodyIndex = candidate.indexOf('/'); 581 if (firstSlashInBodyIndex < 0) { 582 // No slashes, this is okay. 583 return false; 584 } 585 // Now look for a second one. 586 int secondSlashInBodyIndex = candidate.indexOf('/', firstSlashInBodyIndex + 1); 587 if (secondSlashInBodyIndex < 0) { 588 // Only one slash, this is okay. 589 return false; 590 } 591 592 // If the first slash is after the country calling code, this is permitted. 593 boolean candidateHasCountryCode = 594 (number.getCountryCodeSource() == CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN 595 || number.getCountryCodeSource() == CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN); 596 if (candidateHasCountryCode 597 && PhoneNumberUtil.normalizeDigitsOnly(candidate.substring(0, firstSlashInBodyIndex)) 598 .equals(Integer.toString(number.getCountryCode()))) { 599 // Any more slashes and this is illegal. 600 return candidate.substring(secondSlashInBodyIndex + 1).contains("/"); 601 } 602 return true; 603 } 604 containsOnlyValidXChars( PhoneNumber number, String candidate, PhoneNumberUtil util)605 static boolean containsOnlyValidXChars( 606 PhoneNumber number, String candidate, PhoneNumberUtil util) { 607 // The characters 'x' and 'X' can be (1) a carrier code, in which case they always precede the 608 // national significant number or (2) an extension sign, in which case they always precede the 609 // extension number. We assume a carrier code is more than 1 digit, so the first case has to 610 // have more than 1 consecutive 'x' or 'X', whereas the second case can only have exactly 1 'x' 611 // or 'X'. We ignore the character if it appears as the last character of the string. 612 for (int index = 0; index < candidate.length() - 1; index++) { 613 char charAtIndex = candidate.charAt(index); 614 if (charAtIndex == 'x' || charAtIndex == 'X') { 615 char charAtNextIndex = candidate.charAt(index + 1); 616 if (charAtNextIndex == 'x' || charAtNextIndex == 'X') { 617 // This is the carrier code case, in which the 'X's always precede the national 618 // significant number. 619 index++; 620 if (util.isNumberMatch(number, candidate.substring(index)) != MatchType.NSN_MATCH) { 621 return false; 622 } 623 // This is the extension sign case, in which the 'x' or 'X' should always precede the 624 // extension number. 625 } else if (!PhoneNumberUtil.normalizeDigitsOnly(candidate.substring(index)).equals( 626 number.getExtension())) { 627 return false; 628 } 629 } 630 } 631 return true; 632 } 633 isNationalPrefixPresentIfRequired(PhoneNumber number, PhoneNumberUtil util)634 static boolean isNationalPrefixPresentIfRequired(PhoneNumber number, PhoneNumberUtil util) { 635 // First, check how we deduced the country code. If it was written in international format, then 636 // the national prefix is not required. 637 if (number.getCountryCodeSource() != CountryCodeSource.FROM_DEFAULT_COUNTRY) { 638 return true; 639 } 640 String phoneNumberRegion = 641 util.getRegionCodeForCountryCode(number.getCountryCode()); 642 PhoneMetadata metadata = util.getMetadataForRegion(phoneNumberRegion); 643 if (metadata == null) { 644 return true; 645 } 646 // Check if a national prefix should be present when formatting this number. 647 String nationalNumber = util.getNationalSignificantNumber(number); 648 NumberFormat formatRule = 649 util.chooseFormattingPatternForNumber(metadata.numberFormats(), nationalNumber); 650 // To do this, we check that a national prefix formatting rule was present and that it wasn't 651 // just the first-group symbol ($1) with punctuation. 652 if ((formatRule != null) && formatRule.getNationalPrefixFormattingRule().length() > 0) { 653 if (formatRule.getNationalPrefixOptionalWhenFormatting()) { 654 // The national-prefix is optional in these cases, so we don't need to check if it was 655 // present. 656 return true; 657 } 658 if (PhoneNumberUtil.formattingRuleHasFirstGroupOnly( 659 formatRule.getNationalPrefixFormattingRule())) { 660 // National Prefix not needed for this number. 661 return true; 662 } 663 // Normalize the remainder. 664 String rawInputCopy = PhoneNumberUtil.normalizeDigitsOnly(number.getRawInput()); 665 StringBuilder rawInput = new StringBuilder(rawInputCopy); 666 // Check if we found a national prefix and/or carrier code at the start of the raw input, and 667 // return the result. 668 return util.maybeStripNationalPrefixAndCarrierCode(rawInput, metadata, null); 669 } 670 return true; 671 } 672 673 @Override hasNext()674 public boolean hasNext() { 675 if (state == State.NOT_READY) { 676 lastMatch = find(searchIndex); 677 if (lastMatch == null) { 678 state = State.DONE; 679 } else { 680 searchIndex = lastMatch.end(); 681 state = State.READY; 682 } 683 } 684 return state == State.READY; 685 } 686 687 @Override next()688 public PhoneNumberMatch next() { 689 // Check the state and find the next match as a side-effect if necessary. 690 if (!hasNext()) { 691 throw new NoSuchElementException(); 692 } 693 694 // Don't retain that memory any longer than necessary. 695 PhoneNumberMatch result = lastMatch; 696 lastMatch = null; 697 state = State.NOT_READY; 698 return result; 699 } 700 701 /** 702 * Always throws {@link UnsupportedOperationException} as removal is not supported. 703 */ 704 @Override remove()705 public void remove() { 706 throw new UnsupportedOperationException(); 707 } 708 } 709