/
XmlRenderer.java
112 lines (95 loc) · 4.31 KB
/
XmlRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.theoryinpractise.halbuilder.impl.xml;
import com.google.common.base.Optional;
import com.theoryinpractise.halbuilder.spi.Link;
import com.theoryinpractise.halbuilder.spi.ReadableResource;
import com.theoryinpractise.halbuilder.spi.Renderer;
import com.theoryinpractise.halbuilder.spi.Resource;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.Text;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import static com.theoryinpractise.halbuilder.impl.api.Support.HREF;
import static com.theoryinpractise.halbuilder.impl.api.Support.HREFLANG;
import static com.theoryinpractise.halbuilder.impl.api.Support.LINK;
import static com.theoryinpractise.halbuilder.impl.api.Support.NAME;
import static com.theoryinpractise.halbuilder.impl.api.Support.REL;
import static com.theoryinpractise.halbuilder.impl.api.Support.SELF;
import static com.theoryinpractise.halbuilder.impl.api.Support.TITLE;
import static com.theoryinpractise.halbuilder.impl.api.Support.XSI_NAMESPACE;
public class XmlRenderer<T> implements Renderer<T> {
public Optional<T> render(ReadableResource resource, Writer writer) {
final Element element = renderElement(resource, false);
final XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
try {
outputter.output(element, writer);
} catch (IOException e) {
throw new RuntimeException(e);
}
return Optional.absent();
}
private Element renderElement(ReadableResource resource, boolean embedded) {
final Link resourceLink = resource.getResourceLink();
final String href = resourceLink.getHref();
// Create the root element
final Element resourceElement = new Element("resource");
resourceElement.setAttribute("href", href);
if (!resourceLink.getRel().equals("self")) {
resourceElement.setAttribute("rel", resourceLink.getRel());
}
// Only add namespaces to non-embedded resources
if (!embedded) {
for (Map.Entry<String, String> entry : resource.getNamespaces().entrySet()) {
resourceElement.addNamespaceDeclaration(
Namespace.getNamespace(entry.getKey(), entry.getValue()));
}
// Add the instance namespace if there are null properties on this
// resource or on any embedded resources.
if(resource.hasNullProperties()) {
resourceElement.addNamespaceDeclaration(XSI_NAMESPACE);
}
}
//add a comment
// resourceElement.addContent(new Comment("Description of a resource"));
// add links
List<Link> links = resource.getLinks();
for (Link link : links) {
Element linkElement = new Element(LINK);
if (!link.getRel().contains(SELF)) {
linkElement.setAttribute(REL, link.getRel());
linkElement.setAttribute(HREF, link.getHref());
if (link.getName().isPresent()) {
linkElement.setAttribute(NAME, link.getName().get());
}
if (link.getTitle().isPresent()) {
linkElement.setAttribute(TITLE, link.getTitle().get());
}
if (link.getHreflang().isPresent()) {
linkElement.setAttribute(HREFLANG, link.getHreflang().get());
}
resourceElement.addContent(linkElement);
}
}
// add properties
for (Map.Entry<String, Optional<Object>> entry : resource.getProperties().entrySet()) {
Element propertyElement = new Element(entry.getKey());
if(entry.getValue().isPresent()) {
propertyElement.setContent(new Text(entry.getValue().get().toString()));
}
else {
propertyElement.setAttribute("nil", "true", XSI_NAMESPACE);
}
resourceElement.addContent(propertyElement);
}
// add subresources
for (Resource halResource : resource.getResources()) {
Element subResourceElement = renderElement(halResource, true);
resourceElement.addContent(subResourceElement);
}
return resourceElement;
}
}