}
return element;
}
private void setElementValue(Element element, Object value) {
CharacterData data = null;
Element prop = element;
if (value instanceof Collection) {
Iterator items = ((Collection) value).iterator();
while (items.hasNext()) {
Document valdoc = (Document) items.next();
NodeList list = valdoc.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node newNode = element.getOwnerDocument().importNode(list.item(i), true);
element.appendChild(newNode);
}
}
} else if (value instanceof Document) {
Document valdoc = (Document) value;
Node lastChild = valdoc.getLastChild();
NodeList list = lastChild.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node newNode = element.getOwnerDocument().importNode(list.item(i), true);
element.appendChild(newNode);
}
} else if (value instanceof Element) {
Node newNode = element.getOwnerDocument().importNode((Element) value, true);
element.appendChild(newNode);
} else {
// Find text child element
NodeList texts = prop.getChildNodes();
if (texts.getLength() == 1) {
Node child = texts.item(0);
if (child instanceof CharacterData) {
// Use existing text.
data = (CharacterData) child;
} else {
// Remove non-text, add text.
prop.removeChild(child);
Text text = prop.getOwnerDocument().createTextNode(String.valueOf(value));
prop.appendChild(text);
data = text;
}
} else if (texts.getLength() > 1) {
// Remove all, add text.
for (int i = texts.getLength() - 1; i >= 0; i--) {
prop.removeChild(texts.item(i));
}
Text text = prop.getOwnerDocument().createTextNode(String.valueOf(value));
prop.appendChild(text);
data = text;
} else {
// Add text.
Text text = prop.getOwnerDocument().createTextNode(String.valueOf(value));
prop.appendChild(text);
data = text;
}
data.setData(String.valueOf(value));
}
// Set type attribute
//prop.setAttribute("type", value == null ? "null" : value.getClass().getName());