src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/reflect/TransducedAccessor.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/runtime/reflect/TransducedAccessor.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,345 @@
     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.runtime.reflect;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.util.concurrent.Callable;
    1.33 +
    1.34 +import javax.xml.bind.JAXBException;
    1.35 +import javax.xml.bind.annotation.XmlValue;
    1.36 +import javax.xml.stream.XMLStreamException;
    1.37 +
    1.38 +import com.sun.istack.internal.NotNull;
    1.39 +import com.sun.istack.internal.Nullable;
    1.40 +import com.sun.istack.internal.SAXException2;
    1.41 +import com.sun.xml.internal.bind.WhiteSpaceProcessor;
    1.42 +import com.sun.xml.internal.bind.api.AccessorException;
    1.43 +import com.sun.xml.internal.bind.v2.model.core.ID;
    1.44 +import com.sun.xml.internal.bind.v2.model.impl.RuntimeModelBuilder;
    1.45 +import com.sun.xml.internal.bind.v2.model.nav.Navigator;
    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.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 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
    1.52 +import com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedTransducedAccessorFactory;
    1.53 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Patcher;
    1.54 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
    1.55 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LocatorEx;
    1.56 +
    1.57 +import org.xml.sax.SAXException;
    1.58 +
    1.59 +/**
    1.60 + * {@link Accessor} and {@link Transducer} combined into one object.
    1.61 + *
    1.62 + * <p>
    1.63 + * This allows efficient conversions between primitive values and
    1.64 + * String without using boxing.
    1.65 + *
    1.66 + * <p>
    1.67 + * This abstraction only works for a single-value property.
    1.68 + *
    1.69 + * <p>
    1.70 + * An instance of {@link TransducedAccessor} implicitly holds a
    1.71 + * field of the {@code BeanT} that the accessors access.
    1.72 + *
    1.73 + * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    1.74 + */
    1.75 +public abstract class TransducedAccessor<BeanT> {
    1.76 +
    1.77 +    /**
    1.78 +     * @see Transducer#useNamespace()
    1.79 +     */
    1.80 +    public boolean useNamespace() {
    1.81 +        return false;
    1.82 +    }
    1.83 +
    1.84 +    /**
    1.85 +     * Obtain the value of the field and declares the namespace URIs used in
    1.86 +     * the value.
    1.87 +     *
    1.88 +     * @see Transducer#declareNamespace(Object, XMLSerializer)
    1.89 +     */
    1.90 +    public void declareNamespace( BeanT o, XMLSerializer w ) throws AccessorException, SAXException {
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * Prints the responsible field of the given bean to the writer.
    1.95 +     *
    1.96 +     * <p>
    1.97 +     * Use {@link XMLSerializer#getInstance()} to access to the namespace bindings
    1.98 +     *
    1.99 +     * @return
   1.100 +     *      if the accessor didn't yield a value, return null.
   1.101 +     */
   1.102 +    public abstract @Nullable CharSequence print(@NotNull BeanT o) throws AccessorException, SAXException;
   1.103 +
   1.104 +    /**
   1.105 +     * Parses the text value into the responsible field of the given bean.
   1.106 +     *
   1.107 +     * <p>
   1.108 +     * Use {@link UnmarshallingContext#getInstance()} to access to the namespace bindings
   1.109 +     *
   1.110 +     * @throws AccessorException
   1.111 +     *      if the transducer is used to parse an user bean that uses {@link XmlValue},
   1.112 +     *      then this exception may occur when it tries to set the leaf value to the bean.
   1.113 +     * @throws RuntimeException
   1.114 +     *      if the lexical form is incorrect. The method may throw a RuntimeException,
   1.115 +     *      but it shouldn't cause the entire unmarshalling to fail.
   1.116 +     * @throws SAXException
   1.117 +     *      if the parse method found an error, the error is reported, and then
   1.118 +     *      the processing is aborted.
   1.119 +     */
   1.120 +    public abstract void parse(BeanT o, CharSequence lexical) throws AccessorException, SAXException;
   1.121 +
   1.122 +    /**
   1.123 +     * Checks if the field has a value.
   1.124 +     */
   1.125 +    public abstract boolean hasValue(BeanT o) throws AccessorException;
   1.126 +
   1.127 +
   1.128 +
   1.129 +
   1.130 +
   1.131 +
   1.132 +
   1.133 +
   1.134 +
   1.135 +
   1.136 +
   1.137 +    /**
   1.138 +     * Gets the {@link TransducedAccessor} appropriately configured for
   1.139 +     * the given property.
   1.140 +     *
   1.141 +     * <p>
   1.142 +     * This allows the implementation to use an optimized code.
   1.143 +     */
   1.144 +    public static <T> TransducedAccessor<T> get(JAXBContextImpl context, RuntimeNonElementRef ref) {
   1.145 +        Transducer xducer = RuntimeModelBuilder.createTransducer(ref);
   1.146 +        RuntimePropertyInfo prop = ref.getSource();
   1.147 +
   1.148 +        if(prop.isCollection()) {
   1.149 +            return new ListTransducedAccessorImpl(xducer,prop.getAccessor(),
   1.150 +                    Lister.create(Navigator.REFLECTION.erasure(prop.getRawType()),prop.id(),
   1.151 +                    prop.getAdapter()));
   1.152 +        }
   1.153 +
   1.154 +        if(prop.id()==ID.IDREF)
   1.155 +            return new IDREFTransducedAccessorImpl(prop.getAccessor());
   1.156 +
   1.157 +        if(xducer.isDefault() && context != null && !context.fastBoot) {
   1.158 +            TransducedAccessor xa = OptimizedTransducedAccessorFactory.get(prop);
   1.159 +            if(xa!=null)    return xa;
   1.160 +        }
   1.161 +
   1.162 +        if(xducer.useNamespace())
   1.163 +            return new CompositeContextDependentTransducedAccessorImpl( context, xducer, prop.getAccessor() );
   1.164 +        else
   1.165 +            return new CompositeTransducedAccessorImpl( context, xducer, prop.getAccessor() );
   1.166 +    }
   1.167 +
   1.168 +    /**
   1.169 +     * Convenience method to write the value as a text inside an element
   1.170 +     * without any attributes.
   1.171 +     * Can be overridden for improved performance.
   1.172 +     *
   1.173 +     * <p>
   1.174 +     * The callee assumes that there's an associated value in the field.
   1.175 +     * No @xsi:type handling is expected.
   1.176 +     */
   1.177 +    public abstract void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException;
   1.178 +
   1.179 +    /**
   1.180 +     * Invokes one of the {@link XMLSerializer#text(String, String)} method
   1.181 +     * with the representation of data bested suited for this transduced accessor.
   1.182 +     */
   1.183 +    public abstract void writeText(XMLSerializer w, BeanT o, String fieldName) throws AccessorException, SAXException, IOException, XMLStreamException;
   1.184 +
   1.185 +    static class CompositeContextDependentTransducedAccessorImpl<BeanT,ValueT> extends CompositeTransducedAccessorImpl<BeanT,ValueT> {
   1.186 +        public CompositeContextDependentTransducedAccessorImpl(JAXBContextImpl context,Transducer<ValueT> xducer, Accessor<BeanT,ValueT> acc) {
   1.187 +            super(context,xducer,acc);
   1.188 +            assert xducer.useNamespace();
   1.189 +        }
   1.190 +
   1.191 +        @Override
   1.192 +        public boolean useNamespace() {
   1.193 +            return true;
   1.194 +        }
   1.195 +
   1.196 +        @Override
   1.197 +        public void declareNamespace(BeanT bean, XMLSerializer w) throws AccessorException {
   1.198 +            ValueT o = acc.get(bean);
   1.199 +            if(o!=null)
   1.200 +                xducer.declareNamespace(o,w);
   1.201 +        }
   1.202 +
   1.203 +        @Override
   1.204 +        public void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException {
   1.205 +            w.startElement(tagName,null);
   1.206 +            declareNamespace(o,w);
   1.207 +            w.endNamespaceDecls(null);
   1.208 +            w.endAttributes();
   1.209 +            xducer.writeText(w,acc.get(o),fieldName);
   1.210 +            w.endElement();
   1.211 +        }
   1.212 +    }
   1.213 +
   1.214 +
   1.215 +    /**
   1.216 +     * Implementation of {@link TransducedAccessor} that
   1.217 +     * simply combines a {@link Transducer} and {@link Accessor}.
   1.218 +     */
   1.219 +    public static class CompositeTransducedAccessorImpl<BeanT,ValueT> extends TransducedAccessor<BeanT> {
   1.220 +        protected final Transducer<ValueT> xducer;
   1.221 +        protected final Accessor<BeanT,ValueT> acc;
   1.222 +
   1.223 +        public CompositeTransducedAccessorImpl(JAXBContextImpl context, Transducer<ValueT> xducer, Accessor<BeanT,ValueT> acc) {
   1.224 +            this.xducer = xducer;
   1.225 +            this.acc = acc.optimize(context);
   1.226 +        }
   1.227 +
   1.228 +        public CharSequence print(BeanT bean) throws AccessorException {
   1.229 +            ValueT o = acc.get(bean);
   1.230 +            if(o==null)     return null;
   1.231 +            return xducer.print(o);
   1.232 +        }
   1.233 +
   1.234 +        public void parse(BeanT bean, CharSequence lexical) throws AccessorException, SAXException {
   1.235 +            acc.set(bean,xducer.parse(lexical));
   1.236 +        }
   1.237 +
   1.238 +        public boolean hasValue(BeanT bean) throws AccessorException {
   1.239 +            return acc.getUnadapted(bean)!=null;
   1.240 +        }
   1.241 +
   1.242 +        @Override
   1.243 +        public void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException {
   1.244 +            xducer.writeLeafElement(w,tagName,acc.get(o),fieldName);
   1.245 +        }
   1.246 +
   1.247 +        @Override
   1.248 +        public void writeText(XMLSerializer w, BeanT o, String fieldName) throws AccessorException, SAXException, IOException, XMLStreamException {
   1.249 +            xducer.writeText(w,acc.get(o),fieldName);
   1.250 +        }
   1.251 +    }
   1.252 +
   1.253 +    /**
   1.254 +     * {@link TransducedAccessor} for IDREF.
   1.255 +     *
   1.256 +     * BeanT: the type of the bean that contains this the IDREF field.
   1.257 +     * TargetT: the type of the bean pointed by IDREF.
   1.258 +     */
   1.259 +    private static final class IDREFTransducedAccessorImpl<BeanT,TargetT> extends DefaultTransducedAccessor<BeanT> {
   1.260 +        private final Accessor<BeanT,TargetT> acc;
   1.261 +        /**
   1.262 +         * The object that an IDREF resolves to should be
   1.263 +         * assignable to this type.
   1.264 +         */
   1.265 +        private final Class<TargetT> targetType;
   1.266 +
   1.267 +        public IDREFTransducedAccessorImpl(Accessor<BeanT, TargetT> acc) {
   1.268 +            this.acc = acc;
   1.269 +            this.targetType = acc.getValueType();
   1.270 +        }
   1.271 +
   1.272 +        public String print(BeanT bean) throws AccessorException, SAXException {
   1.273 +            TargetT target = acc.get(bean);
   1.274 +            if(target==null)    return null;
   1.275 +
   1.276 +            XMLSerializer w = XMLSerializer.getInstance();
   1.277 +            try {
   1.278 +                String id = w.grammar.getBeanInfo(target,true).getId(target,w);
   1.279 +                if(id==null)
   1.280 +                    w.errorMissingId(target);
   1.281 +                return id;
   1.282 +            } catch (JAXBException e) {
   1.283 +                w.reportError(null,e);
   1.284 +                return null;
   1.285 +            }
   1.286 +        }
   1.287 +
   1.288 +        private void assign( BeanT bean, TargetT t, UnmarshallingContext context ) throws AccessorException {
   1.289 +            if(!targetType.isInstance(t))
   1.290 +                context.handleError(Messages.UNASSIGNABLE_TYPE.format(targetType,t.getClass()));
   1.291 +            else
   1.292 +                acc.set(bean,t);
   1.293 +        }
   1.294 +
   1.295 +        public void parse(final BeanT bean, CharSequence lexical) throws AccessorException, SAXException {
   1.296 +            final String idref = WhiteSpaceProcessor.trim(lexical).toString();
   1.297 +            final UnmarshallingContext context = UnmarshallingContext.getInstance();
   1.298 +
   1.299 +            final Callable callable = context.getObjectFromId(idref,acc.valueType);
   1.300 +            if(callable==null) {
   1.301 +                // the IDResolver decided to abort it now
   1.302 +                context.errorUnresolvedIDREF(bean,idref,context.getLocator());
   1.303 +                return;
   1.304 +            }
   1.305 +
   1.306 +            TargetT t;
   1.307 +            try {
   1.308 +                t = (TargetT)callable.call();
   1.309 +            } catch (SAXException e) {// from callable.call
   1.310 +                throw e;
   1.311 +            } catch (RuntimeException e) {// from callable.call
   1.312 +                throw e;
   1.313 +            } catch (Exception e) {// from callable.call
   1.314 +                throw new SAXException2(e);
   1.315 +            }
   1.316 +            if(t!=null) {
   1.317 +                assign(bean,t,context);
   1.318 +            } else {
   1.319 +                // try again later
   1.320 +                final LocatorEx loc = new LocatorEx.Snapshot(context.getLocator());
   1.321 +                context.addPatcher(new Patcher() {
   1.322 +                    public void run() throws SAXException {
   1.323 +                        try {
   1.324 +                            TargetT t = (TargetT)callable.call();
   1.325 +                            if(t==null) {
   1.326 +                                context.errorUnresolvedIDREF(bean,idref,loc);
   1.327 +                            } else {
   1.328 +                                assign(bean,t,context);
   1.329 +                            }
   1.330 +                        } catch (AccessorException e) {
   1.331 +                            context.handleError(e);
   1.332 +                        } catch (SAXException e) {// from callable.call
   1.333 +                            throw e;
   1.334 +                        } catch (RuntimeException e) {// from callable.call
   1.335 +                            throw e;
   1.336 +                        } catch (Exception e) {// from callable.call
   1.337 +                            throw new SAXException2(e);
   1.338 +                        }
   1.339 +                    }
   1.340 +                });
   1.341 +            }
   1.342 +        }
   1.343 +
   1.344 +        public boolean hasValue(BeanT bean) throws AccessorException {
   1.345 +            return acc.get(bean)!=null;
   1.346 +        }
   1.347 +    }
   1.348 +}

mercurial