Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/main/java/com/siemens/ct/exi/main/api/stream/StAXEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public class StAXEncoder implements XMLStreamWriter {
protected final boolean preserveComment;
protected final boolean preservePI;

// indicate whether an "empty" element was started
protected boolean emptyElement;

// AT or NS Events pending
protected boolean pendingATs;

Expand Down Expand Up @@ -115,6 +118,7 @@ public void setOutputStream(OutputStream os) throws EXIException,
}

protected void init() {
emptyElement = false;
pendingATs = false;
exiAttributes.clear();
nsContext.reset();
Expand Down Expand Up @@ -289,8 +293,12 @@ protected void checkPendingATEvents() throws EXIException, IOException {
// encode NS decls and attributes
encoder.encodeAttributeList(exiAttributes);
exiAttributes.clear();

pendingATs = false;

if (emptyElement) {
encoder.encodeEndElement();
emptyElement = false;
}
}
}

Expand Down Expand Up @@ -618,7 +626,9 @@ public void writeEmptyElement(String namespaceURI, String localName)
public void writeEmptyElement(String prefix, String localName,
String namespaceURI) throws XMLStreamException {
this.writeStartElement(prefix, localName, namespaceURI);
this.writeEndElement();
// Note: we cannot close the element immediately since attributes might follow
// this.writeEndElement();
emptyElement = true;
}

public void writeStartDocument(String version) throws XMLStreamException {
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/siemens/ct/exi/main/ParallelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.siemens.ct.exi.main.data.AbstractTestCase;
import com.siemens.ct.exi.main.data.TestXSDResolver;

@Ignore("https://github.com/EXIficient/exificient/issues/45")
public class ParallelTest extends AbstractTestCase {

static final int ENCODE_THREADS = 4; // 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,96 @@ public void testIssue18_simpifiedNoPrefix() throws AssertionFailedError,
}
}


// https://github.com/EXIficient/exificient/issues/43
public void testEmptyElement() throws Exception {
// <head>
// <link rel="stylesheet" href="css/xmlwriter01.css" />
// <title>myTitle</title>
// </head>

EXIFactory ef = DefaultEXIFactory.newInstance();

// Version A: empty Element with SE ... EE
{
StAXEncoder staxEncoder = new StAXEncoder(ef);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
staxEncoder.setOutputStream(baos);

staxEncoder.writeStartDocument();
staxEncoder.writeStartElement("head");
staxEncoder.writeStartElement("link");
staxEncoder.writeAttribute("rel", "stylesheet");
staxEncoder.writeAttribute("href", "css/xmlwriter01.css");
staxEncoder.writeEndElement();
staxEncoder.writeStartElement("title");
staxEncoder.writeCharacters("myTitle");
staxEncoder.writeEndElement();
staxEncoder.writeEndElement();
staxEncoder.writeEndDocument();

// decode
StAXDecoder staxDecoder = new StAXDecoder(ef);
staxDecoder.setInputStream(new ByteArrayInputStream(baos.toByteArray()));
_checkDecodeEmptyElement(staxDecoder);
}


// Version B: using writeEmptyElement
{
StAXEncoder staxEncoder = new StAXEncoder(ef);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
staxEncoder.setOutputStream(baos);

staxEncoder.writeStartDocument();
staxEncoder.writeStartElement("head");
staxEncoder.writeEmptyElement("link");
staxEncoder.writeAttribute("rel", "stylesheet");
staxEncoder.writeAttribute("href", "css/xmlwriter01.css");
staxEncoder.writeStartElement("title");
staxEncoder.writeCharacters("myTitle");
staxEncoder.writeEndElement();
staxEncoder.writeEndElement();
staxEncoder.writeEndDocument();

// decode
StAXDecoder staxDecoder = new StAXDecoder(ef);
staxDecoder.setInputStream(new ByteArrayInputStream(baos.toByteArray()));
_checkDecodeEmptyElement(staxDecoder);
}
}

void _checkDecodeEmptyElement(StAXDecoder staxDecoder) throws Exception {
assertTrue(staxDecoder.hasNext());
assertEquals(XMLStreamConstants.START_ELEMENT, staxDecoder.next());
assertEquals("head", staxDecoder.getName().getLocalPart());

assertTrue(staxDecoder.hasNext());
assertEquals(XMLStreamConstants.START_ELEMENT, staxDecoder.next());
assertEquals("link", staxDecoder.getName().getLocalPart());

assertEquals(2, staxDecoder.getAttributeCount());
assertEquals("rel", staxDecoder.getAttributeName(0).getLocalPart());
assertEquals("stylesheet", staxDecoder.getAttributeValue(0));

assertTrue(staxDecoder.hasNext());
assertEquals(XMLStreamConstants.END_ELEMENT, staxDecoder.next());

assertTrue(staxDecoder.hasNext());
assertEquals(XMLStreamConstants.START_ELEMENT, staxDecoder.next());
assertEquals("title", staxDecoder.getName().getLocalPart());

assertTrue(staxDecoder.hasNext());
assertEquals(XMLStreamConstants.CHARACTERS, staxDecoder.next());
assertEquals("myTitle", new String(staxDecoder.getTextCharacters()));

assertTrue(staxDecoder.hasNext());
assertEquals(XMLStreamConstants.END_ELEMENT, staxDecoder.next());

assertTrue(staxDecoder.hasNext());
assertEquals(XMLStreamConstants.END_ELEMENT, staxDecoder.next());
}

public static void main(String[] args) throws Exception {

StAXCoderTestCase st = new StAXCoderTestCase("StAX");
Expand Down
Loading