diff options
Diffstat (limited to 'src/com/isode/stroke/parser/AttributeMap.java')
-rw-r--r-- | src/com/isode/stroke/parser/AttributeMap.java | 91 |
1 files changed, 84 insertions, 7 deletions
diff --git a/src/com/isode/stroke/parser/AttributeMap.java b/src/com/isode/stroke/parser/AttributeMap.java index fb4aa68..0e43785 100644 --- a/src/com/isode/stroke/parser/AttributeMap.java +++ b/src/com/isode/stroke/parser/AttributeMap.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, Isode Limited, London, England. + * Copyright (c) 2010-2012, Isode Limited, London, England. * All rights reserved. */ /* @@ -8,26 +8,103 @@ */ package com.isode.stroke.parser; -import java.util.HashMap; +import com.isode.stroke.base.NotNull; +import java.util.ArrayList; +import java.util.List; /** * XML element attributes. */ -public class AttributeMap extends HashMap<String, String> { +public class AttributeMap { + + /** + * Internal class. + */ + public class Entry { + + public Entry(Attribute attribute, String value) { + NotNull.exceptIfNull(attribute, "attribute"); + NotNull.exceptIfNull(value, "value"); + this.attribute = attribute; + this.value = value; + } + + public Attribute getAttribute() { + return attribute; + } + + public String getValue() { + return value; + } + private final Attribute attribute; + private final String value; + }; + + public AttributeMap() { + } + + /** Not null */ public String getAttribute(String attribute) { - return this.containsKey(attribute) ? this.get(attribute) : ""; + NotNull.exceptIfNull(attribute, "attribute"); + return getAttribute(attribute, ""); } - public String getAttributeValue(String attribute) { - return this.containsKey(attribute) ? this.get(attribute) : null; + /** Not null*/ + public String getAttribute(String attribute, String ns) { + String value = getInternal(attribute, ns); + return value != null ? value : ""; } + /** + * + * @param attribute attribute name. + * @return boolean value, defaulting to false if missing. + */ public boolean getBoolAttribute(String attribute) { return getBoolAttribute(attribute, false); } public boolean getBoolAttribute(String attribute, boolean defaultValue) { - String value = getAttribute(attribute); + String value = getInternal(attribute, ""); + if (value == null) { + return defaultValue; + } return "true".equals(value) || "1".equals(value); } + + /** @return Attribute or null if missing.*/ + public String getAttributeValue(String attribute) { + return getInternal(attribute, ""); + } + + /** + * + * @param name Attribute name (non-null). + * @param ns Attribute namespace (non-null). + * @param value Attribute value (non-null). + */ + public void addAttribute(String name, String ns, String value) { + NotNull.exceptIfNull(name, "name"); + NotNull.exceptIfNull(ns, "ns"); + NotNull.exceptIfNull(value, "value"); + attributes.add(new Entry(new Attribute(name, ns), value)); + } + + /** + * Internal method (used for unit tests). + */ + public List<Entry> getEntries() { + return new ArrayList(attributes); + } + + private String getInternal(String name, String ns) { + Attribute attribute = new Attribute(name, ns); + for (Entry entry : attributes) { + if (entry.getAttribute().equals(attribute)) { + return entry.value; + } + } + return null; + } + private final List<Entry> attributes = new ArrayList<Entry>(); } |