Skip to content

Commit

Permalink
Prepare for XML usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriedenhagen committed Oct 15, 2010
1 parent 759fc8a commit ebddc94
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.classpath
.project
.settings/
*.trace
bin/
gen/
target/
Expand Down
4 changes: 4 additions & 0 deletions app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<artifactId>guice</artifactId>
<classifier>no_aop</classifier>
</dependency>
<dependency>
<groupId>org.simpleframework</groupId>
<artifactId>simple-xml</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2010 Mirko Friedenhagen
*/

package de.friedenhagen.android.mittagstischka.model;

import java.util.List;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

/**
* @author mirko
*
*/
@Root
public class Eateries {

@ElementList(name = "list", inline = true)
private final List<Eatery> list;

/**
* @param list
*/
public Eateries(@ElementList(name = "list", inline = true) final List<Eatery> list) {
this.list = list;
}

public Eatery get(int index) {
return list.get(index);
}

public int size() {
return list.size();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,97 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;

/**
* @author mirko
*
*/
@Root
public class Eatery implements Serializable {

@Attribute(name = "title")
public final String title;

@Attribute(name = "id")
public final Integer id;

@Attribute(name = "latitude")
public final Double latitude;

@Attribute(name = "longitude")
public final Double longitude;

@Attribute(name = "homepage", required = false)
public final String homepage;

@Attribute(name = "date")
public final Date date;

public Eatery(JSONObject o) {
/**
* @param title
* @param id
* @param latitude
* @param longitude
* @param homepage
* @param date
*/
public Eatery(@Attribute(name = "title") String title, @Attribute(name = "id") Integer id,
@Attribute(name = "latitude") Double latitude, @Attribute(name = "longitude") Double longitude,
@Attribute(name = "homepage", required = false) String homepage, @Attribute(name = "date") Date date) {
this.title = title;
this.id = id;
this.latitude = latitude;
this.longitude = longitude;
this.homepage = homepage;
this.date = date;
}

/**
* @param title
* @param id
* @param latitude
* @param longitude
* @param date
*/
public Eatery(String title, Integer id, Double latitude, Double longitude, Date date) {
this.title = title;
this.id = id;
this.latitude = latitude;
this.longitude = longitude;
this.homepage = null;
this.date = date;
}

@Override
public String toString() {
return "Eatery(" + title + ", " + id + ")";
}

public static Eatery fromJsonObject(JSONObject o) {
try {
title = o.getString("title");
id = o.getInt("id");
latitude = o.getDouble("lat");
longitude = o.getDouble("long");
final String title = o.getString("title");
final Integer id = o.getInt("id");
final Double latitude = o.getDouble("lat");
final Double longitude = o.getDouble("long");
final String homepage;
if (o.has("homepage")) {
homepage = o.getString("homepage");
} else {
homepage = null;
}
final String dateAsString = o.getString("date");
final SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
date = format.parse(dateAsString);
final Date date = format.parse(dateAsString);
return new Eatery(title, id, latitude, longitude, homepage, date);
} catch (JSONException e) {
throw new RuntimeException("Message:", e);
} catch (ParseException e) {
throw new RuntimeException("Message:", e);
}
}

@Override
public String toString() {
return "Eatery(" + title + ", " + id + ")";
}

public static Eatery fromJsonObject(JSONObject o) {
return new Eatery(o);
}

public static List<Eatery> fromJsonArray(JSONArray jsonArray) {
final int length = jsonArray.length();
final List<Eatery> eateryList = new ArrayList<Eatery>(length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
Expand All @@ -19,6 +21,7 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.simpleframework.xml.core.Persister;

import de.friedenhagen.android.mittagstischka.retrievers.IOUtils;

Expand Down Expand Up @@ -111,6 +114,28 @@ public void testInvalidJSONArray() throws JSONException {
}
}

@Test
public void serializeToXml() throws Exception {
Persister persister = new Persister();
final Eateries eateryList = new Eateries(createEateryList());
final ByteArrayOutputStream out = new ByteArrayOutputStream(eateryList.size() * 20);
persister.write(eateryList, out, "utf-8");
final String string = out.toString("UTF-8");
System.out.println(string);
persister.read(Eateries.class, string);
}

@Test
public void serializeToXmlSingle() throws Exception {
Persister persister = new Persister();
Eatery eatery = createEateryList().get(0);
final ByteArrayOutputStream out = new ByteArrayOutputStream(20);
persister.write(eatery, out, "utf-8");
final String string = out.toString("UTF-8");
System.out.println(string);
persister.read(Eatery.class, string);
}

/**
* @return
*/
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<version>2.0</version>
<classifier>no_aop</classifier>
</dependency>
<dependency>
<groupId>org.simpleframework</groupId>
<artifactId>simple-xml</artifactId>
<version>2.3.6</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${appArtifactId}</artifactId>
Expand Down

0 comments on commit ebddc94

Please sign in to comment.