View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.util;
23  
24  import javax.faces.application.FacesMessage;
25  import javax.faces.convert.ConverterException;
26  
27  import org.primefaces.util.LangUtils;
28  
29  import com.google.i18n.phonenumbers.NumberParseException;
30  import com.google.i18n.phonenumbers.PhoneNumberUtil;
31  import com.google.i18n.phonenumbers.Phonenumber;
32  
33  /**
34   * @author Jasper de Vries <jepsar@gmail.com>
35   * @since 8.0.1
36   */
37  public class PhoneNumberUtilWrapper {
38  
39      private static final String MESSAGE_INVALID_VALUE_KEY = "primefaces.extensions.inputphone.INVALID";
40  
41      private PhoneNumberUtilWrapper() {
42          // private constructor to prevent instantiation
43      }
44  
45      public static void validate(final String number, final String country, final String validatorMessage) {
46          try {
47              final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
48              final Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(number, country);
49              if (!phoneNumberUtil.isValidNumber(phoneNumber)) {
50                  throw getInvalidValueConverterException(validatorMessage);
51              }
52          }
53          catch (final NumberParseException e) {
54              throw getInvalidValueConverterException(validatorMessage);
55          }
56      }
57  
58      private static ConverterException getInvalidValueConverterException(final String validatorMessage) {
59          return new ConverterException(getInvalidValueFacesMessage(validatorMessage));
60      }
61  
62      private static FacesMessage getInvalidValueFacesMessage(final String validatorMessage) {
63          if (LangUtils.isEmpty(validatorMessage)) {
64              return MessageFactory.getMessage(MESSAGE_INVALID_VALUE_KEY, FacesMessage.SEVERITY_ERROR);
65          }
66          return new FacesMessage(FacesMessage.SEVERITY_ERROR, validatorMessage, null);
67      }
68  
69  }