001: /*
002: * $Id: CreditCardConverter.java 53793 2004-10-05 13:47:48Z vgritsenko $
003: */
004:
005: /*
006: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Redistribution and use in source and binary forms, with or
009: * without modification, are permitted provided that the following
010: * conditions are met:
011: *
012: * - Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * - Redistribution in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials
018: * provided with the distribution.
019: *
020: * Neither the name of Sun Microsystems, Inc. or the names of
021: * contributors may be used to endorse or promote products derived
022: * from this software without specific prior written permission.
023: *
024: * This software is provided "AS IS," without a warranty of any
025: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
026: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
027: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
028: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
029: * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
030: * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
031: * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
032: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
033: * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
034: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
035: * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
036: * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
037: *
038: * You acknowledge that this software is not designed, licensed or
039: * intended for use in the design, construction, operation or
040: * maintenance of any nuclear facility.
041: */
042:
043: package org.apache.cocoon.faces.samples.carstore;
044:
045: import javax.faces.application.FacesMessage;
046: import javax.faces.component.UIComponent;
047: import javax.faces.context.FacesContext;
048: import javax.faces.convert.Converter;
049: import javax.faces.convert.ConverterException;
050:
051: /**
052: * CreditCardConverter Class accepts a Credit Card Number of type String
053: * and strips blanks and <oode>"-"</code> if any from it. It also formats the
054: * CreditCardNumber such a blank space separates every four characters.
055: * Blanks and <oode>"-"</code> characters are the expected demiliters
056: * that could be used as part of a CreditCardNumber.
057: */
058: public class CreditCardConverter implements Converter {
059:
060: /**
061: * <p>The message identifier of the Message to be created if
062: * the conversion fails. The message format string for this
063: * message may optionally include a <code>{0}</code> and
064: * <code>{1}</code> placeholders, which
065: * will be replaced by the object and value.</p>
066: */
067: public static final String CONVERSION_ERROR_MESSAGE_ID = "carstore.Conversion_Error";
068:
069: /**
070: * Parses the CreditCardNumber and strips any blanks or <oode>"-"</code>
071: * characters from it.
072: */
073: public Object getAsObject(FacesContext context,
074: UIComponent component, String newValue)
075: throws ConverterException {
076:
077: String convertedValue = null;
078: if (newValue == null) {
079: return newValue;
080: }
081: // Since this is only a String to String conversion, this conversion
082: // does not throw ConverterException.
083: convertedValue = newValue.trim();
084: if (((convertedValue.indexOf("-")) != -1)
085: || ((convertedValue.indexOf(" ")) != -1)) {
086: char[] input = convertedValue.toCharArray();
087: StringBuffer buffer = new StringBuffer(50);
088: for (int i = 0; i < input.length; ++i) {
089: if (input[i] == '-' || input[i] == ' ') {
090: continue;
091: } else {
092: buffer.append(input[i]);
093: }
094: }
095: convertedValue = buffer.toString();
096: }
097: // System.out.println("Converted value " + convertedValue);
098: return convertedValue;
099: }
100:
101: /**
102: * Formats the value by inserting space after every four characters
103: * for better readability if they don't already exist. In the process
104: * converts any <oode>"-"</code> characters into blanks for consistency.
105: */
106: public String getAsString(FacesContext context,
107: UIComponent component, Object value)
108: throws ConverterException {
109:
110: String inputVal = null;
111: if (value == null) {
112: return null;
113: }
114: // value must be of the type that can be cast to a String.
115: try {
116: inputVal = (String) value;
117: } catch (ClassCastException ce) {
118: FacesMessage errMsg = MessageFactory.getMessage(
119: CONVERSION_ERROR_MESSAGE_ID, (new Object[] { value,
120: inputVal }));
121: throw new ConverterException(errMsg.getSummary());
122: }
123:
124: // insert spaces after every four characters for better
125: // readability if it doesn't already exist.
126: char[] input = inputVal.toCharArray();
127: StringBuffer buffer = new StringBuffer(50);
128: for (int i = 0; i < input.length; ++i) {
129: if ((i % 4) == 0 && i != 0) {
130: if (input[i] != ' ' || input[i] != '-') {
131: buffer.append(" ");
132: // if there any "-"'s convert them to blanks.
133: } else if (input[i] == '-') {
134: buffer.append(" ");
135: }
136: }
137: buffer.append(input[i]);
138: }
139: String convertedValue = buffer.toString();
140: // System.out.println("Formatted value " + convertedValue);
141: return convertedValue;
142: }
143: }
|