Class PhoneNumberUtil

java.lang.Object
com.jalios.jcms.i18n.PhoneNumberUtil

public final class PhoneNumberUtil extends Object
Utility class providing misc methods to deal with phone number.
Since:
jcms-9.0
See Also:
  • Field Details

    • EXCLUSION_PATTERN_PROPERTY

      public static final String EXCLUSION_PATTERN_PROPERTY
      Name of the JCMS property used to exclude number from parsing (value is a regular expression).

      If defined, all text that match the regular expression will be excluded from parsing into a valid PhoneNumber instance.

      See Also:
    • exclusionPattern

      public static Pattern exclusionPattern
  • Constructor Details

    • PhoneNumberUtil

      public PhoneNumberUtil()
  • Method Details

    • getPhoneHtml

      public static String getPhoneHtml(String number, String region)
      Retrieve the specified telephone number properly formated in an HTML tag.

      Uses the RFC 3966 ("tel:" uri) as the anchor href, and the international format in the anchor body text, the original input is placed inside the title attribute.

      Phone number is parsed with rule explained in parseNumber(String, String). For example :

        assertEquals("<a href=\"tel:+33-4-46-68-18-00\" title=\"+33 44 66 8 180 0\" class=\"tel\">+33 4 46 68 18 00</a>", 
                     PhoneTag.getPhoneHtml("+33 44 66 8 180 0", null));
        
        final String sourceNumber = "0446681800";
        assertEquals("<a href=\"tel:+41-44-668-18-00\" title=\"0446681800\" class=\"tel\">+41 44 668 18 00</a>", 
                     PhoneTag.getPhoneHtml(sourceNumber, "CH"));    
        assertEquals("<a href=\"tel:+33-4-46-68-18-00\" title=\"0446681800\" class=\"tel\">+33 4 46 68 18 00</a>", 
                     PhoneTag.getPhoneHtml(sourceNumber, "FR"));
       
      If number could not be parsed, it is returned encoded for HTML, inside a dedicated span with the invalid-tel CSS class
        assertEquals("<span class=\"invalid-tel\">do not call me!</span>", 
                     PhoneTag.getPhoneHtml("do not call me!", null));
       
      Parameters:
      number - the telephone number to parse and display
      region - the ISO 3166-1 two-letter region code that denotes the region that we are expecting the number to be from. This is only used if the number being parsed is not written in international format.
      Returns:
      an HTML string suitable for displaying a phone number, an empty string if number was empty or could not be parsed
    • parseNumber

      public static PhoneNumber parseNumber(String number, String region)
      Parse a phone number string into a PhoneNumber instance.

      The following rules are applied regarding regionnal information :

      • regionnal information contained in the number is used first if available
      • otherwise the region paremeter is used, if specified and valid
      • otherwise the default site country is used
      Parameters:
      number - the telephone number to parse
      region - the ISO 3166-1 two-letter region code that denotes the region that we are expecting the number to be from. This is only used if the number being parsed is not written in international format.
      Returns:
      a PhoneNumber instance or null if number could not be parsed properly