// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package test;

import java.io.IOException;

import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.restlet.representation.Representation;
import org.restlet.data.MediaType;
import org.restlet.ext.xml.DomRepresentation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

@Path("/TestCXml")
public class TestCXmlResource {
    @GET
    @Produces("application/xml")
    public Representation get() {
        T1 o = new T1(123, "ABC");
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            Element f1 = doc.createElement("f1");
            f1.appendChild(doc.createTextNode(Integer.toString(o.getF1())));
            Element f2 = doc.createElement("f2");
            f2.appendChild(doc.createTextNode(o.getF2()));
            Element t1 = doc.createElement("t1");
            t1.appendChild(f1);
            t1.appendChild(f2);
            doc.appendChild(t1);
            DomRepresentation r = new DomRepresentation(MediaType.APPLICATION_XML);
            r.setIndenting(true);
            r.setDocument(doc);
            return r;
        } catch(ParserConfigurationException ex) {
            ex.printStackTrace();
            return null;
        } catch(IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
