DocumentUriGenerator implements UriGenerator<Document> { @Override public String generateUri(Document document) throws Exception { Element el = null; if (namespace == null) { el = document.getRootElement().getChild(elementId); } else { el = document.getRootElement().getChild(elementId, Namespace.getNamespace(namespace)); } if (el == null) { throw new RuntimeException(namespace + ":" + elementId + " does not exist"); } else if (el.getText().isEmpty()) { throw new RuntimeException(namespace + ":" + elementId + " is an empty string"); } else { return el.getText(); } } DocumentUriGenerator(String namespace, String elementId);  DocumentUriGenerator(String elementId); @Override String generateUri(Document document);  }
@Test public void generateIdFromDocumentWithoutNamespaceTest() throws Exception { SAXBuilder jdomBuilder = new SAXBuilder(); Document document = jdomBuilder.build(new StringReader("<xml><id>ABC</id><notID>123</notID></xml>")); DocumentUriGenerator uriGenerator = new DocumentUriGenerator("id"); assertThat("Expecting ABC is generated URI", uriGenerator.generateUri(document).equals("ABC")); }
@Test public void generateIdFromDocumentWithNamespaceTest() throws Exception { SAXBuilder jdomBuilder = new SAXBuilder(); Document document = jdomBuilder.build(new StringReader("<xml xmlns=\"abc\"><id>ABC</id><notID>123</notID></xml>")); DocumentUriGenerator uriGenerator = new DocumentUriGenerator("abc","id"); assertThat("Expecting ABC is generated uri", uriGenerator.generateUri(document).equals("ABC")); }