001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.rio.rdfxml;
007:
008: import java.util.ArrayList;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: /**
013: * A collection of XML attributes.
014: */
015: class Atts {
016:
017: /*-----------*
018: * Variables *
019: *-----------*/
020:
021: /**
022: * List containing Att objects.
023: */
024: private List<Att> attributes;
025:
026: /*--------------*
027: * Constructors *
028: *--------------*/
029:
030: /**
031: * Creates a new <tt>Atts</tt> object.
032: */
033: public Atts() {
034: this (4);
035: }
036:
037: /**
038: * Creates a new <tt>Atts</tt> object.
039: *
040: * @param size
041: * The initial size of the array for storing attributes.
042: */
043: public Atts(int size) {
044: attributes = new ArrayList<Att>(size);
045: }
046:
047: /*---------*
048: * Methods *
049: *---------*/
050:
051: /**
052: * Adds an attribute.
053: */
054: public void addAtt(Att att) {
055: attributes.add(att);
056: }
057:
058: /**
059: * Get an iterator on the attributes.
060: *
061: * @return an Iterator over Att objects.
062: */
063: public Iterator<Att> iterator() {
064: return attributes.iterator();
065: }
066:
067: /**
068: * Gets the attribute with the specified QName.
069: *
070: * @param qName
071: * The QName of an attribute.
072: * @return The attribute with the specified QName, or <tt>null</tt> if no
073: * such attribute could be found.
074: */
075: public Att getAtt(String qName) {
076: for (int i = 0; i < attributes.size(); i++) {
077: Att att = attributes.get(i);
078:
079: if (att.getQName().equals(qName)) {
080: return att;
081: }
082: }
083:
084: return null;
085: }
086:
087: /**
088: * Gets the attribute with the specified namespace and local name.
089: *
090: * @param namespace
091: * The namespace of an attribute.
092: * @param localName
093: * The local name of an attribute.
094: * @return The attribute with the specified namespace and local name, or
095: * <tt>null</tt> if no such attribute could be found.
096: */
097: public Att getAtt(String namespace, String localName) {
098: for (int i = 0; i < attributes.size(); i++) {
099: Att att = attributes.get(i);
100:
101: if (att.getLocalName().equals(localName)
102: && att.getNamespace().equals(namespace)) {
103: return att;
104: }
105: }
106:
107: return null;
108: }
109:
110: /**
111: * Removes the attribute with the specified QName and returns it.
112: *
113: * @param qName
114: * The QName of an attribute.
115: * @return The removed attribute, or <tt>null</tt> if no attribute with the
116: * specified QName could be found.
117: */
118: public Att removeAtt(String qName) {
119: for (int i = 0; i < attributes.size(); i++) {
120: Att att = attributes.get(i);
121:
122: if (att.getQName().equals(qName)) {
123: attributes.remove(i);
124: return att;
125: }
126: }
127:
128: return null;
129: }
130:
131: /**
132: * Removes the attribute with the specified namespace and local name and
133: * returns it.
134: *
135: * @param namespace
136: * The namespace of an attribute.
137: * @param localName
138: * The local name of an attribute.
139: * @return The removed attribute, or <tt>null</tt> if no attribute with the
140: * specified namespace and local name could be found.
141: */
142: public Att removeAtt(String namespace, String localName) {
143: for (int i = 0; i < attributes.size(); i++) {
144: Att att = attributes.get(i);
145:
146: if (att.getLocalName().equals(localName)
147: && att.getNamespace().equals(namespace)) {
148: attributes.remove(i);
149: return att;
150: }
151: }
152:
153: return null;
154: }
155:
156: /**
157: * Returns the number of attributes contained in this object.
158: */
159: public int size() {
160: return attributes.size();
161: }
162:
163: /**
164: * Produces a String-representation of this object.
165: */
166: @Override
167: public String toString() {
168: StringBuilder sb = new StringBuilder();
169: sb.append("Atts[");
170: for (int i = 0; i < attributes.size(); i++) {
171: Att att = attributes.get(i);
172: sb.append(att.getQName());
173: sb.append("=");
174: sb.append(att.getValue());
175: sb.append("; ");
176: }
177: sb.append("]");
178: return sb.toString();
179: }
180: }
|