1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.faces.converter;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  
19  import java.util.Locale;
20  
21  import javax.faces.application.FacesMessage;
22  import javax.faces.component.StateHolder;
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.ExternalContext;
25  import javax.faces.context.FacesContext;
26  import javax.faces.convert.Converter;
27  import javax.faces.convert.ConverterException;
28  
29  /**
30   * <a href="PhoneNumberConverter.java.html"><b><i>View Source</i></b></a>
31   *
32   * <p>
33   * This class is a JSF converter that can be used to convert phone numbers.
34   * Since phone numbers in the United States of America always have 10 digits,
35   * this converter provides automatic conversion of 10 digit phone numbers to a
36   * desired format. The format is specified by the unitedStatesFormat component
37   * attribute.
38   * </p>
39   *
40   * @author Neil Griffin
41   */
42  public class PhoneNumberConverter implements Converter, StateHolder {
43  
44      public boolean isTransient() {
45          return _transient;
46      }
47  
48      public void setTransient(boolean value) {
49          _transient = value;
50      }
51  
52      public String getUnitedStatesFormat() {
53          return _unitedStatesFormat;
54      }
55  
56      public void setUnitedStatesFormat(String unitedStatesFormat) {
57          _unitedStatesFormat = unitedStatesFormat;
58      }
59  
60      public Object getAsObject(
61          FacesContext facesContext, UIComponent uiComponent, String value) {
62  
63          if (value != null) {
64              StringBuilder integerChars = new StringBuilder(value.length());
65              StringBuilder invalidChars = new StringBuilder(value.length());
66  
67              for (int i = 0; i < value.length(); i++) {
68                  char curChar = value.charAt(i);
69  
70                  if (Character.isDigit(curChar)) {
71                      integerChars.append(curChar);
72                  }
73                  else if ((curChar != '-') && (curChar != '(') &&
74                           (curChar != ')') && (curChar != '.') &&
75                           (curChar != '+') && (curChar != ' ')) {
76  
77                      invalidChars.append(curChar);
78                  }
79              }
80  
81              if (invalidChars.length() > 0) {
82                  ExternalContext externalContext =
83                      facesContext.getExternalContext();
84  
85                  Locale locale = externalContext.getRequestLocale();
86  
87                  String summary = LanguageUtil.get(
88                      locale, "the-following-are-invalid-characters");
89  
90                  summary += " " + invalidChars.toString();
91  
92                  FacesMessage facesMessage = new FacesMessage(
93                      FacesMessage.SEVERITY_ERROR, summary, null);
94  
95                  throw new ConverterException(facesMessage);
96              }
97              else if ((integerChars.length() == 10)) {
98                  StringBuilder unitedStatesPhoneNumber = new StringBuilder(
99                      _unitedStatesFormat.length());
100 
101                 int integerDigitIndex = 0;
102 
103                 for (int i = 0; i < _unitedStatesFormat.length(); i++) {
104                     char curChar = _unitedStatesFormat.charAt(i);
105 
106                     if (curChar == '#') {
107                         unitedStatesPhoneNumber.append(
108                             integerChars.charAt(integerDigitIndex++));
109                     }
110                     else {
111                         unitedStatesPhoneNumber.append(curChar);
112                     }
113                 }
114 
115                 return unitedStatesPhoneNumber.toString();
116             }
117         }
118 
119         return value;
120     }
121 
122     public String getAsString(
123         FacesContext facesContext, UIComponent uiComponent, Object value)
124         throws ConverterException {
125 
126         // ICE-1537
127 
128         return (String)value;
129     }
130 
131     public void restoreState(FacesContext facesContext, Object obj) {
132         Object[] values = (Object[])obj;
133 
134         _unitedStatesFormat = (String)values[0];
135     }
136 
137     public Object saveState(FacesContext facesContext) {
138         Object[] values = new Object[1];
139 
140         values[0] = _unitedStatesFormat;
141 
142         return values;
143     }
144 
145     private boolean _transient;
146     private String _unitedStatesFormat;
147 
148 }