001 /** 002 * 003 */ 004 package dk.jkkn.util.swixml; 005 006 import org.jdom.Attribute; 007 import org.swixml.Converter; 008 import org.swixml.Localizer; 009 010 /** 011 * Converts comma-seperated string into array of strings 012 * 013 * @author Kristian Krĉmmer Nielsen <jkkn@jkkn.dk> 014 */ 015 public class StringArrayConverter implements Converter { 016 017 /** 018 * Converts a comma-seperated string into a string array. 019 * 020 * @param type The type we convert into (String[].array) 021 * @param attr Attribute to convert 022 * @param localizer Localizer we use to localize each part of the array 023 * @see org.swixml.Converter#convert(java.lang.Class, org.jdom.Attribute, 024 * org.swixml.Localizer) 025 */ 026 @SuppressWarnings("unchecked") 027 @Override 028 public Object convert(final Class type, final Attribute attr, 029 final Localizer localizer) throws Exception { 030 if (attr != null && attr.getValue() != null) { 031 final String[] strs = attr.getValue().split(", *"); 032 for (int i = 0; i < strs.length; i++) { 033 strs[i] = localizer.getString(strs[i]); 034 } 035 return strs; 036 } 037 038 return null; 039 } 040 041 /** 042 * Type we ocnvert into (String[].class) 043 * 044 * @return String[].class 045 * @see org.swixml.Converter#convertsTo() 046 */ 047 @SuppressWarnings("unchecked") 048 @Override 049 public Class convertsTo() { 050 return String[].class; 051 } 052 053 }