Source code: edu/ou/kmi/buddyspace/xml/XDataField.java
1 /*
2 * License
3 *
4 * The contents of this file are subject to the Jabber Open Source License
5 * Version 1.0 (the "License"). You may not copy or use this file, in either
6 * source code or executable form, except in compliance with the License. You
7 * may obtain a copy of the License at http://www.jabber.com/license/ or at
8 * http://www.opensource.org/.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * Copyrights
16 *
17 * Portions created by or assigned to Jabber.com, Inc. are
18 * Copyright (c) 2000 Jabber.com, Inc. All Rights Reserved. Contact
19 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
20 *
21 * Portions Copyright (c) 1999-2000 David Waite
22 *
23 * Acknowledgements
24 *
25 * Special thanks to the Jabber Open Source Contributors for their
26 * suggestions and support of Jabber.
27 *
28 * Changes
29 *
30 * @author David Waite <a href="mailto:dwaite@jabber.com">
31 * <i><dwaite@jabber.com></i></a>
32 *
33 * @author $Author: brouk $
34 * @version $Revision: 1.2 $
35 *
36 * @author Jiri Komzak <a href="mailto:j.komzak@open.ac.uk">
37 * <i><j.komzak@open.ac.uk></i></a>
38 * j.komzak
39 * Changed into XDataField
40 */
41
42 package edu.ou.kmi.buddyspace.xml;
43
44 /*
45 * XDataField.java
46 *
47 * Project: BuddySpace
48 * (C) Copyright Knowledge Media Institute 2003
49 *
50 *
51 * Created on 5 February 2003, 12:43
52 */
53
54 import java.util.Vector;
55 import java.util.Enumeration;
56 import org.jabber.jabberbeans.*;
57 import org.jabber.jabberbeans.Extension.*;
58
59 /**
60 * <code>XDataField</code> contains <field> tag from jabber:x:data.
61 *
62 * @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
63 */
64 public class XDataField
65 extends XMLData
66 implements MessageExtension, QueryExtension
67 {
68 /** Vector of values fields contained. */
69 private Vector valueList;
70
71 /** Vector of option tags contained. */
72 private Vector optionList;
73
74 /** Attributes. */
75 private String label;
76 private String type;
77 private String var;
78 private String desc;
79 private boolean required;
80
81 /**
82 * Creates a new <code>XDataField</code> instance, based on the builder
83 * state.
84 *
85 * @param builder an <code>XDataFieldBuilder</code> value
86 * @exception InstantiationException if malformed or insufficient data is
87 * in the builder.
88 */
89 public XDataField(XDataFieldBuilder builder)
90 throws InstantiationException
91 {
92 optionList = (Vector) builder.getOptions().clone();
93 valueList = (Vector) builder.getValues().clone();
94
95 type = builder.getType();
96 label = builder.getLabel();
97 var = builder.getVar();
98 desc = builder.getDesc();
99
100 required = builder.getRequired();
101 }
102
103 public String getLabel() {
104 return label;
105 }
106
107 public String getType() {
108 return type;
109 }
110
111 public String getVar() {
112 return var;
113 }
114
115 public String getDesc() {
116 return desc;
117 }
118
119 public boolean getRequired() {
120 return required;
121 }
122
123 /**
124 * <code>getOptions</code> returns the enumeration representing the option
125 * objects associated with this object.
126 *
127 * @return a <code>Enumeration</code> value
128 */
129 public Enumeration options()
130 { return optionList.elements(); }
131
132 /**
133 * <code>getOptions</code> returns the enumeration representing the value
134 * objects associated with this object.
135 *
136 * @return a <code>Enumeration</code> value
137 */
138 public Enumeration values()
139 { return valueList.elements(); }
140
141
142 /**
143 * <code>appendItem</code> converts this packet to XML.
144 * It then appends it to a StringBuffer.
145 *
146 * @param retval The <code>StringBuffer</code> to append to
147 */
148 public void appendItem(StringBuffer retval)
149 {
150 retval.append("<field"
151 + ((type != null)? " type=\"" + type + "\"" : "")
152 + ((label != null)? " label=\"" + label + "\"" : "")
153 + ((var != null)? " var=\"" + var + "\"" : ""));
154
155 boolean emptyTag = true;
156
157 if (required) {
158 retval.append(">\n<required/>\n");
159 emptyTag = false;
160 }
161
162 if (desc != null) {
163 if (emptyTag)
164 retval.append(">\n");
165 retval.append("<desc>" + desc + "</desc>\n");
166 emptyTag = false;
167 }
168
169 Enumeration items = values();
170 if (items.hasMoreElements() && emptyTag) {
171 retval.append(">\n");
172 emptyTag = false;
173 }
174 while (items.hasMoreElements()) {
175 String val = (String)items.nextElement();
176 if ("".equals(val))
177 retval.append("<value/>\n");
178 else
179 retval.append("<value>" + val + "</value>\n");
180 }
181
182 items = options();
183 if (items.hasMoreElements() && emptyTag) {
184 retval.append(">\n");
185 emptyTag = false;
186 }
187 while (items.hasMoreElements())
188 ((XDataFieldOption)items.nextElement()).appendItem(retval);
189
190 if (emptyTag)
191 retval.append("/>\n");
192 else
193 retval.append("</field>\n");
194 }
195 }