src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/impl/RuntimeEnumLeafInfoImpl.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/impl/RuntimeEnumLeafInfoImpl.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,157 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.model.impl;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.lang.reflect.Field;
    1.33 +import java.lang.reflect.Method;
    1.34 +import java.lang.reflect.Type;
    1.35 +import java.util.EnumMap;
    1.36 +import java.util.HashMap;
    1.37 +import java.util.Map;
    1.38 +
    1.39 +import javax.xml.namespace.QName;
    1.40 +import javax.xml.stream.XMLStreamException;
    1.41 +
    1.42 +import com.sun.xml.internal.bind.api.AccessorException;
    1.43 +import com.sun.xml.internal.bind.v2.model.annotation.FieldLocatable;
    1.44 +import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
    1.45 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeEnumLeafInfo;
    1.46 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElement;
    1.47 +import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
    1.48 +import com.sun.xml.internal.bind.v2.runtime.Name;
    1.49 +import com.sun.xml.internal.bind.v2.runtime.Transducer;
    1.50 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
    1.51 +
    1.52 +import org.xml.sax.SAXException;
    1.53 +
    1.54 +/**
    1.55 + * @author Kohsuke Kawaguchi
    1.56 + */
    1.57 +final class RuntimeEnumLeafInfoImpl<T extends Enum<T>,B> extends EnumLeafInfoImpl<Type,Class,Field,Method>
    1.58 +    implements RuntimeEnumLeafInfo, Transducer<T> {
    1.59 +
    1.60 +    public Transducer<T> getTransducer() {
    1.61 +        return this;
    1.62 +    }
    1.63 +
    1.64 +    /**
    1.65 +     * {@link Transducer} that knows how to convert a lexical value
    1.66 +     * into the Java value that we can handle.
    1.67 +     */
    1.68 +    private final Transducer<B> baseXducer;
    1.69 +
    1.70 +    private final Map<B,T> parseMap = new HashMap<B,T>();
    1.71 +    private final Map<T,B> printMap;
    1.72 +
    1.73 +    RuntimeEnumLeafInfoImpl(RuntimeModelBuilder builder, Locatable upstream, Class<T> enumType) {
    1.74 +        super(builder,upstream,enumType,enumType);
    1.75 +        this.printMap = new EnumMap<T,B>(enumType);
    1.76 +
    1.77 +        baseXducer = ((RuntimeNonElement)baseType).getTransducer();
    1.78 +    }
    1.79 +
    1.80 +    @Override
    1.81 +    public RuntimeEnumConstantImpl createEnumConstant(String name, String literal, Field constant, EnumConstantImpl<Type,Class,Field,Method> last) {
    1.82 +        T t;
    1.83 +        try {
    1.84 +            try {
    1.85 +                constant.setAccessible(true);
    1.86 +            } catch (SecurityException e) {
    1.87 +                // in case the constant is already accessible, swallow this error.
    1.88 +                // if the constant is indeed not accessible, we will get IllegalAccessException
    1.89 +                // in the following line, and that is not too late.
    1.90 +            }
    1.91 +            t = (T)constant.get(null);
    1.92 +        } catch (IllegalAccessException e) {
    1.93 +            // impossible, because this is an enum constant
    1.94 +            throw new IllegalAccessError(e.getMessage());
    1.95 +        }
    1.96 +
    1.97 +        B b = null;
    1.98 +        try {
    1.99 +            b = baseXducer.parse(literal);
   1.100 +        } catch (Exception e) {
   1.101 +            builder.reportError(new IllegalAnnotationException(
   1.102 +                Messages.INVALID_XML_ENUM_VALUE.format(literal,baseType.getType().toString()), e,
   1.103 +                    new FieldLocatable<Field>(this,constant,nav()) ));
   1.104 +        }
   1.105 +
   1.106 +        parseMap.put(b,t);
   1.107 +        printMap.put(t,b);
   1.108 +
   1.109 +        return new RuntimeEnumConstantImpl(this, name, literal, last);
   1.110 +    }
   1.111 +
   1.112 +    public QName[] getTypeNames() {
   1.113 +        return new QName[]{getTypeName()};
   1.114 +    }
   1.115 +
   1.116 +    public boolean isDefault() {
   1.117 +        return false;
   1.118 +    }
   1.119 +
   1.120 +    @Override
   1.121 +    public Class getClazz() {
   1.122 +        return clazz;
   1.123 +    }
   1.124 +
   1.125 +    public boolean useNamespace() {
   1.126 +        return baseXducer.useNamespace();
   1.127 +    }
   1.128 +
   1.129 +    public void declareNamespace(T t, XMLSerializer w) throws AccessorException {
   1.130 +        baseXducer.declareNamespace(printMap.get(t),w);
   1.131 +    }
   1.132 +
   1.133 +    public CharSequence print(T t) throws AccessorException {
   1.134 +        return baseXducer.print(printMap.get(t));
   1.135 +    }
   1.136 +
   1.137 +    public T parse(CharSequence lexical) throws AccessorException, SAXException {
   1.138 +        // TODO: error handling
   1.139 +
   1.140 +        B b = baseXducer.parse(lexical);
   1.141 +
   1.142 +        if (tokenStringType) {
   1.143 +            b = (B) ((String)b).trim();
   1.144 +        }
   1.145 +
   1.146 +        return parseMap.get(b);
   1.147 +    }
   1.148 +
   1.149 +    public void writeText(XMLSerializer w, T t, String fieldName) throws IOException, SAXException, XMLStreamException, AccessorException {
   1.150 +        baseXducer.writeText(w,printMap.get(t),fieldName);
   1.151 +    }
   1.152 +
   1.153 +    public void writeLeafElement(XMLSerializer w, Name tagName, T o, String fieldName) throws IOException, SAXException, XMLStreamException, AccessorException {
   1.154 +        baseXducer.writeLeafElement(w,tagName,printMap.get(o),fieldName);
   1.155 +    }
   1.156 +
   1.157 +    public QName getTypeName(T instance) {
   1.158 +        return null;
   1.159 +    }
   1.160 +}

mercurial