}
sb.append(encode);
encodedEntities++;
}
} catch (RuntimeException e) {
throw new EntityEncodingException("Failure during internal output encoding of entity set on entity: " + ref, ref.toString(), e);
}
}
if (!("discover".equals(ref.getPrefix())||"statistics".equals(ref.getPrefix()))) {
// make footer
if (Formats.HTML.equals(format)
|| Formats.FORM.equals(format)) {
sb.append("\n<b>Collection size:</b> "+encodedEntities+"\n");
} else if (Formats.JSON.equals(format)) {
sb.append("\n]}");
} else if (Formats.XML.equals(format)) {
sb.append("</"+ ref.getPrefix() + ">");
} else { // general case
sb.append("\nSize: " + encodedEntities + "\n");
}
}
encoded = sb.toString();
} else {
// encoding a single entity
EntityData toEncode = entities.get(0);
if (toEncode == null) {
throw new EntityEncodingException("Failed to encode data for entity (" + ref
+ "), entity object to encode could not be found (null object in list)", ref.toString());
} else {
try {
String prefix = ref.getPrefix();
String segments[] = {};
if (params.get("pathInfo") != null) {
segments = params.get("pathInfo").toString().split("/");
}
if (segments.length > 3) {
prefix = segments[segments.length - 1].substring(0, segments[segments.length - 1].lastIndexOf("."));
}else if (segments[segments.length - 1].startsWith("count")) {
prefix = "count";
}
encoded = encodeEntity(prefix, format, toEncode, view);
} catch (RuntimeException e) {
throw new EntityEncodingException("Failure during internal output encoding of entity: " + ref, ref.toString(), e);
}
}
}
// add the HTML headers and footers
if (Formats.FORM.equals(format)) {
String title = view.getViewKey() + ":" + ref;
encoded = XML_HEADER + XHTML_HEADER.replace("{title}", title) + encoded + XHTML_FOOTER;
} else if (Formats.XML.equals(format)) {
if (!("discover".equals(ref.getPrefix())||"statistics".equals(ref.getPrefix()))) {
encoded = XML_HEADER + encoded;
}
}
// put the encoded data into the stream
try {
byte[] b = encoded.getBytes(Formats.UTF_8);
output.write(b);
} catch (UnsupportedEncodingException e) {
throw new EntityEncodingException("Failed to encode UTF-8: " + ref, ref.toString(), e);
} catch (IOException e) {
throw new EntityEncodingException("Failed to encode into output stream: " + ref, ref.toString(), e);
}
}