View Javadoc
1   /**
2    * Copyright 2014 Internet2
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  /*
17   * Copyright (C) 2007 XStream Committers.
18   * All rights reserved.
19   *
20   * The software in this package is published under the terms of the BSD
21   * style license a copy of which has been included with this distribution in
22   * the LICENSE.txt file.
23   * 
24   * Created on 01. February 2007 by Joerg Schaible
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   * An abstract converter implementation for constants of
42   * {@link AttributedCharacterIterator.Attribute} and derived types.
43   * 
44   * @author Jörg Schaible
45   * @since 1.2.2
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 }