Upgrade libphonenumber to v4.7

Change-Id: Ia10e96d76b830f264b51431700a2edee99def6ea
diff --git a/README.android b/README.android
index e84d172..67485b0 100644
--- a/README.android
+++ b/README.android
@@ -1,5 +1,5 @@
 URL: http://code.google.com/p/libphonenumber/
-Version: 4.6 (r429)
+Version: 4.7 (r436)
 License: Apache 2
 Description: Google Phone Number Library.
 Local Modification:
diff --git a/java/release_notes.txt b/java/release_notes.txt
index 5970332..ecfdd28 100644
--- a/java/release_notes.txt
+++ b/java/release_notes.txt
@@ -1,3 +1,12 @@
+March 16th, 2012: libphonenumber-4.7
+* Bug fixes
+ - Now return "too long" for input that is longer than 250 chars when parsing
+* Metadata changes
+ - AC,AM,AT,AZ,BF,BR,BY,BZ,CR,CU,DE,DJ,EE,EG,ET,GA,GH,HK,HN,ID,IN,JP,KE,KG,KH,KR,KW,KY,LB,LU,MA,MD,
+   MK,NA,NE,NZ,SA,SB,SC,SE,SL,SN,UZ
+ * Refactoring of test classes so that tests requiring the test metadata to be loaded have a common
+   base class which handles this.
+
 February 9th, 2012: libphonenumber-4.6
 * Bug fixes
  - Fix for formatByPattern to enable RFC formatting to work
diff --git a/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java b/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
index 5120aab..4e3fbc3 100644
--- a/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
+++ b/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
@@ -63,6 +63,9 @@
   static final int MAX_LENGTH_FOR_NSN = 16;
   // The maximum length of the country calling code.
   static final int MAX_LENGTH_COUNTRY_CODE = 3;
+  // We don't allow input strings for parsing to be longer than 250 chars. This prevents malicious
+  // input from overflowing the regular-expression engine.
+  private static final int MAX_INPUT_STRING_LENGTH = 250;
   static final String META_DATA_FILE_PREFIX =
       "/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto";
   private String currentFilePrefix = META_DATA_FILE_PREFIX;
@@ -261,7 +264,7 @@
   // plus_sign*(([punctuation]|[star])*[digits]){3,}([punctuation]|[star]|[digits]|[alpha])*
   // Note VALID_PUNCTUATION starts with a -, so must be the first in the range.
   private static final String VALID_PHONE_NUMBER =
-      "[" + PLUS_CHARS + "]*(?:[" + VALID_PUNCTUATION + STAR_SIGN + "]*" + DIGITS + "){3,}[" +
+      "[" + PLUS_CHARS + "]*+(?:[" + VALID_PUNCTUATION + STAR_SIGN + "]*" + DIGITS + "){3,}[" +
       VALID_PUNCTUATION + STAR_SIGN + VALID_ALPHA + DIGITS + "]*";
 
   // Default extension prefix to use when formatting. This will be put in front of any extension
@@ -356,9 +359,10 @@
    * INTERNATIONAL and NATIONAL formats are consistent with the definition in ITU-T Recommendation
    * E123. For example, the number of the Google Switzerland office will be written as
    * "+41 44 668 1800" in INTERNATIONAL format, and as "044 668 1800" in NATIONAL format.
-   * E164 format is as per INTERNATIONAL format but with no formatting applied, e.g. +41446681800.
-   * RFC3966 is as per INTERNATIONAL format, but with all spaces and other separating symbols
-   * replaced with a hyphen, and with any phone number extension appended with ";ext=".
+   * E164 format is as per INTERNATIONAL format but with no formatting applied, e.g.
+   * "+41446681800". RFC3966 is as per INTERNATIONAL format, but with all spaces and other
+   * separating symbols replaced with a hyphen, and with any phone number extension appended with
+   * ";ext=". It also will have a prefix of "tel:" added, e.g. "tel:+41-44-668-1800".
    *
    * Note: If you are considering storing the number in a neutral format, you are highly advised to
    * use the PhoneNumber class.
