add MarcXML writer

This commit is contained in:
Jörg Prante 2022-12-06 00:47:15 +01:00
parent ddae3f750c
commit af6ace4750
4 changed files with 89 additions and 6 deletions

View file

@ -0,0 +1,9 @@
package org.xbib.marc;
public interface MarcXmlConstants {
String MARCXML_NS_URI = "http://www.loc.gov/MARC21/slim";
String MARCXML_SCHEMA_LOCATION = "http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd";
}

View file

@ -22,6 +22,7 @@ import org.xbib.marc.MarcListener;
import org.xbib.marc.MarcRecord;
import org.xbib.marc.MarcRecordListener;
import org.xbib.marc.MarcXchangeConstants;
import org.xbib.marc.MarcXmlConstants;
import org.xbib.marc.label.RecordLabel;
import org.xbib.marc.transformer.field.MarcFieldTransformers;
import org.xbib.marc.transformer.value.MarcValueTransformers;
@ -51,7 +52,7 @@ import java.util.logging.Logger;
* and fires events to a Marc listener.
*/
public class MarcContentHandler
implements MarcXchangeConstants, MarcListener, MarcRecordListener,
implements MarcXmlConstants, MarcXchangeConstants, MarcListener, MarcRecordListener,
EntityResolver, DTDHandler, ContentHandler, ErrorHandler {
private static final Logger logger = Logger.getLogger(MarcContentHandler.class.getName());

View file

@ -82,7 +82,7 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
private static final QName SUBFIELD_ELEMENT = new QName(NAMESPACE_URI, SUBFIELD, "");
private final XMLEventFactory eventFactory;
protected final XMLEventFactory eventFactory;
private final Namespace namespace;
@ -304,10 +304,7 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
String realtype = getType() != null ? getType() : type != null ? type : getDefaultType();
attrs.add(eventFactory.createAttribute(TYPE_ATTRIBUTE, realtype));
if (!schemaWritten) {
attrs.add(eventFactory.createAttribute("xmlns:xsi",
XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI));
attrs.add(eventFactory.createAttribute("xsi:schemaLocation",
NAMESPACE_URI + " " + NAMESPACE_SCHEMA_LOCATION));
writeSchema(attrs);
schemaWritten = true;
}
xmlEventConsumer.add(eventFactory.createStartElement(RECORD_ELEMENT, attrs.iterator(), namespaces));
@ -488,6 +485,13 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
return exception;
}
protected void writeSchema(List<Attribute> attrs) throws XMLStreamException {
attrs.add(eventFactory.createAttribute("xmlns:xsi",
XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI));
attrs.add(eventFactory.createAttribute("xsi:schemaLocation",
NAMESPACE_URI + " " + NAMESPACE_SCHEMA_LOCATION));
}
/**
* Split records if configured. A splitlimit of -1 prevents splitting.
*/

View file

@ -0,0 +1,69 @@
package org.xbib.marc.xml;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.util.XMLEventConsumer;
public class MarcXmlWriter extends MarcXchangeWriter {
private static final String NAMESPACE_URI = MARCXML_NS_URI;
private static final String NAMESPACE_SCHEMA_LOCATION = MARCXML_SCHEMA_LOCATION;
public MarcXmlWriter(OutputStream out) throws IOException {
super(out);
}
/**
* Create a MarcXML writer on an underlying output stream.
* @param out the underlying output stream
* @param indent if true, indent MarcXchange output
* @throws IOException if writer can not be created
*/
public MarcXmlWriter(OutputStream out, boolean indent) throws IOException {
super(new OutputStreamWriter(out, StandardCharsets.UTF_8), indent);
}
/**
* Create a MarcXML writer on an underlying writer.
* @param writer the underlying writer
* @throws IOException if writer can not be created
*/
public MarcXmlWriter(Writer writer) throws IOException {
super(writer, false);
}
/**
* Create a MarcXML writer on an underlying writer.
* @param writer the underlying writer
* @param indent if true, indent MarcXchange output
* @throws IOException if writer can not be created
*/
public MarcXmlWriter(Writer writer, boolean indent) throws IOException {
super(writer, indent);
}
public MarcXmlWriter(String fileNamePattern, int splitlimit, int bufferSize, boolean compress, boolean indent)
throws IOException {
super(fileNamePattern, splitlimit, bufferSize, compress, indent);
}
public MarcXmlWriter(XMLEventConsumer consumer) {
super(consumer);
}
@Override
protected void writeSchema(List<Attribute> attrs) throws XMLStreamException {
attrs.add(eventFactory.createAttribute("xmlns:xsi",
XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI));
attrs.add(eventFactory.createAttribute("xsi:schemaLocation",
NAMESPACE_URI + " " + NAMESPACE_SCHEMA_LOCATION));
}
}