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