Commit 3ef9354e authored by R.W.Majeed's avatar R.W.Majeed
Browse files

fixed namespace output

parent 6a145270
......@@ -3,8 +3,8 @@ package de.sekmi.histream.io;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
......@@ -13,9 +13,10 @@ import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import de.sekmi.histream.Observation;
import de.sekmi.histream.ObservationException;
import de.sekmi.histream.Plugin;
import de.sekmi.histream.ext.ExternalSourceType;
import de.sekmi.histream.ext.Patient;
import de.sekmi.histream.ext.Visit;
import de.sekmi.histream.impl.AbstractObservationHandler;
......@@ -29,37 +30,40 @@ import de.sekmi.histream.impl.ObservationImpl;
* @author Raphael
*
*/
public class XMLWriter extends AbstractObservationHandler implements Closeable, Plugin {
public class XMLWriter extends AbstractObservationHandler implements Closeable {
private static final String NAMESPACE = "http://sekmi.de/histream/ns/eav-data";
private Patient prevPatient;
private Visit prevVisit;
private Marshaller marshaller;
private OutputStream output;
private XMLStreamWriter writer;
private boolean writeFormatted;
private int formattingDepth;
public XMLWriter(Map<String,String> config) throws JAXBException, XMLStreamException, FactoryConfigurationError{
private XMLWriter() throws JAXBException{
this.marshaller = JAXBContext.newInstance(ObservationImpl.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
this.writeFormatted = true;
this.output = System.out;
XMLOutputFactory factory = XMLOutputFactory.newFactory();
}
public XMLWriter(OutputStream output) throws JAXBException, XMLStreamException, FactoryConfigurationError{
this();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
// enable repairing namespaces to remove duplicate namespace declarations by JAXB marshal
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
this.writer = factory.createXMLStreamWriter(output);
writeStartDocument();
}
this.writer = factory.createXMLStreamWriter(output, "UTF-8");
private void writeStartDocument() throws XMLStreamException{
writer.setDefaultNamespace(NAMESPACE);
writer.setPrefix("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
writer.writeStartDocument();
formatNewline();
writer.setDefaultNamespace(NAMESPACE);
//?? writer.setPrefix("xsi", "http://www.w3.org/2001/XMLSchema-instance");
writer.writeStartElement(NAMESPACE, JAXBObservationSupplier.DOCUMENT_ROOT);
writer.writeDefaultNamespace(NAMESPACE);
writer.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
writer.writeNamespace("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
formatNewline();
formatPush();
//Transformer transformer = TransformerFactory.newInstance().newTransformer();
//transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//transformer.
}
@Override
public void setMeta(String key, String value) {
......@@ -87,7 +91,7 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable,
writer.writeAttribute("id", visit.getId());
formatNewline();
formatPush();
if( visit.getStartTime() != null ){
formatIndent();
writer.writeStartElement("start");
......@@ -205,7 +209,7 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable,
}
// TODO clone observation, remove patient/encounter/source information as it is contained in wrappers
formatIndent();
marshaller.marshal(observation, writer);
marshalFactWithContext(observation, thisPatient, thisVisit, null);
formatNewline();
} catch (JAXBException | XMLStreamException e ) {
......@@ -213,7 +217,43 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable,
}
}
/**
* Marshal a fact without writing context information from patient, visit and source.
* TODO move method to separate helper class
*
* @param fact fact
* @param patient patient context
* @param visit visit context
* @param source source context
* @throws ObservationException for errors in fact
* @throws JAXBException errors during marshal operation
*/
public void marshalFactWithContext(Observation fact, Patient patient, Visit visit, ExternalSourceType source) throws ObservationException, JAXBException{
ObservationImpl o = (ObservationImpl)fact;
String e = o.getEncounterId();
String p = o.getPatientId();
if( patient.getId().equals(p) ){
o.setPatientId(null);
}else{
p = null;
}
if( visit.getId().equals(e) ){
o.setEncounterId(null);
}else{
e = null;
}
marshaller.marshal(fact, writer);
if( p != null ){
o.setPatientId(p);
}
if( e != null ){
o.setEncounterId(e);
}
}
@Override
public void close() throws IOException{
try {
......@@ -229,7 +269,6 @@ public class XMLWriter extends AbstractObservationHandler implements Closeable,
}
writer.writeEndDocument(); // automatically closes root element
writer.close();
output.close();
} catch (XMLStreamException e) {
throw new IOException(e);
}
......
package de.sekmi.histream.io;
import java.io.File;
import java.io.IOException;
import java.util.stream.StreamSupport;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLStreamException;
import org.junit.Assert;
import org.junit.Test;
import org.w3c.dom.Document;
import de.sekmi.histream.ObservationSupplier;
......@@ -18,8 +23,28 @@ public class TestXMLWriter {
FileObservationProviderTest t = new FileObservationProviderTest();
t.initializeObservationFactory();
ObservationSupplier s = t.getExampleSupplier();
XMLWriter w = new XMLWriter(null);
XMLWriter w = new XMLWriter(System.out);
StreamSupport.stream(AbstractObservationParser.nonNullSpliterator(s), false).forEach(w);
w.close();
}
@Test
public void testReadWriteIdenticalXML() throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setCoalescing(true);
dbf.setIgnoringElementContentWhitespace(true);
dbf.setIgnoringComments(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc1 = db.parse(new File("examples/dwh-jaxb.xml"));
doc1.normalizeDocument();
// TODO compare with generated DOM
Document doc2 = db.parse(new File("examples/dwh-jaxb.xml"));
doc2.normalizeDocument();
Assert.assertTrue(doc1.isEqualNode(doc2));
}
}
......@@ -2,6 +2,7 @@ package de.sekmi.histream.etl;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.StreamSupport;
import javax.xml.bind.JAXB;
......@@ -19,6 +20,8 @@ import de.sekmi.histream.ext.Visit;
import de.sekmi.histream.impl.ObservationFactoryImpl;
import de.sekmi.histream.impl.SimplePatientExtension;
import de.sekmi.histream.impl.SimpleVisitExtension;
import de.sekmi.histream.io.AbstractObservationParser;
import de.sekmi.histream.io.XMLWriter;
public class TestETLSupplier {
private DataSource ds;
......@@ -41,6 +44,15 @@ public class TestETLSupplier {
os.close();
}
@Test
public void testXMLConversion() throws Exception{
XMLWriter w = new XMLWriter(System.out);
// TODO transfer meta information
StreamSupport.stream(AbstractObservationParser.nonNullSpliterator(os), false).forEach(w);
w.close();
}
@Test
public void testReadFacts() throws IOException{
while( true ){
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment