Skip to content

Commit

Permalink
Add a SimpleXML converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienric committed Oct 27, 2013
1 parent 7fd6ede commit c6776aa
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<protobuf.version>2.5.0</protobuf.version>
<jackson.version>2.2.2</jackson.version>
<wire.version>1.0.1</wire.version>
<simplexml.version>2.7.1</simplexml.version>

<!-- Test Dependencies -->
<junit.version>4.10</junit.version>
Expand Down Expand Up @@ -119,7 +120,12 @@
<artifactId>wire-runtime</artifactId>
<version>${wire.version}</version>
</dependency>

<dependency>
<groupId>org.simpleframework</groupId>
<artifactId>simple-xml</artifactId>
<version>${simplexml.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
1 change: 1 addition & 0 deletions retrofit-converters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<module>protobuf</module>
<module>jackson</module>
<module>wire</module>
<module>simplexml</module>
</modules>
</project>
43 changes: 43 additions & 0 deletions retrofit-converters/simplexml/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit-converters</artifactId>
<version>1.2.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>converter-simplexml</artifactId>
<name>Converter: SimpleXML</name>

<dependencies>
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.simpleframework</groupId>
<artifactId>simple-xml</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package retrofit.converter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;

import org.simpleframework.xml.Serializer;

import retrofit.mime.TypedByteArray;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;

/**
* A {@link Converter} which uses SimpleXML for reading and writing entities.
*
* @author Fabien Ric ([email protected])
*/
public class SimpleXMLConverter implements Converter {
private static final String CHARSET = "UTF-8";
private static final String MIME_TYPE = "application/xml; charset=" + CHARSET;

private Serializer serializer = null;

public SimpleXMLConverter(Serializer serializer) {
this.serializer = serializer;
}

@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
try {
return serializer.read((Class<?>) type, body.in());
} catch (Exception e) {
throw new ConversionException(e);
}
}

@Override
public TypedOutput toBody(Object source) {
OutputStreamWriter osw = null;

try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
osw = new OutputStreamWriter(bos, CHARSET);
serializer.write(source, osw);
osw.flush();
return new TypedByteArray(MIME_TYPE, bos.toByteArray());
} catch (final NullPointerException e) {
throw new AssertionError(e);
} catch (IOException e) {
throw new AssertionError(e);
} catch (Exception e) {
throw new AssertionError(e);
} finally {
try {
if (osw != null) {
osw.close();
}
} catch (IOException e) {
throw new AssertionError(e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package retrofit.converter;

import static org.fest.assertions.api.Assertions.assertThat;

import java.io.ByteArrayOutputStream;

import org.junit.Test;
import org.simpleframework.xml.Default;
import org.simpleframework.xml.DefaultType;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.stream.Format;
import org.simpleframework.xml.stream.HyphenStyle;
import org.simpleframework.xml.stream.Verbosity;

import retrofit.mime.TypedByteArray;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;

public class SimpleXMLConverterTest {
private static final String MIME_TYPE = "application/xml; charset=UTF-8";

private final MyObject obj = new MyObject("hello world", 10);
private final String objAsXML = String.format(
"<my-object><message>%s</message><count>%d</count></my-object>",
obj.getMessage(), obj.getCount());
private final Converter converter = initConverter();

private static Converter initConverter() {

Format format = new Format(0, null, new HyphenStyle(), Verbosity.HIGH);
Persister persister = new Persister(format);
return new SimpleXMLConverter(persister);
}

@Test
public void serialize() throws Exception {
final TypedOutput typedOutput = converter.toBody(obj);
assertThat(typedOutput.mimeType()).isEqualTo(MIME_TYPE);
assertThat(asString(typedOutput)).isEqualTo(objAsXML);
}

@Test
public void deserialize() throws Exception {
final TypedInput input = new TypedByteArray(MIME_TYPE,
objAsXML.getBytes());
final MyObject result = (MyObject) converter.fromBody(input,
MyObject.class);
assertThat(result).isEqualTo(obj);
}

@Test(expected = ConversionException.class)
public void deserializeWrongValue() throws Exception {
final TypedInput input = new TypedByteArray(MIME_TYPE,
"<myObject><foo/><bar/></myObject>".getBytes());
converter.fromBody(input, MyObject.class);

}

@Test
public void deserializeWrongClass() throws Exception {
final TypedInput input = new TypedByteArray(MIME_TYPE,
objAsXML.getBytes());
Object result = converter.fromBody(input, String.class);
assertThat(result).isNull();
}

private String asString(TypedOutput typedOutput) throws Exception {
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
typedOutput.writeTo(bytes);
return new String(bytes.toByteArray());
}

@Default(value = DefaultType.FIELD)
static class MyObject {
@Element
private String message;
@Element
private int count;

public MyObject() {
}

public MyObject(String message, int count) {
this.message = message;
this.count = count;
}

public void setMessage(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

public void setCount(int count) {
this.count = count;
}

public int getCount() {
return count;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + count;
result = prime * result
+ ((message == null) ? 0 : message.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
MyObject other = (MyObject) obj;
if (count != other.count) {
return false;
}
if (message == null) {
if (other.message != null) {
return false;
}
} else if (!message.equals(other.message)) {
return false;
}
return true;
}
}
}

0 comments on commit c6776aa

Please sign in to comment.