Skip to content

Commit d4d4068

Browse files
committed
Add XML examples.
1 parent 084c6f6 commit d4d4068

File tree

7 files changed

+25
-25
lines changed

7 files changed

+25
-25
lines changed

httprpc-test/src/main/java/org/httprpc/test/mongodb/RestaurantService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.httprpc.WebService;
2626
import org.httprpc.io.CSVEncoder;
2727
import org.httprpc.io.JSONEncoder;
28+
import org.httprpc.io.XMLEncoder;
2829
import org.httprpc.RequestMethod;
2930
import org.httprpc.Response;
3031

@@ -85,6 +86,12 @@ public Iterator<Document> iterator() {
8586
CSVEncoder csvEncoder = new CSVEncoder(Arrays.asList("name", "address.building", "address.street", "borough", "cuisine"));
8687

8788
csvEncoder.write(cursorAdapter, getResponse().getOutputStream());
89+
} else if (format.equals("xml")) {
90+
getResponse().setContentType("text/xml");
91+
92+
XMLEncoder xmlEncoder = new XMLEncoder();
93+
94+
xmlEncoder.write(cursorAdapter, getResponse().getOutputStream());
8895
} else {
8996
throw new UnsupportedOperationException();
9097
}

httprpc-test/src/main/java/org/httprpc/test/mysql/PetService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.httprpc.WebService;
3434
import org.httprpc.io.CSVEncoder;
3535
import org.httprpc.io.JSONEncoder;
36+
import org.httprpc.io.XMLEncoder;
3637
import org.httprpc.RequestMethod;
3738
import org.httprpc.ResourcePath;
3839
import org.httprpc.Response;
@@ -104,6 +105,12 @@ public void getPets(String owner, String format) throws SQLException, IOExceptio
104105
CSVEncoder csvEncoder = new CSVEncoder(Arrays.asList("name", "species", "sex", "birth"));
105106

106107
csvEncoder.write(resultSetAdapter, getResponse().getOutputStream());
108+
} else if (format.equals("xml")) {
109+
getResponse().setContentType("text/xml");
110+
111+
XMLEncoder xmlEncoder = new XMLEncoder();
112+
113+
xmlEncoder.write(resultSetAdapter, getResponse().getOutputStream());
107114
} else {
108115
throw new UnsupportedOperationException();
109116
}

httprpc-test/src/main/resources/org/httprpc/test/mongodb/RestaurantService.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ RestaurantService = Example service based on MongoDB sample "restaurants" datase
22

33
getRestaurants = Retreives a list of all restaurants in a given zip code.
44
getRestaurants.zipCode = The zip code to search.
5-
getRestaurants.format = The response format (either "json" or "csv"). The default is "json".
5+
getRestaurants.format = The response format ("json", "csv", or "xml"). The default is "json".

httprpc-test/src/main/resources/org/httprpc/test/mysql/PetService.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ PetService = Example service based on MySQL sample "menagerie" database.
22

33
getPets = Retreives a list of all pets belonging to a given owner.
44
getPets.owner = The pet owner.
5-
getPets.format = The response format (either "json" or "csv"). The default is "json".
5+
getPets.format = The response format ("json", "csv", or "xml"). The default is "json".
66

77
getAverageAge = Calculates the average age (in years) of all pets in the database.

httprpc-test/src/main/webapp/index.jsp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,15 @@
187187
<br/>
188188
<a href="${pageContext.request.contextPath}/restaurants?zipCode=10462">Restaurants (JSON)</a><br/>
189189
<a href="${pageContext.request.contextPath}/restaurants?zipCode=10462&format=csv">Restaurants (CSV)</a><br/>
190+
<a href="${pageContext.request.contextPath}/restaurants?zipCode=10462&format=xml">Restaurants (XML)</a><br/>
190191

191192
<h2>MySQL</h2>
192193

