[jboss-user] [JBoss Seam] - Re: How to mask credit card output?

matt.drees do-not-reply at jboss.com
Thu Aug 16 19:23:54 EDT 2007


Just because I'm probably going to use one at some point...


  | import java.io.Serializable;
  | 
  | import javax.faces.component.UIComponent;
  | import javax.faces.context.FacesContext;
  | import javax.faces.convert.Converter;
  | 
  | import org.jboss.seam.annotations.Logger;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.log.Log;
  | 
  | /**
  |  * Just for h:outputText components
  |  * @author Matthew.Drees
  |  *
  |  */
  | @Name("creditCardMaskingConverter")
  | @org.jboss.seam.annotations.faces.Converter
  | public class CreditCardMaskingConverter implements Converter, Serializable {
  | 
  | 	private static final long serialVersionUID = 1L;
  | 	
  | 	@Logger Log log;
  | 
  | 	String pattern = "1111********1111";
  | 	
  | 	public Object getAsObject(FacesContext context, UIComponent component, String value) {
  | 		throw new UnsupportedOperationException();
  | 	}
  | 
  | 	public String getAsString(FacesContext context, UIComponent component, Object value) {
  | 		log.trace("getting #0 as String", value);
  | 		if (value == null) {
  | 			throw new IllegalArgumentException("value to convert is null");
  | 		}
  | 		String toString = value.toString();
  | 		assertValidity(toString);
  | 		StringBuilder sb = new StringBuilder();
  | 		for(int i = 0; i < 16; i++) {
  | 			if (pattern.charAt(i) == '*') {
  | 				sb.append('*');
  | 			} else {
  | 				sb.append(toString.charAt(i));
  | 			}
  | 		}
  | 		return sb.toString();
  | 	}
  | 
  | 	private void assertValidity(String string) {
  | 		if (string.length() != 16) {
  | 			throw new IllegalArgumentException("not a 16-digit string: " + string);
  | 		}
  | 	}
  | 	
  | 	/**
  | 	 * A 16-character string indicating the masking pattern. A '*' at index i means
  | 	 * the converted output will have a '*' at index i. 
  | 	 * @param pattern
  | 	 */
  | 	public void setPattern(String pattern) {
  | 		assertValidity(pattern);
  | 		this.pattern = pattern;
  | 	}
  | 
  | 	public String getPattern() {
  | 		return pattern;
  | 	}
  | 
  | 	public Log getLog() {
  | 		return log;
  | 	}
  | 
  | 	public void setLog(Log log) {
  | 		this.log = log;
  | 	}
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4075043#4075043

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4075043



More information about the jboss-user mailing list