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

fix CMDI restrictions of use; use LinkedList, not NodeList #738

Merged
merged 1 commit into from
Aug 27, 2024
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
27 changes: 6 additions & 21 deletions dspace-oai/src/main/java/org/dspace/utils/LicenseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
* Class is copied from the LINDAT/CLARIAH-CZ (This class is taken from UFAL-clarin.
* https://github.com/ufal/clarin-dspace/blob
Expand Down Expand Up @@ -200,28 +198,15 @@ public static String uriToAvailability(String uri) {
return "available-restrictedUse";
}

public static org.w3c.dom.NodeList uriToRestrictions(String uri)
public static List<String> uriToRestrictions(String uri)
throws ParserConfigurationException {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder builder;
builder = factory.newDocumentBuilder();

Document doc = builder.newDocument();
Element root = doc.createElement("restrictions");

String restrictions = _uri2restrictions.get(uri);
if (Objects.isNull(restrictions)) {
restrictions = "other";
}

for (String restriction : restrictions.split(",")) {
Element res = doc.createElement("restriction");
res.appendChild(doc.createTextNode(restriction));
root.appendChild(res);
}

return root.getElementsByTagName("restriction");
List<String> ret = new LinkedList<>();
Collections.addAll(ret, restrictions.split(","));
return ret;
}

public static void main(String[] args) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,25 @@

import static org.dspace.xoai.services.impl.resources.functions.StringXSLFunction.BASE;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.ExtensionFunction;
import net.sf.saxon.s9api.ItemType;
import net.sf.saxon.s9api.OccurrenceIndicator;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.SequenceType;
import net.sf.saxon.s9api.XdmAtomicValue;
import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmValue;
import org.apache.logging.log4j.Logger;
import org.bouncycastle.util.Arrays;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;



/**
Expand All @@ -43,15 +42,15 @@ public abstract class NodeListXslFunction implements ExtensionFunction {
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(NodeListXslFunction.class);
protected abstract String getFnName();

protected abstract NodeList getNodeList(String param);
protected abstract List<String> getList(String param);
@Override
final public QName getName() {
return new QName(BASE, getFnName());
}

@Override
final public SequenceType getResultType() {
return SequenceType.makeSequenceType(ItemType.ANY_NODE, OccurrenceIndicator.ZERO_OR_MORE);
return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ZERO_OR_MORE);
}

@Override
Expand All @@ -76,12 +75,7 @@ final public XdmValue call(XdmValue[] xdmValues) throws SaxonApiException {
val = "";
}


NodeList nodeList = getNodeList(val);
// TODO: 2024/07 (mb) Below is attempted fix that does not work yet.
// see https://github.com/dataquest-dev/DSpace/issues/709
// Node oneNode = nodeList.item(0);

List<String> list = getList(val);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder db = null;
try {
Expand All @@ -90,24 +84,15 @@ final public XdmValue call(XdmValue[] xdmValues) throws SaxonApiException {
Element rootElement = newDoc.createElement("root");
newDoc.appendChild(rootElement);

for (int i = 0; i < nodeList.getLength(); i++) {
try {
rootElement.appendChild(newDoc.importNode(nodeList.item(i), true));
} catch (Exception e) {
log.error("Error while importing node", e);
}

List<XdmItem> items = new LinkedList<>();
for (String item : list) {
items.add(new XdmAtomicValue(item));
}
Processor processor = new Processor(false);
DocumentBuilder saxonDb = processor.newDocumentBuilder();
XdmValue xdmValue = saxonDb.wrap(newDoc);
return new XdmValue(items);

return xdmValue;
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

package org.dspace.xoai.services.impl.resources.functions;

import java.util.List;
import javax.xml.parsers.ParserConfigurationException;

import org.dspace.utils.LicenseUtil;


/**
* Serves as proxy for call from XSL engine. Calls LicenseUtil
* @author Marian Berger (marian.berger at dataquest.sk)
Expand All @@ -23,7 +25,7 @@ protected String getFnName() {
}

@Override
protected org.w3c.dom.NodeList getNodeList(String param) {
protected List<String> getList(String param) {
try {
return LicenseUtil.uriToRestrictions(param);
} catch (ParserConfigurationException e) {
Expand Down
Loading