add isClosed flag to Marc XML writing

This commit is contained in:
Jörg Prante 2023-02-03 17:23:13 +01:00
parent ae10eb3485
commit 3aae6c5b7e
2 changed files with 14 additions and 24 deletions

View file

@ -117,43 +117,31 @@ public class IndentingXMLEventWriter implements XMLEventWriter {
@Override @Override
public void add(XMLEvent event) throws XMLStreamException { public void add(XMLEvent event) throws XMLStreamException {
switch (event.getEventType()) { switch (event.getEventType()) {
case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.CHARACTERS, XMLStreamConstants.CDATA, XMLStreamConstants.SPACE -> {
case XMLStreamConstants.CDATA:
case XMLStreamConstants.SPACE: {
out.add(event); out.add(event);
afterData(); afterData();
return;
} }
case XMLStreamConstants.START_ELEMENT: { case XMLStreamConstants.START_ELEMENT -> {
beforeStartElement(); beforeStartElement();
out.add(event); out.add(event);
afterStartElement(); afterStartElement();
return;
} }
case XMLStreamConstants.END_ELEMENT -> {
case XMLStreamConstants.END_ELEMENT: {
beforeEndElement(); beforeEndElement();
out.add(event); out.add(event);
afterEndElement(); afterEndElement();
return;
} }
case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.START_DOCUMENT, XMLStreamConstants.PROCESSING_INSTRUCTION, XMLStreamConstants.COMMENT, XMLStreamConstants.DTD -> {
case XMLStreamConstants.PROCESSING_INSTRUCTION:
case XMLStreamConstants.COMMENT:
case XMLStreamConstants.DTD: {
beforeMarkup(); beforeMarkup();
out.add(event); out.add(event);
afterMarkup(); afterMarkup();
return;
} }
case XMLStreamConstants.END_DOCUMENT: { case XMLStreamConstants.END_DOCUMENT -> {
out.add(event); out.add(event);
break;
} }
default: { default -> {
out.add(event); out.add(event);
afterEndDocument(); afterEndDocument();
break;
} }
} }
} }
@ -220,12 +208,7 @@ public class IndentingXMLEventWriter implements XMLEventWriter {
private String getIndent(int depth, int size) { private String getIndent(int depth, int size) {
final int length = depth * size; final int length = depth * size;
String indent = indentCache.get(length); return indentCache.computeIfAbsent(length, l -> LINE_SEPARATOR + repeat(' ', l));
if (indent == null) {
indent = LINE_SEPARATOR + repeat(' ', length);
indentCache.put(length, indent);
}
return indent;
} }
private enum State { private enum State {

View file

@ -119,6 +119,8 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
private boolean compress; private boolean compress;
private boolean isClosed;
/** /**
* Create a MarcXchange writer on an underlying output stream. * Create a MarcXchange writer on an underlying output stream.
* @param out the underlying output stream * @param out the underlying output stream
@ -487,9 +489,14 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
@Override @Override
public void close() throws IOException { public void close() throws IOException {
isClosed = true;
writer.close(); writer.close();
} }
public boolean isClosed() {
return isClosed;
}
public Exception getException() { public Exception getException() {
return exception; return exception;
} }