diff options
author | Kevin Smith <git@kismith.co.uk> | 2011-07-01 09:19:49 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2011-07-01 09:19:49 (GMT) |
commit | 2da71a8a85486a494343f1662d64fb5ae5a2a44e (patch) | |
tree | 23992f9f2a00bac23b345e5c2cc9c1194efc25be /src/com/isode/stroke/serializer/xml/XMLElement.java | |
download | stroke-2da71a8a85486a494343f1662d64fb5ae5a2a44e.zip stroke-2da71a8a85486a494343f1662d64fb5ae5a2a44e.tar.bz2 |
Initial import
Diffstat (limited to 'src/com/isode/stroke/serializer/xml/XMLElement.java')
-rw-r--r-- | src/com/isode/stroke/serializer/xml/XMLElement.java | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/com/isode/stroke/serializer/xml/XMLElement.java b/src/com/isode/stroke/serializer/xml/XMLElement.java new file mode 100644 index 0000000..909f277 --- /dev/null +++ b/src/com/isode/stroke/serializer/xml/XMLElement.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2010, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Remko Tron¨on. + * All rights reserved. + */ +package com.isode.stroke.serializer.xml; + +import java.util.HashMap; +import java.util.Vector; + +public class XMLElement implements XMLNode { + + private final String tag_; + private final HashMap<String, String> attributes_ = new HashMap<String, String>(); + private final Vector<XMLNode> childNodes_ = new Vector<XMLNode>(); + + public XMLElement(String tag) { + this(tag, ""); + } + + public XMLElement(String tag, String xmlns) { + tag_ = tag; + if (xmlns.length()!=0) { + setAttribute("xmlns", xmlns); + } + } + + public XMLElement(String tag, String xmlns, String text) { + this(tag, xmlns); + if (text.length() > 0) { + addNode(new XMLTextNode(text)); + } + } + + public String serialize() { + String result = ""; + result += "<" + tag_; + for (String key : attributes_.keySet()) { + result += " " + key + "=\"" + attributes_.get(key) + "\""; + } + + if (childNodes_.size() > 0) { + result += ">"; + for (XMLNode node : childNodes_) { + result += node.serialize(); + } + result += "</" + tag_ + ">"; + } else { + result += "/>"; + } + return result; + } + + public void setAttribute(String attribute, String value) { + String escapedValue = value; + escapedValue.replaceAll("&", "&"); + escapedValue.replaceAll("<", "<"); + escapedValue.replaceAll(">", ">"); + escapedValue.replaceAll("'", "'"); + escapedValue.replaceAll("\"", """); + attributes_.put(attribute, escapedValue); + } + + public void addNode(XMLNode node) { + childNodes_.add(node); + } +} |