ArrayList<XMLFragment> fieldElements = new ArrayList<XMLFragment>();
List<Attribute> fieldAttributes = new ArrayList<Attribute>();
if (field.getVar() != null) {
fieldAttributes.add(new Attribute("var", field.getVar()));
}
if (field.getLabel() != null) {
fieldAttributes.add(new Attribute("label", field.getLabel()));
}
if (field.getType() != null) {
fieldAttributes.add(new Attribute("type", field.getType().value()));
}
if (field.getDesc() != null) {
ArrayList<XMLFragment> descFragment = new ArrayList<XMLFragment>();
descFragment.add(new XMLText(field.getDesc()));
fieldElements.add(new XMLElement(NamespaceURIs.JABBER_X_DATA, "desc", null, null, descFragment));
}
if (field.isRequired()) {
fieldElements.add(createEmptyElement(NamespaceURIs.JABBER_X_DATA, "required"));
}
Iterator<String> valueIterator = field.getValueIterator();
while (valueIterator.hasNext()) {
String value = valueIterator.next();
XMLElement valueElement = createTextOnlyElement(NamespaceURIs.JABBER_X_DATA, "value", value);
fieldElements.add(valueElement);
}
Iterator<Option> optionIterator = field.getOptions();
while (optionIterator.hasNext()) {
Option option = optionIterator.next();
Attribute[] attributes = option.getLabel() == null ? null : new Attribute[] { new Attribute("label", option
.getLabel()) };
XMLFragment[] elements = option.getValue() == null ? null : new XMLFragment[] { new XMLText(option
.getValue()) };
XMLElement optionElement = new XMLElement(NamespaceURIs.JABBER_X_DATA, "option", null, attributes, elements);