1 package net.sourceforge.harness.xml.jaxb;
2
3 import net.sourceforge.harness.xml.conversion.ExceptionConverter;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7
8 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import javax.xml.bind.ConversionException;
13 import javax.xml.bind.Dispatcher;
14 import javax.xml.bind.DuplicateAttributeException;
15 import javax.xml.bind.Element;
16 import javax.xml.bind.InvalidAttributeException;
17 import javax.xml.bind.InvalidContentObjectException;
18 import javax.xml.bind.LocalValidationException;
19 import javax.xml.bind.MarshallableObject;
20 import javax.xml.bind.Marshaller;
21 import javax.xml.bind.MissingAttributeException;
22 import javax.xml.bind.MissingContentException;
23 import javax.xml.bind.PredicatedLists;
24 import javax.xml.bind.PredicatedLists.Predicate;
25 import javax.xml.bind.StructureValidationException;
26 import javax.xml.bind.UnmarshalException;
27 import javax.xml.bind.Unmarshaller;
28 import javax.xml.bind.ValidatableObject;
29 import javax.xml.bind.Validator;
30 import javax.xml.marshal.XMLScanner;
31 import javax.xml.marshal.XMLWriter;
32
33
34 public class Error extends MarshallableObject implements Element {
35 private List _ParamList = PredicatedLists.createInvalidating(this,
36 new ParamListPredicate(), new ArrayList());
37 private PredicatedLists.Predicate pred_ParamList = new ParamListPredicate();
38 private Exception _Exception;
39 private String _Message;
40
41 public List getParamList() {
42 return _ParamList;
43 }
44
45 public void deleteParamList() {
46 _ParamList = null;
47 invalidate();
48 }
49
50 public void emptyParamList() {
51 _ParamList = PredicatedLists.createInvalidating(this, pred_ParamList,
52 new ArrayList());
53 }
54
55 public Exception getException() {
56 return _Exception;
57 }
58
59 public void setException(Exception _Exception) {
60 this._Exception = _Exception;
61
62 if (_Exception == null) {
63 invalidate();
64 }
65 }
66
67 public String getMessage() {
68 return _Message;
69 }
70
71 public void setMessage(String _Message) {
72 this._Message = _Message;
73
74 if (_Message == null) {
75 invalidate();
76 }
77 }
78
79 public void validateThis() throws LocalValidationException {
80 if (_ParamList == null) {
81 throw new MissingContentException("paramList");
82 }
83
84 if (_Exception == null) {
85 throw new MissingAttributeException("exception");
86 }
87 }
88
89 public void validate(Validator v) throws StructureValidationException {
90 for (Iterator i = _ParamList.iterator(); i.hasNext();) {
91 v.validate(((ValidatableObject) i.next()));
92 }
93 }
94
95 public void marshal(Marshaller m) throws IOException {
96 XMLWriter w = m.writer();
97 w.start("error");
98 w.attribute("exception", ExceptionConverter.print(_Exception));
99
100 if (_Message != null) {
101 w.attribute("message", _Message.toString());
102 }
103
104 for (Iterator i = _ParamList.iterator(); i.hasNext();) {
105 m.marshal(((MarshallableObject) i.next()));
106 }
107
108 w.end("error");
109 }
110
111 public void unmarshal(Unmarshaller u) throws UnmarshalException {
112 XMLScanner xs = u.scanner();
113 Validator v = u.validator();
114 xs.takeStart("error");
115
116 while (xs.atAttribute()) {
117 String an = xs.takeAttributeName();
118
119 if (an.equals("exception")) {
120 if (_Exception != null) {
121 throw new DuplicateAttributeException(an);
122 }
123
124 try {
125 _Exception = ExceptionConverter.parse(xs.takeAttributeValue());
126 } catch (Exception x) {
127 throw new ConversionException(an, x);
128 }
129
130 continue;
131 }
132
133 if (an.equals("message")) {
134 if (_Message != null) {
135 throw new DuplicateAttributeException(an);
136 }
137
138 _Message = xs.takeAttributeValue();
139
140 continue;
141 }
142
143 throw new InvalidAttributeException(an);
144 }
145
146 {
147 List l = PredicatedLists.create(this, pred_ParamList,
148 new ArrayList());
149
150 while (xs.atStart("stackTrace") || xs.atStart("param")) {
151 l.add(((MarshallableObject) u.unmarshal()));
152 }
153
154 _ParamList = PredicatedLists.createInvalidating(this,
155 pred_ParamList, l);
156 }
157
158 xs.takeEnd("error");
159 }
160
161 public static Error unmarshal(InputStream in) throws UnmarshalException {
162 return unmarshal(XMLScanner.open(in));
163 }
164
165 public static Error unmarshal(XMLScanner xs) throws UnmarshalException {
166 return unmarshal(xs, newDispatcher());
167 }
168
169 public static Error unmarshal(XMLScanner xs, Dispatcher d)
170 throws UnmarshalException {
171 return ((Error) d.unmarshal(xs, (Error.class)));
172 }
173
174 public boolean equals(Object ob) {
175 if (this == ob) {
176 return true;
177 }
178
179 if (!(ob instanceof Error)) {
180 return false;
181 }
182
183 Error tob = ((Error) ob);
184
185 if (_ParamList != null) {
186 if (tob._ParamList == null) {
187 return false;
188 }
189
190 if (!_ParamList.equals(tob._ParamList)) {
191 return false;
192 }
193 } else {
194 if (tob._ParamList != null) {
195 return false;
196 }
197 }
198
199 if (_Exception != null) {
200 if (tob._Exception == null) {
201 return false;
202 }
203
204 if (!_Exception.equals(tob._Exception)) {
205 return false;
206 }
207 } else {
208 if (tob._Exception != null) {
209 return false;
210 }
211 }
212
213 if (_Message != null) {
214 if (tob._Message == null) {
215 return false;
216 }
217
218 if (!_Message.equals(tob._Message)) {
219 return false;
220 }
221 } else {
222 if (tob._Message != null) {
223 return false;
224 }
225 }
226
227 return true;
228 }
229
230 public int hashCode() {
231 int h = 0;
232 h = ((127 * h) + ((_ParamList != null) ? _ParamList.hashCode() : 0));
233 h = ((127 * h) + ((_Exception != null) ? _Exception.hashCode() : 0));
234 h = ((127 * h) + ((_Message != null) ? _Message.hashCode() : 0));
235
236 return h;
237 }
238
239 public String toString() {
240 StringBuffer sb = new StringBuffer("<<error");
241
242 if (_ParamList != null) {
243 sb.append(" paramList=");
244 sb.append(_ParamList.toString());
245 }
246
247 if (_Exception != null) {
248 sb.append(" exception=");
249 sb.append(ExceptionConverter.print(_Exception));
250 }
251
252 if (_Message != null) {
253 sb.append(" message=");
254 sb.append(_Message.toString());
255 }
256
257 sb.append(">>");
258
259 return sb.toString();
260 }
261
262 public static Dispatcher newDispatcher() {
263 return Author.newDispatcher();
264 }
265
266 private static class ParamListPredicate implements PredicatedLists.Predicate {
267 public void check(Object ob) {
268 if (!(ob instanceof MarshallableObject)) {
269 throw new InvalidContentObjectException(ob,
270 (MarshallableObject.class));
271 }
272 }
273 }
274 }