1
15 package com.amazonaws.transform;
16
17 import com.amazonaws.SdkClientException;
18 import com.amazonaws.util.Base64;
19 import com.amazonaws.util.DateUtils;
20
21 import com.amazonaws.util.TimestampFormat;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import java.math.BigDecimal;
26 import java.math.BigInteger;
27 import java.nio.ByteBuffer;
28 import java.util.Date;
29
30
33 public class SimpleTypeStaxUnmarshallers {
34
35 private static Log log = LogFactory.getLog(SimpleTypeStaxUnmarshallers.class);
36
37
40 public static class StringStaxUnmarshaller implements Unmarshaller<String, StaxUnmarshallerContext> {
41 public String unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
42 return unmarshallerContext.readText();
43 }
44
45 private static final StringStaxUnmarshaller instance = new StringStaxUnmarshaller();
46
47 public static StringStaxUnmarshaller getInstance() {
48 return instance;
49 }
50 }
51
52 public static class BigDecimalStaxUnmarshaller implements Unmarshaller<BigDecimal, StaxUnmarshallerContext> {
53 public BigDecimal unmarshall(StaxUnmarshallerContext unmarshallerContext)
54 throws Exception {
55 String s = unmarshallerContext.readText();
56 return (s == null) ? null : new BigDecimal(s);
57 }
58
59 private static final BigDecimalStaxUnmarshaller instance = new BigDecimalStaxUnmarshaller();
60
61 public static BigDecimalStaxUnmarshaller getInstance() {
62 return instance;
63 }
64 }
65
66 public static class BigIntegerStaxUnmarshaller implements Unmarshaller<BigInteger, StaxUnmarshallerContext> {
67 public BigInteger unmarshall(StaxUnmarshallerContext unmarshallerContext)
68 throws Exception {
69 String s = unmarshallerContext.readText();
70 return (s == null) ? null : new BigInteger(s);
71 }
72
73 private static final BigIntegerStaxUnmarshaller instance = new BigIntegerStaxUnmarshaller();
74
75 public static BigIntegerStaxUnmarshaller getInstance() {
76 return instance;
77 }
78 }
79
80
83 public static class DoubleStaxUnmarshaller implements Unmarshaller<Double, StaxUnmarshallerContext> {
84 public Double unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
85 String doubleString = unmarshallerContext.readText();
86 return (doubleString == null) ? null : Double.parseDouble(doubleString);
87 }
88
89 private static final DoubleStaxUnmarshaller instance = new DoubleStaxUnmarshaller();
90
91 public static DoubleStaxUnmarshaller getInstance() {
92 return instance;
93 }
94 }
95
96
99 public static class IntegerStaxUnmarshaller implements Unmarshaller<Integer, StaxUnmarshallerContext> {
100 public Integer unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
101 String intString = unmarshallerContext.readText();
102 return (intString == null) ? null : Integer.parseInt(intString);
103 }
104
105 private static final IntegerStaxUnmarshaller instance = new IntegerStaxUnmarshaller();
106
107 public static IntegerStaxUnmarshaller getInstance() {
108 return instance;
109 }
110 }
111
112
115 public static class BooleanStaxUnmarshaller implements Unmarshaller<Boolean, StaxUnmarshallerContext> {
116 public Boolean unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
117 String booleanString = unmarshallerContext.readText();
118 return (booleanString == null) ? null : Boolean.parseBoolean(booleanString);
119 }
120
121 private static final BooleanStaxUnmarshaller instance = new BooleanStaxUnmarshaller();
122
123 public static BooleanStaxUnmarshaller getInstance() {
124 return instance;
125 }
126 }
127
128
131 public static class FloatStaxUnmarshaller implements Unmarshaller<Float, StaxUnmarshallerContext> {
132 public Float unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
133 String floatString = unmarshallerContext.readText();
134 return (floatString == null) ? null : Float.valueOf(floatString);
135 }
136
137 private static final FloatStaxUnmarshaller instance = new FloatStaxUnmarshaller();
138
139 public static FloatStaxUnmarshaller getInstance() {
140 return instance;
141 }
142 }
143
144
147 public static class LongStaxUnmarshaller implements Unmarshaller<Long, StaxUnmarshallerContext> {
148 public Long unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
149 String longString = unmarshallerContext.readText();
150 return (longString == null) ? null : Long.parseLong(longString);
151 }
152
153 private static final LongStaxUnmarshaller instance = new LongStaxUnmarshaller();
154
155 public static LongStaxUnmarshaller getInstance() {
156 return instance;
157 }
158 }
159
160
163 public static class ByteStaxUnmarshaller implements Unmarshaller<Byte, StaxUnmarshallerContext> {
164 public Byte unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
165 String byteString = unmarshallerContext.readText();
166 return (byteString == null) ? null : Byte.valueOf(byteString);
167 }
168
169 private static final ByteStaxUnmarshaller instance = new ByteStaxUnmarshaller();
170
171 public static ByteStaxUnmarshaller getInstance() {
172 return instance;
173 }
174 }
175
176
177 public static class DateStaxUnmarshallerFactory implements Unmarshaller<Date, StaxUnmarshallerContext> {
178
179 private final String dateFormatType;
180
181 private DateStaxUnmarshallerFactory(String dateFormatType) {
182 this.dateFormatType = dateFormatType;
183 }
184
185 @Override
186 public Date unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
187 String dateString = unmarshallerContext.readText();
188 if (dateString == null) return null;
189
190 try {
191 if (TimestampFormat.RFC_822.getFormat().equals(dateFormatType)) {
192 return DateUtils.parseRFC822Date(dateString);
193 }
194
195 if (TimestampFormat.UNIX_TIMESTAMP.getFormat().equals(dateFormatType)) {
196 return DateUtils.parseServiceSpecificDate(dateString);
197 }
198
199 return DateUtils.parseISO8601Date(dateString);
200
201 } catch (Exception e) {
202 log.warn("Unable to parse date '" + dateString + "': " + e.getMessage(), e);
203 return null;
204 }
205 }
206
207 public static DateStaxUnmarshallerFactory getInstance(String dateFormatType) {
208 return new DateStaxUnmarshallerFactory(dateFormatType);
209 }
210 }
211
212
215 public static class DateStaxUnmarshaller implements Unmarshaller<Date, StaxUnmarshallerContext> {
216 public Date unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
217 String dateString = unmarshallerContext.readText();
218 if (dateString == null) return null;
219
220 try {
221 return DateUtils.parseISO8601Date(dateString);
222 } catch (Exception e) {
223 log.warn("Unable to parse date '" + dateString + "': " + e.getMessage(), e);
224 return null;
225 }
226 }
227
228 private static final DateStaxUnmarshaller instance = new DateStaxUnmarshaller();
229
230 public static DateStaxUnmarshaller getInstance() {
231 return instance;
232 }
233 }
234
235
238 public static class ByteBufferStaxUnmarshaller implements Unmarshaller<ByteBuffer, StaxUnmarshallerContext> {
239 public ByteBuffer unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
240 String base64EncodedString = unmarshallerContext.readText();
241 byte[] decodedBytes = Base64.decode(base64EncodedString);
242 return ByteBuffer.wrap(decodedBytes);
243
244 }
245
246 private static final ByteBufferStaxUnmarshaller instance = new ByteBufferStaxUnmarshaller();
247
248 public static ByteBufferStaxUnmarshaller getInstance() {
249 return instance;
250 }
251 }
252
253
256 public static class CharacterJsonUnmarshaller implements Unmarshaller<Character, StaxUnmarshallerContext> {
257 public Character unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
258 String charString = unmarshallerContext.readText();
259
260 if (charString == null) return null;
261
262 charString = charString.trim();
263 if (charString.isEmpty() || charString.length() > 1)
264 throw new SdkClientException("'" + charString
265 + "' cannot be converted to Character");
266 return Character.valueOf(charString.charAt(0));
267 }
268
269 private static final CharacterJsonUnmarshaller instance = new CharacterJsonUnmarshaller();
270
271 public static CharacterJsonUnmarshaller getInstance() {
272 return instance;
273 }
274 }
275
276
279 public static class ShortJsonUnmarshaller implements Unmarshaller<Short, StaxUnmarshallerContext> {
280 public Short unmarshall(StaxUnmarshallerContext unmarshallerContext) throws Exception {
281 String shortString = unmarshallerContext.readText();
282 return (shortString == null) ? null : Short.valueOf(shortString);
283 }
284
285 private static final ShortJsonUnmarshaller instance = new ShortJsonUnmarshaller();
286
287 public static ShortJsonUnmarshaller getInstance() {
288 return instance;
289 }
290 }
291 }
292