001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.model.impl;
038:
039: import java.io.IOException;
040: import java.lang.reflect.Field;
041: import java.lang.reflect.Method;
042: import java.lang.reflect.Type;
043: import java.util.EnumMap;
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: import javax.xml.namespace.QName;
048: import javax.xml.stream.XMLStreamException;
049:
050: import com.sun.xml.bind.api.AccessorException;
051: import com.sun.xml.bind.v2.model.annotation.FieldLocatable;
052: import com.sun.xml.bind.v2.model.annotation.Locatable;
053: import com.sun.xml.bind.v2.model.runtime.RuntimeEnumLeafInfo;
054: import com.sun.xml.bind.v2.model.runtime.RuntimeNonElement;
055: import com.sun.xml.bind.v2.runtime.IllegalAnnotationException;
056: import com.sun.xml.bind.v2.runtime.Name;
057: import com.sun.xml.bind.v2.runtime.Transducer;
058: import com.sun.xml.bind.v2.runtime.XMLSerializer;
059:
060: import org.xml.sax.SAXException;
061:
062: /**
063: * @author Kohsuke Kawaguchi
064: */
065: final class RuntimeEnumLeafInfoImpl<T extends Enum<T>, B> extends
066: EnumLeafInfoImpl<Type, Class, Field, Method> implements
067: RuntimeEnumLeafInfo, Transducer<T> {
068:
069: public Transducer<T> getTransducer() {
070: return this ;
071: }
072:
073: /**
074: * {@link Transducer} that knows how to convert a lexical value
075: * into the Java value that we can handle.
076: */
077: private final Transducer<B> baseXducer;
078:
079: private final Map<B, T> parseMap = new HashMap<B, T>();
080: private final Map<T, B> printMap;
081:
082: RuntimeEnumLeafInfoImpl(RuntimeModelBuilder builder,
083: Locatable upstream, Class<T> enumType) {
084: super (builder, upstream, enumType, enumType);
085: this .printMap = new EnumMap<T, B>(enumType);
086:
087: baseXducer = ((RuntimeNonElement) baseType).getTransducer();
088: }
089:
090: @Override
091: public RuntimeEnumConstantImpl createEnumConstant(String name,
092: String literal, Field constant,
093: EnumConstantImpl<Type, Class, Field, Method> last) {
094: T t;
095: try {
096: try {
097: constant.setAccessible(true);
098: } catch (SecurityException e) {
099: // in case the constant is already accessible, swallow this error.
100: // if the constant is indeed not accessible, we will get IllegalAccessException
101: // in the following line, and that is not too late.
102: }
103: t = (T) constant.get(null);
104: } catch (IllegalAccessException e) {
105: // impossible, because this is an enum constant
106: throw new IllegalAccessError(e.getMessage());
107: }
108:
109: B b = null;
110: try {
111: b = baseXducer.parse(literal);
112: } catch (Exception e) {
113: builder.reportError(new IllegalAnnotationException(
114: Messages.INVALID_XML_ENUM_VALUE.format(literal,
115: baseType.getType().toString()), e,
116: new FieldLocatable<Field>(this , constant, nav())));
117: }
118:
119: parseMap.put(b, t);
120: printMap.put(t, b);
121:
122: return new RuntimeEnumConstantImpl(this , name, literal, last);
123: }
124:
125: public QName[] getTypeNames() {
126: return new QName[] { getTypeName() };
127: }
128:
129: public boolean isDefault() {
130: return false;
131: }
132:
133: public Class getClazz() {
134: return clazz;
135: }
136:
137: public boolean useNamespace() {
138: return baseXducer.useNamespace();
139: }
140:
141: public void declareNamespace(T t, XMLSerializer w)
142: throws AccessorException {
143: baseXducer.declareNamespace(printMap.get(t), w);
144: }
145:
146: public CharSequence print(T t) throws AccessorException {
147: return baseXducer.print(printMap.get(t));
148: }
149:
150: public T parse(CharSequence lexical) throws AccessorException,
151: SAXException {
152: // TODO: error handling
153:
154: B b = baseXducer.parse(lexical);
155: if (b == null) {
156:
157: return null;
158: }
159:
160: return parseMap.get(b);
161: }
162:
163: public void writeText(XMLSerializer w, T t, String fieldName)
164: throws IOException, SAXException, XMLStreamException,
165: AccessorException {
166: baseXducer.writeText(w, printMap.get(t), fieldName);
167: }
168:
169: public void writeLeafElement(XMLSerializer w, Name tagName, T o,
170: String fieldName) throws IOException, SAXException,
171: XMLStreamException, AccessorException {
172: baseXducer.writeLeafElement(w, tagName, printMap.get(o),
173: fieldName);
174: }
175:
176: public QName getTypeName(T instance) {
177: return null;
178: }
179: }
|