diff options
author | iNPUTmice <daniel@gultsch.de> | 2014-06-13 11:16:52 +0200 |
---|---|---|
committer | iNPUTmice <daniel@gultsch.de> | 2014-06-13 11:16:52 +0200 |
commit | 899da6155561a43e51f62bdf7ccab657c02522f3 (patch) | |
tree | de82b7dbb90430a556196d70b6a24af83d75d307 /src/eu/siacs/conversations/xml/Element.java | |
parent | a92fb88e5177d4df27223b6cb13f00944850f77e (diff) |
further bullet proofing
Diffstat (limited to 'src/eu/siacs/conversations/xml/Element.java')
-rw-r--r-- | src/eu/siacs/conversations/xml/Element.java | 65 |
1 files changed, 33 insertions, 32 deletions
diff --git a/src/eu/siacs/conversations/xml/Element.java b/src/eu/siacs/conversations/xml/Element.java index ce1d10ce1..f8e070f7e 100644 --- a/src/eu/siacs/conversations/xml/Element.java +++ b/src/eu/siacs/conversations/xml/Element.java @@ -9,24 +9,24 @@ public class Element { protected Hashtable<String, String> attributes = new Hashtable<String, String>(); protected String content; protected List<Element> children = new ArrayList<Element>(); - + public Element(String name) { this.name = name; } - + public Element addChild(Element child) { this.content = null; children.add(child); return child; } - + public Element addChild(String name) { this.content = null; Element child = new Element(name); children.add(child); return child; } - + public Element addChild(String name, String xmlns) { this.content = null; Element child = new Element(name); @@ -34,64 +34,65 @@ public class Element { children.add(child); return child; } - + public Element setContent(String content) { this.content = content; this.children.clear(); return this; } - + public Element findChild(String name) { - for(Element child : this.children) { + for (Element child : this.children) { if (child.getName().equals(name)) { return child; } } return null; } - + public Element findChild(String name, String xmlns) { - for(Element child : this.children) { - if (child.getName().equals(name)&&(child.getAttribute("xmlns").equals(xmlns))) { + for (Element child : this.children) { + if (child.getName().equals(name) + && (child.getAttribute("xmlns").equals(xmlns))) { return child; } } return null; } - + public boolean hasChild(String name) { return findChild(name) != null; } - + public boolean hasChild(String name, String xmlns) { return findChild(name, xmlns) != null; } - - - + public List<Element> getChildren() { return this.children; } - + public Element setChildren(List<Element> children) { this.children = children; return this; } - + public String getContent() { return content; } - + public Element setAttribute(String name, String value) { - this.attributes.put(name, value); + if (name != null && value != null) { + this.attributes.put(name, value); + } return this; } - + public Element setAttributes(Hashtable<String, String> attributes) { this.attributes = attributes; return this; } - + public String getAttribute(String name) { if (this.attributes.containsKey(name)) { return this.attributes.get(name); @@ -99,14 +100,14 @@ public class Element { return null; } } - + public Hashtable<String, String> getAttributes() { return this.attributes; } - + public String toString() { StringBuilder elementOutput = new StringBuilder(); - if ((content==null)&&(children.size() == 0)) { + if ((content == null) && (children.size() == 0)) { Tag emptyTag = Tag.empty(name); emptyTag.setAtttributes(this.attributes); elementOutput.append(emptyTag.toString()); @@ -114,10 +115,10 @@ public class Element { Tag startTag = Tag.start(name); startTag.setAtttributes(this.attributes); elementOutput.append(startTag); - if (content!=null) { + if (content != null) { elementOutput.append(encodeEntities(content)); } else { - for(Element child : children) { + for (Element child : children) { elementOutput.append(child.toString()); } } @@ -130,13 +131,13 @@ public class Element { public String getName() { return name; } - + private String encodeEntities(String content) { - content = content.replace("&","&"); - content = content.replace("<","<"); - content = content.replace(">",">"); - content = content.replace("\"","""); - content = content.replace("'","'"); + content = content.replace("&", "&"); + content = content.replace("<", "<"); + content = content.replace(">", ">"); + content = content.replace("\"", """); + content = content.replace("'", "'"); return content; } |