1 9 package org.jboss.portal.server.util; 10 11 import java.util.ArrayList ; 12 13 import org.jboss.portal.common.value.Converter; 14 import org.jboss.portal.common.value.FormatConversionException; 15 import org.jboss.portal.common.value.NullConversionException; 16 import org.jboss.portal.server.ServerObjectID; 17 18 22 public class ServerObjectIDConverter implements Converter 23 { 24 25 public static final ServerObjectIDConverter instance = new ServerObjectIDConverter(); 26 27 public static ServerObjectID decode(String value) throws NullConversionException, FormatConversionException 28 { 29 if (value == null) 30 { 31 throw new NullConversionException(); 32 } 33 if (value.length() == 0) 34 { 35 throw new FormatConversionException(); 36 } 37 int index = value.indexOf('.'); 38 ArrayList list = new ArrayList (); 39 int previous = 0; 40 while (index != -1) 41 { 42 String name = value.substring(previous, index); 43 list.add(name); 44 previous = index + 1; 45 index = value.indexOf('.', previous); 46 } 47 list.add(value.substring(previous)); 48 if (list.size() == 0) 49 { 50 throw new NullConversionException(); 51 } 52 String [] names = (String [])list.toArray(new String [list.size()]); 53 return new ServerObjectID(names); 54 } 55 56 public boolean accept(Class clazz) 57 { 58 return ServerObjectID.class.equals(clazz); 59 } 60 61 public Object toObject(String value) throws NullConversionException, FormatConversionException 62 { 63 return decode(value); 64 } 65 66 public String toString(Object value) throws NullConversionException, FormatConversionException 67 { 68 if (value == null) 69 { 70 throw new NullConversionException(); 71 } 72 if (value instanceof ServerObjectID) 73 { 74 return value.toString(); 75 } 76 throw new FormatConversionException("Unrecognized object " + value.getClass().getName()); 77 } 78 } 79 | Popular Tags |