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 hashtable field.
22 * @author Jawaid Hakim.
23 */
24 public abstract class RFldHashtable extends RFld
25 {
26 /***
27 * Default constructor.
28 */
29 public RFldHashtable()
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 protected RFldHashtable(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 protected RFldHashtable(String name, int fieldId, String desc)
56 {
57 super(name, fieldId, desc);
58 }
59
60 /***
61 * Write the field as XML to target writer.
62 * @param writer Output target.
63 * @param indent Indentation.
64 * @param newLines Newlines are inserted after each element if <tt>true</tt>.
65 * @param expandEmptyElements Empty elements - elements with no content - are expanded if <tt>true</tt>.
66 */
67 public void marshal(java.io.Writer writer, int indentLevel, String indent, boolean newLines, boolean expandEmptyElements) throws ConverterException
68 {
69 if (! isValSet())
70 throw new ConverterException("Field not set: " + getName());
71
72 marshal(writer, getTag(), getName(), getValueAsObject(), getType(), indentLevel, indent, newLines, expandEmptyElements);
73 }
74
75 /***
76 * Write the field as XML to target writer.
77 * @param writer Output target.
78 * @param indent Indentation.
79 * @param newLines Newlines are inserted after each element if <tt>true</tt>.
80 * @param expandEmptyElements Empty elements - elements with no content - are expanded if <tt>true</tt>.
81 */
82 final static void marshal(java.io.Writer writer, String tag, String name, Object value, RFldType type, int indentLevel, String indent, boolean newLines, boolean expandEmptyElements) throws ConverterException
83 {
84 try
85 {
86 java.util.Hashtable data = (java.util.Hashtable)value;
87
88 ConverterXML.openTag(writer, tag, indentLevel, indent);
89 ConverterXML.writeAttribute(writer, XML_ATTR_NAME, name);
90 ConverterXML.writeAttribute(writer, XML_ATTR_SIZE, String.valueOf(data.size()));
91 ConverterXML.closeStartTag(writer, newLines);
92
93 ++indentLevel;
94 if (type == RFldType.STRINGHASHTABLE)
95 {
96 for (java.util.Enumeration enum = data.keys(); enum.hasMoreElements(); )
97 {
98 String key = (String)enum.nextElement();
99 String nestedValue = (String)data.get(key);
100 marshal(writer, RFldString.XML_TAG, key, nestedValue, RFldType.STRING.toString(), indentLevel, indent, newLines, expandEmptyElements);
101 }
102 }
103 else if (type == RFldType.MSGOBJHASHTABLE)
104 {
105 for (java.util.Enumeration enum = data.keys(); enum.hasMoreElements(); )
106 {
107 String key = (String)enum.nextElement();
108 RMsg msgObj = (RMsg)data.get(key);
109 RFldMsgObj.marshal(msgObj, key, writer, indentLevel, indent, newLines, expandEmptyElements);
110 //ConverterXML.marshal(false, true, indentLevel + 1, msgObj.XML_TAG, msgObj, writer);
111 }
112 }
113 else
114 throw new ConverterException("Unsupported data type");
115
116 --indentLevel;
117 ConverterXML.closeTag(writer, tag, indentLevel, indent, newLines);
118 }
119 catch (java.io.IOException ex)
120 {
121 throw new ConverterException(ex);
122 }
123 }
124
125 /***
126 * Set state from a <tt>java.util.Hashtable</tt>.
127 * @param newData New value.
128 * @return Reference to self so method chaining can be used.
129 */
130 public abstract RFld set(java.util.Hashtable newData) throws FieldValidationException;
131
132 /***
133 * Delimiter for string array values.
134 */
135 static transient final String ELEMENT_DELIMITER = "|";
136
137 /***
138 * XML attribute name for hashtable size.
139 */
140 static transient final String XML_ATTR_SIZE = "size";
141 }
This page was automatically generated by Maven