1 13 package com.tonbeller.wcf.format; 14 15 import java.util.List ; 16 17 import org.apache.log4j.Logger; 18 import org.apache.regexp.RE; 19 import org.apache.regexp.RESyntaxException; 20 21 import com.tonbeller.wcf.utils.SoftException; 22 23 24 27 public class RegexHandler extends FormatHandlerSupport { 28 private static Logger logger = Logger.getLogger(RegexHandler.class); 29 32 public String format(Object o, String userPattern) { 33 return String.valueOf(o); 34 } 35 36 40 public Object parse(String s, String userPattern) throws FormatException { 41 try { 42 String regex = super.findPattern(userPattern); 43 RE re = new RE(regex); 44 if (!re.match(s)) { 45 throw new FormatException(getErrorMessage(s)); 46 } 47 return s; 48 } catch (RESyntaxException e) { 49 logger.error("exception caught", e); 50 throw new SoftException(e); 51 } 52 } 53 54 public boolean canHandle(Object value) { 55 return false; 56 } 57 58 public Object toNativeArray(List list) { 59 String [] array = new String [list.size()]; 60 for (int i = 0; i < array.length; i++) 61 array[i] = (String )list.get(i); 62 return array; 63 } 64 65 66 public Object [] toObjectArray(Object value) { 67 if (value instanceof String ) 68 return new String [] {(String ) value }; 69 return (String []) value; 70 } 71 72 } | Popular Tags |