add disbling control fields

This commit is contained in:
Jörg Prante 2023-02-08 16:54:54 +01:00
parent d971082000
commit a425667565
16 changed files with 116 additions and 76 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib
name = marc
version = 2.9.18
version = 2.9.19
org.gradle.warning.mode = ALL

View file

@ -15,9 +15,6 @@
*/
package org.xbib.marc;
import static org.xbib.marc.MarcXchangeConstants.BIBLIOGRAPHIC_TYPE;
import static org.xbib.marc.MarcXchangeConstants.MARCXCHANGE_FORMAT;
import org.w3c.dom.Document;
import org.xbib.marc.dialects.aleph.AlephSequentialInputStream;
import org.xbib.marc.dialects.bibliomondo.BiblioMondoInputStream;
@ -46,6 +43,17 @@ import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
@ -65,17 +73,8 @@ import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import static org.xbib.marc.MarcXchangeConstants.BIBLIOGRAPHIC_TYPE;
import static org.xbib.marc.MarcXchangeConstants.MARCXCHANGE_FORMAT;
/**
* A MARC instance for top-level fluent API style access to the most viable methods of
@ -680,6 +679,8 @@ public final class Marc {
private Pattern valuePattern;
private boolean disableControlFields;
private Builder() {
this.charset = StandardCharsets.UTF_8;
this.recordLabel = RecordLabel.EMPTY;
@ -855,6 +856,11 @@ public final class Marc {
return this;
}
public Builder disableControlFields() {
this.disableControlFields = true;
return this;
}
/**
* Set XML content handler.
* @param contentHandler the XML content handler
@ -864,6 +870,9 @@ public final class Marc {
if (contentHandler instanceof MarcContentHandler) {
MarcContentHandler marcContentHandler = (MarcContentHandler) contentHandler;
marcContentHandler.setMarcFieldTransformers(marcFieldTransformers);
if (disableControlFields) {
marcContentHandler.disabledControlFields();
}
}
this.defaultContentHandler = new InverseMarcContentHandler(contentHandler);
defaultContentHandler.setSchema(schema);
@ -896,6 +905,9 @@ public final class Marc {
.setMarcTransformer(marcTransformer)
.setMarcFieldTransformers(marcFieldTransformers)
.setMarcValueTransformers(marcValueTransformers);
if (disableControlFields) {
marcGenerator.disableControlFields();
}
return marcGenerator;
}

View file

@ -211,12 +211,17 @@ public class MarcField implements Comparable<MarcField> {
/**
* Returns if this MARC field is a control field.
* We invoke the isControl() function in the builder because the variable might have to be evaluated.
* @return true if control field, false if not
*/
public boolean isControl() {
return builder.isControl();
}
public boolean isControlFieldsDisabled() {
return builder.disableControlFields;
}
/**
* Returns if this MARC field is empty.
* @return true if MARC field is empty, false if not

View file

@ -15,10 +15,6 @@
*/
package org.xbib.marc;
import static org.xbib.marc.io.InformationSeparator.GS;
import static org.xbib.marc.io.InformationSeparator.RS;
import static org.xbib.marc.io.InformationSeparator.US;
import org.xbib.marc.io.BytesReference;
import org.xbib.marc.io.Chunk;
import org.xbib.marc.io.ChunkListener;
@ -33,6 +29,10 @@ import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
import static org.xbib.marc.io.InformationSeparator.GS;
import static org.xbib.marc.io.InformationSeparator.RS;
import static org.xbib.marc.io.InformationSeparator.US;
/**
* This chunk listener interprets the chunks from a stream and generates MARC events to a given MARC listener.
*/
@ -128,6 +128,11 @@ public class MarcGenerator implements ChunkListener<byte[], BytesReference> {
return this;
}
public MarcGenerator disableControlFields() {
builder.disableControlFields();
return this;
}
@Override
public void chunk(Chunk<byte[], BytesReference> chunk) throws IOException {
char separator = (char) chunk.separator()[0];

View file

@ -15,9 +15,7 @@
*/
package org.xbib.marc;
import static org.xbib.marc.json.MarcJsonWriter.FORMAT_TAG;
import static org.xbib.marc.json.MarcJsonWriter.LEADER_TAG;
import static org.xbib.marc.json.MarcJsonWriter.TYPE_TAG;
import org.xbib.marc.label.RecordLabel;
import java.io.InputStream;
import java.nio.charset.Charset;
@ -26,9 +24,6 @@ import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.stream.Stream;
import org.xbib.marc.label.RecordLabel;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@ -42,6 +37,11 @@ import java.util.TreeMap;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static org.xbib.marc.json.MarcJsonWriter.FORMAT_TAG;
import static org.xbib.marc.json.MarcJsonWriter.LEADER_TAG;
import static org.xbib.marc.json.MarcJsonWriter.TYPE_TAG;
/**
* A MARC record. This is an extended MARC record augmented with MarcXchange information.

View file

@ -15,13 +15,13 @@
*/
package org.xbib.marc.dialects.pica;
import java.util.HashSet;
import org.xbib.marc.MarcField;
import org.xbib.marc.label.RecordLabel;
import org.xbib.marc.xml.MarcContentHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import java.util.HashSet;
import java.util.Set;
/**

View file

@ -15,12 +15,12 @@
*/
package org.xbib.marc.io;
import static java.util.Objects.requireNonNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import static java.util.Objects.requireNonNull;
/**
* A buffered input stream that is organized in chunks separated by byte array patterns.
* Convenience implements are give by {@code PatternInputStream.lf()} for line-feed separated

View file

@ -18,6 +18,8 @@ package org.xbib.marc.tools;
import org.xbib.marc.Marc;
import org.xbib.marc.xml.MarcXchangeWriter;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
@ -28,9 +30,6 @@ import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
/**
* Command line tool.
*/

View file

@ -15,8 +15,6 @@
*/
package org.xbib.marc.transformer.field;
import static org.xbib.marc.transformer.field.MarcFieldTransformer.Operator.HEAD;
import org.xbib.marc.MarcField;
import java.util.Collection;
@ -28,6 +26,8 @@ import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.xbib.marc.transformer.field.MarcFieldTransformer.Operator.HEAD;
/**
*
*/

View file

@ -69,6 +69,9 @@ public class MarcValueTransformers {
marcValueTransformerMap.get(key) : marcValueTransformerMap.get(DEFAULT);
if (transformer != null) {
MarcField.Builder builder = MarcField.builder();
if (field.isControlFieldsDisabled()) {
builder.disableControlFields();
}
builder.tag(field.getTag()).indicator(field.getIndicator());
if (field.getValue() != null) {
builder.value(transformer.transform(field.getValue()));

View file

@ -15,15 +15,6 @@
*/
package org.xbib.marc.xml;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
@ -32,6 +23,14 @@ import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.XMLEvent;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
public class IndentingXMLEventWriter implements XMLEventWriter {

View file

@ -15,8 +15,6 @@
*/
package org.xbib.marc.xml;
import java.util.Comparator;
import java.util.HashSet;
import org.xbib.marc.MarcField;
import org.xbib.marc.MarcListener;
import org.xbib.marc.MarcRecord;
@ -37,8 +35,10 @@ import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.io.IOException;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -91,6 +91,8 @@ public class MarcContentHandler
private Comparator<String> comparator;
private boolean disabledControlFields;
public MarcContentHandler() {
this.recordCounter = new AtomicInteger();
this.stack = new LinkedList<>();
@ -175,6 +177,11 @@ public class MarcContentHandler
return this;
}
public MarcContentHandler disabledControlFields() {
this.disabledControlFields = true;
return this;
}
@Override
public void beginCollection() {
// early setup of MARC listener, even before beginRecord(format, type), it works only
@ -360,7 +367,11 @@ public class MarcContentHandler
}
}
}
MarcField.Builder builder = MarcField.builder().tag(tag);
MarcField.Builder builder = MarcField.builder();
if (disabledControlFields) {
builder.disableControlFields();
}
builder.tag(tag);
if (max > 0) {
builder.indicator(sb.substring(min - 1, max));
}

View file

@ -15,20 +15,12 @@
*/
package org.xbib.marc.xml;
import java.util.HashSet;
import org.xbib.marc.MarcField;
import org.xbib.marc.MarcListener;
import org.xbib.marc.MarcXchangeConstants;
import org.xbib.marc.label.RecordLabel;
import org.xbib.marc.transformer.value.MarcValueTransformers;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
@ -37,6 +29,13 @@ import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.util.XMLEventConsumer;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
/**
* The MarcXchange event consumer listens to StaX events and converts them to MarcXchange events.
@ -63,6 +62,8 @@ public class MarcXchangeEventConsumer implements XMLEventConsumer, MarcXchangeCo
private long numberOfRecords;
private boolean disabledControlFields;
public MarcXchangeEventConsumer() {
this.stack = new LinkedList<>();
this.marcListeners = new HashMap<>();
@ -95,6 +96,11 @@ public class MarcXchangeEventConsumer implements XMLEventConsumer, MarcXchangeCo
return this;
}
public MarcXchangeEventConsumer disableControlFields() {
this.disabledControlFields = true;
return this;
}
@Override
public void beginCollection() {
if (marcListener != null) {
@ -206,10 +212,12 @@ public class MarcXchangeEventConsumer implements XMLEventConsumer, MarcXchangeCo
setType(thistype);
beginRecord(thisformat, thistype);
}
case LEADER -> {
}
case CONTROLFIELD, DATAFIELD -> {
MarcField.Builder builder = MarcField.builder().tag(tag);
MarcField.Builder builder = MarcField.builder();
if (disabledControlFields) {
builder.disableControlFields();
}
builder.tag(tag);
if (max > 0) {
builder.indicator(sb.substring(min - 1, max));
}

View file

@ -15,7 +15,6 @@
*/
package org.xbib.marc.xml;
import java.util.ArrayList;
import org.xbib.marc.MarcField;
import org.xbib.marc.MarcListener;
import org.xbib.marc.MarcRecord;
@ -23,6 +22,15 @@ import org.xbib.marc.MarcRecordListener;
import org.xbib.marc.label.RecordLabel;
import org.xbib.marc.transformer.value.MarcValueTransformers;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.util.XMLEventConsumer;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.Flushable;
@ -35,6 +43,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
@ -47,16 +56,6 @@ import java.util.logging.Logger;
import java.util.zip.Deflater;
import java.util.zip.GZIPOutputStream;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.util.XMLEventConsumer;
/**
* This MarcXchange Writer writes MarcXchange events to a StaX XML output stream or XML event consumer.
* Default output format is MarcXchange.

View file

@ -1,17 +1,17 @@
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.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.util.XMLEventConsumer;
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;
public class MarcXmlWriter extends MarcXchangeWriter {

View file

@ -25,13 +25,12 @@ import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
/**
* Receive SAX events and create a DOM.
*/