1 22 package org.jboss.util.propertyeditor; 23 24 import java.beans.PropertyEditorSupport ; 25 import java.util.ArrayList ; 26 import java.util.regex.Pattern ; 27 28 37 public class StringArrayEditor 38 extends PropertyEditorSupport 39 { 40 Pattern commaDelim = Pattern.compile("','|[^,\r\n]+"); 41 42 static String [] parseList(String text) 43 { 44 ArrayList list = new ArrayList (); 45 StringBuffer tmp = new StringBuffer (); 46 for(int n = 0; n < text.length(); n ++) 47 { 48 char c = text.charAt(n); 49 switch( c) 50 { 51 case '\\': 52 tmp.append(c); 53 if( n < text.length() && text.charAt(n+1) == ',' ) 54 { 55 tmp.setCharAt(tmp.length()-1, ','); 56 n ++; 57 } 58 break; 59 case ',': 60 case '\n': 61 case '\r': 62 if( tmp.length() > 0 ) 63 list.add(tmp.toString()); 64 tmp.setLength(0); 65 break; 66 default: 67 tmp.append(c); 68 break; 69 } 70 } 71 if( tmp.length() > 0 ) 72 list.add(tmp.toString()); 73 74 String [] x = new String [list.size()]; 75 list.toArray(x); 76 return x; 77 } 78 79 83 public void setAsText(final String text) 84 { 85 String [] theValue = parseList(text); 86 setValue(theValue); 87 } 88 89 92 public String getAsText() 93 { 94 String [] theValue = (String []) getValue(); 95 StringBuffer text = new StringBuffer (); 96 int length = theValue == null ? 0 : theValue.length; 97 for(int n = 0; n < length; n ++) 98 { 99 String s = theValue[n]; 100 if( s.equals(",") ) 101 text.append('\\'); 102 text.append(s); 103 text.append(','); 104 } 105 if( text.length() > 0 ) 107 text.setLength(text.length()-1); 108 return text.toString(); 109 } 110 } 111 | Popular Tags |