-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Marc21XmlEncoder acts as a wrapper. It makes use of Marc21Encoder, Marc21Decoder and MarcXmlEncoder to ensure a proper MarcXml, especially regarding the leader. Also - in contrast to MarcXmlEncoder - the record id (field 001) is mandatory.
- Loading branch information
Showing
6 changed files
with
287 additions
and
14 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/AbstractMarcXmlEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.metafacture.biblio.marc21; | ||
|
||
import org.metafacture.framework.ObjectReceiver; | ||
import org.metafacture.framework.helpers.DefaultStreamPipe; | ||
|
||
public abstract class AbstractMarcXmlEncoder extends DefaultStreamPipe<ObjectReceiver<String>> implements MarcXmlEncoderInterface { | ||
|
||
protected void onResetStream() { | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21XmlEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright 2024 hbz | ||
* | ||
* Licensed under the Apache License, Version 2.0 the "License"; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.metafacture.biblio.marc21; | ||
|
||
import org.metafacture.framework.FluxCommand; | ||
import org.metafacture.framework.StreamReceiver; | ||
import org.metafacture.framework.annotations.Description; | ||
import org.metafacture.framework.annotations.In; | ||
import org.metafacture.framework.annotations.Out; | ||
|
||
/** | ||
* Acts as a wrapper: pipes input to Marc21Encoder which output is piped to Marc21Decoder which output is piped to MarcXmlEncoder. | ||
* | ||
* @author Pascal Christoph (dr0i) | ||
* | ||
*/ | ||
@In(StreamReceiver.class) | ||
@Out(String.class) | ||
@Description("Encodes MARC21 records as MARCXML. It wraps 'encode-marc21 | decode-marc21 | encode-marcxml ' to generate MARCXML more safely, especially when the building the 'leader'.") | ||
@FluxCommand("encode-marc21xml") | ||
public class Marc21XmlEncoder extends AbstractMarcXmlEncoder { | ||
private final Marc21Decoder marc21Decoder = new Marc21Decoder(); | ||
private final Marc21Encoder marc21Encoder = new Marc21Encoder(); | ||
private final MarcXmlEncoder marcXmlEncoder = new MarcXmlEncoder(); | ||
|
||
/** | ||
* Creates an instance of {@link Marc21XmlEncoder}. | ||
*/ | ||
public Marc21XmlEncoder() { | ||
marc21Decoder.setEmitLeaderAsWhole(true); | ||
|
||
marc21Encoder.setReceiver(marc21Decoder); | ||
marc21Decoder.setReceiver(marcXmlEncoder); | ||
} | ||
|
||
@Override | ||
protected void onSetReceiver() { | ||
marcXmlEncoder.setReceiver(getReceiver()); | ||
} | ||
|
||
@Override | ||
public void startRecord(final String identifier) { | ||
marc21Encoder.startRecord(identifier); | ||
} | ||
|
||
@Override | ||
public void endRecord() { | ||
marc21Encoder.endRecord(); | ||
} | ||
|
||
@Override | ||
public void startEntity(final String name) { | ||
marc21Encoder.startEntity(name); | ||
} | ||
|
||
@Override | ||
public void endEntity() { | ||
marc21Encoder.endEntity(); | ||
} | ||
|
||
@Override | ||
public void literal(final String name, final String value) { | ||
marc21Encoder.literal(name, value); | ||
} | ||
|
||
@Override | ||
protected void onCloseStream() { | ||
marc21Encoder.closeStream(); | ||
} | ||
|
||
@Override | ||
public void onResetStream() { | ||
marc21Encoder.resetStream(); | ||
} | ||
|
||
@Override | ||
public void setEmitNamespace(final boolean emitNamespace) { | ||
marcXmlEncoder.setEmitNamespace(emitNamespace); | ||
} | ||
|
||
@Override | ||
public void omitXmlDeclaration(final boolean currentOmitXmlDeclaration) { | ||
marcXmlEncoder.omitXmlDeclaration(currentOmitXmlDeclaration); | ||
} | ||
|
||
@Override | ||
public void setXmlVersion(final String xmlVersion) { | ||
marcXmlEncoder.setXmlVersion(xmlVersion); | ||
} | ||
|
||
@Override | ||
public void setXmlEncoding(final String xmlEncoding) { | ||
marcXmlEncoder.setXmlEncoding(xmlEncoding); | ||
} | ||
|
||
@Override | ||
public void setFormatted(final boolean formatted) { | ||
marcXmlEncoder.setFormatted(formatted); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoderInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.metafacture.biblio.marc21; | ||
|
||
public interface MarcXmlEncoderInterface { | ||
|
||
/** | ||
* Sets the flag to decide whether to emit the {@value MarcXmlEncoder#NAMESPACE_NAME} | ||
* namespace | ||
* | ||
* @param emitNamespace true if the namespace is emitted, otherwise false | ||
*/ | ||
void setEmitNamespace(boolean emitNamespace); | ||
|
||
/** | ||
* Sets the flag to decide whether to omit the XML declaration. | ||
* | ||
* <strong>Default value: {@value MarcXmlEncoder#OMIT_XML_DECLARATION}</strong> | ||
* | ||
* @param currentOmitXmlDeclaration true if the XML declaration is omitted, otherwise | ||
* false | ||
*/ | ||
void omitXmlDeclaration(boolean currentOmitXmlDeclaration); | ||
|
||
/** | ||
* Sets the XML version. | ||
* | ||
* <strong>Default value: {@value MarcXmlEncoder#XML_VERSION}</strong> | ||
* | ||
* @param xmlVersion the XML version | ||
*/ | ||
void setXmlVersion(String xmlVersion); | ||
|
||
/** | ||
* Sets the XML encoding. | ||
* | ||
* <strong>Default value: {@value MarcXmlEncoder#XML_ENCODING}</strong> | ||
* | ||
* @param xmlEncoding the XML encoding | ||
*/ | ||
void setXmlEncoding(String xmlEncoding); | ||
|
||
/** | ||
* Formats the resulting xml by indentation. Aka "pretty printing". | ||
* | ||
* <strong>Default value: {@value MarcXmlEncoder#PRETTY_PRINTED}</strong> | ||
* | ||
* @param formatted true if formatting is activated, otherwise false | ||
*/ | ||
void setFormatted(boolean formatted); | ||
} |
37 changes: 37 additions & 0 deletions
37
metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21XmlEncoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.metafacture.biblio.marc21; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.metafacture.framework.FormatException; | ||
import org.metafacture.framework.MissingIdException; | ||
|
||
public class Marc21XmlEncoderTest { | ||
MarcXmlEncoderTest marcXmlEncoderTest = new MarcXmlEncoderTest(); | ||
|
||
@Before | ||
public void setUp() { | ||
marcXmlEncoderTest.encoder=new Marc21XmlEncoder(); | ||
marcXmlEncoderTest.initializeEncoder(); | ||
} | ||
|
||
@Test(expected = FormatException.class) | ||
public void createAnRecordWithLeader() { | ||
marcXmlEncoderTest.createAnRecordWithLeader(); | ||
} | ||
|
||
@Test(expected = FormatException.class) | ||
public void issue336_createRecordWithTopLevelLeader() { | ||
marcXmlEncoderTest.issue336_createRecordWithTopLevelLeader(); | ||
} | ||
|
||
@Test | ||
public void issue336_createRecordWithTopLevelLeader_Marc21Xml() { | ||
marcXmlEncoderTest.issue336_createRecordWithTopLevelLeader_Marc21Xml(); | ||
} | ||
|
||
@Test(expected = MissingIdException.class) | ||
public void issue527ShouldEmitLeaderAlwaysAsWholeString() { | ||
marcXmlEncoderTest.issue527ShouldEmitLeaderAlwaysAsWholeString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters