com.theoryinpractise.halbuilder.xml.XmlRepresentationWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of halbuilder-xml Show documentation
Show all versions of halbuilder-xml Show documentation
HAL+XML serializer/deserializer extension to the HalBuilder Library
package com.theoryinpractise.halbuilder.xml;
import com.google.common.base.Strings;
import com.theoryinpractise.halbuilder.api.*;
import com.theoryinpractise.halbuilder.api.RepresentationWriter;
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.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.TEMPLATED;
import static com.theoryinpractise.halbuilder.impl.api.Support.PROFILE;
import static com.theoryinpractise.halbuilder.impl.api.Support.TITLE;
import static com.theoryinpractise.halbuilder.impl.api.Support.XSI_NAMESPACE;
public class XmlRepresentationWriter implements RepresentationWriter {
public void write(ReadableRepresentation representation, Set flags, Writer writer) {
final Element element = renderElement("self", representation, false);
try {
Format prettyFormat = flags.contains(RepresentationFactory.PRETTY_PRINT)
? Format.getPrettyFormat()
: Format.getCompactFormat();
final XMLOutputter outputter = new XMLOutputter(prettyFormat);
outputter.output(element, writer);
} catch (IOException e) {
throw new RepresentationException(e);
}
}
private Element renderElement(String rel, ReadableRepresentation representation, boolean embedded) {
final Link resourceLink = representation.getResourceLink();
// Create the root element
final Element resourceElement = new Element("resource");
if (resourceLink != null) {
resourceElement.setAttribute("href", resourceLink.getHref());
}
if (!rel.equals("self")) {
resourceElement.setAttribute("rel", rel);
}
// Only add namespaces to non-embedded resources
if (!embedded) {
for (Map.Entry entry : representation.getNamespaces().entrySet()) {
resourceElement.addNamespaceDeclaration(
Namespace.getNamespace(entry.getKey(), entry.getValue()));
}
// Add the instance namespace if there are null properties on this
// representation or on any embedded resources.
if (representation.hasNullProperties()) {
resourceElement.addNamespaceDeclaration(XSI_NAMESPACE);
}
}
//add a comment
// resourceElement.addContent(new Comment("Description of a representation"));
// add links
List links = representation.getLinks();
for (Link link : links) {
Element linkElement = new Element(LINK);
if (!link.getRel().equals(SELF)) {
linkElement.setAttribute(REL, link.getRel());
linkElement.setAttribute(HREF, link.getHref());
if (!Strings.isNullOrEmpty(link.getName())) {
linkElement.setAttribute(NAME, link.getName());
}
if (!Strings.isNullOrEmpty(link.getTitle())) {
linkElement.setAttribute(TITLE, link.getTitle());
}
if (!Strings.isNullOrEmpty(link.getHreflang())) {
linkElement.setAttribute(HREFLANG, link.getHreflang());
}
if (!Strings.isNullOrEmpty(link.getProfile())) {
linkElement.setAttribute(PROFILE, link.getProfile());
}
if (link.hasTemplate()) {
linkElement.setAttribute(TEMPLATED, "true");
}
resourceElement.addContent(linkElement);
}
}
// add properties
for (Map.Entry entry : representation.getProperties().entrySet()) {
Element propertyElement = new Element(entry.getKey());
if (entry.getValue() != null) {
propertyElement.setContent(new Text(entry.getValue().toString()));
} else {
propertyElement.setAttribute("nil", "true", XSI_NAMESPACE);
}
resourceElement.addContent(propertyElement);
}
// add subresources
for (Map.Entry halResource : representation.getResources()) {
Element subResourceElement = renderElement(halResource.getKey(), halResource.getValue(), true);
resourceElement.addContent(subResourceElement);
}
return resourceElement;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy