From 44d0fcd0d313b32b506b561aac9041dd0f2b1264 Mon Sep 17 00:00:00 2001 From: Juraj Roka <95219754+jr-rk@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:39:02 +0100 Subject: [PATCH] JCU/Fix dspace-customers 1083 V4: bind vocabulary node id as an XPath variable Port of dtq-dev-93 PR 1650 (commit f08abc4357). --- .../authority/DSpaceControlledVocabulary.java | 62 +++++++++++++++---- .../config/controlled-vocabularies/farm.xml | 6 +- .../DSpaceControlledVocabularyTest.java | 33 ++++++++++ 3 files changed, 88 insertions(+), 13 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java b/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java index b0c7c5c4aa7d..6db79fdc13bb 100644 --- a/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java +++ b/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java @@ -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"; @@ -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); @@ -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 getChoicesFromNodeList(NodeList results, int start, int limit) { @@ -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 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), diff --git a/dspace-api/src/test/data/dspaceFolder/config/controlled-vocabularies/farm.xml b/dspace-api/src/test/data/dspaceFolder/config/controlled-vocabularies/farm.xml index c77d1c5ff291..e4c1469b6fd7 100644 --- a/dspace-api/src/test/data/dspaceFolder/config/controlled-vocabularies/farm.xml +++ b/dspace-api/src/test/data/dspaceFolder/config/controlled-vocabularies/farm.xml @@ -2,6 +2,10 @@ - + + + + + diff --git a/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java b/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java index 7012556ceb52..8b9128f32671 100644 --- a/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java +++ b/dspace-api/src/test/java/org/dspace/content/authority/DSpaceControlledVocabularyTest.java @@ -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; @@ -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)