/** * Finds the first descendant of this node with the given tag name * @param node * @param tagName * @return */ public static Node findDescendant(Node node, String tagName) { if (node == null) return null; NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) { Node child = children.item(i); if (tagName.equals(node.getNodeName())) return node; Node matchingDescendant = findDescendant(child, tagName); if (matchingDescendant != null) { return matchingDescendant; } } return null; }
private Node getSectionNode(String xml) throws Exception { Document document = HtmlFormEntryUtil.stringToDocument("<htmlform>" + xml + "</htmlform>"); Node section = HtmlFormEntryUtil.findDescendant(document, "section"); if (section == null) { throw new IllegalArgumentException("Could not find <section> tag"); } return section; }
private TagAnalysis validateObsGroupTag(String xml) throws Exception { Document document = HtmlFormEntryUtil.stringToDocument(xml); Node obsGroupNode = HtmlFormEntryUtil.findDescendant(document, "obsgroup"); TagAnalysis analysis = new ObsGroupTagHandler().validate(obsGroupNode); return analysis; }
private TagAnalysis validateObsTag(String xml) throws Exception { Document document = HtmlFormEntryUtil.stringToDocument(xml); Node obsNode = HtmlFormEntryUtil.findDescendant(document, "obs"); TagAnalysis analysis = new ObsTagHandler().validate(obsNode); return analysis; } }
@Test public void validate_shouldWarnWhenObsgroupConceptIsNotASet() throws Exception { String xml = "<htmlform>\n" + "<obsgroup groupingConceptId=\"10\">\n" + "<obs conceptId=\"3\" />\n" + "<obs conceptId=\"4\" />\n" + "</obsgroup>\n" + "</htmlform>"; Document document = HtmlFormEntryUtil.stringToDocument(xml); Node obsGroupNode = HtmlFormEntryUtil.findDescendant(document, "obsgroup"); TagAnalysis analysis = new ObsGroupTagHandler().validate(obsGroupNode); Assert.assertEquals(1, analysis.getWarnings().size()); }
@Test public void validate_shouldWarnWhenObsIsNotAMemberOfParentObsgroup() throws Exception { String xml = "<htmlform>\n" + "<obsgroup groupingConceptId=\"23\">\n" + "<obs conceptId=\"4\" />\n" + "</obsgroup>\n </htmlform>"; Document document = HtmlFormEntryUtil.stringToDocument(xml); Node parentObsGroupNode = HtmlFormEntryUtil.findDescendant(document, "obsgroup"); Assert.assertEquals("23", HtmlFormEntryUtil.getNodeAttribute(parentObsGroupNode, "groupingConceptId", null)); Node obsNode = HtmlFormEntryUtil.findChild(parentObsGroupNode, "obs"); TagAnalysis analysis = new ObsTagHandler().validate(obsNode); Assert.assertEquals(1, analysis.getWarnings().size()); }
@Test public void validate_shouldPassWhenObsIsAMemberOfParentObsgroup() throws Exception { String xml = "<htmlform>\n" + "<obsgroup groupingConceptId=\"23\">\n" + "<obs conceptId=\"18\" />\n" + " </obsgroup>\n </htmlform>"; Document document = HtmlFormEntryUtil.stringToDocument(xml); Node parentObsGroupNode = HtmlFormEntryUtil.findDescendant(document, "obsgroup"); Assert.assertEquals("23", HtmlFormEntryUtil.getNodeAttribute(parentObsGroupNode, "groupingConceptId", null)); Node obsNode = HtmlFormEntryUtil.findChild(parentObsGroupNode, "obs"); TagAnalysis analysis = new ObsTagHandler().validate(obsNode); Assert.assertEquals(0, analysis.getWarnings().size()); }