-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
152 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
api/src/main/java/org/openmrs/module/htmlformentry/HtmlFormEntryTag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
api/src/main/java/org/openmrs/module/htmlformentry/TagRegister.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |