update to Gradle 6.6.1, add a MARC record listener to a MARC content handler

This commit is contained in:
Jörg Prante 2020-11-20 13:01:09 +01:00
parent aef7c53a04
commit 3fa26ade80
13 changed files with 11014 additions and 45 deletions

View file

@ -1,8 +1,8 @@
group = org.xbib group = org.xbib
name = marc name = marc
version = 2.3.1 version = 2.4.0
gradle.wrapper.version = 6.4.1 gradle.wrapper.version = 6.6.1
xbib-content.version = 2.3.1 xbib-content.version = 2.3.1
xbib-bibliographic-character-sets.version = 2.0.0 xbib-bibliographic-character-sets.version = 2.0.0
xalan.version = 2.7.2 xalan.version = 2.7.2

Binary file not shown.

View file

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

2
gradlew vendored
View file

@ -130,7 +130,7 @@ fi
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"` APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"` JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath # We build the pattern for arguments to be converted via cygpath

21
gradlew.bat vendored
View file

@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1 %JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init if "%ERRORLEVEL%" == "0" goto execute
echo. echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
@ -54,7 +54,7 @@ goto fail
set JAVA_HOME=%JAVA_HOME:"=% set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init if exist "%JAVA_EXE%" goto execute
echo. echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
@ -64,21 +64,6 @@ echo location of your Java installation.
goto fail goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute :execute
@rem Setup the command line @rem Setup the command line
@ -86,7 +71,7 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle @rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end :end
@rem End local scope for the variables with windows NT shell @rem End local scope for the variables with windows NT shell

View file

@ -576,6 +576,10 @@ public final class Marc {
builder.getMarcListener().endCollection(); builder.getMarcListener().endCollection();
} }
} }
public void parseRecords() throws IOException {
}
} }
/** /**

View file

@ -56,7 +56,7 @@ public class MarcRecord extends LinkedHashMap<String, Object> {
* @param marcFields the MARC field * @param marcFields the MARC field
* @param lightweight true if MARC record fields should not be entered into the underlying hash map. * @param lightweight true if MARC record fields should not be entered into the underlying hash map.
*/ */
MarcRecord(String format, String type, RecordLabel recordLabel, public MarcRecord(String format, String type, RecordLabel recordLabel,
List<MarcField> marcFields, boolean lightweight) { List<MarcField> marcFields, boolean lightweight) {
super(); super();
this.format = format; this.format = format;

View file

@ -481,6 +481,10 @@ public class RecordLabel {
return this; return this;
} }
public Builder from(String label) {
return from(label.toCharArray());
}
/** /**
* Parse given record label. * Parse given record label.
* @param recordLabel the record label * @param recordLabel the record label
@ -493,9 +497,7 @@ public class RecordLabel {
} else if (label.length < LENGTH) { } else if (label.length < LENGTH) {
// fill with blanks // fill with blanks
char[] ch = new char[LENGTH - label.length]; char[] ch = new char[LENGTH - label.length];
for (int i = 0; i < ch.length; i++) { Arrays.fill(ch, ' ');
ch[i] = ' ';
}
char[] newLabel = new char[LENGTH]; char[] newLabel = new char[LENGTH];
System.arraycopy(label, 0, newLabel, 0, label.length); System.arraycopy(label, 0, newLabel, 0, label.length);
System.arraycopy(ch, 0, newLabel, label.length, ch.length); System.arraycopy(ch, 0, newLabel, label.length, ch.length);

View file

@ -67,10 +67,14 @@ public class MarcContentHandler
protected MarcListener marcListener; protected MarcListener marcListener;
protected MarcRecordListener marcRecordListener;
protected String format; protected String format;
protected String type; protected String type;
protected String label;
protected MarcValueTransformers marcValueTransformers; protected MarcValueTransformers marcValueTransformers;
protected boolean trim = false; protected boolean trim = false;
@ -79,9 +83,9 @@ public class MarcContentHandler
private boolean isCollection = false; private boolean isCollection = false;
private List<MarcField> marcFieldList = new LinkedList<>(); private final List<MarcField> marcFieldList = new LinkedList<>();
private Set<String> validNamespaces = private final Set<String> validNamespaces =
new HashSet<>(Arrays.asList(MARCXCHANGE_V1_NS_URI, MARCXCHANGE_V2_NS_URI, MARC21_SCHEMA_URI)); new HashSet<>(Arrays.asList(MARCXCHANGE_V1_NS_URI, MARCXCHANGE_V2_NS_URI, MARC21_SCHEMA_URI));
protected String getDefaultFormat() { protected String getDefaultFormat() {
@ -113,6 +117,16 @@ public class MarcContentHandler
return this; return this;
} }
/**
* Set MARC record listener..
* @param listener the MARC recordlistener
* @return this handler
*/
public MarcContentHandler setMarcRecordListener(MarcRecordListener listener) {
this.marcRecordListener = listener;
return this;
}
public MarcContentHandler addNamespace(String uri) { public MarcContentHandler addNamespace(String uri) {
this.validNamespaces.add(uri); this.validNamespaces.add(uri);
return this; return this;
@ -151,6 +165,9 @@ public class MarcContentHandler
if (marcListener != null) { if (marcListener != null) {
marcListener.beginCollection(); marcListener.beginCollection();
} }
if (marcRecordListener != null) {
marcRecordListener.beginCollection();
}
} }
@Override @Override
@ -158,6 +175,9 @@ public class MarcContentHandler
if (marcListener != null) { if (marcListener != null) {
marcListener.endCollection(); marcListener.endCollection();
} }
if (marcRecordListener != null) {
marcRecordListener.endCollection();
}
} }
@Override @Override
@ -170,6 +190,7 @@ public class MarcContentHandler
@Override @Override
public void leader(String label) { public void leader(String label) {
this.label = label;
if (marcListener != null) { if (marcListener != null) {
marcListener.leader(label); marcListener.leader(label);
} }
@ -177,16 +198,10 @@ public class MarcContentHandler
@Override @Override
public void field(MarcField marcField) { public void field(MarcField marcField) {
MarcField field = marcField;
if (marcValueTransformers != null) { if (marcValueTransformers != null) {
field = marcValueTransformers.transformValue(field); marcFieldList.add(marcValueTransformers.transformValue(marcField));
}
if (marcFieldTransformers != null) {
marcFieldList.add(marcField);
} else { } else {
if (!marcField.isEmpty() && marcListener != null) { marcFieldList.add(marcField);
marcListener.field(field);
}
} }
} }
@ -203,20 +218,38 @@ public class MarcContentHandler
@Override @Override
public void endRecord() { public void endRecord() {
try { try {
if (marcFieldTransformers != null) { if (marcListener != null) {
for (MarcField marcField : marcFieldTransformers.transform(marcFieldList)) { if (marcFieldTransformers != null) {
if (!marcField.isEmpty() && marcListener != null) { for (MarcField marcField : marcFieldTransformers.transform(marcFieldList)) {
marcListener.field(marcField); if (!marcField.isEmpty()) {
marcListener.field(marcField);
}
}
} else {
for (MarcField marcField : marcFieldList) {
if (!marcField.isEmpty()) {
marcListener.field(marcField);
}
} }
} }
marcFieldTransformers.reset();
marcFieldList.clear();
}
if (marcListener != null) {
marcListener.endRecord(); marcListener.endRecord();
} }
if (marcRecordListener != null) {
if (label == null) {
logger.log(Level.WARNING, "label is null, skipping record");
} else {
MarcRecord marcRecord = new MarcRecord(getFormat(), getType(),
RecordLabel.builder().from(label).build(),
marcFieldList, false);
marcRecordListener.record(marcRecord);
}
}
} finally { } finally {
recordCounter.incrementAndGet(); recordCounter.incrementAndGet();
if (marcFieldTransformers != null) {
marcFieldTransformers.reset();
}
marcFieldList.clear();
} }
} }

View file

@ -19,6 +19,7 @@ package org.xbib.marc.xml;
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;
import org.xbib.marc.MarcRecordListener;
import org.xbib.marc.transformer.value.MarcValueTransformers; import org.xbib.marc.transformer.value.MarcValueTransformers;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
@ -232,6 +233,11 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
return this; return this;
} }
public MarcXchangeWriter setMarcRecordListener(MarcRecordListener recordListener) {
super.setMarcRecordListener(recordListener);
return this;
}
@Override @Override
public void startDocument() { public void startDocument() {
if (exception != null) { if (exception != null) {

View file

@ -2,18 +2,22 @@ package org.xbib.marc.xml;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xbib.marc.Marc; import org.xbib.marc.Marc;
import org.xbib.marc.MarcRecord;
import org.xbib.marc.MarcRecordListener;
import org.xbib.marc.MarcXchangeConstants; import org.xbib.marc.MarcXchangeConstants;
import org.xmlunit.matchers.CompareMatcher; import org.xmlunit.matchers.CompareMatcher;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* *
*/ */
public class MarcXMLTest { public class DNBSRUMarcXMLTest {
/** /**
* Parsing MARC XML embedded in OAI response from DNB/ZDB. * Parsing MARC XML embedded in OAI response from DNB/ZDB.
@ -92,4 +96,71 @@ public class MarcXMLTest {
assertThat(sw.toString(), assertThat(sw.toString(),
CompareMatcher.isIdenticalTo(getClass().getResource(s + "-marcxchange.xml").openStream())); CompareMatcher.isIdenticalTo(getClass().getResource(s + "-marcxchange.xml").openStream()));
} }
@Test
public void testSRUMarcXmlPlus1() throws Exception {
String s = "zdb-sru-marcxmlplus1.xml";
InputStream in = getClass().getResourceAsStream(s);
StringWriter sw = new StringWriter();
//FileWriter sw = new FileWriter("zdb-sru-marcxmlplus1.xml-marcxchange.xml");
try (MarcXchangeWriter writer = new MarcXchangeWriter(sw, true)
.setFormat(MarcXchangeConstants.MARCXCHANGE_FORMAT)) {
writer.startDocument();
writer.startCustomElement("custom", "http://foobar", "root");
Marc.builder()
.setInputStream(in)
.setCharset(StandardCharsets.UTF_8)
.setContentHandler(new MarcContentHandler()
.setFormat("MarcXML")
.setType(MarcXchangeConstants.BIBLIOGRAPHIC_TYPE)
.addNamespace("http://www.loc.gov/MARC21/slim")
.setMarcListener(MarcXchangeConstants.BIBLIOGRAPHIC_TYPE, writer)
.setMarcListener(MarcXchangeConstants.HOLDINGS_TYPE, writer))
.build()
.xmlReader().parse();
writer.endCustomElement("custom", "http://foobar", "root");
writer.endDocument();
assertNull(writer.getException());
}
sw.close();
assertThat(sw.toString(),
CompareMatcher.isIdenticalTo(getClass().getResource(s + "-marcxchange.xml").openStream()));
}
@Test
public void testSRUMarcXmlPlus1RecordListener() throws Exception {
String s = "zdb-sru-marcxmlplus1.xml";
InputStream in = getClass().getResourceAsStream(s);
AtomicBoolean found = new AtomicBoolean();
MarcContentHandler marcListener = new MarcContentHandler();
MarcRecordListener marcRecordListener = new MarcRecordListener() {
@Override
public void beginCollection() {
}
@Override
public void record(MarcRecord marcRecord) {
found.set(true);
}
@Override
public void endCollection() {
}
};
// attach record listener
marcListener.setMarcRecordListener(marcRecordListener);
Marc.builder()
.setInputStream(in)
.setCharset(StandardCharsets.UTF_8)
.setContentHandler(new MarcContentHandler()
.setFormat("MarcXML")
.setType(MarcXchangeConstants.BIBLIOGRAPHIC_TYPE)
.addNamespace("http://www.loc.gov/MARC21/slim")
.setMarcListener(MarcXchangeConstants.BIBLIOGRAPHIC_TYPE, marcListener)
.setMarcListener(MarcXchangeConstants.HOLDINGS_TYPE,marcListener))
.build()
.xmlReader()
.parse();
assertTrue(found.get());
}
} }

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff