Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public class DSpaceControlledVocabulary extends SelfNamedPlugin implements Hiera
xpathTemplate = "//node[contains(translate(@label,'" + upper + "','" + lower + "'),%s)]";
}
protected static String idTemplate = "//node[@id = %s]";
protected static String idTemplateQuoted = "//node[@id = '%s']";
protected static String idTemplateQuoted = "//node[@id = $nodeId]";
protected static String labelTemplate = "//node[@label = %s]";
protected static String idParentTemplate = "//node[@id = '%s']/parent::isComposedBy/parent::node";
protected static String idParentTemplate = "//node[@id = $nodeId]/parent::isComposedBy/parent::node";
protected static String rootTemplate = "/node";
protected static String idAttribute = "id";
protected static String labelAttribute = "label";
Expand Down Expand Up @@ -288,16 +288,14 @@ public Choices getTopChoices(String authorityName, int start, int limit, String
@Override
public Choices getChoicesByParent(String authorityName, String parentId, int start, int limit, String locale) {
init(locale);
String xpathExpression = String.format(idTemplateQuoted, parentId);
return getChoicesByXpath(xpathExpression, start, limit);
return getChoicesByXpathWithId(idTemplateQuoted, parentId, start, limit);
}

@Override
public Choice getParentChoice(String authorityName, String childId, String locale) {
init(locale);
try {
String xpathExpression = String.format(idParentTemplate, childId);
Choice choice = createChoiceFromNode(getNodeFromXPath(xpathExpression));
Choice choice = createChoiceFromNode(getNodeByIdFromXPath(idParentTemplate, childId));
return choice;
} catch (XPathExpressionException e) {
log.error(e.getMessage(), e);
Expand All @@ -317,15 +315,18 @@ private boolean isRootElement(Node node) {

private Node getNode(String key, String locale) throws XPathExpressionException {
init(locale);
String xpathExpression = String.format(idTemplateQuoted, key);
Node node = getNodeFromXPath(xpathExpression);
return node;
return getNodeByIdFromXPath(idTemplateQuoted, key);
}

private Node getNodeFromXPath(String xpathExpression) throws XPathExpressionException {
private Node getNodeByIdFromXPath(String xpathTemplate, String nodeId) throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
Node node = (Node) xpath.evaluate(xpathExpression, vocabulary, XPathConstants.NODE);
return node;
xpath.setXPathVariableResolver(variableName -> {
if ("nodeId".equals(variableName.getLocalPart())) {
return nodeId;
}
throw new IllegalArgumentException("Unexpected variable: " + variableName);
});
return (Node) xpath.evaluate(xpathTemplate, vocabulary, XPathConstants.NODE);
}

private List<Choice> getChoicesFromNodeList(NodeList results, int start, int limit) {
Expand Down Expand Up @@ -493,6 +494,43 @@ private Choices getChoicesByXpath(String xpathExpression, int start, int limit)
return new Choices(false);
}

private Choices getChoicesByXpathWithId(String xpathTemplate, String nodeId, int start, int limit) {
List<Choice> choices = new ArrayList<>();
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setXPathVariableResolver(variableName -> {
if ("nodeId".equals(variableName.getLocalPart())) {
return nodeId;
}
throw new IllegalArgumentException("Unexpected variable: " + variableName);
});
try {
Node parentNode = (Node) xpath.evaluate(xpathTemplate, vocabulary, XPathConstants.NODE);
int count = 0;
if (parentNode != null) {
NodeList childNodes = (NodeList) xpath.evaluate(".//isComposedBy", parentNode, XPathConstants.NODE);
if (null != childNodes) {
for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
if (childNode != null && "node".equals(childNode.getNodeName())) {
if (count < start || choices.size() >= limit) {
count++;
continue;
}
count++;
choices.add(createChoiceFromNode(childNode));
}
}
}
return new Choices(choices.toArray(new Choice[choices.size()]), start, count,
Choices.CF_AMBIGUOUS, false);
}
} catch (XPathExpressionException e) {
log.warn(e.getMessage(), e);
return new Choices(true);
}
return new Choices(false);
}

private Choice createChoiceFromNode(Node node) {
if (node != null && !isRootElement(node)) {
Choice choice = new Choice(getAuthority(node), getLabel(node), getValue(node),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<node label='the farm'>
<isComposedBy>
<node label='north 40'/>
<node id='s40' label='south 40'/>
<node id='s40' label='south 40'>
<isComposedBy>
<node id='pond' label='pond'/>
</isComposedBy>
</node>
</isComposedBy>
</node>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.IOException;

Expand Down Expand Up @@ -91,6 +92,38 @@ public void testGetMatches() throws IOException, ClassNotFoundException {
assertEquals("north 40", result.values[0].value);
}

/**
* Node ids (entryID, parent, child) must be compared as plain strings, not spliced into the XPath expression.
* @throws java.lang.ClassNotFoundException passed through.
*/
@Test
public void testNodeIdIsNotEvaluatedAsXPath() throws ClassNotFoundException {
DSpaceControlledVocabulary instance = (DSpaceControlledVocabulary)
CoreServiceFactory.getInstance().getPluginService()
.getNamedPlugin(Class.forName("org.dspace.content.authority.ChoiceAuthority"), "farm");
assertNotNull(instance);

Choice choice = instance.getChoice("s40", null);
assertNotNull(choice);
assertEquals("south 40", choice.label);

// Spliced into the expression this would select //node[@id = 'x' or @label='south 40']
assertNull(instance.getChoice("x' or @label='south 40", null));
// ... and here the root node, returning its children
Choices children = instance.getChoicesByParent("farm", "x' or @label='the farm", 0, 10, null);
assertEquals(0, children.values.length);

children = instance.getChoicesByParent("farm", "s40", 0, 10, null);
assertEquals(1, children.values.length);
assertEquals("pond", children.values[0].authority);

Choice parent = instance.getParentChoice("farm", "pond", null);
assertNotNull(parent);
assertEquals("s40", parent.authority);
// ... and here the parent of 'pond'
assertNull(instance.getParentChoice("farm", "x' or @id='pond", null));
}

/**
* Test of getMatches method of class
* DSpaceControlledVocabulary using a localized controlled vocabulary with no locale (fallback to default)
Expand Down
Loading