further bullet proofing

This commit is contained in:
iNPUTmice 2014-06-13 11:16:52 +02:00
parent a92fb88e51
commit 899da61555
1 changed files with 33 additions and 32 deletions

View File

@ -9,24 +9,24 @@ public class Element {
protected Hashtable<String, String> attributes = new Hashtable<String, String>(); protected Hashtable<String, String> attributes = new Hashtable<String, String>();
protected String content; protected String content;
protected List<Element> children = new ArrayList<Element>(); protected List<Element> children = new ArrayList<Element>();
public Element(String name) { public Element(String name) {
this.name = name; this.name = name;
} }
public Element addChild(Element child) { public Element addChild(Element child) {
this.content = null; this.content = null;
children.add(child); children.add(child);
return child; return child;
} }
public Element addChild(String name) { public Element addChild(String name) {
this.content = null; this.content = null;
Element child = new Element(name); Element child = new Element(name);
children.add(child); children.add(child);
return child; return child;
} }
public Element addChild(String name, String xmlns) { public Element addChild(String name, String xmlns) {
this.content = null; this.content = null;
Element child = new Element(name); Element child = new Element(name);
@ -34,64 +34,65 @@ public class Element {
children.add(child); children.add(child);
return child; return child;
} }
public Element setContent(String content) { public Element setContent(String content) {
this.content = content; this.content = content;
this.children.clear(); this.children.clear();
return this; return this;
} }
public Element findChild(String name) { public Element findChild(String name) {
for(Element child : this.children) { for (Element child : this.children) {
if (child.getName().equals(name)) { if (child.getName().equals(name)) {
return child; return child;
} }
} }
return null; return null;
} }
public Element findChild(String name, String xmlns) { public Element findChild(String name, String xmlns) {
for(Element child : this.children) { for (Element child : this.children) {
if (child.getName().equals(name)&&(child.getAttribute("xmlns").equals(xmlns))) { if (child.getName().equals(name)
&& (child.getAttribute("xmlns").equals(xmlns))) {
return child; return child;
} }
} }
return null; return null;
} }
public boolean hasChild(String name) { public boolean hasChild(String name) {
return findChild(name) != null; return findChild(name) != null;
} }
public boolean hasChild(String name, String xmlns) { public boolean hasChild(String name, String xmlns) {
return findChild(name, xmlns) != null; return findChild(name, xmlns) != null;
} }
public List<Element> getChildren() { public List<Element> getChildren() {
return this.children; return this.children;
} }
public Element setChildren(List<Element> children) { public Element setChildren(List<Element> children) {
this.children = children; this.children = children;
return this; return this;
} }
public String getContent() { public String getContent() {
return content; return content;
} }
public Element setAttribute(String name, String value) { public Element setAttribute(String name, String value) {
this.attributes.put(name, value); if (name != null && value != null) {
this.attributes.put(name, value);
}
return this; return this;
} }
public Element setAttributes(Hashtable<String, String> attributes) { public Element setAttributes(Hashtable<String, String> attributes) {
this.attributes = attributes; this.attributes = attributes;
return this; return this;
} }
public String getAttribute(String name) { public String getAttribute(String name) {
if (this.attributes.containsKey(name)) { if (this.attributes.containsKey(name)) {
return this.attributes.get(name); return this.attributes.get(name);
@ -99,14 +100,14 @@ public class Element {
return null; return null;
} }
} }
public Hashtable<String, String> getAttributes() { public Hashtable<String, String> getAttributes() {
return this.attributes; return this.attributes;
} }
public String toString() { public String toString() {
StringBuilder elementOutput = new StringBuilder(); StringBuilder elementOutput = new StringBuilder();
if ((content==null)&&(children.size() == 0)) { if ((content == null) && (children.size() == 0)) {
Tag emptyTag = Tag.empty(name); Tag emptyTag = Tag.empty(name);
emptyTag.setAtttributes(this.attributes); emptyTag.setAtttributes(this.attributes);
elementOutput.append(emptyTag.toString()); elementOutput.append(emptyTag.toString());
@ -114,10 +115,10 @@ public class Element {
Tag startTag = Tag.start(name); Tag startTag = Tag.start(name);
startTag.setAtttributes(this.attributes); startTag.setAtttributes(this.attributes);
elementOutput.append(startTag); elementOutput.append(startTag);
if (content!=null) { if (content != null) {
elementOutput.append(encodeEntities(content)); elementOutput.append(encodeEntities(content));
} else { } else {
for(Element child : children) { for (Element child : children) {
elementOutput.append(child.toString()); elementOutput.append(child.toString());
} }
} }
@ -130,13 +131,13 @@ public class Element {
public String getName() { public String getName() {
return name; return name;
} }
private String encodeEntities(String content) { private String encodeEntities(String content) {
content = content.replace("&","&amp;"); content = content.replace("&", "&amp;");
content = content.replace("<","&lt;"); content = content.replace("<", "&lt;");
content = content.replace(">","&gt;"); content = content.replace(">", "&gt;");
content = content.replace("\"","&quot;"); content = content.replace("\"", "&quot;");
content = content.replace("'","&apos;"); content = content.replace("'", "&apos;");
return content; return content;
} }