Skip to content

Commit 8b064ba

Browse files
committed
A simple implementation of a feature where an OAI client can be instructed to return harvested metadata records unparsed #284
1 parent 9e22f93 commit 8b064ba

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

xoai-common/src/main/java/io/gdcc/xoai/model/oaipmh/results/record/Metadata.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public XOAIMetadata getXoaiMetadata() {
7777
if (element instanceof XOAIMetadata) return (XOAIMetadata) element;
7878
else return null;
7979
}
80+
81+
public String getMetadataAsString() {
82+
if (element instanceof EchoElement) return ((EchoElement)element).asUnparsedString();
83+
return null;
84+
}
8085

8186
/**
8287
* This is here for Dataverse 4/5 backward compatibility.

xoai-common/src/main/java/io/gdcc/xoai/xml/EchoElement.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public void write(final XmlWriter writer) throws XmlWriteException {
5252
"Cannot write XML when none given (both stream and string null)");
5353
}
5454
}
55+
56+
public String asUnparsedString() {
57+
if (xmlString != null) {
58+
return xmlString;
59+
}
60+
return null;
61+
}
5562

5663
private void write(final XmlWriter writer, final InputStream inStream)
5764
throws XmlWriteException {

xoai-service-provider/src/main/java/io/gdcc/xoai/serviceprovider/model/Context.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class Context {
2424
private final Map<String, Transformer> metadataTransformers = new HashMap<>();
2525
private String baseUrl;
2626
private Granularity granularity;
27+
private boolean saveUnparsedMetadata = false;
2728
private OAIClient client;
2829

2930
public Context() {
@@ -90,6 +91,29 @@ public Context withOAIClient(OAIClient client) {
9091
public OAIClient getClient() {
9192
return client;
9293
}
94+
95+
/**
96+
* Should this harvester skip parsing the "metadata" sections of oai records
97+
* in the bodies of GetRecord and ListRecords responses, and cache and make
98+
* them available as unparsed Strings instead.
99+
*
100+
* @return boolean
101+
*/
102+
public boolean isSaveUnparsedMetadata() {
103+
return this.saveUnparsedMetadata;
104+
}
105+
106+
/**
107+
* Instruct this harvester not to attempt to parse the "metadata" sections of
108+
* oai records in the bodies of GetRecord and ListRecords responses, but
109+
* cache and make them available as Strings instead.
110+
*
111+
* @return A Context
112+
*/
113+
public Context withSaveUnparsedMetadata() {
114+
this.saveUnparsedMetadata = true;
115+
return this;
116+
}
93117

94118
public enum KnownTransformer {
95119
OAI_DC("to_xoai/oai_dc.xsl");

xoai-service-provider/src/main/java/io/gdcc/xoai/serviceprovider/parsers/RecordParser.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,27 @@ public Record parse(XmlReader reader) throws XmlReaderException {
4747
if (!record.getHeader().isDeleted()) {
4848
reader.next(elementName(localPart(equalTo("metadata")))).next(aStartElement());
4949
String content = reader.retrieveCurrentAsString();
50-
ByteArrayInputStream inputStream =
51-
new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
52-
XSLPipeline pipeline =
53-
new XSLPipeline(inputStream, true)
54-
.apply(context.getMetadataTransformer(metadataPrefix));
50+
System.out.println("Metadata content: "+content);
51+
52+
if (this.context.isSaveUnparsedMetadata()) {
53+
record.withMetadata(new Metadata(content));
54+
} else {
55+
ByteArrayInputStream inputStream
56+
= new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
5557

56-
if (context.hasTransformer()) pipeline.apply(context.getTransformer());
58+
XSLPipeline pipeline
59+
= new XSLPipeline(inputStream, true)
60+
.apply(context.getMetadataTransformer(metadataPrefix));
5761

58-
try {
59-
record.withMetadata(new Metadata(new MetadataParser().parse(pipeline.process())));
60-
} catch (TransformerException e) {
61-
throw new InternalHarvestException("Unable to process transformer", e);
62+
if (context.hasTransformer()) {
63+
pipeline.apply(context.getTransformer());
64+
}
65+
66+
try {
67+
record.withMetadata(new Metadata(new MetadataParser().parse(pipeline.process())));
68+
} catch (TransformerException e) {
69+
throw new InternalHarvestException("Unable to process transformer", e);
70+
}
6271
}
6372
}
6473

0 commit comments

Comments
 (0)