193194
<a href="${pageContext.request.contextPath}/pets?api">Pets (API)</a><br/>
194195
<br/>
195196
<a href="${pageContext.request.contextPath}/pets?owner=Gwen">Pets (JSON)</a><br/>
196197
<a href="${pageContext.request.contextPath}/pets?owner=Gwen&format=csv">Pets (CSV)</a><br/>
198+
<a href="${pageContext.request.contextPath}/pets?owner=Gwen&format=xml">Pets (XML)</a><br/>
197199
<br/>
198200
<a href="${pageContext.request.contextPath}/pets/average-age">Average Age</a><br/>
199201

httprpc/src/main/java/org/httprpc/io/XMLEncoder.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,6 @@
3131
* XML encoder.
3232
*/
3333
public class XMLEncoder {
34-
private String rootElementName;
35-
36-
/**
37-
* Constructs a new XML encoder.
38-
*
39-
* @param rootElementName
40-
* The root element name.
41-
*/
42-
public XMLEncoder(String rootElementName) {
43-
if (rootElementName == null) {
44-
throw new IllegalArgumentException();
45-
}
46-
47-
this.rootElementName = rootElementName;
48-
}
49-
5034
/**
5135
* Writes a sequence of values to an output stream.
5236
*
@@ -82,9 +66,9 @@ public void write(Iterable<? extends Map<String, ?>> values, Writer writer) thro
8266
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
8367

8468
streamWriter.writeStartDocument();
85-
streamWriter.writeStartElement(rootElementName);
69+
streamWriter.writeStartElement("root");
8670

87-
writeSequence(values, streamWriter);
71+
writeValues(values, streamWriter);
8872

8973
streamWriter.writeEndElement();
9074
streamWriter.writeEndDocument();
@@ -95,7 +79,7 @@ public void write(Iterable<? extends Map<String, ?>> values, Writer writer) thro
9579
writer.flush();
9680
}
9781

98-
private void writeSequence(Iterable<? extends Map<String, ?>> values, XMLStreamWriter streamWriter) throws XMLStreamException {
82+
private void writeValues(Iterable<? extends Map<String, ?>> values, XMLStreamWriter streamWriter) throws XMLStreamException {
9983
for (Map<String, ?> map : values) {
10084
streamWriter.writeStartElement("item");
10185

@@ -135,7 +119,7 @@ private void writeMap(Map<String, ?> map, XMLStreamWriter streamWriter) throws X
135119
for (Map.Entry<String, Iterable<?>> entry : sequences.entrySet()) {
136120
streamWriter.writeStartElement(entry.getKey());
137121

138-
writeSequence((Iterable<? extends Map<String, ?>>)entry.getValue(), streamWriter);
122+
writeValues((Iterable<? extends Map<String, ?>>)entry.getValue(), streamWriter);
139123

140124
streamWriter.writeEndElement();
141125
}

httprpc/src/test/java/org/httprpc/io/XMLEncoderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public class XMLEncoderTest extends AbstractTest {
2929
@Test
3030
public void testWrite() throws IOException {
3131
String expected = "<?xml version=\"1.0\" ?>"
32-
+ "<test>"
32+
+ "<root>"
3333
+ "<item a=\"ABC\" b=\"1\" c=\"2.345\" f=\"0\" g=\"3\">"
3434
+ "<d><item e=\"true\"></item><item e=\"false\"></item></d>"
3535
+ "<e><f g=\"XYZ\"></f></e>"
3636
+ "</item>"
3737
+ "<item a=\"DÉF\" b=\"2\" c=\"4.5678\"></item>"
38-
+ "</test>";
38+
+ "</root>";
3939

4040
List<Map<String, ?>> values = listOf(
4141
mapOf(
@@ -63,7 +63,7 @@ public void testWrite() throws IOException {
6363

6464
StringWriter writer = new StringWriter();
6565

66-
XMLEncoder xmlEncoder = new XMLEncoder("test");
66+
XMLEncoder xmlEncoder = new XMLEncoder();
6767

6868
xmlEncoder.write(values, writer);
6969

0 commit comments

Comments
 (0)