// This element represents a property
if (element == null) {
throw new SerializationException("Root node must represent a typed object.");
}
BeanDictionary propertyDictionary = new BeanDictionary(element.value);
if (propertyDictionary.isReadOnly(localName)) {
Object value = propertyDictionary.get(localName);
assert (value != null) : "Read-only properties cannot be null.";
element = new Element(element, Element.Type.READ_ONLY_PROPERTY, attributes, value);
} else {
if (attributes.getLength() > 0) {
throw new SerializationException("Writable property elements cannot have attributes.");
}
element = new Element(element, Element.Type.WRITABLE_PROPERTY, null, null);
}
}
}
switch (element.type) {
case INCLUDE:
case INSTANCE: {
// If the element's parent is a sequence or a listener list, add
// the element value to it
if (element.parent != null) {
if (element.parent.value instanceof Sequence) {
Sequence<Object> sequence = (Sequence<Object>)element.parent.value;
sequence.add(element.value);
} else {
if (element.parent.value instanceof ListenerList) {
ListenerList<Object> listenerList = (ListenerList<Object>)element.parent.value;
listenerList.add(element.value);
}
}
}
// If an ID was specified, add the value to the named object map
if (id != null) {
if (id.length() == 0) {
throw new IllegalArgumentException(WTKX_PREFIX + ":" + ID_ATTRIBUTE
+ " must not be null.");
}
namedObjects.put(id, element.value);
if (scriptEngineManager != null) {
try {
Method putMethod = scriptEngineManagerClass.getMethod("put",
new Class<?>[] {String.class, Object.class});
putMethod.invoke(scriptEngineManager, new Object[] {id, namedObjects.get(id)});
} catch(Exception exception) {
throw new RuntimeException(exception);
}
}
}
break;
}
}
break;
}
case XMLStreamConstants.END_ELEMENT: {
String localName = reader.getLocalName();
switch (element.type) {
case WRITABLE_PROPERTY: {
BeanDictionary propertyDictionary = new BeanDictionary(element.parent.value);
propertyDictionary.put(localName, element.value);
break;
}
case SCRIPT: {
break;
}
default: {
if (element.value instanceof Dictionary) {
// The element is an untyped object
Dictionary<String, Object> dictionary = (Dictionary<String, Object>)element.value;
for (Attribute attribute : element.attributes) {
if (Character.isUpperCase(attribute.localName.charAt(0))) {
throw new SerializationException("Static setters are only supported for typed instances.");
}
// Resolve and apply the attribute
dictionary.put(attribute.localName, resolve(attribute.value, null));
}
} else {
// The element represents a typed object; apply the attributes
BeanDictionary valueDictionary = new BeanDictionary(element.value);
for (Attribute attribute : element.attributes) {
if (Character.isUpperCase(attribute.localName.charAt(0))) {
// The property represents an attached value
setStaticProperty(attribute, element.value);
} else {
Class<?> type = valueDictionary.getType(attribute.localName);
if (type != null
&& ListenerList.class.isAssignableFrom(type)) {
// The property represents a listener list
ListenerList<Object> listenerList = (ListenerList<Object>)valueDictionary.get(attribute.localName);
// The attribute value is a comma-separated list of listener IDs
String[] listenerIDs = attribute.value.split(",");
for (int i = 0, n = listenerIDs.length; i < n; i++) {
String listenerID = listenerIDs[i].trim();
if (listenerID.length() > 0) {
listenerID = listenerID.substring(1);
if (listenerID.length() > 0) {
listenerList.add(namedObjectDictionary.get(listenerID));
}
}
}
} else {
valueDictionary.put(attribute.localName,
resolve(attribute.value, valueDictionary.getType(attribute.localName)));
}
}
}
}