Skip to content

Commit

Permalink
HTML-845 - initial code for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton committed Aug 30, 2024
1 parent c0b5a61 commit 0244acc
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1522,4 +1522,8 @@ public String generateControlFormPath(String controlId, Integer controlCounter)

return formField;
}

public TagRegister getTagRegister() {
return htmlGenerator.getTagRegister();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,13 +13,28 @@
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 <htmlform>...</htmlform>} xml block and turns it into HTML to
* be displayed as a form in a web browser. It can apply the {@code <macros>...</macros>} section,
* and replace tags like {@code <obs/>}.
*/
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
Expand Down Expand Up @@ -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
Expand All @@ -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("<div class=\"error\">" + handler
Expand Down Expand Up @@ -857,6 +863,10 @@ public static StringBuilder removeFirstTagOccurrence(StringBuilder sb, String ta
return sb;
}

public TagRegister getTagRegister() {
return tagRegister;
}

/**
* Deprecated methods
*/
Expand All @@ -868,5 +878,4 @@ public static StringBuilder removeFirstTagOccurrence(StringBuilder sb, String ta
public String applyTemplates(String xml) throws Exception {
return applyRepeats(xml);
}

}
Original file line number Diff line number Diff line change
@@ -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<String, String> attributes = new HashMap<>();

private List<HtmlFormEntryTag> 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<String, String> getAttributes() {
if (attributes == null) {
attributes = new HashMap<>();
}
return attributes;
}

public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}

public List<HtmlFormEntryTag> getChildTags() {
return childTags;
}

public void setChildTags(List<HtmlFormEntryTag> childTags) {
this.childTags = childTags;
}
}
Original file line number Diff line number Diff line change
@@ -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<HtmlFormEntryTag> tags = new ArrayList<>();

private final Stack<HtmlFormEntryTag> tagStack = new Stack<>();

public List<HtmlFormEntryTag> 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());
}
}

0 comments on commit 0244acc

Please sign in to comment.