Make JAXB XML marshaller pretty-print

This commit is contained in:
Andrew Donald Kennedy 2012-02-10 12:30:40 +00:00
parent 4fdaa3b6a6
commit 7ca842f44c
2 changed files with 4 additions and 7 deletions

View File

@ -28,7 +28,6 @@ import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
import org.jclouds.http.functions.ParseXMLWithJAXB;
import org.jclouds.xml.XMLParser; import org.jclouds.xml.XMLParser;
/** /**
@ -49,6 +48,7 @@ public class JAXBParser implements XMLParser {
try { try {
JAXBContext context = JAXBContext.newInstance(type); JAXBContext context = JAXBContext.newInstance(type);
Marshaller marshaller = context.createMarshaller(); Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
marshaller.marshal(src, writer); marshaller.marshal(src, writer);
return writer.toString(); return writer.toString();
@ -57,7 +57,6 @@ public class JAXBParser implements XMLParser {
} }
} }
@SuppressWarnings("unchecked")
@Override @Override
public <T> T fromXML(final String xml, final Class<T> type) throws IOException { public <T> T fromXML(final String xml, final Class<T> type) throws IOException {
try { try {

View File

@ -18,7 +18,7 @@
*/ */
package org.jclouds.rest.binders; package org.jclouds.rest.binders;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.*;
import java.net.URI; import java.net.URI;
@ -52,8 +52,7 @@ public class BindToXMLPayloadTest {
HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build(); HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
request = binder.bindToRequest(request, obj); request = binder.bindToRequest(request, obj);
assertEquals(request.getPayload().getRawContent(), XMLParser.DEFAULT_XML_HEADER assertEquals(request.getPayload().getRawContent(), XMLParser.DEFAULT_XML_HEADER + "\n<test>\n <elem>Hello World</elem>\n</test>\n");
+ "<test><elem>Hello World</elem></test>");
assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.APPLICATION_XML); assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.APPLICATION_XML);
} }
@ -72,8 +71,7 @@ public class BindToXMLPayloadTest {
.build(); .build();
request = binder.bindToRequest(request, obj); request = binder.bindToRequest(request, obj);
assertEquals(request.getPayload().getRawContent(), XMLParser.DEFAULT_XML_HEADER assertEquals(request.getPayload().getRawContent(), XMLParser.DEFAULT_XML_HEADER + "\n<test>\n <elem>Hello World</elem>\n</test>\n");
+ "<test><elem>Hello World</elem></test>");
assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.APPLICATION_XML); assertEquals(request.getPayload().getContentMetadata().getContentType(), MediaType.APPLICATION_XML);
} }