Class PhoneNumberUtil


  • public final class PhoneNumberUtil
    extends java.lang.Object
    Utility class providing misc methods to deal with phone number.
    Since:
    jcms-9.0
    Version:
    $Revision: 118899 $
    See Also:
    PhoneNumber
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  PhoneNumberUtil.PhoneHtmlGenerator
      Generator of the HTML suitable to display the specified number (with additionnal optional parameters).
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String EXCLUSION_PATTERN_PROPERTY
      Name of the JCMS property used to exclude number from parsing (value is a regular expression).
      static java.util.regex.Pattern exclusionPattern  
      static java.lang.String REVISION  
    • Constructor Summary

      Constructors 
      Constructor Description
      PhoneNumberUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getPhoneHtml​(java.lang.String number, java.lang.String region)
      Retrieve the specified telephone number properly formated in an HTML tag.
      static PhoneNumber parseNumber​(java.lang.String number, java.lang.String region)
      Parse a phone number string into a PhoneNumber instance.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EXCLUSION_PATTERN_PROPERTY

        public static final java.lang.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:
        Constant Field Values
      • exclusionPattern

        public static java.util.regex.Pattern exclusionPattern
    • Constructor Detail

      • PhoneNumberUtil

        public PhoneNumberUtil()
    • Method Detail

      • getPhoneHtml

        public static java.lang.String getPhoneHtml​(java.lang.String number,
                                                    java.lang.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​(java.lang.String number,
                                              java.lang.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