in juneau-core/juneau-marshall/src/main/java/org/apache/juneau/html/HtmlSerializerSession.java [391:552]
protected ContentResult serializeAnything(XmlWriter out, Object o,
ClassMeta<?> eType, String name, BeanPropertyMeta pMeta, int xIndent, boolean isRoot, boolean nlIfElement) throws SerializeException {
ClassMeta<?> aType = null; // The actual type
ClassMeta<?> wType = null; // The wrapped type (delegate)
ClassMeta<?> sType = object(); // The serialized type
if (eType == null)
eType = object();
aType = push2(name, o, eType);
// Handle recursion
if (aType == null) {
o = null;
aType = object();
}
// Handle Optional<X>
if (isOptional(aType)) {
o = getOptionalValue(o);
eType = getOptionalType(eType);
aType = getClassMetaForObject(o, object());
}
indent += xIndent;
ContentResult cr = CR_ELEMENTS;
// Determine the type.
if (o == null || (aType.isChar() && ((Character)o).charValue() == 0)) {
out.tag("null");
cr = ContentResult.CR_MIXED;
} else {
if (aType.isDelegate()) {
wType = aType;
aType = ((Delegate)o).getClassMeta();
}
sType = aType;
String typeName = null;
if (isAddBeanTypes() && ! eType.equals(aType))
typeName = aType.getDictionaryName();
// Swap if necessary
ObjectSwap swap = aType.getSwap(this);
if (swap != null) {
o = swap(swap, o);
sType = swap.getSwapClassMeta(this);
// If the getSwapClass() method returns Object, we need to figure out
// the actual type now.
if (sType.isObject())
sType = getClassMetaForObject(o);
}
// Handle the case where we're serializing a raw stream.
if (sType.isReader() || sType.isInputStream()) {
pop();
indent -= xIndent;
if (sType.isReader())
pipe((Reader)o, out, SerializerSession::handleThrown);
else
pipe((InputStream)o, out, SerializerSession::handleThrown);
return ContentResult.CR_MIXED;
}
HtmlClassMeta cHtml = getHtmlClassMeta(sType);
HtmlBeanPropertyMeta bpHtml = getHtmlBeanPropertyMeta(pMeta);
HtmlRender render = firstNonNull(bpHtml.getRender(), cHtml.getRender());
if (render != null) {
Object o2 = render.getContent(this, o);
if (o2 != o) {
indent -= xIndent;
pop();
out.nl(indent);
return serializeAnything(out, o2, null, typeName, null, xIndent, false, false);
}
}
if (cHtml.isXml() || bpHtml.isXml()) {
pop();
indent++;
if (nlIfElement)
out.nl(0);
super.serializeAnything(out, o, null, null, null, null, false, XmlFormat.MIXED, false, false, null);
indent -= xIndent+1;
return cr;
} else if (cHtml.isPlainText() || bpHtml.isPlainText()) {
out.w(o == null ? "null" : o.toString());
cr = CR_MIXED;
} else if (o == null || (sType.isChar() && ((Character)o).charValue() == 0)) {
out.tag("null");
cr = CR_MIXED;
} else if (sType.isNumber()) {
if (eType.isNumber() && ! isRoot)
out.append(o);
else
out.sTag("number").append(o).eTag("number");
cr = CR_MIXED;
} else if (sType.isBoolean()) {
if (eType.isBoolean() && ! isRoot)
out.append(o);
else
out.sTag("boolean").append(o).eTag("boolean");
cr = CR_MIXED;
} else if (sType.isMap() || (wType != null && wType.isMap())) {
out.nlIf(! isRoot, xIndent+1);
if (o instanceof BeanMap)
serializeBeanMap(out, (BeanMap)o, eType, pMeta);
else
serializeMap(out, (Map)o, sType, eType.getKeyType(), eType.getValueType(), typeName, pMeta);
} else if (sType.isBean()) {
BeanMap m = toBeanMap(o);
if (aType.hasAnnotation(HtmlLink.class)) {
Value<String> uriProperty = Value.empty(), nameProperty = Value.empty();
aType.forEachAnnotation(HtmlLink.class, x -> isNotEmpty(x.uriProperty()), x -> uriProperty.set(x.uriProperty()));
aType.forEachAnnotation(HtmlLink.class, x -> isNotEmpty(x.nameProperty()), x -> nameProperty.set(x.nameProperty()));
Object urlProp = m.get(uriProperty.orElse(""));
Object nameProp = m.get(nameProperty.orElse(""));
out.oTag("a").attrUri("href", urlProp).w('>').text(nameProp).eTag("a");
cr = CR_MIXED;
} else {
out.nlIf(! isRoot, xIndent+2);
serializeBeanMap(out, m, eType, pMeta);
}
} else if (sType.isCollection() || sType.isArray() || (wType != null && wType.isCollection())) {
out.nlIf(! isRoot, xIndent+1);
serializeCollection(out, o, sType, eType, name, pMeta);
} else if (isUri(sType, pMeta, o)) {
String label = getAnchorText(pMeta, o);
out.oTag("a").attrUri("href", o).w('>');
out.text(label);
out.eTag("a");
cr = CR_MIXED;
} else {
if (isRoot)
out.sTag("string").text(toString(o)).eTag("string");
else
out.text(toString(o));
cr = CR_MIXED;
}
}
pop();
indent -= xIndent;
return cr;
}