Commit a1ef1d54 authored by R.W.Majeed's avatar R.W.Majeed

more tests, unsolved bug in DOM XML stream writer

parent 88b6a9e7
......@@ -6,6 +6,7 @@ import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
......@@ -22,12 +23,13 @@ import de.sekmi.histream.impl.ObservationImpl;
/**
* Writes observations to a single XML file. Observations must be grouped by patient and encounter.
* TODO fix duplicate namespace prefixes with JAXB marshaller
*
* @author Raphael
*
*/
public class GroupedXMLWriter extends GroupedObservationHandler{
private static final String NAMESPACE = "http://sekmi.de/histream/ns/eav-data";
public static final String NAMESPACE = ObservationImpl.XML_NAMESPACE;//"http://sekmi.de/histream/ns/eav-data";
private Marshaller marshaller;
private boolean writeFormatted;
private int formattingDepth;
......@@ -87,20 +89,26 @@ public class GroupedXMLWriter extends GroupedObservationHandler{
*/
public void setFormatted(boolean formattedOutput){
this.writeFormatted = formattedOutput;
try {
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
} catch (PropertyException e) {
}
}
@Override
protected void beginStream() throws ObservationException{
try {
writer.setDefaultNamespace(NAMESPACE);
writer.setPrefix("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
//writer.setPrefix(XMLConstants.DEFAULT_NS_PREFIX, NAMESPACE);
writer.writeStartDocument();
writer.writeStartElement(JAXBObservationSupplier.DOCUMENT_ROOT);
writer.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX,JAXBObservationSupplier.DOCUMENT_ROOT,NAMESPACE);
writer.setDefaultNamespace(NAMESPACE);
writer.writeDefaultNamespace(NAMESPACE);
writer.setPrefix("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
writer.writeNamespace("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
formatNewline();
formatPush();
// doesn't work for DOM writers
//Objects.requireNonNull(writer.getPrefix(NAMESPACE));
formatIndent();
marshaller.marshal(meta, writer);
formatNewline();
......
......@@ -7,9 +7,20 @@ import java.io.OutputStream;
import java.time.Instant;
import java.util.stream.StreamSupport;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXB;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
......@@ -23,6 +34,7 @@ import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import de.sekmi.histream.ObservationException;
import de.sekmi.histream.ObservationSupplier;
import de.sekmi.histream.impl.ExternalSourceImpl;
import de.sekmi.histream.impl.Meta;
......@@ -36,16 +48,113 @@ public class TestXMLWriter {
// for debugging, set debugLog to System.out
debugFile = File.createTempFile("xmlwriterlog", ".xml");
// debugLog = System.out
debugLog = new FileOutputStream(debugFile);
debugLog = System.out;//new FileOutputStream(debugFile);
}
@After
public void cleanLog() throws IOException{
debugLog.close();
if( debugLog != System.out )debugLog.close();
if( debugFile != null )
debugFile.delete();
}
private void printDocument(Document doc) throws TransformerException{
DOMSource source = new DOMSource(doc);
Transformer t;
t = TransformerFactory.newInstance().newTransformer();
t.transform(source, new StreamResult(debugLog));
}
private Document createDocument() throws ParserConfigurationException{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
doc.getDomConfig().setParameter("namespaces", true);
doc.getDomConfig().setParameter("namespace-declarations", true);
return doc;
}
private void testStreamWriterNamespaces(XMLStreamWriter w) throws XMLStreamException{
w.writeStartDocument();
// w.writeStartElement("root");
// write element with default namespace
w.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "root", "urn:ns:def");
w.setDefaultNamespace("urn:ns:def");
w.writeDefaultNamespace("urn:ns:def");
// write additional prefix
w.setPrefix("pre", "urn:ns:pre");
w.writeNamespace("pre", "urn:ns:pre");
Assert.assertEquals(XMLConstants.DEFAULT_NS_PREFIX, w.getPrefix("urn:ns:def"));
// write child with default namespace
w.writeStartElement("urn:ns:def", "defelem");
w.writeCharacters("some content");
w.writeEndElement();
// write child with other prefix
w.writeStartElement("urn:ns:pre", "preelem");
Assert.assertEquals("pre", w.getPrefix("urn:ns:pre"));
w.writeCharacters("some content");
w.writeEndElement();
// test default namespace
Assert.assertEquals(XMLConstants.DEFAULT_NS_PREFIX, w.getPrefix("urn:ns:def"));
w.writeEndDocument();
}
@Test
public void testStreamWriterNamespaces() throws XMLStreamException, ParserConfigurationException{
XMLOutputFactory factory = XMLOutputFactory.newInstance();
// enable repairing namespaces to remove duplicate namespace declarations by JAXB marshal
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
// test output stream writer
XMLStreamWriter w = factory.createXMLStreamWriter(System.out);
testStreamWriterNamespaces(w);
w.close();
// test DOM stream writer
DOMResult result = new DOMResult(createDocument());
w = factory.createXMLStreamWriter(result);
// will not work for DOM stream writers
//testStreamWriterNamespaces(w);
w.close();
}
@Test
public void testNamespaces() throws ParserConfigurationException, XMLStreamException, ObservationException{
FileObservationProviderTest t = new FileObservationProviderTest();
t.initializeObservationFactory();
DOMResult result = new DOMResult(createDocument());
GroupedXMLWriter w = new GroupedXMLWriter(result);
w.beginStream();
// doesn't work for DOM writers
/*
String pre = w.writer.getPrefix(GroupedXMLWriter.NAMESPACE);
// make sure prefixes are working..
//Assert.assertNotNull(pre);
Assert.assertEquals(XMLConstants.DEFAULT_NS_PREFIX, pre);
*/
w.close();
}
@Test
public void testWriteDOM() throws Exception{
FileObservationProviderTest t = new FileObservationProviderTest();
t.initializeObservationFactory();
ObservationSupplier s = t.getExampleSupplier();
Document doc = createDocument();
GroupedXMLWriter w = new GroupedXMLWriter(new DOMResult(doc));
Meta.transfer(s, w);
StreamSupport.stream(AbstractObservationParser.nonNullSpliterator(s), false).forEach(w);
w.close();
s.close();
doc.normalizeDocument();
printDocument(doc);
}
@Test
public void testWrite() throws Exception{
public void testWriteStream() throws Exception{
FileObservationProviderTest t = new FileObservationProviderTest();
t.initializeObservationFactory();
ObservationSupplier s = t.getExampleSupplier();
......
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