update to Gradle 6.6.1, add a MARC record listener to a MARC content handler
This commit is contained in:
parent
aef7c53a04
commit
3fa26ade80
13 changed files with 11014 additions and 45 deletions
|
@ -1,8 +1,8 @@
|
|||
group = org.xbib
|
||||
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-bibliographic-character-sets.version = 2.0.0
|
||||
xalan.version = 2.7.2
|
||||
|
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
|
@ -1,5 +1,5 @@
|
|||
distributionBase=GRADLE_USER_HOME
|
||||
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
|
||||
zipStorePath=wrapper/dists
|
||||
|
|
2
gradlew
vendored
2
gradlew
vendored
|
@ -130,7 +130,7 @@ fi
|
|||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
|
||||
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
|
|
21
gradlew.bat
vendored
21
gradlew.bat
vendored
|
@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome
|
|||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
if "%ERRORLEVEL%" == "0" goto execute
|
||||
|
||||
echo.
|
||||
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_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
|
@ -64,21 +64,6 @@ echo location of your Java installation.
|
|||
|
||||
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
|
||||
@rem Setup the command line
|
||||
|
||||
|
@ -86,7 +71,7 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
|||
|
||||
|
||||
@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
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
|
|
|
@ -576,6 +576,10 @@ public final class Marc {
|
|||
builder.getMarcListener().endCollection();
|
||||
}
|
||||
}
|
||||
|
||||
public void parseRecords() throws IOException {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -56,7 +56,7 @@ public class MarcRecord extends LinkedHashMap<String, Object> {
|
|||
* @param marcFields the MARC field
|
||||
* @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) {
|
||||
super();
|
||||
this.format = format;
|
||||
|
|
|
@ -481,6 +481,10 @@ public class RecordLabel {
|
|||
return this;
|
||||
}
|
||||
|
||||
public Builder from(String label) {
|
||||
return from(label.toCharArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse given record label.
|
||||
* @param recordLabel the record label
|
||||
|
@ -493,9 +497,7 @@ public class RecordLabel {
|
|||
} else if (label.length < LENGTH) {
|
||||
// fill with blanks
|
||||
char[] ch = new char[LENGTH - label.length];
|
||||
for (int i = 0; i < ch.length; i++) {
|
||||
ch[i] = ' ';
|
||||
}
|
||||
Arrays.fill(ch, ' ');
|
||||
char[] newLabel = new char[LENGTH];
|
||||
System.arraycopy(label, 0, newLabel, 0, label.length);
|
||||
System.arraycopy(ch, 0, newLabel, label.length, ch.length);
|
||||
|
|
|
@ -67,10 +67,14 @@ public class MarcContentHandler
|
|||
|
||||
protected MarcListener marcListener;
|
||||
|
||||
protected MarcRecordListener marcRecordListener;
|
||||
|
||||
protected String format;
|
||||
|
||||
protected String type;
|
||||
|
||||
protected String label;
|
||||
|
||||
protected MarcValueTransformers marcValueTransformers;
|
||||
|
||||
protected boolean trim = false;
|
||||
|
@ -79,9 +83,9 @@ public class MarcContentHandler
|
|||
|
||||
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));
|
||||
|
||||
protected String getDefaultFormat() {
|
||||
|
@ -113,6 +117,16 @@ public class MarcContentHandler
|
|||
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) {
|
||||
this.validNamespaces.add(uri);
|
||||
return this;
|
||||
|
@ -151,6 +165,9 @@ public class MarcContentHandler
|
|||
if (marcListener != null) {
|
||||
marcListener.beginCollection();
|
||||
}
|
||||
if (marcRecordListener != null) {
|
||||
marcRecordListener.beginCollection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -158,6 +175,9 @@ public class MarcContentHandler
|
|||
if (marcListener != null) {
|
||||
marcListener.endCollection();
|
||||
}
|
||||
if (marcRecordListener != null) {
|
||||
marcRecordListener.endCollection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -170,6 +190,7 @@ public class MarcContentHandler
|
|||
|
||||
@Override
|
||||
public void leader(String label) {
|
||||
this.label = label;
|
||||
if (marcListener != null) {
|
||||
marcListener.leader(label);
|
||||
}
|
||||
|
@ -177,16 +198,10 @@ public class MarcContentHandler
|
|||
|
||||
@Override
|
||||
public void field(MarcField marcField) {
|
||||
MarcField field = marcField;
|
||||
if (marcValueTransformers != null) {
|
||||
field = marcValueTransformers.transformValue(field);
|
||||
}
|
||||
if (marcFieldTransformers != null) {
|
||||
marcFieldList.add(marcField);
|
||||
marcFieldList.add(marcValueTransformers.transformValue(marcField));
|
||||
} else {
|
||||
if (!marcField.isEmpty() && marcListener != null) {
|
||||
marcListener.field(field);
|
||||
}
|
||||
marcFieldList.add(marcField);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -203,20 +218,38 @@ public class MarcContentHandler
|
|||
@Override
|
||||
public void endRecord() {
|
||||
try {
|
||||
if (marcFieldTransformers != null) {
|
||||
for (MarcField marcField : marcFieldTransformers.transform(marcFieldList)) {
|
||||
if (!marcField.isEmpty() && marcListener != null) {
|
||||
marcListener.field(marcField);
|
||||
if (marcListener != null) {
|
||||
if (marcFieldTransformers != null) {
|
||||
for (MarcField marcField : marcFieldTransformers.transform(marcFieldList)) {
|
||||
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();
|
||||
}
|
||||
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 {
|
||||
recordCounter.incrementAndGet();
|
||||
if (marcFieldTransformers != null) {
|
||||
marcFieldTransformers.reset();
|
||||
}
|
||||
marcFieldList.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.xbib.marc.xml;
|
|||
import org.xbib.marc.MarcField;
|
||||
import org.xbib.marc.MarcListener;
|
||||
import org.xbib.marc.MarcRecord;
|
||||
import org.xbib.marc.MarcRecordListener;
|
||||
import org.xbib.marc.transformer.value.MarcValueTransformers;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
|
@ -232,6 +233,11 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
|
|||
return this;
|
||||
}
|
||||
|
||||
public MarcXchangeWriter setMarcRecordListener(MarcRecordListener recordListener) {
|
||||
super.setMarcRecordListener(recordListener);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startDocument() {
|
||||
if (exception != null) {
|
||||
|
|
|
@ -2,18 +2,22 @@ package org.xbib.marc.xml;
|
|||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.xbib.marc.Marc;
|
||||
import org.xbib.marc.MarcRecord;
|
||||
import org.xbib.marc.MarcRecordListener;
|
||||
import org.xbib.marc.MarcXchangeConstants;
|
||||
import org.xmlunit.matchers.CompareMatcher;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringWriter;
|
||||
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.
|
||||
|
@ -92,4 +96,71 @@ public class MarcXMLTest {
|
|||
assertThat(sw.toString(),
|
||||
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());
|
||||
}
|
||||
}
|
5433
src/test/resources/org/xbib/marc/xml/zdb-sru-marcxmlplus1.xml
Normal file
5433
src/test/resources/org/xbib/marc/xml/zdb-sru-marcxmlplus1.xml
Normal file
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue