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