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

changeset 286
f50545b5e2f1
child 450
b0c2840e2513
     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/RuntimeModelBuilder.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,184 @@
     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.lang.reflect.Field;
    1.32 +import java.lang.reflect.Method;
    1.33 +import java.lang.reflect.Type;
    1.34 +import java.util.Map;
    1.35 +
    1.36 +import javax.activation.MimeType;
    1.37 +
    1.38 +import com.sun.xml.internal.bind.WhiteSpaceProcessor;
    1.39 +import com.sun.xml.internal.bind.api.AccessorException;
    1.40 +import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
    1.41 +import com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader;
    1.42 +import com.sun.xml.internal.bind.v2.model.core.ID;
    1.43 +import com.sun.xml.internal.bind.v2.model.nav.Navigator;
    1.44 +import com.sun.xml.internal.bind.v2.model.nav.ReflectionNavigator;
    1.45 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElement;
    1.46 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElementRef;
    1.47 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
    1.48 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfoSet;
    1.49 +import com.sun.xml.internal.bind.v2.runtime.FilterTransducer;
    1.50 +import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
    1.51 +import com.sun.xml.internal.bind.v2.runtime.InlineBinaryTransducer;
    1.52 +import com.sun.xml.internal.bind.v2.runtime.MimeTypedTransducer;
    1.53 +import com.sun.xml.internal.bind.v2.runtime.SchemaTypeTransducer;
    1.54 +import com.sun.xml.internal.bind.v2.runtime.Transducer;
    1.55 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
    1.56 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
    1.57 +import com.sun.istack.internal.Nullable;
    1.58 +
    1.59 +import com.sun.xml.internal.bind.v2.WellKnownNamespace;
    1.60 +import javax.xml.namespace.QName;
    1.61 +import org.xml.sax.SAXException;
    1.62 +
    1.63 +/**
    1.64 + * {@link ModelBuilder} that works at the run-time by using
    1.65 + * the {@code java.lang.reflect} package.
    1.66 + *
    1.67 + * <p>
    1.68 + * This extends {@link ModelBuilder} by providing more functionalities such
    1.69 + * as accessing the fields and classes.
    1.70 + *
    1.71 + * @author Kohsuke Kawaguchi (kk@kohsuke.org)
    1.72 + */
    1.73 +public class RuntimeModelBuilder extends ModelBuilder<Type,Class,Field,Method> {
    1.74 +    /**
    1.75 +     * The {@link JAXBContextImpl} for which the model is built.
    1.76 +     * Null when created for reflection.
    1.77 +     */
    1.78 +    public final @Nullable JAXBContextImpl context;
    1.79 +
    1.80 +    public RuntimeModelBuilder(JAXBContextImpl context, RuntimeAnnotationReader annotationReader, Map<Class, Class> subclassReplacements, String defaultNamespaceRemap) {
    1.81 +        super(annotationReader, Navigator.REFLECTION, subclassReplacements, defaultNamespaceRemap);
    1.82 +        this.context = context;
    1.83 +    }
    1.84 +
    1.85 +    @Override
    1.86 +    public RuntimeNonElement getClassInfo( Class clazz, Locatable upstream ) {
    1.87 +        return (RuntimeNonElement)super.getClassInfo(clazz,upstream);
    1.88 +    }
    1.89 +
    1.90 +    @Override
    1.91 +    public RuntimeNonElement getClassInfo( Class clazz, boolean searchForSuperClass, Locatable upstream ) {
    1.92 +        return (RuntimeNonElement)super.getClassInfo(clazz,searchForSuperClass,upstream);
    1.93 +    }
    1.94 +
    1.95 +    @Override
    1.96 +    protected RuntimeEnumLeafInfoImpl createEnumLeafInfo(Class clazz, Locatable upstream) {
    1.97 +        return new RuntimeEnumLeafInfoImpl(this,upstream,clazz);
    1.98 +    }
    1.99 +
   1.100 +    @Override
   1.101 +    protected RuntimeClassInfoImpl createClassInfo( Class clazz, Locatable upstream ) {
   1.102 +        return new RuntimeClassInfoImpl(this,upstream,clazz);
   1.103 +    }
   1.104 +
   1.105 +    @Override
   1.106 +    public RuntimeElementInfoImpl createElementInfo(RegistryInfoImpl<Type,Class,Field,Method> registryInfo, Method method) throws IllegalAnnotationException {
   1.107 +        return new RuntimeElementInfoImpl(this,registryInfo, method);
   1.108 +    }
   1.109 +
   1.110 +    @Override
   1.111 +    public RuntimeArrayInfoImpl createArrayInfo(Locatable upstream, Type arrayType) {
   1.112 +        return new RuntimeArrayInfoImpl(this, upstream, (Class)arrayType);
   1.113 +    }
   1.114 +
   1.115 +    public ReflectionNavigator getNavigator() {
   1.116 +        return (ReflectionNavigator)nav;
   1.117 +    }
   1.118 +
   1.119 +    @Override
   1.120 +    protected RuntimeTypeInfoSetImpl createTypeInfoSet() {
   1.121 +        return new RuntimeTypeInfoSetImpl(reader);
   1.122 +    }
   1.123 +
   1.124 +    @Override
   1.125 +    public RuntimeTypeInfoSet link() {
   1.126 +        return (RuntimeTypeInfoSet)super.link();
   1.127 +    }
   1.128 +
   1.129 +    /**
   1.130 +     * Creates a {@link Transducer} given a reference.
   1.131 +     *
   1.132 +     * Used to implement {@link RuntimeNonElementRef#getTransducer()}.
   1.133 +     * Shouldn't be called from anywhere else.
   1.134 +     *
   1.135 +     * TODO: this is not the proper place for this class to be in.
   1.136 +     */
   1.137 +    public static Transducer createTransducer(RuntimeNonElementRef ref) {
   1.138 +        Transducer t = ref.getTarget().getTransducer();
   1.139 +        RuntimePropertyInfo src = ref.getSource();
   1.140 +        ID id = src.id();
   1.141 +
   1.142 +        if(id==ID.IDREF)
   1.143 +            return RuntimeBuiltinLeafInfoImpl.STRING;
   1.144 +
   1.145 +        if(id==ID.ID)
   1.146 +            t = new IDTransducerImpl(t);
   1.147 +
   1.148 +        MimeType emt = src.getExpectedMimeType();
   1.149 +        if(emt!=null)
   1.150 +            t = new MimeTypedTransducer(t,emt);
   1.151 +
   1.152 +        if(src.inlineBinaryData())
   1.153 +            t = new InlineBinaryTransducer(t);
   1.154 +
   1.155 +        if(src.getSchemaType()!=null) {
   1.156 +            if (src.getSchemaType().equals(createXSSimpleType())) {
   1.157 +                return RuntimeBuiltinLeafInfoImpl.STRING;
   1.158 +            }
   1.159 +            t = new SchemaTypeTransducer(t,src.getSchemaType());
   1.160 +        }
   1.161 +
   1.162 +        return t;
   1.163 +    }
   1.164 +
   1.165 +    private static QName createXSSimpleType() {
   1.166 +        return new QName(WellKnownNamespace.XML_SCHEMA,"anySimpleType");
   1.167 +    }
   1.168 +
   1.169 +    /**
   1.170 +     * Transducer implementation for ID.
   1.171 +     *
   1.172 +     * This transducer wraps another {@link Transducer} and adds
   1.173 +     * handling for ID.
   1.174 +     */
   1.175 +    private static final class IDTransducerImpl<ValueT> extends FilterTransducer<ValueT> {
   1.176 +        public IDTransducerImpl(Transducer<ValueT> core) {
   1.177 +            super(core);
   1.178 +        }
   1.179 +
   1.180 +        @Override
   1.181 +        public ValueT parse(CharSequence lexical) throws AccessorException, SAXException {
   1.182 +            String value = WhiteSpaceProcessor.trim(lexical).toString();
   1.183 +            UnmarshallingContext.getInstance().addToIdTable(value);
   1.184 +            return core.parse(value);
   1.185 +        }
   1.186 +    }
   1.187 +}

mercurial