1
2package org.ksoap2.serialization;
3
4import java.util.Vector;
5
6public class AttributeContainer {
7    private Vector attributes = new Vector();
8
9    /**
10     * Places AttributeInfo of desired attribute into a designated AttributeInfo object
11     *
12     * @param index         index of desired attribute
13     * @param attributeInfo designated retainer of desired attribute
14     */
15    public void getAttributeInfo(int index, AttributeInfo attributeInfo) {
16        AttributeInfo p = (AttributeInfo) attributes.elementAt(index);
17        attributeInfo.name = p.name;
18        attributeInfo.namespace = p.namespace;
19        attributeInfo.flags = p.flags;
20        attributeInfo.type = p.type;
21        attributeInfo.elementType = p.elementType;
22        attributeInfo.value = p.getValue();
23    }
24
25    /**
26     * Get the attribute at the given index
27     */
28    public Object getAttribute(int index) {
29        return ((AttributeInfo) attributes.elementAt(index)).getValue();
30    }
31
32    /**
33    * Get the attribute's toString value.
34    */
35    public String getAttributeAsString(int index) {
36        AttributeInfo attributeInfo = (AttributeInfo) attributes.elementAt(index);
37        return attributeInfo.getValue().toString();
38    }
39
40    /**
41     * Get the attribute with the given name
42     *
43     * @throws RuntimeException if the attribute does not exist
44     */
45    public Object getAttribute(String name) {
46        Integer i = attributeIndex(name);
47        if (i != null) {
48            return getAttribute(i.intValue());
49        } else {
50            throw new RuntimeException("illegal property: " + name);
51        }
52    }
53
54    /**
55     * Get the toString value of the attribute with the given name.
56     *
57     * @throws RuntimeException if the attribute does not exist
58     */
59    public String getAttributeAsString(String name) {
60        Integer i = attributeIndex(name);
61        if (i != null) {
62            return getAttribute(i.intValue()).toString();
63        } else {
64            throw new RuntimeException("illegal property: " + name);
65        }
66    }
67
68    /**
69     * Knows whether the given attribute exists
70     */
71    public boolean hasAttribute(final String name) {
72        if (attributeIndex(name) != null) {
73            return true;
74        } else {
75            return false;
76        }
77    }
78
79    /**
80     * Get an attribute without chance of throwing an exception
81     *
82     * @param name the name of the attribute to retrieve
83     * @return the value of the attribute if it exists; {@code null} if it does not exist
84     */
85    public Object getAttributeSafely(String name) {
86        Integer i = attributeIndex(name);
87        if (i != null) {
88            return getAttribute(i.intValue());
89        } else {
90            return null;
91        }
92    }
93
94    /**
95     * Get an attributes' toString value without chance of throwing an
96     * exception.
97
98     * @param name
99     * @return the value of the attribute,s toString method if it exists; ""
100     * if it does not exist
101     */
102    public Object getAttributeSafelyAsString(String name) {
103        Integer i = attributeIndex(name);
104        if (i != null) {
105            return getAttribute(i.intValue()).toString();
106        } else {
107            return "";
108        }
109    }
110
111    private Integer attributeIndex(String name) {
112        for (int i = 0; i < attributes.size(); i++) {
113            if (name.equals(((AttributeInfo) attributes.elementAt(i)).getName())) {
114                return new Integer(i);
115            }
116        }
117        return null;
118    }
119
120    /**
121     * Returns the number of attributes
122     *
123     * @return the number of attributes
124     */
125    public int getAttributeCount() {
126        return attributes.size();
127    }
128
129    /**
130     * Checks that the two objects have identical sets of attributes.
131     *
132     * @param other
133     * @return {@code true} of the attrubte sets are equal, {@code false} otherwise.
134     */
135    protected boolean attributesAreEqual(AttributeContainer other) {
136        int numAttributes = getAttributeCount();
137        if (numAttributes != other.getAttributeCount()) {
138            return false;
139        }
140
141        for (int attribIndex = 0; attribIndex < numAttributes; attribIndex++) {
142            AttributeInfo thisAttrib = (AttributeInfo) this.attributes.elementAt(attribIndex);
143            Object thisAttribValue = thisAttrib.getValue();
144            if (!other.hasAttribute(thisAttrib.getName())) {
145                return false;
146            }
147            Object otherAttribValue = other.getAttributeSafely(thisAttrib.getName());
148            if (!thisAttribValue.equals(otherAttribValue)) {
149                return false;
150            }
151        }
152        return true;
153    }
154
155    /**
156     * Adds a attribute (parameter) to the object.
157     *
158     * @param name  The name of the attribute
159     * @param value the value of the attribute
160     * @return {@code this} object.
161     */
162    public void addAttribute(String name, Object value) {
163        AttributeInfo attributeInfo = new AttributeInfo();
164        attributeInfo.name = name;
165        attributeInfo.type = value == null ? PropertyInfo.OBJECT_CLASS : value.getClass();
166        attributeInfo.value = value;
167        addAttribute(attributeInfo);
168    }
169
170    /**
171     * Add an attribute if the value is not null.
172     * @param name
173     * @param value
174     */
175    public void addAttributeIfValue(String name, Object value) {
176        if (value != null) {
177            addAttribute(name, value);
178        }
179    }
180
181    /**
182     * Add a new attribute by providing an {@link AttributeInfo} object.  {@code AttributeInfo}
183     * contains all data about the attribute, including name and value.}
184     *
185     * @param attributeInfo the {@code AttributeInfo} object to add.
186     * @return {@code this} object.
187     */
188    public void addAttribute(AttributeInfo attributeInfo) {
189        attributes.addElement(attributeInfo);
190    }
191
192    /**
193     * Add an attributeInfo if its value is not null.
194     * @param attributeInfo
195     */
196    public void addAttributeIfValue(AttributeInfo attributeInfo) {
197        if (attributeInfo.value != null) {
198            attributes.addElement(attributeInfo);
199        }
200    }
201}
202