add Marc XML Schema

This commit is contained in:
Jörg Prante 2022-12-06 09:46:57 +01:00
parent af6ace4750
commit c84b1c3756
3 changed files with 20 additions and 11 deletions

View file

@ -4,6 +4,6 @@ public interface MarcXmlConstants {
String MARCXML_NS_URI = "http://www.loc.gov/MARC21/slim"; String MARCXML_NS_URI = "http://www.loc.gov/MARC21/slim";
String MARCXML_SCHEMA_LOCATION = "http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"; String MARCXML_SCHEMA_LOCATION = "https://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd";
} }

View file

@ -15,6 +15,7 @@
*/ */
package org.xbib.marc.xml; package org.xbib.marc.xml;
import java.util.ArrayList;
import org.xbib.marc.MarcField; import org.xbib.marc.MarcField;
import org.xbib.marc.MarcListener; import org.xbib.marc.MarcListener;
import org.xbib.marc.MarcRecord; import org.xbib.marc.MarcRecord;
@ -33,7 +34,6 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
@ -160,7 +160,7 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
this.documentStarted = false; this.documentStarted = false;
this.collectionStarted = false; this.collectionStarted = false;
eventFactory = XMLEventFactory.newInstance(); eventFactory = XMLEventFactory.newInstance();
namespace = eventFactory.createNamespace("", NAMESPACE_URI); namespace = createNameSpace();
setupEventConsumer(writer, indent); setupEventConsumer(writer, indent);
} }
@ -185,7 +185,7 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
this.documentStarted = false; this.documentStarted = false;
this.collectionStarted = false; this.collectionStarted = false;
this.eventFactory = XMLEventFactory.newInstance(); this.eventFactory = XMLEventFactory.newInstance();
this.namespace = eventFactory.createNamespace("", NAMESPACE_URI); this.namespace = createNameSpace();
newWriter(fileNamePattern, fileNameCounter, bufferSize, compress); newWriter(fileNamePattern, fileNameCounter, bufferSize, compress);
setupEventConsumer(writer, indent); setupEventConsumer(writer, indent);
} }
@ -199,7 +199,7 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
this.xmlEventConsumer = consumer; this.xmlEventConsumer = consumer;
this.lock = new ReentrantLock(); this.lock = new ReentrantLock();
this.eventFactory = XMLEventFactory.newInstance(); this.eventFactory = XMLEventFactory.newInstance();
this.namespace = eventFactory.createNamespace("", NAMESPACE_URI); this.namespace = createNameSpace();
this.namespaces = Collections.singletonList(namespace).iterator(); this.namespaces = Collections.singletonList(namespace).iterator();
} }
@ -275,12 +275,12 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
} }
try { try {
if (!collectionStarted) { if (!collectionStarted) {
Iterator<Attribute> attrs = schemaWritten ? null : Arrays.asList( Iterator<Attribute> attrs = null;
eventFactory.createAttribute("xmlns:xsi", if (!schemaWritten) {
XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI), List<Attribute> list = new ArrayList<>();
eventFactory.createAttribute("xsi:schemaLocation", writeSchema(list);
NAMESPACE_URI + " " + NAMESPACE_SCHEMA_LOCATION) attrs = list.iterator();
).iterator(); }
xmlEventConsumer.add(eventFactory.createStartElement(COLLECTION_ELEMENT, attrs, namespaces)); xmlEventConsumer.add(eventFactory.createStartElement(COLLECTION_ELEMENT, attrs, namespaces));
schemaWritten = true; schemaWritten = true;
collectionStarted = true; collectionStarted = true;
@ -485,6 +485,10 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
return exception; return exception;
} }
protected Namespace createNameSpace() {
return eventFactory.createNamespace("", NAMESPACE_URI);
}
protected void writeSchema(List<Attribute> attrs) throws XMLStreamException { protected void writeSchema(List<Attribute> attrs) throws XMLStreamException {
attrs.add(eventFactory.createAttribute("xmlns:xsi", attrs.add(eventFactory.createAttribute("xmlns:xsi",
XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)); XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI));

View file

@ -9,6 +9,7 @@ import java.util.List;
import javax.xml.XMLConstants; import javax.xml.XMLConstants;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute; import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.util.XMLEventConsumer; import javax.xml.stream.util.XMLEventConsumer;
public class MarcXmlWriter extends MarcXchangeWriter { public class MarcXmlWriter extends MarcXchangeWriter {
@ -59,6 +60,10 @@ public class MarcXmlWriter extends MarcXchangeWriter {
super(consumer); super(consumer);
} }
protected Namespace createNameSpace() {
return eventFactory.createNamespace("", NAMESPACE_URI);
}
@Override @Override
protected void writeSchema(List<Attribute> attrs) throws XMLStreamException { protected void writeSchema(List<Attribute> attrs) throws XMLStreamException {
attrs.add(eventFactory.createAttribute("xmlns:xsi", attrs.add(eventFactory.createAttribute("xmlns:xsi",