@@ -647,13 +651,13 @@
         // We assume that the first-group symbol will never be _before_ the national prefix.
         candidateNationalPrefixRule =
             candidateNationalPrefixRule.substring(0, candidateNationalPrefixRule.indexOf("$1"));
-        candidateNationalPrefixRule = util.normalizeDigitsOnly(candidateNationalPrefixRule);
+        candidateNationalPrefixRule = normalizeDigitsOnly(candidateNationalPrefixRule);
         if (candidateNationalPrefixRule.length() == 0) {
           // National Prefix not needed for this number.
           return true;
         }
         // Normalize the remainder.
-        String rawInputCopy = util.normalizeDigitsOnly(number.getRawInput());
+        String rawInputCopy = 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.
@@ -1623,9 +1627,13 @@
     } else {
       // Invalid region entered as country-calling-from (so no metadata was found for it) or the
       // region chosen has multiple international dialling prefixes.
+      LOGGER.log(Level.WARNING,
+                 "Trying to format number from invalid region "
+                 + regionCallingFrom
+                 + ". International formatting applied.");
       prefixNumberWithCountryCallingCode(countryCode,
-                           PhoneNumberFormat.INTERNATIONAL,
-                           formattedNumber);
+                                         PhoneNumberFormat.INTERNATIONAL,
+                                         formattedNumber);
     }
     return formattedNumber.toString();
   }
@@ -1658,7 +1666,8 @@
         formattedNumber.insert(0, " ").insert(0, countryCallingCode).insert(0, PLUS_SIGN);
         return;
       case RFC3966:
-        formattedNumber.insert(0, "-").insert(0, countryCallingCode).insert(0, PLUS_SIGN);
+        formattedNumber.insert(0, "-").insert(0, countryCallingCode).insert(0, PLUS_SIGN)
+            .insert(0, "tel:");
         return;
       case NATIONAL:
       default:
@@ -2737,6 +2746,9 @@
     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.");
     }
     // Extract a possible number from the string passed in (this strips leading characters that
     // could not be the start of a phone number.)
diff --git a/java/src/com/android/i18n/phonenumbers/ShortNumberUtil.java b/java/src/com/android/i18n/phonenumbers/ShortNumberUtil.java
index 951164d..3f47e7d 100644
--- a/java/src/com/android/i18n/phonenumbers/ShortNumberUtil.java
+++ b/java/src/com/android/i18n/phonenumbers/ShortNumberUtil.java
@@ -17,7 +17,6 @@
 package com.android.i18n.phonenumbers;
 
 import com.android.i18n.phonenumbers.Phonemetadata.PhoneMetadata;
