Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AttributeList.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*--
  2. $Id: AttributeList.java,v 1.24 2007/11/10 05:28:58 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom;
  46. import java.util.*;
  47. /**
  48. * <code>AttributeList</code> represents legal JDOM <code>Attribute</code>
  49. * content. This class is NOT PUBLIC; users should see it as a simple List
  50. * implementation.
  51. *
  52. * @author Alex Rosen
  53. * @author Philippe Riand
  54. * @author Bradley S. Huffman
  55. * @version $Revision: 1.24 $, $Date: 2007/11/10 05:28:58 $
  56. * @see CDATA
  57. * @see Comment
  58. * @see Element
  59. * @see EntityRef
  60. * @see ProcessingInstruction
  61. * @see Text
  62. */
  63. class AttributeList extends AbstractList
  64. implements List, java.io.Serializable {
  65. private static final String CVS_ID =
  66. "@(#) $RCSfile: AttributeList.java,v $ $Revision: 1.24 $ $Date: 2007/11/10 05:28:58 $ $Name: jdom_1_1 $";
  67. private static final int INITIAL_ARRAY_SIZE = 5;
  68. /** The backing list */
  69. private Attribute elementData[];
  70. private int size;
  71. /** The parent Element */
  72. private Element parent;
  73. /** Force an Element parent */
  74. private AttributeList() {}
  75. /**
  76. * Create a new instance of the AttributeList representing
  77. * Element content
  78. *
  79. * @param parent element whose attributes are to be held
  80. */
  81. AttributeList(Element parent) {
  82. this.parent = parent;
  83. }
  84. /**
  85. * Package internal method to support building from sources that are
  86. * 100% trusted.
  87. *
  88. * @param a attribute to add without any checks
  89. */
  90. final void uncheckedAddAttribute(Attribute a) {
  91. a.parent = parent;
  92. ensureCapacity(size + 1);
  93. elementData[size++] = a;
  94. modCount++;
  95. }
  96. /**
  97. * Add a attribute to the end of the list or replace a existing
  98. * attribute with the same name and <code>Namespace</code>.
  99. *
  100. * @param obj The object to insert into the list.
  101. * @return true (as per the general contract of Collection.add).
  102. * @throws IndexOutOfBoundsException if index < 0 || index > size()
  103. */
  104. public boolean add(Object obj) {
  105. if (obj instanceof Attribute) {
  106. Attribute attribute = (Attribute) obj;
  107. int duplicate = indexOfDuplicate(attribute);
  108. if (duplicate < 0) {
  109. add(size(), attribute);
  110. }
  111. else {
  112. set(duplicate, attribute);
  113. }
  114. }
  115. else if (obj == null) {
  116. throw new IllegalAddException("Cannot add null attribute");
  117. }
  118. else {
  119. throw new IllegalAddException("Class " +
  120. obj.getClass().getName() +
  121. " is not an attribute");
  122. }
  123. return true;
  124. }
  125. /**
  126. * Inserts the specified attribute at the specified position in this list.
  127. * Shifts the attribute currently at that position (if any) and any
  128. * subsequent attributes to the right (adds one to their indices).
  129. *
  130. * @param index The location to set the value to.
  131. * @param obj The object to insert into the list.
  132. * throws IndexOutOfBoundsException if index < 0 || index > size()
  133. */
  134. public void add(int index, Object obj) {
  135. if (obj instanceof Attribute) {
  136. Attribute attribute = (Attribute) obj;
  137. int duplicate = indexOfDuplicate(attribute);
  138. if (duplicate >= 0) {
  139. throw new IllegalAddException("Cannot add duplicate attribute");
  140. }
  141. add(index, attribute);
  142. }
  143. else if (obj == null) {
  144. throw new IllegalAddException("Cannot add null attribute");
  145. }
  146. else {
  147. throw new IllegalAddException("Class " +
  148. obj.getClass().getName() +
  149. " is not an attribute");
  150. }
  151. modCount++;
  152. }
  153. /**
  154. * Check and add the <code>Attribute</code> to this list at
  155. * the given index. Note: does not check for duplicate
  156. * attributes.
  157. *
  158. * @param index index where to add <code>Attribute</code>
  159. * @param attribute <code>Attribute</code> to add
  160. */
  161. void add(int index, Attribute attribute) {
  162. if (attribute.getParent() != null) {
  163. throw new IllegalAddException(
  164. "The attribute already has an existing parent \"" +
  165. attribute.getParent().getQualifiedName() + "\"");
  166. }
  167. String reason = Verifier.checkNamespaceCollision(attribute, parent);
  168. if (reason != null) {
  169. throw new IllegalAddException(parent, attribute, reason);
  170. }
  171. if (index<0 || index>size) {
  172. throw new IndexOutOfBoundsException("Index: " + index +
  173. " Size: " + size());
  174. }
  175. attribute.setParent(parent);
  176. ensureCapacity(size+1);
  177. if( index==size ) {
  178. elementData[size++] = attribute;
  179. } else {
  180. System.arraycopy(elementData, index, elementData, index + 1, size - index);
  181. elementData[index] = attribute;
  182. size++;
  183. }
  184. modCount++;
  185. }
  186. /**
  187. * Add all the objects in the specified collection.
  188. *
  189. * @param collection The collection containing all the objects to add.
  190. * @return <code>true</code> if the list was modified as a result of
  191. * the add.
  192. */
  193. public boolean addAll(Collection collection) {
  194. return addAll(size(), collection);
  195. }
  196. /**
  197. * Inserts the specified collecton at the specified position in this list.
  198. * Shifts the attribute currently at that position (if any) and any
  199. * subsequent attributes to the right (adds one to their indices).
  200. *
  201. * @param index The offset to start adding the data in the collection
  202. * @param collection The collection to insert into the list.
  203. * @return <code>true</code> if the list was modified as a result of
  204. * the add.
  205. * throws IndexOutOfBoundsException if index < 0 || index > size()
  206. */
  207. public boolean addAll(int index, Collection collection) {
  208. if (index<0 || index>size) {
  209. throw new IndexOutOfBoundsException("Index: " + index +
  210. " Size: " + size());
  211. }
  212. if ((collection == null) || (collection.size() == 0)) {
  213. return false;
  214. }
  215. ensureCapacity(size() + collection.size());
  216. int count = 0;
  217. try {
  218. Iterator i = collection.iterator();
  219. while (i.hasNext()) {
  220. Object obj = i.next();
  221. add(index + count, obj);
  222. count++;
  223. }
  224. }
  225. catch (RuntimeException exception) {
  226. for (int i = 0; i < count; i++) {
  227. remove(index);
  228. }
  229. throw exception;
  230. }
  231. return true;
  232. }
  233. /**
  234. * Clear the current list.
  235. */
  236. public void clear() {
  237. if (elementData != null) {
  238. for (int i = 0; i < size; i++) {
  239. Attribute attribute = elementData[i];
  240. attribute.setParent(null);
  241. }
  242. elementData = null;
  243. size = 0;
  244. }
  245. modCount++;
  246. }
  247. /**
  248. * Clear the current list and set it to the contents
  249. * of the <code>Collection</code>.
  250. * object.
  251. *
  252. * @param collection The collection to use.
  253. */
  254. void clearAndSet(Collection collection) {
  255. Attribute[] old = elementData;
  256. int oldSize = size;
  257. elementData = null;
  258. size = 0;
  259. if ((collection != null) && (collection.size() != 0)) {
  260. ensureCapacity(collection.size());
  261. try {
  262. addAll(0, collection);
  263. }
  264. catch (RuntimeException exception) {
  265. elementData = old;
  266. size = oldSize;
  267. throw exception;
  268. }
  269. }
  270. if (old != null) {
  271. for (int i = 0; i < oldSize; i++) {
  272. Attribute attribute = old[i];
  273. attribute.setParent(null);
  274. }
  275. }
  276. modCount++;
  277. }
  278. /**
  279. * Increases the capacity of this <code>AttributeList</code> instance,
  280. * if necessary, to ensure that it can hold at least the number of
  281. * items specified by the minimum capacity argument.
  282. *
  283. * @param minCapacity the desired minimum capacity.
  284. */
  285. private void ensureCapacity(int minCapacity) {
  286. if (elementData == null) {
  287. elementData = new Attribute[Math.max(minCapacity, INITIAL_ARRAY_SIZE)];
  288. }
  289. else {
  290. int oldCapacity = elementData.length;
  291. if (minCapacity > oldCapacity) {
  292. Attribute oldData[] = elementData;
  293. int newCapacity = (oldCapacity * 3)/2 + 1;
  294. if (newCapacity < minCapacity)
  295. newCapacity = minCapacity;
  296. elementData = new Attribute[newCapacity];
  297. System.arraycopy(oldData, 0, elementData, 0, size);
  298. }
  299. }
  300. }
  301. /**
  302. * Return the object at the specified offset.
  303. *
  304. * @param index The offset of the object.
  305. * @return The Object which was returned.
  306. */
  307. public Object get(int index) {
  308. if (index<0 || index>=size) {
  309. throw new IndexOutOfBoundsException("Index: " + index +
  310. " Size: " + size());
  311. }
  312. return elementData[index];
  313. }
  314. /**
  315. * Return the <code>Attribute</code> with the
  316. * given name and <code>Namespace</code>.
  317. *
  318. * @param name name of attribute to return
  319. * @param namespace <code>Namespace</code> to match
  320. * @return the <code>Attribute</code>, or null if one doesn't exist.
  321. */
  322. Object get(String name, Namespace namespace) {
  323. int index = indexOf(name, namespace);
  324. if (index < 0) {
  325. return null;
  326. }
  327. return elementData[index];
  328. }
  329. /**
  330. * Return index of the <code>Attribute</code> with the
  331. * given name and uri.
  332. */
  333. int indexOf(String name, Namespace namespace) {
  334. String uri = namespace.getURI();
  335. if (elementData != null) {
  336. for (int i = 0; i < size; i++) {
  337. Attribute old = elementData[i];
  338. String oldURI = old.getNamespaceURI();
  339. String oldName = old.getName();
  340. if (oldURI.equals(uri) && oldName.equals(name)) {
  341. return i;
  342. }
  343. }
  344. }
  345. return -1;
  346. }
  347. /**
  348. * Remove the object at the specified offset.
  349. *
  350. * @param index The offset of the object.
  351. * @return The Object which was removed.
  352. */
  353. public Object remove(int index) {
  354. if (index<0 || index>=size)
  355. throw new IndexOutOfBoundsException("Index: " + index +
  356. " Size: " + size());
  357. Attribute old = elementData[index];
  358. old.setParent(null);
  359. int numMoved = size - index - 1;
  360. if (numMoved > 0)
  361. System.arraycopy(elementData, index+1, elementData, index,numMoved);
  362. elementData[--size] = null; // Let gc do its work
  363. modCount++;
  364. return old;
  365. }
  366. /**
  367. * Remove the <code>Attribute</code> with the
  368. * given name and <code>Namespace</code>.
  369. *
  370. * @param namespace <code>Namespace</code> to match
  371. * @return the <code>true</code> if attribute was removed,
  372. * <code>false</code> otherwise
  373. */
  374. boolean remove(String name, Namespace namespace) {
  375. int index = indexOf(name, namespace);
  376. if (index < 0) {
  377. return false;
  378. }
  379. remove(index);
  380. return true;
  381. }
  382. /**
  383. * Set the object at the specified location to the supplied
  384. * object.
  385. *
  386. * @param index The location to set the value to.
  387. * @param obj The location to set the value to.
  388. * @return The object which was replaced.
  389. * throws IndexOutOfBoundsException if index < 0 || index >= size()
  390. */
  391. public Object set(int index, Object obj) {
  392. if (obj instanceof Attribute) {
  393. Attribute attribute = (Attribute) obj;
  394. int duplicate = indexOfDuplicate(attribute);
  395. if ((duplicate >= 0) && (duplicate != index)) {
  396. throw new IllegalAddException("Cannot set duplicate attribute");
  397. }
  398. return set(index, attribute);
  399. }
  400. else if (obj == null) {
  401. throw new IllegalAddException("Cannot add null attribute");
  402. }
  403. else {
  404. throw new IllegalAddException("Class " +
  405. obj.getClass().getName() +
  406. " is not an attribute");
  407. }
  408. }
  409. /**
  410. * Set the object at the specified location to the supplied
  411. * object. Note: does not check for duplicate attributes.
  412. *
  413. * @param index The location to set the value to.
  414. * @param attribute The attribute to set.
  415. * @return The object which was replaced.
  416. * throws IndexOutOfBoundsException if index < 0 || index >= size()
  417. */
  418. Object set(int index, Attribute attribute) {
  419. if (index < 0 || index >= size)
  420. throw new IndexOutOfBoundsException("Index: " + index +
  421. " Size: " + size());
  422. if (attribute.getParent() != null) {
  423. throw new IllegalAddException(
  424. "The attribute already has an existing parent \"" +
  425. attribute.getParent().getQualifiedName() + "\"");
  426. }
  427. String reason = Verifier.checkNamespaceCollision(attribute, parent);
  428. if (reason != null) {
  429. throw new IllegalAddException(parent, attribute, reason);
  430. }
  431. Attribute old = (Attribute) elementData[index];
  432. old.setParent(null);
  433. elementData[index] = attribute;
  434. attribute.setParent(parent);
  435. return old;
  436. }
  437. /**
  438. * Return index of attribute with same name and Namespace, or
  439. * -1 if one doesn't exist
  440. */
  441. private int indexOfDuplicate(Attribute attribute) {
  442. int duplicate = -1;
  443. String name = attribute.getName();
  444. Namespace namespace = attribute.getNamespace();
  445. duplicate = indexOf(name, namespace);
  446. return duplicate;
  447. }
  448. /**
  449. * Return the number of items in this list
  450. *
  451. * @return The number of items in this list.
  452. */
  453. public int size() {
  454. return size;
  455. }
  456. /**
  457. * Return this list as a <code>String</code>
  458. */
  459. public String toString() {
  460. return super.toString();
  461. }
  462. }