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>String</tt> field. This <tt>String</tt> should typically be a XML string.
22 * @author Jawaid Hakim.
23 */
24 public class RFldXml extends RFld
25 {
26 /***
27 * Constructor.
28 * @param name Field name.
29 * @param fieldId Field id. Field ids must be either <tt>0</tt>
30 * to indicate that there is no id on the field, or greater. In addition,
31 * field ids must be unique within a messages - no two fields are allowed
32 * to have the same field id.
33 */
34 public RFldXml(String name, int fieldId)
35 {
36 super(name, fieldId);
37 }
38
39 /***
40 * Constructor.
41 * @param name Field name.
42 * @param fieldId Field id. Field ids must be either <tt>0</tt>
43 * to indicate that there is no id on the field, or greater. In addition,
44 * field ids must be unique within a messages - no two fields are allowed
45 * to have the same field id.
46 * @param desc Field description.
47 */
48 public RFldXml(String name, int fieldId, String desc)
49 {
50 super(name, fieldId, desc);
51 }
52
53 /***
54 * Get field type.
55 * @return Field type <tt>XML</tt>.
56 * @see RFldType
57 */
58 public final RFldType getType()
59 {
60 return RFldType.XML;
61 }
62
63 /***
64 * Check if another field is equal to this field. Equality is defined
65 * as the fields having the same value. The bytes of the xml message
66 * are compared.
67 * @param anObject Another field.
68 * @return <tt>true</tt> if another field is equal to this field.
69 * Otherwise, returns <tt>false</tt>.
70 */
71 public boolean equals(Object anObject)
72 {
73 if (this == anObject)
74 {
75 return true;
76 }
77 else if (! (anObject instanceof RFldXml))
78 {
79 return false;
80 }
81
82 RFldXml another = (RFldXml)anObject;
83 if (valSet_ != another.isValSet())
84 return false;
85 else
86 return (! valSet_ || RArrayCompare.equals(dataObj_.getBytes(), another.getValue().getBytes()));
87 }
88
89 /***
90 * Returns the hash code value for the field array. The hash code of
91 * the array is the sum of the hash code (byte values) for each entry in the
92 * array, the byte in the array are cast to an int.
93 *
94 * @return a value of type 'int'
95 */
96 public int hashCode()
97 {
98 int h = 0;
99 if (valSet_)
100 {
101 byte [] thisData = dataObj_.getBytes();
102 for (int i = thisData.length - 1; i >= 0; --i)
103 h += thisData[i];
104 }
105 return h;
106 }
107
108 /***
109 * Reset the field value.
110 * @see #isValSet()
111 */
112 public void reset() throws FieldValidationException
113 {
114 if (isLocked())
115 throw new FieldValidationException("Field " + this.getName() + " is locked");
116
117 valSet_ = false;
118 dataObj_ = null;
119 }
120
121 /***
122 * Set data. The <tt>String</tt> must be Base64 encoded.
123 * @param newData New data.
124 * @see RBase64
125 */
126 public RFld set(String newData) throws FieldValidationException
127 {
128 validate(newData);
129 dataObj_ = newData;
130 valSet_ = true;
131 return this;
132 }
133
134 /***
135 * Set data.
136 * @param newData New data.
137 */
138 public RFld set(Object newData) throws FieldValidationException
139 {
140 if (newData instanceof String)
141 return set((String)newData);
142 else
143 throw new FieldValidationException("Invalid object type");
144 }
145
146 /***
147 * Set data.
148 * @param newData New data.
149 */
150 public RFld set(byte[] newData) throws FieldValidationException
151 {
152 return set(RFldOpaque.encode(newData));
153 }
154
155 /***
156 * Set the field value from a JDOM element.
157 * @param elem Field value as a JDOM element.
158 */
159 public final RFld set(org.jdom.Element elem) throws FieldValidationException
160 {
161 return set(elem.getAttributeValue(XML_ATTR_VALUE));
162 }
163
164 /***
165 * Validate against constraints. A field is valid if either it's value is set
166 * and satisfies all constraints, or the the field is optional.
167 */
168 public void validate() throws FieldValidationException
169 {
170 // Only need to check that non-optional fields have been set. If a
171 // field has been set then it must be valid since validation is done
172 // with each set.
173 if (! valSet_ && ! optional_)
174 throw new FieldValidationException("Field not set: " + this.getName());
175 }
176
177 /***
178 * Check if a new value will satifsy constraints.
179 * @param newData New value.
180 */
181 public void validate(String newData) throws FieldValidationException
182 {
183 if (locked_)
184 throw new FieldValidationException("Cannot modify locked field: " + getName());
185
186 if (newData == null)
187 throw new FieldValidationException("New value is NULL: " + this.getName());
188 }
189
190 /***
191 * Get data.
192 * @return data Data. Returns <tt>null</tt> if the field value is not set.
193 */
194 public String getValue()
195 {
196 return dataObj_;
197 }
198
199 /***
200 * Get the field value as an object.
201 * @return Field value as an object. Returns <tt>null</tt> if the field value is not set.
202 */
203 public Object getValueAsObject()
204 {
205 return dataObj_;
206 }
207
208 /***
209 * Get the field value as a Base64 encoded <soce>String</tt>.
210 * @return Field value as a Base64 encoded <soce>String</tt>.
211 * Returns <tt>null</tt> if the field value is not set.
212 * @see RBase64
213 */
214 public String getValueAsString()
215 {
216 return (valSet_) ? dataObj_ : null;
217 }
218
219 /***
220 * Get the field value as a <tt>java.util.Hashtable</tt>. Not supported - throws
221 * an exception.
222 * @return Field value as java.util.Hashtable. Returns <tt>null</tt> if the field
223 * value is not set.
224 */
225 public java.util.Hashtable getValueAsHashtable() throws FieldValidationException
226 {
227 throw new FieldValidationException("Not supported");
228 }
229
230 /***
231 * Get the XML tag for this field type.
232 * @return XML tag for this field type.
233 */
234 public final String getTag()
235 {
236 return XML_TAG;
237 }
238
239 /***
240 * Set the XML tag for this field type.
241 * @param tag New XML tag for this field type.
242 */
243 public static void setTag(String tag)
244 {
245 XML_TAG = tag;
246 }
247
248 /***
249 * XML tag for this element type.
250 */
251 protected static transient String XML_TAG = "xml";
252
253 /***
254 * Data.
255 */
256 protected String dataObj_;
257 }
This page was automatically generated by Maven