DSpaceControlledVocabulary extends SelfNamedPlugin implements ChoiceAuthority { @Override public Choices getMatches(String field, String text, Collection collection, int start, int limit, String locale) { init(); log.debug("Getting matches for '" + text + "'"); String xpathExpression = ""; String[] textHierarchy = text.split(hierarchyDelimiter, -1); for (int i = 0; i < textHierarchy.length; i++) { xpathExpression += String.format(xpathTemplate, textHierarchy[i].replaceAll("'", "&apos;").toLowerCase()); } XPath xpath = XPathFactory.newInstance().newXPath(); Choice[] choices; try { NodeList results = (NodeList) xpath.evaluate(xpathExpression, vocabulary, XPathConstants.NODESET); String[] authorities = new String[results.getLength()]; String[] values = new String[results.getLength()]; String[] labels = new String[results.getLength()]; for (int i = 0; i < results.getLength(); i++) { Node node = results.item(i); String hierarchy = this.buildString(node); if (this.suggestHierarchy) { labels[i] = hierarchy; } else { labels[i] = node.getAttributes().getNamedItem("label").getNodeValue(); } if (this.storeHierarchy) { values[i] = hierarchy; } else { values[i] = node.getAttributes().getNamedItem("label").getNodeValue(); } Node idAttr = node.getAttributes().getNamedItem("id"); if (null != idAttr) authorities[i] = idAttr.getNodeValue(); } int resultCount = labels.length - start; if ((limit > 0) && (resultCount > limit)) resultCount = limit; choices = new Choice[resultCount]; if (resultCount > 0) { for (int i = 0; i < resultCount; i++) { choices[i] = new Choice(authorities[start + i], values[start + i], labels[start + i]); } } } catch(XPathExpressionException e) { choices = new Choice[0]; } return new Choices(choices, 0, choices.length, Choices.CF_AMBIGUOUS, false); } DSpaceControlledVocabulary(); static String[] getPluginNames(); @Override Choices getMatches(String field, String text, Collection collection, int start, int limit, String locale); @Override Choices getBestMatch(String field, String text, Collection collection, String locale); @Override String getLabel(String field, String key, String locale);  }
@Test public void testGetMatches() throws IOException, ClassNotFoundException { System.out.println("getMatches"); final String PLUGIN_INTERFACE = "org.dspace.content.authority.ChoiceAuthority"; String field = null; String text = "north 40"; Collection collection = null; int start = 0; int limit = 0; String locale = null; DSpaceControlledVocabulary instance = (DSpaceControlledVocabulary) CoreServiceFactory.getInstance().getPluginService().getNamedPlugin(Class.forName(PLUGIN_INTERFACE), "farm"); assertNotNull(instance); Choices result = instance.getMatches(field, text, collection, start, limit, locale); assertEquals("the farm::north 40", result.values[0].value); }