-import com.android.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc;
 
 import java.util.regex.Pattern;
 
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AC b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AC
index fbb7a91..52de7be 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AC
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AC
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AM b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AM
index c6a5144..5461867 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AM
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AM
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AT b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AT
index 3568444..751674c 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AT
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AT
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AZ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AZ
index 75b798b..34d4f94 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AZ
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_AZ
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BF b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BF
index 802997e..ad53ba8 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BF
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BF
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BR b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BR
index 31ed9bc..96d2ae4 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BR
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BR
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BY b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BY
index 284b3b3..a78037b 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BY
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BY
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BZ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BZ
index b1f0a88..0b04b15 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BZ
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_BZ
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR
index 307c8ab..de374d1 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CU b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CU
index ec56f92..4ff3351 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CU
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_CU
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_DE b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_DE
index 673ba16..9163e1b 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_DE
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_DE
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_DJ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_DJ
index 0683a64..6813305 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_DJ
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_DJ
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_EE b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_EE
index 89ecb6e..5b83276 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_EE
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_EE
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_EG b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_EG
index e827816..463aaa6 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_EG
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_EG
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_ET b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_ET
index b3bde00..15e0c7d 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_ET
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_ET
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GA b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GA
index 438299c..fb204d0 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GA
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GA
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GH b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GH
index aa6e8e7..e268fcc 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GH
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_GH
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_HK b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_HK
index 9e5fa06..0b3561a 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_HK
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_HK
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_HN b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_HN
index 9339afb..c6dfaf9 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_HN
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_HN
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_ID b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_ID
index 5c45880..fc2cd49 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_ID
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_ID
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN
index f07407a..6f1ad28 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP
index eaae271..e4032ea 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE
index 197ff1e..a7c91d4 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KG b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KG
index 9384fb9..f71f076 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KG
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KG
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KH b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KH
index 3c8e5bd..9e97693 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KH
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KH
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KR b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KR
index d8344af..894849e 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KR
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KR
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KW b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KW
index adb7f31..cf386ee 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KW
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KW
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KY b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KY
index cacfd5f..dc864dd 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KY
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_KY
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_LB b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_LB
index 255bc5f..6b2dba9 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_LB
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_LB
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_LU b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_LU
index c4951ef..f80d827 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_LU
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_LU
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MA b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MA
index 2850cab..bf444b7 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MA
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MA
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MD b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MD
index 565dc36..3002dfb 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MD
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MD
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MK b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MK
index 010e126..5dcd384 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MK
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_MK
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NA b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NA
index 2ee1fa9..3bcfaa8 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NA
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NA
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NE b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NE
index 096e049..e9d67f4 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NE
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NE
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NZ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NZ
index 1c8cb63..d67fad0 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NZ
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_NZ
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SA b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SA
index b4f4e3b..fd3ed6b 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SA
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SA
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SB b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SB
index a5ea179..605df51 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SB
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SB
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SC b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SC
index 7f55e5a..a9ca75a 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SC
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SC
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SE b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SE
index a023e7b..e52ec10 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SE
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SE
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SL b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SL
index d059d66..8f73182 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SL
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SL
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SN b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SN
index 3f9e567..0350bb9 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SN
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_SN
Binary files differ
diff --git a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_UZ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_UZ
index 18262d2..aedacce 100644
--- a/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_UZ
+++ b/java/src/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProto_UZ
Binary files differ
diff --git a/java/test/com/android/i18n/phonenumbers/AsYouTypeFormatterTest.java b/java/test/com/android/i18n/phonenumbers/AsYouTypeFormatterTest.java
index aa11a63..e572fe0 100644
--- a/java/test/com/android/i18n/phonenumbers/AsYouTypeFormatterTest.java
+++ b/java/test/com/android/i18n/phonenumbers/AsYouTypeFormatterTest.java
@@ -16,23 +16,15 @@
 
 package com.android.i18n.phonenumbers;
 
-import junit.framework.TestCase;
-
 /**
  * Unit tests for AsYouTypeFormatter.java
  *
- * Note that these tests use the metadata contained in the files with TEST_META_DATA_FILE_PREFIX,
- * not the normal metadata files, so should not be used for regression test purposes - these tests
- * are illustrative only and test functionality.
+ * Note that these tests use the test metadata, not the normal metadata file, so should not be used
+ * for regression test purposes - these tests are illustrative only and test functionality.
  *
  * @author Shaopeng Jia
  */
-public class AsYouTypeFormatterTest extends TestCase {
-  private PhoneNumberUtil phoneUtil;
-
-  public AsYouTypeFormatterTest() {
-    phoneUtil = PhoneNumberUtilTest.initializePhoneUtilForTesting();
-  }
+public class AsYouTypeFormatterTest extends TestMetadataTestCase {
 
   public void testInvalidRegion() {
     AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter(RegionCode.ZZ);
diff --git a/java/test/com/android/i18n/phonenumbers/PhoneNumberMatcherTest.java b/java/test/com/android/i18n/phonenumbers/PhoneNumberMatcherTest.java
index dea226d..82ea830 100644
--- a/java/test/com/android/i18n/phonenumbers/PhoneNumberMatcherTest.java
+++ b/java/test/com/android/i18n/phonenumbers/PhoneNumberMatcherTest.java
@@ -19,8 +19,6 @@
 import com.android.i18n.phonenumbers.PhoneNumberUtil.Leniency;
 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
 
-import junit.framework.TestCase;
-
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
@@ -33,13 +31,7 @@
  * @author Tom Hofmann
  * @see PhoneNumberUtilTest {@link PhoneNumberUtilTest} for the origin of the test data
  */
-public class PhoneNumberMatcherTest extends TestCase {
-  private PhoneNumberUtil phoneUtil;
-
-  @Override
-  protected void setUp() throws Exception {
-    phoneUtil = PhoneNumberUtilTest.initializePhoneUtilForTesting();
-  }
+public class PhoneNumberMatcherTest extends TestMetadataTestCase {
 
   /** See {@link PhoneNumberUtilTest#testParseNationalNumber()}. */
   public void testFindNationalNumber() throws Exception {
@@ -710,6 +702,19 @@
     assertEquals(expected, actual);
   }
 
+  public void testNonPlusPrefixedNumbersNotFoundForInvalidRegion() throws Exception {
+    // Does not start with a "+", we won't match it.
+    Iterable<PhoneNumberMatch> iterable = phoneUtil.findNumbers("1 456 764 156", RegionCode.ZZ);
+    Iterator<PhoneNumberMatch> iterator = iterable.iterator();
+
+    assertFalse(iterator.hasNext());
+    try {
+      iterator.next();
+      fail("Violation of the Iterator contract.");
+    } catch (NoSuchElementException e) { /* Success */ }
+    assertFalse(iterator.hasNext());
+  }
+
   public void testEmptyIteration() throws Exception {
     Iterable<PhoneNumberMatch> iterable = phoneUtil.findNumbers("", RegionCode.ZZ);
     Iterator<PhoneNumberMatch> iterator = iterable.iterator();
diff --git a/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java
index 00718bb..83e5da7 100644
--- a/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java
+++ b/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java
@@ -23,27 +23,19 @@
 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource;
 
-import junit.framework.TestCase;
-
 import java.util.ArrayList;
 import java.util.List;
 
 /**
  * Unit tests for PhoneNumberUtil.java
  *
- * Note that these tests use the metadata contained in the files with TEST_META_DATA_FILE_PREFIX,
- * not the normal metadata files, so should not be used for regression test purposes - these tests
- * are illustrative only and test functionality.
+ * Note that these tests use the test metadata, not the normal metadata file, so should not be used
+ * for regression test purposes - these tests are illustrative only and test functionality.
  *
  * @author Shaopeng Jia
  * @author Lara Rennie
  */
-public class PhoneNumberUtilTest extends TestCase {
-  private PhoneNumberUtil phoneUtil;
-  // This is used by BuildMetadataProtoFromXml.
-  static final String TEST_META_DATA_FILE_PREFIX =
-      "/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting";
-
+public class PhoneNumberUtilTest extends TestMetadataTestCase {
   // Set up some test numbers to re-use.
   private static final PhoneNumber ALPHA_NUMERIC_NUMBER =
       new PhoneNumber().setCountryCode(1).setNationalNumber(80074935247L);
@@ -110,17 +102,6 @@
   private static final PhoneNumber INTERNATIONAL_TOLL_FREE_TOO_LONG =
       new PhoneNumber().setCountryCode(800).setNationalNumber(1234567890L);
 
-  public PhoneNumberUtilTest() {
-    phoneUtil = initializePhoneUtilForTesting();
-  }
-
-  static PhoneNumberUtil initializePhoneUtilForTesting() {
-    PhoneNumberUtil.resetInstance();
-    return PhoneNumberUtil.getInstance(
-        TEST_META_DATA_FILE_PREFIX,
-        CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap());
-  }
-
   public void testGetSupportedRegions() {
     assertTrue(phoneUtil.getSupportedRegions().size() > 0);
   }
@@ -358,7 +339,7 @@
 
     assertEquals("900 253 0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.NATIONAL));
     assertEquals("+1 900 253 0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.INTERNATIONAL));
-    assertEquals("+1-900-253-0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.RFC3966));
+    assertEquals("tel:+1-900-253-0000", phoneUtil.format(US_PREMIUM, PhoneNumberFormat.RFC3966));
     // Numbers with all zeros in the national number part will be formatted by using the raw_input
     // if that is available no matter which format is specified.
     assertEquals("000-000-0000",
@@ -384,7 +365,7 @@
     deNumber.setCountryCode(49).setNationalNumber(301234L);
     assertEquals("030/1234", phoneUtil.format(deNumber, PhoneNumberFormat.NATIONAL));
     assertEquals("+49 30/1234", phoneUtil.format(deNumber, PhoneNumberFormat.INTERNATIONAL));
-    assertEquals("+49-30-1234", phoneUtil.format(deNumber, PhoneNumberFormat.RFC3966));
+    assertEquals("tel:+49-30-1234", phoneUtil.format(deNumber, PhoneNumberFormat.RFC3966));
 
     deNumber.clear();
     deNumber.setCountryCode(49).setNationalNumber(291123L);
@@ -709,9 +690,9 @@
     assertEquals("+1 (650) 253-0000", phoneUtil.formatByPattern(US_NUMBER,
                                                                 PhoneNumberFormat.INTERNATIONAL,
                                                                 newNumberFormats));
-    assertEquals("+1-650-253-0000", phoneUtil.formatByPattern(US_NUMBER,
-                                                              PhoneNumberFormat.RFC3966,
-                                                              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.
@@ -769,7 +750,8 @@
     // Uses default extension prefix:
     assertEquals("03-331 6005 ext. 1234", phoneUtil.format(nzNumber, PhoneNumberFormat.NATIONAL));
     // Uses RFC 3966 syntax.
-    assertEquals("+64-3-331-6005;ext=1234", phoneUtil.format(nzNumber, PhoneNumberFormat.RFC3966));
+    assertEquals("tel:+64-3-331-6005;ext=1234",
+        phoneUtil.format(nzNumber, PhoneNumberFormat.RFC3966));
     // Extension prefix overridden in the territory information for the US:
     PhoneNumber usNumberWithExtension = new PhoneNumber().mergeFrom(US_NUMBER).setExtension("4567");
     assertEquals("650 253 0000 extn. 4567", phoneUtil.format(usNumberWithExtension,
@@ -1036,11 +1018,9 @@
     // Invalid country calling codes.
     invalidNumber.setCountryCode(3923).setNationalNumber(2366L);
     assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.ZZ));
-    invalidNumber.setCountryCode(3923).setNationalNumber(2366L);
-    assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.UN001));
-    invalidNumber.setCountryCode(0).setNationalNumber(2366L);
     assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.UN001));
     invalidNumber.setCountryCode(0);
+    assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.UN001));
     assertFalse(phoneUtil.isValidNumberForRegion(invalidNumber, RegionCode.ZZ));
   }
 
@@ -1594,6 +1574,38 @@
     assertEquals(premiumNumber, phoneUtil.parse("0900 a332 600A5", RegionCode.NZ));
   }
 
+  public void testParseMaliciousInput() throws Exception {
+    // Lots of leading + signs before the possible number.
+    StringBuilder maliciousNumber = new StringBuilder(6000);
+    for (int i = 0; i < 6000; i++) {
+      maliciousNumber.append('+');
+    }
+    maliciousNumber.append("12222-33-244 extensioB 343+");
+    try {
+      phoneUtil.parse(maliciousNumber.toString(), RegionCode.US);
+      fail("This should not parse without throwing an exception " + maliciousNumber);
+    } catch (NumberParseException e) {
+      // Expected this exception.
+      assertEquals("Wrong error type stored in exception.",
+                   NumberParseException.ErrorType.TOO_LONG,
+                   e.getErrorType());
+    }
+    StringBuilder maliciousNumberWithAlmostExt = new StringBuilder(6000);
+    for (int i = 0; i < 350; i++) {
+      maliciousNumberWithAlmostExt.append("200");
+    }
+    maliciousNumberWithAlmostExt.append(" extensiOB 345");
+    try {
+      phoneUtil.parse(maliciousNumberWithAlmostExt.toString(), RegionCode.US);
+      fail("This should not parse without throwing an exception " + maliciousNumberWithAlmostExt);
+    } catch (NumberParseException e) {
+      // Expected this exception.
+      assertEquals("Wrong error type stored in exception.",
+                   NumberParseException.ErrorType.TOO_LONG,
+                   e.getErrorType());
+    }
+  }
+
   public void testParseWithInternationalPrefixes() throws Exception {
     assertEquals(US_NUMBER, phoneUtil.parse("+1 (650) 253-0000", RegionCode.NZ));
     assertEquals(INTERNATIONAL_TOLL_FREE, phoneUtil.parse("011 800 1234 5678", RegionCode.US));
diff --git a/java/test/com/android/i18n/phonenumbers/ShortNumberUtilTest.java b/java/test/com/android/i18n/phonenumbers/ShortNumberUtilTest.java
index b1aaade..99858af 100644
--- a/java/test/com/android/i18n/phonenumbers/ShortNumberUtilTest.java
+++ b/java/test/com/android/i18n/phonenumbers/ShortNumberUtilTest.java
@@ -16,25 +16,15 @@
 
 package com.android.i18n.phonenumbers;
 
-import junit.framework.TestCase;
-
-import java.io.InputStream;
-
 /**
  * Unit tests for ShortNumberUtil.java
  *
  * @author Shaopeng Jia
  */
-public class ShortNumberUtilTest extends TestCase {
+public class ShortNumberUtilTest extends TestMetadataTestCase {
   private ShortNumberUtil shortUtil;
-  static final String TEST_META_DATA_FILE_PREFIX =
-      "/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting";
 
   public ShortNumberUtilTest() {
-    PhoneNumberUtil.resetInstance();
-    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(
-        TEST_META_DATA_FILE_PREFIX,
-        CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap());
     shortUtil = new ShortNumberUtil(phoneUtil);
   }
 
diff --git a/java/test/com/android/i18n/phonenumbers/TestMetadataTestCase.java b/java/test/com/android/i18n/phonenumbers/TestMetadataTestCase.java
new file mode 100644
index 0000000..f4a40d7
--- /dev/null
+++ b/java/test/com/android/i18n/phonenumbers/TestMetadataTestCase.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2012 The Libphonenumber Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.i18n.phonenumbers;
+
+import junit.framework.TestCase;
+
+/**
+ * Root class for PhoneNumberUtil tests that depend on the test metadata file.
+ * <p>
+ * Note since tests that extend this class do not use the normal metadata file, they should not be
+ * used for regression test purposes.
+ *
+ * @author Shaopeng Jia
+ * @author Lara Rennie
+ */
+public class TestMetadataTestCase extends TestCase {
+  private static final String TEST_META_DATA_FILE_PREFIX =
+      "/com/android/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting";
+
+  protected final PhoneNumberUtil phoneUtil;
+
+  public TestMetadataTestCase() {
+    phoneUtil = initializePhoneUtilForTesting();
+  }
+
+  static PhoneNumberUtil initializePhoneUtilForTesting() {
+    PhoneNumberUtil.resetInstance();
+    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(
+        TEST_META_DATA_FILE_PREFIX,
+        CountryCodeToRegionCodeMapForTesting.getCountryCodeToRegionCodeMap());
+    return phoneUtil;
+  }
+}