001: /*
002: * SAXAttributeMap.java
003: *
004: * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.xml;
010:
011: import java.util.Map;
012: import java.util.Set;
013: import java.util.Iterator;
014: import java.util.Collection;
015: import java.util.AbstractCollection;
016: import java.util.NoSuchElementException;
017: import org.xml.sax.Attributes;
018:
019: /*
020: * A read-only Map object that consists of {qName->value} mappings of a Attributes object.
021: * entrySet()/keySet()/valueSet() conserve the order of the attributes.
022: *
023: * Since Attributes object is mutable, an instance of this class is also mutable. That is,
024: * the state of an AttributeMap object is changing during XML parsing.
025: */
026: class SAXAttributeMap implements Map {
027: protected Attributes attributes;
028:
029: void setAttributes(Attributes attributes) {
030: this .attributes = attributes;
031: }
032:
033: Attributes getAttributes() {
034: return attributes;
035: }
036:
037: public int size() {
038: return attributes.getLength();
039: }
040:
041: public boolean isEmpty() {
042: return size() > 0;
043: }
044:
045: public boolean containsKey(Object key) {
046: return get(key) != null;
047: }
048:
049: public boolean containsValue(Object value) {
050: return values().contains(value);
051: }
052:
053: public Object get(Object key) {
054: return attributes.getValue((String) key);
055: }
056:
057: public Set keySet() {
058: return new KeySet();
059: }
060:
061: public Collection values() {
062: return new ValueSet();
063: }
064:
065: public Set entrySet() {
066: return new EntrySet();
067: }
068:
069: /* Unsupported Operations ...*/
070:
071: public Object put(Object key, Object value) {
072: throw new UnsupportedOperationException();
073: }
074:
075: public Object remove(Object key) {
076: throw new UnsupportedOperationException();
077: }
078:
079: public void putAll(Map t) {
080: throw new UnsupportedOperationException();
081: }
082:
083: public void clear() {
084: throw new UnsupportedOperationException();
085: }
086:
087: class EntrySet extends AbstractCollection implements Set {
088: public int size() {
089: return attributes.getLength();
090: }
091:
092: public boolean isEmpty() {
093: return attributes.getLength() > 0;
094: }
095:
096: public Iterator iterator() {
097: return new EntrySetIterator();
098: }
099: }
100:
101: class KeySet extends EntrySet {
102: public Iterator iterator() {
103: return new KeySetIterator();
104: }
105: }
106:
107: class ValueSet extends EntrySet {
108: public Iterator iterator() {
109: return new ValuesIterator();
110: }
111: }
112:
113: class KeySetIterator extends EntrySetIterator {
114: protected Object get(int idx) {
115: return attributes.getQName(idx);
116: }
117: }
118:
119: class ValuesIterator extends EntrySetIterator {
120: protected Object get(int idx) {
121: return attributes.getValue(idx);
122: }
123: }
124:
125: class EntrySetIterator implements Iterator {
126: private int pos = 0;
127: private int max;
128:
129: EntrySetIterator() {
130: this .max = attributes.getLength();
131: }
132:
133: public boolean hasNext() {
134: return pos < max;
135: }
136:
137: protected Object get(int idx) {
138: return new Entry(idx);
139: }
140:
141: public Object next() {
142: int idx = pos++;
143: if (pos > max) {
144: throw new NoSuchElementException();
145: }
146: return get(idx);
147: }
148:
149: public void remove() {
150: throw new UnsupportedOperationException();
151: }
152: }
153:
154: class Entry implements Map.Entry {
155: private int idx;
156:
157: Entry(int idx) {
158: this .idx = idx;
159: }
160:
161: public Object getKey() {
162: return attributes.getQName(idx);
163: }
164:
165: public Object getValue() {
166: return attributes.getValue(idx);
167: }
168:
169: public Object setValue(Object value) {
170: throw new UnsupportedOperationException();
171: }
172: }
173: }
|