use of com.revolsys.ui.html.serializer.type.TypeSerializer in project com.revolsys.open by revolsys.
the class HtmlUiBuilder method serialize.
/**
* Serialize the value represented by the key from the object.
*
* @param out The XML writer to serialize to.
* @param object The object to get the value from.
* @param key The key to serialize.
* @throws IOException If there was an I/O error serializing the value.
*/
public void serialize(final XmlWriter out, final Object object, final String key) {
if (object == null) {
serializeNullLabel(out, key);
} else {
final Object serializer = this.keySerializers.get(key);
if (serializer == null) {
String path = null;
String valueKey = key;
final Matcher linkMatcher = LINK_KEY_PATTERN.matcher(key);
if (linkMatcher.matches()) {
path = linkMatcher.group(1);
valueKey = linkMatcher.group(2);
}
HtmlUiBuilder<? extends Object> uiBuilder = this;
final String[] parts = valueKey.split("\\.");
Object currentObject = object;
for (int i = 0; i < parts.length - 1; i++) {
final String keyName = parts[i];
try {
currentObject = getProperty(currentObject, keyName);
if (currentObject == null) {
serializeNullLabel(out, keyName);
return;
}
uiBuilder = getBuilder(currentObject);
} catch (final IllegalArgumentException e) {
final String message = currentObject.getClass().getName() + " does not have a property " + keyName;
this.log.error(e.getMessage(), e);
out.element(HtmlElem.B, message);
return;
}
}
final String lastKey = parts[parts.length - 1];
if (path == null) {
if (uiBuilder == this) {
try {
final Object value = getProperty(currentObject, lastKey);
if (value == null) {
serializeNullLabel(out, lastKey);
return;
} else {
final TypeSerializer typeSerializer = this.classSerializers.get(value.getClass());
if (typeSerializer == null) {
final String stringValue;
if (value instanceof Collection) {
final Collection<?> collection = (Collection<?>) value;
stringValue = Strings.toString(", ", collection);
} else {
stringValue = value.toString();
}
if (stringValue.length() > 0) {
out.text(stringValue);
return;
}
} else {
typeSerializer.serialize(out, value);
return;
}
}
} catch (final IllegalArgumentException e) {
final String message = currentObject.getClass().getName() + " does not have a property " + key;
this.log.error(e.getMessage(), e);
out.element(HtmlElem.B, message);
return;
}
} else {
uiBuilder.serialize(out, currentObject, lastKey);
}
} else {
uiBuilder.serializeLink(out, currentObject, lastKey, path);
}
} else {
if (serializer instanceof TypeSerializer) {
final TypeSerializer typeSerializer = (TypeSerializer) serializer;
typeSerializer.serialize(out, object);
return;
} else if (serializer instanceof KeySerializer) {
final KeySerializer keySerializer = (KeySerializer) serializer;
keySerializer.serialize(out, object);
return;
}
}
}
}
Aggregations