1 22 package com.scalagent.kjoram.ksoap; 23 24 import java.util.Hashtable ; 25 26 import com.scalagent.kjoram.excepts.MessageFormatException; 27 28 33 class ConversionHelper { 34 static boolean getBoolean(Hashtable props, String name) 35 throws MessageFormatException { 36 if (props == null) 37 return false; 38 39 Object prop = props.get(name); 40 if (prop == null) 41 return false; 42 else if (prop instanceof Boolean ) 43 return ((Boolean ) prop).booleanValue(); 44 else if (prop instanceof String ) 45 return ((String ) prop).equals("true"); 46 else 47 throw new MessageFormatException("Type " + prop.getClass().getName() 48 + " can't be converted to Boolean."); 49 } 50 51 static byte getByte(Hashtable props, String name) 52 throws MessageFormatException { 53 if (props == null) 54 return Byte.parseByte(null); 55 56 Object prop = props.get(name); 57 if (prop == null) 58 return Byte.parseByte(null); 59 else if (prop instanceof Byte ) 60 return ((Byte ) prop).byteValue(); 61 else if (prop instanceof String ) 62 return Byte.parseByte((String ) prop); 63 else 64 throw new MessageFormatException("Type " + prop.getClass().getName() 65 + " can't be converted to Byte."); 66 } 67 68 static short getShort(Hashtable props, String name) 69 throws MessageFormatException { 70 if (props == null) 71 return Short.parseShort(null); 72 73 Object prop = props.get(name); 74 if (prop == null) 75 return Short.parseShort(null); 76 else if (prop instanceof Byte ) 77 return Short.parseShort(((Byte ) prop).toString()); 78 else if (prop instanceof Short ) 79 return ((Short ) prop).shortValue(); 80 else if (prop instanceof String ) 81 return Short.parseShort((String ) prop); 82 else 83 throw new MessageFormatException("Type " + prop.getClass().getName() 84 + " can't be converted to Short."); 85 } 86 87 static int getInt(Hashtable props, String name) 88 throws MessageFormatException { 89 if (props == null) 90 return Integer.parseInt(null); 91 92 Object prop = props.get(name); 93 if (prop == null) 94 return Integer.parseInt(null); 95 else if (prop instanceof Byte ) 96 return Integer.parseInt(((Byte ) prop).toString()); 97 else if (prop instanceof Short ) 98 return Integer.parseInt(((Short ) prop).toString()); 99 else if (prop instanceof Integer ) 100 return ((Integer ) prop).intValue(); 101 else if (prop instanceof String ) 102 return Integer.parseInt((String ) prop); 103 else 104 throw new MessageFormatException("Type " + prop.getClass().getName() 105 + " can't be converted to Integer."); 106 } 107 108 static long getLong(Hashtable props, String name) 109 throws MessageFormatException { 110 if (props == null) 111 return Long.parseLong(null); 112 113 Object prop = props.get(name); 114 if (prop == null) 115 return Long.parseLong(null); 116 else if (prop instanceof Byte ) 117 return Long.parseLong(((Byte ) prop).toString()); 118 else if (prop instanceof Short ) 119 return Long.parseLong(((Short ) prop).toString()); 120 else if (prop instanceof Integer ) 121 return Long.parseLong(((Integer ) prop).toString()); 122 else if (prop instanceof Long ) 123 return ((Long ) prop).longValue(); 124 else if (prop instanceof String ) 125 return Long.parseLong((String ) prop); 126 else 127 throw new MessageFormatException("Type " + prop.getClass().getName() 128 + " can't be converted to Long."); 129 } 130 131 132 static String getString(Hashtable props, String name) { 133 if (props == null) 134 return null; 135 136 Object prop = props.get(name); 137 if (prop == null) 138 return null; 139 else if (prop instanceof byte[]) 140 return new String ((byte[]) prop); 141 else 142 return prop.toString(); 143 } 144 145 static char getChar(Hashtable map, String name) 146 throws MessageFormatException { 147 Object value = map.get(name); 148 if (value == null) 149 return ((Character ) null).charValue(); 150 else if (value instanceof Character ) 151 return ((Character ) value).charValue(); 152 else 153 throw new MessageFormatException("Type " + value.getClass().getName() 154 + " can't be converted to Character."); 155 } 156 157 static byte[] getBytes(Hashtable map, String name) 158 throws MessageFormatException { 159 Object value = map.get(name); 160 if (value == null) 161 return (byte[]) value; 162 else if (value instanceof byte[]) 163 return (byte[]) value; 164 else 165 throw new MessageFormatException("Type " + value.getClass().getName() 166 + " can't be converted to byte[]."); 167 } 168 } 169 | Popular Tags |