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

https://github.com/rfreedman/intellij-community · Java · 97 lines · 69 code · 13 blank · 15 comment · 7 complexity · 92a135b64971afd366bc2e9657a3599f 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 org.jetbrains.annotations.NotNull;
  20. import sun.reflect.Reflection;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. @SuppressWarnings({"HardCodedStringLiteral"})
  24. public class JDOMExternalizableStringList extends ArrayList<String> implements JDOMExternalizable {
  25. private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.JDOMExternalizableStringList");
  26. private static final String ATTR_LIST = "list";
  27. private static final String ATTR_LISTSIZE = "size";
  28. private static final String ATTR_ITEM = "item";
  29. private static final String ATTR_INDEX = "index";
  30. private static final String ATTR_CLASS = "class";
  31. private static final String ATTR_VALUE = "itemvalue";
  32. public JDOMExternalizableStringList(int initialCapacity) {
  33. super(initialCapacity);
  34. }
  35. public JDOMExternalizableStringList() {
  36. }
  37. public JDOMExternalizableStringList(@NotNull Collection<? extends String> c) {
  38. super(c);
  39. }
  40. public void readExternal(Element element) throws InvalidDataException {
  41. clear();
  42. for (final Object o : element.getChildren()) {
  43. Element listElement = (Element)o;
  44. if (ATTR_LIST.equals(listElement.getName())) {
  45. final ClassLoader classLoader = Reflection.getCallerClass(2).getClassLoader();
  46. for (final Object o1 : listElement.getChildren()) {
  47. Element listItemElement = (Element)o1;
  48. if (!ATTR_ITEM.equals(listItemElement.getName())) {
  49. throw new InvalidDataException(
  50. "Unable to read list item. Unknown element found: " + listItemElement.getName());
  51. }
  52. String itemClassString = listItemElement.getAttributeValue(ATTR_CLASS);
  53. Class itemClass;
  54. try {
  55. itemClass = Class.forName(itemClassString, true, classLoader);
  56. }
  57. catch (ClassNotFoundException ex) {
  58. throw new InvalidDataException(
  59. "Unable to read list item: unable to load class: " + itemClassString + " \n" + ex.getMessage());
  60. }
  61. String listItem = listItemElement.getAttributeValue(ATTR_VALUE);
  62. LOG.assertTrue(String.class.equals(itemClass));
  63. add(listItem);
  64. }
  65. }
  66. }
  67. }
  68. public void writeExternal(Element element) throws WriteExternalException {
  69. int listSize = size();
  70. Element listElement = new Element(ATTR_LIST);
  71. listElement.setAttribute(ATTR_LISTSIZE, Integer.toString(listSize));
  72. element.addContent(listElement);
  73. for (int i = 0; i < listSize; i++) {
  74. String listItem = get(i);
  75. if (listItem != null) {
  76. Element itemElement = new Element(ATTR_ITEM);
  77. itemElement.setAttribute(ATTR_INDEX, Integer.toString(i));
  78. itemElement.setAttribute(ATTR_CLASS, listItem.getClass().getName());
  79. itemElement.setAttribute(ATTR_VALUE, DefaultJDOMExternalizer.filterXMLCharacters(listItem));
  80. listElement.addContent(itemElement);
  81. }
  82. }
  83. }
  84. }