package com.darwinsys.clientware;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.ws.rs.core.MediaType;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

/**
 * A JAX-RS client NOT using the JAX-RS Client API
 */
public class UrlJaxbClient {

	static final String BASE_URL = 
			"http://androidcookbook.com/seam/resource/rest/recipe";

	public static void main(String[] args) throws Exception {
		URL url = new URL(BASE_URL + "/4");
		final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.addRequestProperty("Accept", MediaType.APPLICATION_XML);
		InputStream is = connection.getInputStream();
		// Read "is" to get the response from a GET

		// Read into a DOM Tree and print to stdout
		Document tree = // DOM API
				DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
		Transformer tx = TransformerFactory.newInstance().newTransformer();
		tx.setOutputProperty(OutputKeys.INDENT, "yes");
		tx.transform(new DOMSource(tree), new StreamResult(System.out));
	}
}

@@ -45,27 +45,94 @@
 java.io.
-InputStream
+BufferedReader;%0D%0Aimport java.io.InputStream;%0D%0Aimport java.io.InputStreamReader
 ;%0D%0Aimpor
@@ -225,311 +225,8 @@
 pe;%0D
-%0Aimport javax.xml.parsers.DocumentBuilderFactory;%0D%0Aimport javax.xml.transform.OutputKeys;%0D%0Aimport javax.xml.transform.Transformer;%0D%0Aimport javax.xml.transform.TransformerFactory;%0D%0Aimport javax.xml.transform.dom.DOMSource;%0D%0Aimport javax.xml.transform.stream.StreamResult;%0D%0A%0D%0Aimport org.w3c.dom.Document;%0D
 %0A%0D%0A/
@@ -663,11 +663,12 @@
 ION_
-XML
+JSON
 );%0D%0A
@@ -767,349 +767,178 @@
 ET%0D%0A
-%0D%0A%09%09// Read into a DOM Tree and print to stdout%0D%0A%09%09Document tree = // DOM API%0D%0A%09%09%09%09DocumentBuilderFactory.
+%09%09BufferedReader bi = new BufferedReader(
 new
+ 
 In
-stance().newDocumentBuilder().parse
+putStreamReader
 (is)
+)
 ;%0D%0A%09%09
-Transformer tx = TransformerFactory.newInstance().newTransformer();%0D%0A%09%09tx.setOutputProperty(OutputKeys.INDENT, %22yes%22);%0D%0A%09%09tx.transform(new DOMSource(tree), new StreamResult(System.out));
+String line = null;%0D%0A%09%09while ((line = bi.readLine()) != null) %7B%0D%0A%09%09%09System.out.println(line);%0D%0A%09%09%7D
 %0D%0A%09%7D

XML 0, JSON 1