package org.endeavourhealth.enterprise.core.querydocument;

import org.endeavourhealth.enterprise.core.querydocument.models.QueryDocument;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.bind.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

/**
 * Created by Drew on 11/03/2016.
 */
public abstract class QueryDocumentParser {


    public static QueryDocument readFromXml(String xml) throws ParserConfigurationException, JAXBException, IOException, SAXException {

        //parse XML string into DOM
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = docBuilder.parse(is);
        org.w3c.dom.Element varElement = document.getDocumentElement();

        //parse DOM into POJOs
        JAXBContext context = JAXBContext.newInstance(QueryDocument.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        JAXBElement<QueryDocument> loader = unmarshaller.unmarshal(varElement, QueryDocument.class);
        return loader.getValue();
    }

    public static String writeToXml(QueryDocument q) {

        StringWriter sw = new StringWriter();

        try {
            JAXBContext carContext = JAXBContext.newInstance(QueryDocument.class);
            Marshaller carMarshaller = carContext.createMarshaller();
            carMarshaller.marshal(q, sw);
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }

        //validate the XML against the schema
/*        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsd));
        javax.xml.validation.Validator validator = schema.newValidator();
        validator.validate(new StreamSource(xml));*/



        return sw.toString();
    }

}

@@ -220,16 +220,50 @@
 bind.*;%0A
+import javax.xml.namespace.QName;%0A
 import j
@@ -1527,19 +1527,16 @@
 ontext c
-arC
 ontext =
@@ -1574,32 +1574,35 @@
 ocument.class);%0A
+/*%0A
             Mars
@@ -1608,20 +1608,124 @@
 shaller 
-carM
+marshaller = context.createMarshaller();%0A            marshaller.marshal(q, sw);%0A*/%0A%0A            Marshaller m
 arshalle
@@ -1733,11 +1733,8 @@
  = c
-arC
 onte
@@ -1772,31 +1772,226 @@
     
-carMarshaller.marshal(q
+marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //just makes output easier to read%0A            marshaller.marshal(new JAXBElement%3CQueryDocument%3E(new QName(%22uri%22,%22local%22), QueryDocument.class, q)
 , sw
@@ -2131,16 +2131,17 @@
 chema%0A/*
+%0A
         
@@ -2421,42 +2421,456 @@
 l));
+%0A
 */%0A%0A%0A
-%0A        return sw.toString()
+        String ret = sw.toString();%0A/*%0A        QueryDocument q2 = null;%0A        try %7B%0A            q2 = readFromXml(ret);%0A        %7D catch (ParserConfigurationException e) %7B%0A            e.printStackTrace();%0A        %7D catch (JAXBException e) %7B%0A            e.printStackTrace();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D catch (SAXException e) %7B%0A            e.printStackTrace();%0A        %7D%0A*/%0A%0A%0A        return ret
 ;%0A  

Fix for missing @XmlRootElement annotation