1 /***
2 * Copyright (c) 2002, CodeStreet LLC. All rights reserved.<p>
3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
4 * conditions are met:<p>
5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
7 * in the documentation and/or other materials provided with the distribution. Neither the name of CodeStreet LLC. nor the
8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written
9 * permission.<p>
10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p>
16 */
17
18 package com.codestreet.messageforge;
19
20 /***
21 * Class to represent a <tt>RMsg</tt> field.
22 * @author Jawaid Hakim.
23 */
24 public class RFldMsgObj extends RFld
25 {
26 /***
27 * Default constructor.
28 */
29 public RFldMsgObj()
30 {
31 }
32
33 /***
34 * Constructor.
35 * @param cls Class object of message object.
36 * @param name Field name.
37 * @param fieldId Field id. Field ids must be either <tt>0</tt>
38 * to indicate that there is no id on the field, or greater. In addition,
39 * field ids must be unique within a messages - no two fields are allowed
40 * to have the same field id.
41 */
42 public RFldMsgObj(Class cls, String name, int fieldId)
43 {
44 super(name, fieldId);
45 cls_ = cls;
46 }
47
48 /***
49 * Constructor.
50 * @param cls Class object of message object.
51 * @param name Field name.
52 * @param fieldId Field id. Field ids must be either <tt>0</tt>
53 * to indicate that there is no id on the field, or greater. In addition,
54 * field ids must be unique within a messages - no two fields are allowed
55 * to have the same field id.
56 * @param desc Field description.
57 */
58 public RFldMsgObj(Class cls, String name, int fieldId, String desc)
59 {
60 super(name, fieldId, desc);
61 cls_ = cls;
62 }
63
64 /***
65 * Get field type.
66 * @return Field type <tt>MSGOBJ</tt>.
67 * @see RFldType
68 */
69 public final RFldType getType()
70 {
71 return RFldType.MSGOBJ;
72 }
73
74 /***
75 * Get the <tt>Class</tt> of the data for this object.
76 * @return <tt>Class</tt> of the data for this object.
77 */
78 public Class getClassObject()
79 {
80 return cls_;
81 }
82
83 /***
84 * Check if another field is equal to this field. Equality is defined
85 * as the fields having the same string representation. A short cut
86 * that needs to be fixed for cases where the string representation is
87 * rounding off numbers etc.
88 * @param anObject Another field.
89 * @return <tt>true</tt> if another field is equal to this field.
90 * Otherwise, returns <tt>false</tt>.
91 */
92 public boolean equals(Object anObject)
93 {
94 if (this == anObject)
95 {
96 return true;
97 }
98 else if (anObject == null || ! (anObject instanceof RFldMsgObj))
99 {
100 return false;
101 }
102 RFldMsgObj another = (RFldMsgObj)anObject;
103 if (valSet_ && another.isValSet())
104 return getValue().equals(another.getValue());
105 else
106 return false;
107 }
108
109 /***
110 * Returns the hash code value for the field.
111 * @return Hash code value for the field.
112 */
113 public int hashCode()
114 {
115 return (valSet_) ? dataObj_.hashCode() : 1231;
116 }
117
118 /***
119 * Reset the field value.
120 * @see #isValSet()
121 */
122 public void reset() throws FieldValidationException
123 {
124 if (isLocked())
125 throw new FieldValidationException("Field " + getName() + " is locked");
126
127 if (valSet_)
128 dataObj_.resetFields();
129
130 valSet_ = false;
131 }
132
133 /***
134 * Set data.
135 * @param newData New data.
136 */
137 public RFld set(Object newData) throws FieldValidationException
138 {
139 if (newData instanceof RMsg)
140 return set((RMsg)newData);
141 else if (newData != null)
142 throw new FieldValidationException("Unsupported value type: " + newData.getClass().getName());
143 else
144 throw new FieldValidationException("New value is NULL for field: " + getName());
145 }
146
147 /***
148 * Set data.
149 * @param newData New data.
150 */
151
152 /*
153 public RFld set(TibrvMsg newData) throws FieldValidationException
154 {
155 if (isLocked())
156 throw new FieldValidationException("Cannot modify locked field: " + getName());
157
158 try
159 {
160 if (valSet_)
161 ConverterTibrv.unmarshal(newData, dataObj_);
162 else
163 dataObj_ = (RMsg)ConverterTibrv.unmarshal(newData, getClassObject());
164
165 valSet_ = true;
166
167 return this;
168 }
169 catch (ConverterException ex)
170 {
171 throw new FieldValidationException(ex);
172 }
173 }
174 */
175
176 /***
177 * Set data.
178 * @param newData New data.
179 */
180 public RFld set(RMsg newData) throws FieldValidationException
181 {
182 validate(newData);
183
184 dataObj_ = newData;
185 valSet_ = true;
186
187 return this;
188 }
189
190 /***
191 * Set data.
192 * @param elem New data.
193 */
194 public final RFld set(org.jdom.Element elem) throws FieldValidationException
195 {
196 if (isLocked())
197 throw new FieldValidationException("Cannot modify locked field: " + getName());
198
199 try
200 {
201 org.jdom.Element msgObj = elem.getChild(RMsg.XML_TAG);
202 if (msgObj != null)
203 {
204 dataObj_ = (RMsg)ConverterXML.unmarshal(msgObj);
205 valSet_ = true;
206 }
207 return this;
208 }
209 catch (ConverterException ex)
210 {
211 throw new FieldValidationException(ex);
212 }
213 }
214
215 /***
216 * Validate against constraints. A field is valid if either it's value is set
217 * and satisfies all constraints, or the the field is optional.
218 */
219 public void validate() throws FieldValidationException
220 {
221 // Only need to check that non-optional fields have been set. If a
222 // field has been set then it must be valid since validation is done
223 // with each set.
224 if (! valSet_ && ! optional_)
225 throw new FieldValidationException("Field not set: " + getName());
226 }
227
228 /***
229 * Check if a new value will satifsy constraints.
230 * @param newData New value.
231 */
232 public void validate(RMsg newData) throws FieldValidationException
233 {
234 if (locked_)
235 throw new FieldValidationException("Cannot modify locked field: " + getName());
236
237 if (newData == null)
238 throw new FieldValidationException("New value is NULL for field: " + getName());;
239
240 if (cls_ != null && ! cls_.isInstance(newData))
241 throw new FieldValidationException("Invalid data type. Expected " + cls_.getName() + " saw " + newData.getClass().getName());
242 }
243
244 /***
245 * Get data.
246 * @return data Data.
247 */
248 public RMsg getValue()
249 {
250 return (valSet_) ? dataObj_ : null;
251 }
252
253 /***
254 * Get the field value as an object.
255 * @return Field value as an object. Reference to data is passed out so be
256 * very careful about modifying the data.
257 */
258 public Object getValueAsObject()
259 {
260 return (valSet_) ? dataObj_ : null;
261 }
262
263 /***
264 * Get the field value as a string.
265 * @return Field value as a string. Returns <tt>null</tt>
266 * if the field value is not set.
267 */
268 public String getValueAsString()
269 {
270 if (! valSet_)
271 return null;
272
273 return dataObj_.toString();
274 }
275
276 /***
277 * Get the field value as a <tt>java.util.Hashtable</tt>.
278 * @return Field value as a <tt>java.util.Hashtable</tt>. If the
279 * value of the field has not been set then <tt>null</tt> is returned.
280 */
281 public java.util.Hashtable getValueAsHashtable() throws FieldValidationException
282 {
283 return null;
284 }
285
286 /***
287 * Write the field as XML to target writer.
288 * @param writer Output target.
289 * @param indent Indentation.
290 * @param newLines Newlines are inserted after each element if <tt>true</tt>.
291 * @param expandEmptyElements Empty elements - elements with no content - are expanded if <tt>true</tt>.
292 */
293 public void marshal(java.io.Writer writer, int indentLevel, String indent, boolean newLines, boolean expandEmptyElements) throws ConverterException
294 {
295 if (! isValSet())
296 throw new ConverterException("Field not set: " + getName());
297
298 marshal(dataObj_, getName(), writer, indentLevel, indent, newLines, expandEmptyElements);
299 }
300
301 /***
302 * Write specified data as XML to target writer.
303 * @param data Data.
304 * @param name Name.
305 * @param writer Output target.
306 * @param indent Indentation.
307 * @param newLines Newlines are inserted after each element if <tt>true</tt>.
308 * @param expandEmptyElements Empty elements - elements with no content - are expanded if <tt>true</tt>.
309 */
310 static void marshal(RMsg data, String name, java.io.Writer writer, int indentLevel, String indent, boolean newLines, boolean expandEmptyElements) throws ConverterException
311 {
312 try
313 {
314 // Write start tag opening
315 ConverterXML.openTag(writer, XML_TAG, indentLevel, indent);
316
317 // Write message name
318 ConverterXML.writeAttribute(writer, RMsg.XML_ATTR_NAME, name);
319
320 // Close start tag opening
321 ConverterXML.closeStartTag(writer, newLines);
322
323 ConverterXML.marshal(false, true, indentLevel + 1, RMsg.XML_TAG, data, writer);
324
325 // Write close tag
326 ConverterXML.closeTag(writer, XML_TAG, indentLevel, indent, newLines);
327 }
328 catch (java.io.IOException ex)
329 {
330 throw new ConverterException(ex);
331 }
332 }
333
334 /***
335 * Get the XML tag for this field type.
336 * @return XML tag for this field type.
337 */
338 public final String getTag()
339 {
340 return XML_TAG;
341 }
342
343 /***
344 * Set the XML tag for this field type.
345 * @param tag New XML tag for this field type.
346 */
347 public static void setTag(String tag)
348 {
349 XML_TAG = tag;
350 }
351
352 /***
353 * XML tag for this element type.
354 */
355 static transient String XML_TAG = "msgobj";
356
357 /***
358 * Data.
359 */
360 protected RMsg dataObj_;
361 protected Class cls_;
362 }
This page was automatically generated by Maven