1
2
3
4
5
6
7
8
9
10
11 package com.legstar.coxb.impl;
12
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.sql.Timestamp;
16 import java.util.Date;
17
18 import com.legstar.coxb.CobolElement;
19 import com.legstar.coxb.ICobolComplexBinding;
20 import com.legstar.coxb.common.CBinding;
21 import com.legstar.coxb.host.HostException;
22
23
24
25
26
27 public abstract class AbstractAlphaNumericBinding extends CBinding {
28
29
30 private String mValue = null;
31
32
33
34
35
36
37
38
39
40
41 public AbstractAlphaNumericBinding(final String bindingName,
42 final String jaxbName, final Class < ? > jaxbType,
43 final CobolElement cobolAnnotations,
44 final ICobolComplexBinding parentBinding) {
45 super(bindingName, jaxbName, jaxbType, cobolAnnotations, parentBinding);
46 }
47
48
49 public String getStringValue() throws HostException {
50 return mValue;
51 }
52
53
54 public void setStringValue(final String value) throws HostException {
55 mValue = value;
56 }
57
58
59 public Object getObjectValue(final Class < ? > type) throws HostException {
60 if (type.equals(String.class)) {
61 return mValue;
62 } else if (type.equals(Date.class)) {
63 return new Date(Timestamp.valueOf(mValue).getTime());
64 } else if (type.isEnum()) {
65
66
67
68
69 try {
70 Method fromValue = getFromValueMethod(type);
71 return fromValue.invoke(null, mValue.trim());
72 } catch (SecurityException e) {
73 throw new HostException(e);
74 } catch (IllegalArgumentException e) {
75 throw new HostException(e);
76 } catch (IllegalAccessException e) {
77 throw new HostException(e);
78 } catch (InvocationTargetException e) {
79 throw new HostException(e);
80 }
81
82 } else {
83 throw new HostException("Attempt to get binding "
84 + getBindingName() + " as an incompatible type " + type);
85 }
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99 protected Method getFromValueMethod(Class < ? > type) throws HostException {
100 try {
101 Method fromValueMethod = null;
102 try {
103 fromValueMethod = type.getMethod("fromValue", String.class);
104 } catch (NoSuchMethodException e) {
105 fromValueMethod = type.getMethod("valueOf", String.class);
106 }
107 return fromValueMethod;
108 } catch (SecurityException e) {
109 throw new HostException(e);
110 } catch (NoSuchMethodException e) {
111 throw new HostException(e);
112 }
113 }
114
115
116 public void setObjectValue(final Object value) throws HostException {
117 if (value == null) {
118 mValue = null;
119 return;
120 }
121 if (value instanceof String) {
122 mValue = (String) value;
123 } else if (value instanceof Date) {
124 mValue = new Timestamp(((Date) value).getTime()).toString();
125 } else if (value instanceof Enum < ? >) {
126 try {
127 Method valueMethod = getValueMethod(value.getClass());
128 mValue = (String) valueMethod.invoke(value);
129 } catch (SecurityException e) {
130 throw new HostException(e);
131 } catch (IllegalArgumentException e) {
132 throw new HostException(e);
133 } catch (IllegalAccessException e) {
134 throw new HostException(e);
135 } catch (InvocationTargetException e) {
136 throw new HostException(e);
137 }
138 } else {
139 throw new HostException("Attempt to set binding "
140 + getBindingName() + " from an incompatible value " + value);
141 }
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155 protected Method getValueMethod(Class < ? > type) throws HostException {
156 try {
157 Method valueMethod = null;
158 try {
159 valueMethod = type.getMethod("value");
160 } catch (NoSuchMethodException e) {
161 valueMethod = type.getMethod("toString");
162 }
163 return valueMethod;
164 } catch (SecurityException e) {
165 throw new HostException(e);
166 } catch (NoSuchMethodException e) {
167 throw new HostException(e);
168 }
169 }
170
171
172 public boolean isSet() {
173 return (mValue != null);
174 }
175 }