KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > carstore > CreditCardConverter


1 /*
2  * $Id: CreditCardConverter.java,v 1.1 2005/04/26 17:41:16 russo Exp $
3  */

4
5 /*
6  * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
7  *
8  * Redistribution and use in source and binary forms, with or
9  * without modification, are permitted provided that the following
10  * conditions are met:
11  *
12  * - Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * - Redistribution in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials
18  * provided with the distribution.
19  *
20  * Neither the name of Sun Microsystems, Inc. or the names of
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * This software is provided "AS IS," without a warranty of any
25  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
26  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
28  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
29  * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
30  * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
31  * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
32  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
33  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
34  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
35  * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
36  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37  *
38  * You acknowledge that this software is not designed, licensed or
39  * intended for use in the design, construction, operation or
40  * maintenance of any nuclear facility.
41  */

42
43 package carstore;
44
45
46 import javax.faces.application.FacesMessage;
47 import javax.faces.component.UIComponent;
48 import javax.faces.context.FacesContext;
49 import javax.faces.convert.Converter;
50 import javax.faces.convert.ConverterException;
51
52 /**
53  * CreditCardConverter Class accepts a Credit Card Number of type String
54  * and strips blanks and <oode>"-"</code> if any from it. It also formats the
55  * CreditCardNumber such a blank space separates every four characters.
56  * Blanks and <oode>"-"</code> characters are the expected demiliters
57  * that could be used as part of a CreditCardNumber.
58  */

59 public class CreditCardConverter implements Converter {
60
61     /**
62      * <p>The message identifier of the Message to be created if
63      * the conversion fails. The message format string for this
64      * message may optionally include a <code>{0}</code> and
65      * <code>{1}</code> placeholders, which
66      * will be replaced by the object and value.</p>
67      */

68     public static final String JavaDoc CONVERSION_ERROR_MESSAGE_ID =
69         "carstore.Conversion_Error";
70
71
72     /**
73      * Parses the CreditCardNumber and strips any blanks or <oode>"-"</code>
74      * characters from it.
75      */

76     public Object JavaDoc getAsObject(FacesContext context, UIComponent component,
77                               String JavaDoc newValue) throws ConverterException {
78
79         String JavaDoc convertedValue = null;
80         if (newValue == null) {
81             return newValue;
82         }
83         // Since this is only a String to String conversion, this conversion
84
// does not throw ConverterException.
85
convertedValue = newValue.trim();
86         if (((convertedValue.indexOf("-")) != -1) ||
87             ((convertedValue.indexOf(" ")) != -1)) {
88             char[] input = convertedValue.toCharArray();
89             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
90             for (int i = 0; i < input.length; ++i) {
91                 if (input[i] == '-' || input[i] == ' ') {
92                     continue;
93                 } else {
94                     buffer.append(input[i]);
95                 }
96             }
97             convertedValue = buffer.toString();
98         }
99         // System.out.println("Converted value " + convertedValue);
100
return convertedValue;
101     }
102
103
104     /**
105      * Formats the value by inserting space after every four characters
106      * for better readability if they don't already exist. In the process
107      * converts any <oode>"-"</code> characters into blanks for consistency.
108      */

109     public String JavaDoc getAsString(FacesContext context, UIComponent component,
110                               Object JavaDoc value) throws ConverterException {
111
112         String JavaDoc inputVal = null;
113         if (value == null) {
114             return null;
115         }
116         // value must be of the type that can be cast to a String.
117
try {
118             inputVal = (String JavaDoc) value;
119         } catch (ClassCastException JavaDoc ce) {
120             FacesMessage errMsg = MessageFactory.getMessage(
121                 CONVERSION_ERROR_MESSAGE_ID,
122                 (new Object JavaDoc[]{value, inputVal}));
123             throw new ConverterException(errMsg.getSummary());
124         }
125
126         // insert spaces after every four characters for better
127
// readability if it doesn't already exist.
128
char[] input = inputVal.toCharArray();
129         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
130         for (int i = 0; i < input.length; ++i) {
131             if ((i % 4) == 0 && i != 0) {
132                 if (input[i] != ' ' || input[i] != '-') {
133                     buffer.append(" ");
134                     // if there any "-"'s convert them to blanks.
135
} else if (input[i] == '-') {
136                     buffer.append(" ");
137                 }
138             }
139             buffer.append(input[i]);
140         }
141         String JavaDoc convertedValue = buffer.toString();
142         // System.out.println("Formatted value " + convertedValue);
143
return convertedValue;
144     }
145 }
146
Popular Tags