001: /*
002: * AttributeMap.java
003: */
004:
005: package pnuts.xml;
006:
007: import java.util.AbstractMap;
008: import java.util.AbstractSet;
009: import java.util.Iterator;
010: import java.util.Map;
011: import java.util.Set;
012: import org.w3c.dom.Attr;
013: import org.w3c.dom.Element;
014: import org.w3c.dom.NamedNodeMap;
015: import org.w3c.dom.Node;
016:
017: /**
018: *
019: */
020: public class DOMAttributeMap extends AbstractMap {
021: private NamedNodeMap map;
022: private Element element;
023: private transient Set entrySet = null;
024: transient int size;
025:
026: /**
027: * Constructor
028: */
029: public DOMAttributeMap(Element element, NamedNodeMap map) {
030: this .element = element;
031: this .map = map;
032: }
033:
034: public Set entrySet() {
035: Set es = entrySet;
036: return (es != null ? es : (entrySet = (Set) new EntrySet()));
037: }
038:
039: public Object get(Object key) {
040: Node item = getNamedItem((String) key);
041: if (item instanceof Attr) {
042: return ((Attr) item).getValue();
043: }
044: return null;
045: }
046:
047: public Object put(Object key, Object value) {
048: if (!(key instanceof String)) {
049: throw new IllegalArgumentException(String.valueOf(key));
050: }
051: if (!(value instanceof String)) {
052: throw new IllegalArgumentException(String.valueOf(value));
053: }
054: String name = (String) key;
055: Node old = map.getNamedItem(name);
056: if (old instanceof Attr) {
057: ((Attr) old).setValue((String) value);
058: } else {
059: if (element != null) {
060: element.setAttribute(name, (String) value);
061: }
062: }
063: return old;
064: }
065:
066: public Object remove(Object key) {
067: return map.removeNamedItem((String) key);
068: }
069:
070: Node getNamedItem(String name) {
071: return map.getNamedItem(name);
072: }
073:
074: private class AttrEntry implements Map.Entry {
075: private Attr attr;
076:
077: AttrEntry(Attr attr) {
078: this .attr = attr;
079: }
080:
081: public Object getKey() {
082: return attr.getName();
083: }
084:
085: public Object getValue() {
086: return attr.getValue();
087: }
088:
089: public Object setValue(Object value) {
090: Object old = attr.getValue();
091: attr.setValue((String) value);
092: return old;
093: }
094:
095: public int hashCode() {
096: return attr.hashCode();
097: }
098:
099: public boolean equals(Object obj) {
100: if (obj instanceof AttrEntry) {
101: return attr.equals(((AttrEntry) obj).attr);
102: }
103: return false;
104: }
105:
106: public String toString() {
107: return getKey() + "=" + getValue();
108: }
109: }
110:
111: private class AttrIterator implements Iterator {
112: int idx = 0;
113: int len = map.getLength();
114:
115: Map.Entry nextEntry() {
116: Attr attr = (Attr) map.item(idx++);
117: return new AttrEntry(attr);
118: }
119:
120: public Object next() {
121: return nextEntry();
122: }
123:
124: public boolean hasNext() {
125: return idx < len;
126: }
127:
128: public void remove() {
129: Attr attr = (Attr) map.item(idx);
130: map.removeNamedItem(attr.getName());
131: }
132: }
133:
134: private class EntrySet extends AbstractSet {
135: public Iterator iterator() {
136: return new AttrIterator();
137: }
138:
139: public boolean contains(Object o) {
140: if (!(o instanceof AttrEntry))
141: return false;
142: AttrEntry e = (AttrEntry) o;
143: Object key = e.getKey();
144: Object value = e.getValue();
145: if (!(key instanceof String)) {
146: return false;
147: }
148: if (!(value instanceof String)) {
149: return false;
150: }
151: if (value == null) {
152: return getNamedItem((String) key) == null;
153: } else {
154: return value.equals(getNamedItem((String) key));
155: }
156: }
157:
158: public boolean remove(Object o) {
159: if (!(o instanceof AttrEntry)) {
160: return false;
161: }
162: AttrEntry e = (AttrEntry) o;
163: Object key = e.getKey();
164: Object value = e.getValue();
165: Node item = getNamedItem((String) key);
166: if (item instanceof Attr) {
167: Attr attr = (Attr) item;
168: if (value == null) {
169: if (attr.getValue() == null) {
170: map.removeNamedItem((String) key);
171: return true;
172: }
173: } else {
174: if (value.equals(attr.getValue())) {
175: map.removeNamedItem((String) key);
176: return true;
177: }
178: }
179: }
180: return false;
181: }
182:
183: public int size() {
184: return size;
185: }
186:
187: public void clear() {
188: DOMAttributeMap.this.clear();
189: }
190: }
191:
192: }
|