1 9 package org.jboss.portal.setup.util; 10 11 12 import java.lang.reflect.Constructor ; 13 import java.lang.reflect.InvocationTargetException ; 14 import java.beans.PropertyEditorManager ; 15 import java.beans.PropertyEditor ; 16 17 18 25 public class PropertyConverter 26 { 27 28 static 29 { 30 PropertyEditor lEditor = PropertyEditorManager.findEditor(int.class); 31 if (lEditor != null) 32 { 33 PropertyEditorManager.registerEditor(Integer .class, lEditor.getClass()); 34 } 35 lEditor = null; 36 lEditor = PropertyEditorManager.findEditor(boolean.class); 37 if (lEditor != null) 38 { 39 PropertyEditorManager.registerEditor(Boolean .class, lEditor.getClass()); 40 } 41 42 PropertyEditorManager.registerEditor(Character .class, CharEditor.class); 43 PropertyEditorManager.registerEditor(Character.TYPE, CharEditor.class); 44 45 46 } 47 48 58 public static String convertToString 59 (Object 60 value) 61 throws IllegalArgumentException 62 { 63 String lStrValue = null; 64 if (value != null) 65 { 66 PropertyEditor lPe = PropertyEditorManager.findEditor(value.getClass()); 67 if (lPe != null) 68 { 69 lPe.setValue(value); 70 lStrValue = lPe.getAsText(); 71 } 72 else 73 { 74 throw new IllegalArgumentException ("PropertyEdidor is not defined for a specified type:" + 75 value.getClass().getName()); 76 } 77 } 78 return lStrValue; 79 } 80 81 93 public static Object convertToValue 94 (String 95 strValue, Class propType) 96 throws IllegalArgumentException 97 { 98 Object lValue = null; 99 if (propType == null) 100 { 101 throw new IllegalStateException ("Property class type cannot be null."); 102 } 103 104 PropertyEditor lPe = PropertyEditorManager.findEditor(propType); 105 if (lPe != null) 106 { 107 lPe.setAsText(strValue); 108 lValue = lPe.getValue(); 109 } 110 if (lValue == null && strValue != null) 111 { 112 lValue = genericConvert(strValue, propType); 114 } 115 return lValue; 116 } 117 118 119 private static Object genericConvert(String strValue, Class type) 120 { 121 Object lValue = null; 122 try 123 { 124 Constructor lConstructor = type.getDeclaredConstructor(new Class []{String .class}); 125 lValue = lConstructor.newInstance(new Object []{strValue}); 126 127 } 128 catch (NoSuchMethodException nsme) 129 { 130 IllegalArgumentException iex = new IllegalArgumentException ("Don't know how to convert a String to property of type " + 131 type.getName()); 132 iex.initCause(nsme); 133 throw iex; 134 } 135 catch (InstantiationException ie) 136 { 137 IllegalArgumentException iex = new IllegalArgumentException ("Don't know how to convert a String to property of type " + 138 type.getName()); 139 iex.initCause(ie); 140 throw iex; 141 } 142 catch (IllegalAccessException ia) 143 { 144 IllegalArgumentException iex = new IllegalArgumentException ("Don't know how to convert a String to property of type " + 145 type.getName()); 146 iex.initCause(ia); 147 throw iex; 148 } 149 catch (InvocationTargetException ite) 150 { 151 IllegalArgumentException iex = new IllegalArgumentException ("Don't know how to convert a String to property of type " + 152 type.getName()); 153 iex.initCause(ite.getTargetException()); 154 throw iex; 155 } 156 if (lValue == null) 157 { 158 throw new IllegalArgumentException ("Don't know how to convert a String to property of type " + 159 type.getName()); 160 } 161 return lValue; 162 } 163 164 } 165 | Popular Tags |