/** * Applies all the HTML Form Entry tags in a specific XML file (excluding * {@code <macro>, <translations>, and <repeat>)}, by calling the appropriate tag handler (see * {@see org.openmrs.module.htmlformentry.handler}) for each tag * <p/> * * @param session the current form entry session context * @param xml the xml string to process * @return the xml string (which should now be html) after tag processing * @throws Exception */ public String applyTags(FormEntrySession session, String xml) throws Exception { Document doc = HtmlFormEntryUtil.stringToDocument(xml); Node content = HtmlFormEntryUtil.findChild(doc, "htmlform"); StringWriter out = new StringWriter(); applyTagsHelper(session, new PrintWriter(out), null, content, null); return out.toString(); }
private void loadRenderElementsForEachRepeatElement(Node node, List<List<Map<String, String>>> renderMaps) throws Exception { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n.getNodeName().equalsIgnoreCase("repeat") && !n.hasAttributes()) { Node templateNode = HtmlFormEntryUtil.findChild(n, "template"); if (templateNode == null) { throw new IllegalArgumentException("All <repeat> elements must contain a child <template> element."); } List<Map<String, String>> l = new ArrayList<Map<String, String>>(); NodeList repeatNodes = n.getChildNodes(); for (int j = 0; j < repeatNodes.getLength(); j++) { Node renderNode = repeatNodes.item(j); if (renderNode.getNodeName().equalsIgnoreCase("render")) { l.add(HtmlFormEntryUtil.getNodeAttributes(renderNode)); } } renderMaps.add(l); } else { loadRenderElementsForEachRepeatElement(n, renderMaps); } } }
Node content = HtmlFormEntryUtil.findChild(doc, "htmlform");
@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()); }