1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.converters.reflection;
27
28 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
29
30 import java.lang.reflect.Field;
31 import java.lang.reflect.InvocationTargetException;
32 import java.lang.reflect.Method;
33 import java.lang.reflect.Modifier;
34 import java.text.AttributedCharacterIterator;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.Map;
38
39
40
41
42
43
44
45
46
47 public class AbstractAttributedCharacterIteratorAttributeConverter extends
48 AbstractSingleValueConverter {
49
50 private static final Method getName;
51 static {
52 try {
53 getName = AttributedCharacterIterator.Attribute.class.getDeclaredMethod(
54 "getName", (Class[])null);
55 } catch (NoSuchMethodException e) {
56 throw new ExceptionInInitializerError("Missing AttributedCharacterIterator.Attribute.getName()");
57 }
58 }
59
60 private final Class type;
61 private transient Map attributeMap;
62 private transient FieldDictionary fieldDictionary;
63
64 public AbstractAttributedCharacterIteratorAttributeConverter(final Class type) {
65 super();
66 this.type = type;
67 readResolve();
68 }
69
70 public boolean canConvert(final Class type) {
71 return type == this.type;
72 }
73
74 public String toString(final Object source) {
75 AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute)source;
76 try {
77 if (!getName.isAccessible()) {
78 getName.setAccessible(true);
79 }
80 return (String)getName.invoke(attribute, (Object[])null);
81 } catch (IllegalAccessException e) {
82 throw new ObjectAccessException(
83 "Cannot get name of AttributedCharacterIterator.Attribute", e);
84 } catch (InvocationTargetException e) {
85 throw new ObjectAccessException(
86 "Cannot get name of AttributedCharacterIterator.Attribute", e
87 .getTargetException());
88 }
89 }
90
91 public Object fromString(final String str) {
92 return attributeMap.get(str);
93 }
94
95 private Object readResolve() {
96 fieldDictionary = new FieldDictionary();
97 attributeMap = new HashMap();
98 for (final Iterator iterator = fieldDictionary.fieldsFor(type); iterator
99 .hasNext();) {
100 final Field field = (Field)iterator.next();
101 if (field.getType() == type && Modifier.isStatic(field.getModifiers())) {
102 try {
103 final Object attribute = field.get(null);
104 attributeMap.put(toString(attribute), attribute);
105 } catch (IllegalAccessException e) {
106 throw new ObjectAccessException("Cannot get object of " + field, e);
107 }
108 }
109 }
110 return this;
111 }
112
113 }