1 17 package org.apache.ws.jaxme.impl; 18 19 import java.io.IOException ; 20 import java.math.BigDecimal ; 21 import java.math.BigInteger ; 22 import java.text.ParsePosition ; 23 import java.util.Calendar ; 24 25 import javax.xml.XMLConstants ; 26 import javax.xml.bind.DatatypeConverterInterface; 27 import javax.xml.namespace.NamespaceContext ; 28 import javax.xml.namespace.QName ; 29 30 import org.apache.ws.jaxme.util.Base64Binary; 31 import org.apache.ws.jaxme.util.Duration; 32 import org.apache.ws.jaxme.util.HexBinary; 33 import org.apache.ws.jaxme.xs.util.XsDateFormat; 34 import org.apache.ws.jaxme.xs.util.XsDateTimeFormat; 35 import org.apache.ws.jaxme.xs.util.XsTimeFormat; 36 37 40 public class DatatypeConverterImpl implements DatatypeConverterInterface { 41 public String parseString(String arg0) { 42 return arg0; 43 } 44 45 public BigInteger parseInteger(String arg0) { 46 return new BigInteger (arg0); 47 } 48 49 public int parseInt(String arg0) { 50 return Integer.parseInt(arg0); 51 } 52 53 public long parseLong(String arg0) { 54 return Long.parseLong(arg0); 55 } 56 57 public short parseShort(String arg0) { 58 return Short.parseShort(arg0); 59 } 60 61 public BigDecimal parseDecimal(String arg0) { 62 return new BigDecimal (arg0); 63 } 64 65 public float parseFloat(String arg0) { 66 return Float.parseFloat(arg0); 67 } 68 69 public double parseDouble(String arg0) { 70 return Double.parseDouble(arg0); 71 } 72 73 public boolean parseBoolean(String arg0) { 74 return Boolean.valueOf(arg0).booleanValue(); 75 } 76 77 public byte parseByte(String arg0) { 78 return Byte.parseByte(arg0); 79 } 80 81 public QName parseQName(String arg0, NamespaceContext arg1) { 82 int offset = arg0.indexOf(':'); 83 String uri; 84 String localName; 85 switch (offset) { 86 case -1: 87 localName = arg0; 88 uri = arg1.getNamespaceURI(""); 89 if (uri == null) { 90 throw new IllegalArgumentException ("The default prefix is not bound."); 92 } 93 break; 94 case 0: 95 throw new IllegalArgumentException ("Default prefix must be indicated by not using a colon: " + arg0); 96 default: 97 String prefix = arg0.substring(0, offset); 98 localName = arg0.substring(offset+1); 99 uri = arg1.getNamespaceURI(prefix); 100 if (uri == null) { 101 throw new IllegalArgumentException ("The prefix " + prefix + " is not bound."); 102 } 103 } 104 return new QName (uri, localName); 105 } 106 107 public Calendar parseDateTime(String arg0) { 108 XsDateTimeFormat format = new XsDateTimeFormat(); 109 ParsePosition pos = new ParsePosition (0); 110 Calendar cal = (Calendar ) format.parseObject(arg0, pos); 111 if (cal == null) { 112 throw new IllegalArgumentException ("Failed to parse dateTime " + arg0 + 113 " at:" + arg0.substring(pos.getErrorIndex())); 114 } 115 return cal; 116 } 117 118 public byte[] parseBase64Binary(String arg0) { 119 try { 120 return Base64Binary.decode(arg0); 121 } catch (IOException e) { 122 throw new IllegalArgumentException ("Failed to parse " + arg0 + ": " + 123 e.getMessage()); 124 } 125 } 126 127 public byte[] parseHexBinary(String arg0) { 128 return HexBinary.decode(arg0); 129 } 130 131 private static final long MAX_UNSIGNED_INT = (((long) Integer.MAX_VALUE) * 2) + 1; 132 public long parseUnsignedInt(String arg0) { 133 long l = Long.parseLong(arg0); 134 if (l < 0) { 135 throw new IllegalArgumentException ("Failed to parse UnsignedInt " + arg0 + ": result is negative"); 136 } 137 if (l > MAX_UNSIGNED_INT) { 138 throw new IllegalArgumentException ("Failed to parse UnsignedInt " + arg0 + 139 ": result exceeds maximum value " + MAX_UNSIGNED_INT); 140 } 141 return l; 142 } 143 144 private static final int MAX_UNSIGNED_SHORT = Short.MAX_VALUE * 2 + 1; 145 public int parseUnsignedShort(String arg0) { 146 int i = Integer.parseInt(arg0); 147 if (i < 0) { 148 throw new IllegalArgumentException ("Failed to parse UnsignedShort " + arg0 + ": result is negative"); 149 } 150 if (i > MAX_UNSIGNED_SHORT) { 151 throw new IllegalArgumentException ("Failed to parse UnsignedShort " + arg0 + 152 ": result exceeds maximum value " + MAX_UNSIGNED_SHORT); 153 } 154 return i; 155 } 156 157 public Calendar parseTime(String arg0) { 158 XsTimeFormat format = new XsTimeFormat(); 159 ParsePosition pos = new ParsePosition (0); 160 Calendar cal = (Calendar ) format.parseObject(arg0, pos); 161 if (cal == null) { 162 throw new IllegalArgumentException ("Failed to parse time " + arg0 + 163 " at:" + arg0.substring(pos.getErrorIndex())); 164 } 165 return cal; 166 } 167 168 public Calendar parseDate(String arg0) { 169 XsDateFormat format = new XsDateFormat(); 170 ParsePosition pos = new ParsePosition (0); 171 Calendar cal = (Calendar ) format.parseObject(arg0, pos); 172 if (cal == null) { 173 throw new IllegalArgumentException ("Failed to parse date " + arg0 + 174 " at:" + arg0.substring(pos.getErrorIndex())); 175 } 176 return cal; 177 } 178 179 public String parseAnySimpleType(String arg0) { 180 return arg0; 181 } 182 183 public Duration parseDuration(String pDuration) { 184 return Duration.valueOf(pDuration); 185 } 186 187 public String printString(String arg0) { 188 return arg0; 189 } 190 191 public String printInteger(BigInteger arg0) { 192 return arg0.toString(); 193 } 194 195 public String printInt(int arg0) { 196 return Integer.toString(arg0); 197 } 198 199 public String printLong(long arg0) { 200 return Long.toString(arg0); 201 } 202 203 public String printShort(short arg0) { 204 return Short.toString(arg0); 205 } 206 207 public String printDecimal(BigDecimal arg0) { 208 return arg0.toString(); 209 } 210 211 public String printFloat(float arg0) { 212 return Float.toString(arg0); 213 } 214 215 public String printDouble(double arg0) { 216 return Double.toString(arg0); 217 } 218 219 public String printBoolean(boolean arg0) { 220 return (arg0 ? Boolean.TRUE : Boolean.FALSE).toString(); 221 } 222 223 public String printByte(byte arg0) { 224 return Byte.toString(arg0); 225 } 226 227 public String printQName(QName arg0, NamespaceContext arg1) { 228 String prefix = arg1.getPrefix(arg0.getNamespaceURI()); 229 if (prefix == null) { 230 throw new IllegalArgumentException ("The namespace URI " + 231 arg0.getNamespaceURI() + 232 " is not bound."); 233 } else if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) { 234 return arg0.getLocalPart(); 235 } else { 236 return prefix + ":" + arg0.getLocalPart(); 237 } 238 } 239 240 public String printDateTime(Calendar arg0) { 241 return new XsDateTimeFormat().format(arg0); 242 } 243 244 public String printBase64Binary(byte[] arg0) { 245 return Base64Binary.encode(arg0); 246 } 247 248 public String printHexBinary(byte[] arg0) { 249 return HexBinary.encode(arg0); 250 } 251 252 public String printUnsignedInt(long arg0) { 253 return Long.toString(arg0); 254 } 255 256 public String printUnsignedShort(int arg0) { 257 return Integer.toString(arg0); 258 } 259 260 public String printTime(Calendar arg0) { 261 return new XsTimeFormat().format(arg0); 262 } 263 264 public String printDate(Calendar arg0) { 265 return new XsDateFormat().format(arg0); 266 } 267 268 public String printAnySimpleType(String arg0) { 269 return arg0; 270 } 271 272 public String printDuration(Duration pDuration) { 273 return pDuration.toString(); 274 } 275 } 276 277 278 | Popular Tags |