diff --git a/api/src/main/java/org/openmrs/module/htmlformentry/FormEntrySession.java b/api/src/main/java/org/openmrs/module/htmlformentry/FormEntrySession.java
index 374eb96a2..7d41f619f 100644
--- a/api/src/main/java/org/openmrs/module/htmlformentry/FormEntrySession.java
+++ b/api/src/main/java/org/openmrs/module/htmlformentry/FormEntrySession.java
@@ -1522,4 +1522,8 @@ public String generateControlFormPath(String controlId, Integer controlCounter)
return formField;
}
+
+ public TagRegister getTagRegister() {
+ return htmlGenerator.getTagRegister();
+ }
}
diff --git a/api/src/main/java/org/openmrs/module/htmlformentry/HtmlFormEntryGenerator.java b/api/src/main/java/org/openmrs/module/htmlformentry/HtmlFormEntryGenerator.java
index 3b34f602c..3e3a430bb 100644
--- a/api/src/main/java/org/openmrs/module/htmlformentry/HtmlFormEntryGenerator.java
+++ b/api/src/main/java/org/openmrs/module/htmlformentry/HtmlFormEntryGenerator.java
@@ -1,18 +1,5 @@
package org.openmrs.module.htmlformentry;
-import java.io.ByteArrayInputStream;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
import org.apache.commons.lang.StringUtils;
import org.openmrs.Role;
import org.openmrs.User;
@@ -26,6 +13,19 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
+import java.io.ByteArrayInputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
/**
* Provides methods to take a {@code ...} xml block and turns it into HTML to
* be displayed as a form in a web browser. It can apply the {@code ...} section,
@@ -33,6 +33,8 @@
*/
public class HtmlFormEntryGenerator implements TagHandler {
+ private TagRegister tagRegister = new TagRegister();
+
/**
* @see #applyMacros(FormEntrySession, String) This method simply delegates to the
* applyMacros(FormEntrySession, String) method with a null FormEntry session. This is retained
@@ -608,10 +610,13 @@ private void applyTagsHelper(FormEntrySession session, PrintWriter out, Node par
}
}
- if (handler == null)
+ if (handler == null) {
handler = this; // do default actions
-
+ }
+
try {
+ boolean isRegisteredTag = (handler != this);
+ tagRegister.startTag(node, isRegisteredTag);
boolean handleContents = handler.doStartTag(session, out, parent, node);
// Unless the handler told us to skip them, then iterate over any children
@@ -635,6 +640,7 @@ private void applyTagsHelper(FormEntrySession session, PrintWriter out, Node par
}
handler.doEndTag(session, out, parent, node);
+ tagRegister.endTag(node, isRegisteredTag);
}
catch (BadFormDesignException e) {
out.print("
" + handler
@@ -857,6 +863,10 @@ public static StringBuilder removeFirstTagOccurrence(StringBuilder sb, String ta
return sb;
}
+ public TagRegister getTagRegister() {
+ return tagRegister;
+ }
+
/**
* Deprecated methods
*/
@@ -868,5 +878,4 @@ public static StringBuilder removeFirstTagOccurrence(StringBuilder sb, String ta
public String applyTemplates(String xml) throws Exception {
return applyRepeats(xml);
}
-
}
diff --git a/api/src/main/java/org/openmrs/module/htmlformentry/HtmlFormEntryTag.java b/api/src/main/java/org/openmrs/module/htmlformentry/HtmlFormEntryTag.java
new file mode 100644
index 000000000..5cf184b2d
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/htmlformentry/HtmlFormEntryTag.java
@@ -0,0 +1,72 @@
+package org.openmrs.module.htmlformentry;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * A tag represents a specific xml node in an htmlform
+ */
+public class HtmlFormEntryTag {
+
+ private String name;
+
+ private boolean registeredTag = false;
+
+ private Map attributes = new HashMap<>();
+
+ private List childTags = new ArrayList<>();
+
+ public HtmlFormEntryTag() {
+ }
+
+ public boolean hasAttribute(String name, String value) {
+ String v = attributes.get(name);
+ return v != null && v.equalsIgnoreCase(value);
+ }
+
+ public boolean hasAttribute(String name) {
+ return attributes.containsKey(name);
+ }
+
+ @Override
+ public String toString() {
+ return getName() + getAttributes();
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public boolean isRegisteredTag() {
+ return registeredTag;
+ }
+
+ public void setRegisteredTag(boolean registeredTag) {
+ this.registeredTag = registeredTag;
+ }
+
+ public Map getAttributes() {
+ if (attributes == null) {
+ attributes = new HashMap<>();
+ }
+ return attributes;
+ }
+
+ public void setAttributes(Map attributes) {
+ this.attributes = attributes;
+ }
+
+ public List getChildTags() {
+ return childTags;
+ }
+
+ public void setChildTags(List childTags) {
+ this.childTags = childTags;
+ }
+}
diff --git a/api/src/main/java/org/openmrs/module/htmlformentry/TagRegister.java b/api/src/main/java/org/openmrs/module/htmlformentry/TagRegister.java
new file mode 100644
index 000000000..73ae6d43b
--- /dev/null
+++ b/api/src/main/java/org/openmrs/module/htmlformentry/TagRegister.java
@@ -0,0 +1,51 @@
+package org.openmrs.module.htmlformentry;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Stack;
+
+/**
+ * Provides a register of all handled tags in a html form
+ */
+public class TagRegister {
+
+ protected final Log log = LogFactory.getLog(getClass());
+
+ private final List tags = new ArrayList<>();
+
+ private final Stack tagStack = new Stack<>();
+
+ public List getTags() {
+ return tags;
+ }
+
+ public void startTag(Node node, boolean registeredTag) {
+ HtmlFormEntryTag tag = new HtmlFormEntryTag();
+ tag.setName(node.getNodeName());
+ tag.setRegisteredTag(registeredTag);
+ NamedNodeMap attrs = node.getAttributes();
+ if (attrs != null) {
+ for (int i = 0; i < attrs.getLength(); i++) {
+ Node attr = attrs.item(i);
+ tag.getAttributes().put(attr.getNodeName(), attr.getNodeValue());
+ }
+ }
+ if (tagStack.isEmpty()) {
+ tags.add(tag);
+ } else {
+ tagStack.peek().getChildTags().add(tag);
+ }
+ tagStack.push(tag);
+ log.trace("startTag: " + tag.getName());
+ }
+
+ public void endTag(Node node, boolean registeredTag) {
+ HtmlFormEntryTag tag = tagStack.pop();
+ log.trace("endTag:" + tag.getName());
+ }
+}