Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data): add OAI XSLT support #15 #62

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ It happily accepts an `InputStream`, being read and written as a raw response to
and serve from the main application (Dataverse) without jumping through the
loops of the XML transformer for each item, which is MUCH faster.

Using `DataProvider.getOaiXslt()` an implementing application can retrieve a local copy
of https://github.com/eprints/eprints/blob/3.3/lib/static/oai2.xsl to serve it
from a local (servlet) endpoint. It allows for a much nicer UI experience when
accessing the OAI-PMH endpoint with a browser. Applications may provide a hyperref
to the endpoint (or other static location) by using `xmlWriter.writeStylesheet()`
in their OAI servlet.

## Advanced Configuration

**NOTE: THE FOLLOWING HAS BEEN COPIED FROM THE DSPACE WIKI AND IS OF LIMITED USE
Expand Down
9 changes: 9 additions & 0 deletions xoai-common/src/main/java/io/gdcc/xoai/xml/XmlWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,13 @@ public void write(ResumptionToken.Value value) throws XmlWriteException {
throw new XmlWriteException(e);
}
}

public void writeStylesheet(String href) throws XMLStreamException {
if (href == null) {
throw new XMLStreamException(
"May not pass a null hyper reference to the XSLT processing instruction");
}
super.writeProcessingInstruction(
"xml-stylesheet", "type=\"text/xsl\" href=\"" + href + "\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import io.gdcc.xoai.model.oaipmh.ResumptionToken;
import io.gdcc.xoai.model.oaipmh.verbs.Verb.Type;
import io.gdcc.xoai.services.api.DateProvider;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -189,4 +193,31 @@ public OAIPMH handle(Request request) {
return this.errorsHandler.handle(oaipmh, e);
}
}

/**
* Let an implementing application retrieve the OAI XSLT for nicer UI of results. The app needs
* to create some endpoint to serve the content and reference that endpoint in {@link
* io.gdcc.xoai.xml.XmlWriter#writeStylesheet(String)}. The app might also choose to cache the
* String instead of re-reading it.
*
* @return The XSLT document as complete String
*/
public static final String getOaiXSLT() {
// get resource from classpath
ClassLoader classLoader = DataProvider.class.getClassLoader();
URL oaiXsltResource = classLoader.getResource("oai2.xsl");

// Prevent errors if file not found or could not be loaded
if (oaiXsltResource == null) {
log.warn("Could not find or load OAI XSLT file, class loader returned null");
return "";
}

try (InputStream inStream = oaiXsltResource.openStream()) {
return new String(inStream.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
log.warn("Could not read OAI XSLT file", e);
return "";
}
}
}
Loading