/platform/util/src/com/intellij/openapi/util/JDOMExternalizableStringList.java

https://github.com/maartenh/intellij-community · Java · 100 lines · 75 code · 10 blank · 15 comment · 9 complexity · 9f51959edaafa9788a3e9351ba9d6f6b MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.util;
  17. import com.intellij.openapi.diagnostic.Logger;
  18. import org.jdom.Element;
  19. import sun.reflect.Reflection;
  20. import java.util.ArrayList;
  21. @SuppressWarnings({"HardCodedStringLiteral"})
  22. public class JDOMExternalizableStringList extends ArrayList<String> implements JDOMExternalizable {
  23. private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.JDOMExternalizableStringList");
  24. private static final String ATTR_LIST = "list";
  25. private static final String ATTR_LISTSIZE = "size";
  26. private static final String ATTR_ITEM = "item";
  27. private static final String ATTR_INDEX = "index";
  28. private static final String ATTR_CLASS = "class";
  29. private static final String ATTR_VALUE = "itemvalue";
  30. public void readExternal(Element element) throws InvalidDataException {
  31. clear();
  32. for (final Object o : element.getChildren()) {
  33. Element listElement = (Element)o;
  34. if (ATTR_LIST.equals(listElement.getName())) {
  35. String sizeString = listElement.getAttributeValue(ATTR_LISTSIZE);
  36. int listSize;
  37. try {
  38. listSize = Integer.parseInt(sizeString);
  39. }
  40. catch (NumberFormatException ex) {
  41. throw new InvalidDataException("Size " + sizeString + " found. Must be integer!");
  42. }
  43. for (int j = 0; j < listSize; j++) {
  44. add(null);
  45. }
  46. final ClassLoader classLoader = Reflection.getCallerClass(2).getClassLoader();
  47. for (final Object o1 : listElement.getChildren()) {
  48. Element listItemElement = (Element)o1;
  49. if (!ATTR_ITEM.equals(listItemElement.getName())) {
  50. throw new InvalidDataException(
  51. "Unable to read list item. Unknown element found: " + listItemElement.getName());
  52. }
  53. String itemIndexString = listItemElement.getAttributeValue(ATTR_INDEX);
  54. String itemClassString = listItemElement.getAttributeValue(ATTR_CLASS);
  55. Class itemClass;
  56. try {
  57. itemClass = Class.forName(itemClassString, true, classLoader);
  58. }
  59. catch (ClassNotFoundException ex) {
  60. throw new InvalidDataException(
  61. "Unable to read list item: unable to load class: " + itemClassString + " \n" + ex.getMessage());
  62. }
  63. String listItem = listItemElement.getAttributeValue(ATTR_VALUE);
  64. LOG.assertTrue(String.class.equals(itemClass));
  65. int index = Integer.parseInt(itemIndexString);
  66. if (index >= listSize) {
  67. throw new InvalidDataException("Index out of list size: index " + index + ", size " + listSize);
  68. }
  69. set(index, listItem);
  70. }
  71. }
  72. }
  73. }
  74. public void writeExternal(Element element) throws WriteExternalException {
  75. int listSize = size();
  76. Element listElement = new Element(ATTR_LIST);
  77. listElement.setAttribute(ATTR_LISTSIZE, Integer.toString(listSize));
  78. element.addContent(listElement);
  79. for (int i = 0; i < listSize; i++) {
  80. String listItem = get(i);
  81. if (listItem != null) {
  82. Element itemElement = new Element(ATTR_ITEM);
  83. itemElement.setAttribute(ATTR_INDEX, Integer.toString(i));
  84. itemElement.setAttribute(ATTR_CLASS, listItem.getClass().getName());
  85. itemElement.setAttribute(ATTR_VALUE, DefaultJDOMExternalizer.filterXMLCharacters(listItem));
  86. listElement.addContent(itemElement);
  87. }
  88. }
  89. }
  90. }