Commit c09033da authored by Günter Hipler's avatar Günter Hipler
Browse files

metadata record section of an OAI result needs a namespace - otherwise...

metadata record section of an OAI result needs a namespace - otherwise standard OAI clients are not able to fetch the result
parent 3e67055e
Pipeline #14965 passed with stage
in 2 minutes and 43 seconds
......@@ -61,8 +61,13 @@ trait ElasticsearchComponent extends OaiRepository {
searchResponse match {
case Success(searchResponse) =>
val si = searchResponse.getScrollId
println(si)
val resumptionToken: Option[ResumptionToken] = Option(searchResponse.getScrollId).map(
//scroll_id is always the same -> for this specific context
//condition to finish the fetching is to compare the length of the resultlist
//compare: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html
//otherwise we will get into an empty loop
val scrollId = if (searchResponse.getHits.getHits.length == 0) Option.empty[String] else Option(searchResponse.getScrollId)
val resumptionToken: Option[ResumptionToken] = scrollId.map(
ESResumptionTokenHelper(_))
val myMap = searchResponse.getHits.getHits.map(hit => (hit.getId, hit.getSourceAsMap)).map(
......
......@@ -33,7 +33,9 @@ sealed abstract class OaiResponse {
val oaiFrame =
/*<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" >*/
<OAI-PMH xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd" xmlns="http://www.openarchives.org/OAI/2.0/" >
// <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" >
<OAI-PMH xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd" xmlns="http://www.openarchives.org/OAI/2.0/" >
<responseDate>{Instant.now()}</responseDate>
{createRequestTag}
</OAI-PMH>
......
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="fn">
<!--xmlns:fn="http://www.w3.org/2005/xpath-functions"> -->
<xsl:output method="xml"
encoding="UTF-8"
indent="yes"
omit-xml-declaration="yes"
/>
<!--
<xsl:attribute-set name="edm">
<xsl:attribute name="edm">https://europeana.org/irgendwas</xsl:attribute>
</xsl:attribute-set>
-->
<xsl:template match="record">
<xsl:element namespace="https://europeana.org/irgendwas" name="edm:{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
......@@ -14,7 +14,6 @@ object Json2XML {
def singleJsonDoc2Xml(doc: JsValue, root: String = "record"): Elem = {
//only Helper function until we know how and produce valid OAI data
val esJsonRespone = JsObject(Seq(root -> doc)).toString
val config = new JsonXMLConfigBuilder().multiplePI(false).build();
val output = new StringWriter()
......@@ -28,7 +27,11 @@ object Json2XML {
prettyWriter.add(reader)
reader.close()
writer.close()
scala.xml.XML.loadString(output.toString)
//at least the record tag of the metadata section for each document needs a namespace.
//otherwise standard OAI client tools are not able to process the result
scala.xml.XML.loadString(new TemplateTransformer(output.toString).transform(XSLTWrapper.edmNamespaceTransformer))
}
......
package utilities;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class TemplateCreator {
private String templatePath;
public TemplateCreator(String transformerFactory,
String templatePath) {
this(templatePath);
System.setProperty("javax.xml.transform.TransformerFactory",transformerFactory);
}
public TemplateCreator(String templatePath) {
this.templatePath = templatePath;
//as a more general solution we do not use net.sf.saxon.TransformerFactoryImpl as default
//System.setProperty("javax.xml.transform.TransformerFactory","net.sf.saxon.TransformerFactoryImpl");
}
public Transformer createTransformer() {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
StreamSource source = null;
Transformer transformer = null;
if (new File(templatePath).exists()) {
source = new StreamSource(templatePath);
try {
transformer = transformerFactory.newTransformer(source);
} catch (TransformerConfigurationException ex) {
ex.printStackTrace();
}
}
return transformer;
}
public Transformer createTransformerFromResource() {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
StreamSource source = null;
Transformer transformer = null;
//ClassLoader classLoader = TemplateCreator.class.getClassLoader();
ClassLoader classLoader = TemplateCreator.class.getClassLoader();
//try (InputStream is = getClass()
// .getClassLoader().getResourceAsStream(templatePath)) {
try (InputStream is = classLoader.getResourceAsStream(templatePath)) {
source = new StreamSource(is);
transformer = transformerFactory.newTransformer(source);
} catch (TransformerConfigurationException | IOException exc) {
throw new RuntimeException(exc);
}
return transformer;
}
}
package utilities;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
public class TemplateTransformer {
private final StringReader record;
public TemplateTransformer(String recordToTransform) {
record = new StringReader(recordToTransform);
}
public XSLTDataObject transformIntoDataObject(Transformer transformer) {
StringWriter transformedRecord = getTransformation(transformer);
XSLTDataObject dataObject = new XSLTDataObject();
dataObject.record = transformedRecord.toString();
dataObject.additions = new HashMap<>();
return dataObject;
}
public String transform(Transformer transformer) {
StringWriter transformedRecord = getTransformation(transformer);
return transformedRecord.toString();
}
StringWriter getTransformation(Transformer transformer) {
Source source = new StreamSource(record);
StringWriter transformedRecord = new StringWriter();
StreamResult target = new StreamResult(transformedRecord);
try {
transformer.transform(source, target);
} catch (TransformerException tex) {
throw new RuntimeException(tex);
}
return transformedRecord;
}
}
package utilities;
import java.util.HashMap;
/**
* Created by swissbib on 29.05.17.
*/
public class XSLTDataObject {
public HashMap<String,String> additions = new HashMap<>();
public String record = "";
@Override
public String toString() {
return record;
}
}
package utilities
import javax.xml.transform.Transformer
object XSLTWrapper {
val TRANSFORMERIMPL = "net.sf.saxon.TransformerFactoryImpl"
val EDM_namespace = "resources/recordnscreation.xsl"
lazy val edmNamespaceTransformer: Transformer = {
new TemplateCreator(TRANSFORMERIMPL, EDM_namespace).createTransformerFromResource
}
}
......@@ -36,7 +36,8 @@ lazy val root = (project in file("."))
scala_xml_module,
upickl,
httprequests,
jwtcore
jwtcore,
saxonhe
),
scalacOptions ++= Seq(
"-feature",
......
......@@ -42,5 +42,7 @@ object Dependencies {
lazy val jwtcore = "com.pauldijou" %% "jwt-core" % "4.3.0"
lazy val saxonhe = "net.sf.saxon" % "Saxon-HE" % "10.2"
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment