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 import java.util.Date;
21
22 /***
23 * Class to represent an array field.
24 * @author Jawaid Hakim.
25 */
26 public abstract class RFldArray extends RFld
27 {
28 /***
29 * Default constructor.
30 */
31 protected RFldArray()
32 {
33 }
34
35 /***
36 * Constructor.
37 * @param name Field name.
38 * @param fieldId Field id. Field ids must be either <tt>0</tt>
39 * to indicate that there is no id on the field, or greater. In addition,
40 * field ids must be unique within a messages - no two fields are allowed
41 * to have the same field id.
42 */
43 protected RFldArray(String name, int fieldId)
44 {
45 super(name, fieldId);
46 }
47
48 /***
49 * Constructor.
50 * @param name Field name.
51 * @param fieldId Field id. Field ids must be either <tt>0</tt>
52 * to indicate that there is no id on the field, or greater. In addition,
53 * field ids must be unique within a messages - no two fields are allowed
54 * to have the same field id.
55 * @param desc Field description.
56 */
57 protected RFldArray(String name, int fieldId, String desc)
58 {
59 super(name, fieldId, desc);
60 }
61
62 /***
63 * Write the field as XML to target writer.
64 * @param writer Output target.
65 * @param indent Indentation.
66 * @param newLines Newlines are inserted after each element if <tt>true</tt>.
67 * @param expandEmptyElements Empty elements - elements with no content - are expanded if <tt>true</tt>.
68 */
69 public void marshal(java.io.Writer writer, int indentLevel, String indent, boolean newLines, boolean expandEmptyElements) throws ConverterException
70 {
71 if (! isValSet())
72 throw new ConverterException("Field not set: " + getName());
73
74 marshal(writer, getTag(), getName(), getValueAsObject(), getType().toString(), indentLevel, indent, newLines, expandEmptyElements);
75 }
76
77 /***
78 * Write the field as XML to target writer.
79 * @param writer Output target.
80 * @param tag XML tag.
81 * @param name Field name.
82 * @param val Field value.
83 * @param type Field type.
84 * @param indentLevel Indentation level.
85 * @param indent Indentation <tt>String</tt>.
86 * @param newLines Newlines are inserted after each element if <tt>true</tt>.
87 * @param expandEmptyElements Empty elements - elements with no content - are expanded if <tt>true</tt>.
88 */
89 static void marshal(java.io.Writer writer, String tag, String name, Object val, String type, int indentLevel, String indent, boolean newLines, boolean expandEmptyElements) throws ConverterException
90 {
91 try
92 {
93 ConverterXML.openTag(writer, tag, indentLevel, indent);
94 ConverterXML.writeAttribute(writer, XML_ATTR_NAME, name);
95
96 ++indentLevel;
97 if (val instanceof int[])
98 {
99 int[] elems = (int[])val;
100 ConverterXML.writeAttribute(writer, XML_ATTR_LEN, String.valueOf(elems.length));
101 ConverterXML.closeStartTag(writer, newLines);
102 for (int i = elems.length - 1; i >= 0; --i)
103 {
104 RFld.marshal(writer, RFldI32.XML_TAG, String.valueOf(i), String.valueOf(elems[i]), RFldType.I32.toString(), indentLevel, indent, newLines, expandEmptyElements);
105 }
106 }
107 else if (val instanceof long[])
108 {
109 long[] elems = (long[])val;
110 ConverterXML.writeAttribute(writer, XML_ATTR_LEN, String.valueOf(elems.length));
111 ConverterXML.closeStartTag(writer, newLines);
112 for (int i = elems.length - 1; i >= 0; --i)
113 {
114 RFld.marshal(writer, RFldI64.XML_TAG, String.valueOf(i), String.valueOf(elems[i]), RFldType.I64.toString(), indentLevel, indent, newLines, expandEmptyElements);
115 }
116 }
117 else if (val instanceof double[])
118 {
119 double[] elems = (double[])val;
120 ConverterXML.writeAttribute(writer, XML_ATTR_LEN, String.valueOf(elems.length));
121 ConverterXML.closeStartTag(writer, newLines);
122 for (int i = elems.length - 1; i >= 0; --i)
123 {
124 RFld.marshal(writer, RFldF64.XML_TAG, String.valueOf(i), String.valueOf(elems[i]), RFldType.F64.toString(), indentLevel, indent, newLines, expandEmptyElements);
125 }
126 }
127 else if (val instanceof float[])
128 {
129 float[] elems = (float[])val;
130 ConverterXML.writeAttribute(writer, XML_ATTR_LEN, String.valueOf(elems.length));
131 ConverterXML.closeStartTag(writer, newLines);
132 for (int i = elems.length - 1; i >= 0; --i)
133 {
134 RFld.marshal(writer, RFldF32.XML_TAG, String.valueOf(i), String.valueOf(elems[i]), RFldType.F32.toString(), indentLevel, indent, newLines, expandEmptyElements);
135 }
136 }
137 else if (val instanceof short[])
138 {
139 short[] elems = (short[])val;
140 ConverterXML.writeAttribute(writer, XML_ATTR_LEN, String.valueOf(elems.length));
141 ConverterXML.closeStartTag(writer, newLines);
142 for (int i = elems.length - 1; i >= 0; --i)
143 {
144 RFld.marshal(writer, RFldI16.XML_TAG, String.valueOf(i), String.valueOf(elems[i]), RFldType.I16.toString(), indentLevel + 1, indent, newLines, expandEmptyElements);
145 }
146 }
147 else if (val instanceof String[])
148 {
149 String[] elems = (String[])val;
150 ConverterXML.writeAttribute(writer, XML_ATTR_LEN, String.valueOf(elems.length));
151 ConverterXML.closeStartTag(writer, newLines);
152 for (int i = elems.length - 1; i >= 0; --i)
153 {
154 RFld.marshal(writer, RFldString.XML_TAG, String.valueOf(i), elems[i], RFldType.STRING.toString(), indentLevel, indent, newLines, expandEmptyElements);
155 }
156 }
157 else if (val instanceof Date[])
158 {
159 Date[] elems = (Date[])val;
160 ConverterXML.writeAttribute(writer, XML_ATTR_LEN, String.valueOf(elems.length));
161 ConverterXML.closeStartTag(writer, newLines);
162 for (int i = elems.length - 1; i >= 0; --i)
163 {
164 String sDate = RDateFormat.getInstance().format(elems[i]);
165 RFld.marshal(writer, RFldDatetime.XML_TAG, String.valueOf(i), sDate, RFldType.DATETIME.toString(), indentLevel, indent, newLines, expandEmptyElements);
166 }
167 }
168 else if (val instanceof byte[])
169 {
170 byte[] elems = (byte[])val;
171 ConverterXML.writeAttribute(writer, XML_ATTR_LEN, String.valueOf(elems.length));
172 ConverterXML.closeStartTag(writer, newLines);
173 for (int i = elems.length - 1; i >= 0; --i)
174 {
175 RFld.marshal(writer, RFldI8.XML_TAG, String.valueOf(i), String.valueOf(elems[i]), RFldType.I8.toString(), indentLevel + 1, indent, newLines, expandEmptyElements);
176 }
177 }
178 else if (val instanceof RMsg[])
179 {
180 RMsg[] elems = (RMsg[])val;
181 ConverterXML.writeAttribute(writer, XML_ATTR_LEN, String.valueOf(elems.length));
182 ConverterXML.closeStartTag(writer, newLines);
183 for (int i = elems.length - 1; i >= 0; --i)
184 {
185 RFldMsgObj.marshal(elems[i], String.valueOf(i), writer, indentLevel, indent, newLines, expandEmptyElements);
186 }
187 }
188 else
189 {
190 ConverterXML.writeAttribute(writer, XML_ATTR_LEN, "0");
191 ConverterXML.closeStartTag(writer, newLines);
192 }
193 --indentLevel;
194 ConverterXML.closeTag(writer, tag, indentLevel, indent, newLines);
195 }
196 catch (java.io.IOException ex)
197 {
198 throw new ConverterException(ex);
199 }
200 }
201
202 /***
203 * XML attribute name for key.
204 */
205 static transient final String XML_ATTR_KEY = "key";
206
207 /***
208 * XML attribute name for array length.
209 */
210 static transient final String XML_ATTR_LEN = "len";
211
212 /***
213 * Delimiter for string array values.
214 */
215 static final String ELEMENT_DELIMITER = "|";
216 }
This page was automatically generated by Maven