aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime.reflect; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.util.concurrent.Callable; aoqi@0: aoqi@0: import javax.xml.bind.JAXBException; aoqi@0: import javax.xml.bind.annotation.XmlValue; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: aoqi@0: import com.sun.istack.internal.NotNull; aoqi@0: import com.sun.istack.internal.Nullable; aoqi@0: import com.sun.istack.internal.SAXException2; aoqi@0: import com.sun.xml.internal.bind.WhiteSpaceProcessor; aoqi@0: import com.sun.xml.internal.bind.api.AccessorException; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.ID; aoqi@0: import com.sun.xml.internal.bind.v2.model.impl.RuntimeModelBuilder; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElementRef; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.Name; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.Transducer; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedTransducedAccessorFactory; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Patcher; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LocatorEx; aoqi@0: aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * {@link Accessor} and {@link Transducer} combined into one object. aoqi@0: * aoqi@0: *

aoqi@0: * This allows efficient conversions between primitive values and aoqi@0: * String without using boxing. aoqi@0: * aoqi@0: *

aoqi@0: * This abstraction only works for a single-value property. aoqi@0: * aoqi@0: *

aoqi@0: * An instance of {@link TransducedAccessor} implicitly holds a aoqi@0: * field of the {@code BeanT} that the accessors access. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) aoqi@0: */ aoqi@0: public abstract class TransducedAccessor { aoqi@0: aoqi@0: /** aoqi@0: * @see Transducer#useNamespace() aoqi@0: */ aoqi@0: public boolean useNamespace() { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Obtain the value of the field and declares the namespace URIs used in aoqi@0: * the value. aoqi@0: * aoqi@0: * @see Transducer#declareNamespace(Object, XMLSerializer) aoqi@0: */ aoqi@0: public void declareNamespace( BeanT o, XMLSerializer w ) throws AccessorException, SAXException { aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Prints the responsible field of the given bean to the writer. aoqi@0: * aoqi@0: *

aoqi@0: * Use {@link XMLSerializer#getInstance()} to access to the namespace bindings aoqi@0: * aoqi@0: * @return aoqi@0: * if the accessor didn't yield a value, return null. aoqi@0: */ aoqi@0: public abstract @Nullable CharSequence print(@NotNull BeanT o) throws AccessorException, SAXException; aoqi@0: aoqi@0: /** aoqi@0: * Parses the text value into the responsible field of the given bean. aoqi@0: * aoqi@0: *

aoqi@0: * Use {@link UnmarshallingContext#getInstance()} to access to the namespace bindings aoqi@0: * aoqi@0: * @throws AccessorException aoqi@0: * if the transducer is used to parse an user bean that uses {@link XmlValue}, aoqi@0: * then this exception may occur when it tries to set the leaf value to the bean. aoqi@0: * @throws RuntimeException aoqi@0: * if the lexical form is incorrect. The method may throw a RuntimeException, aoqi@0: * but it shouldn't cause the entire unmarshalling to fail. aoqi@0: * @throws SAXException aoqi@0: * if the parse method found an error, the error is reported, and then aoqi@0: * the processing is aborted. aoqi@0: */ aoqi@0: public abstract void parse(BeanT o, CharSequence lexical) throws AccessorException, SAXException; aoqi@0: aoqi@0: /** aoqi@0: * Checks if the field has a value. aoqi@0: */ aoqi@0: public abstract boolean hasValue(BeanT o) throws AccessorException; aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Gets the {@link TransducedAccessor} appropriately configured for aoqi@0: * the given property. aoqi@0: * aoqi@0: *

aoqi@0: * This allows the implementation to use an optimized code. aoqi@0: */ aoqi@0: public static TransducedAccessor get(JAXBContextImpl context, RuntimeNonElementRef ref) { aoqi@0: Transducer xducer = RuntimeModelBuilder.createTransducer(ref); aoqi@0: RuntimePropertyInfo prop = ref.getSource(); aoqi@0: aoqi@0: if(prop.isCollection()) { aoqi@0: return new ListTransducedAccessorImpl(xducer,prop.getAccessor(), aoqi@0: Lister.create(Utils.REFLECTION_NAVIGATOR.erasure(prop.getRawType()), prop.id(), prop.getAdapter())); aoqi@0: } aoqi@0: aoqi@0: if(prop.id()==ID.IDREF) aoqi@0: return new IDREFTransducedAccessorImpl(prop.getAccessor()); aoqi@0: aoqi@0: if(xducer.isDefault() && context != null && !context.fastBoot) { aoqi@0: TransducedAccessor xa = OptimizedTransducedAccessorFactory.get(prop); aoqi@0: if(xa!=null) return xa; aoqi@0: } aoqi@0: aoqi@0: if(xducer.useNamespace()) aoqi@0: return new CompositeContextDependentTransducedAccessorImpl( context, xducer, prop.getAccessor() ); aoqi@0: else aoqi@0: return new CompositeTransducedAccessorImpl( context, xducer, prop.getAccessor() ); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Convenience method to write the value as a text inside an element aoqi@0: * without any attributes. aoqi@0: * Can be overridden for improved performance. aoqi@0: * aoqi@0: *

aoqi@0: * The callee assumes that there's an associated value in the field. aoqi@0: * No @xsi:type handling is expected. aoqi@0: */ aoqi@0: public abstract void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException; aoqi@0: aoqi@0: /** aoqi@0: * Invokes one of the {@link XMLSerializer#text(String, String)} method aoqi@0: * with the representation of data bested suited for this transduced accessor. aoqi@0: */ aoqi@0: public abstract void writeText(XMLSerializer w, BeanT o, String fieldName) throws AccessorException, SAXException, IOException, XMLStreamException; aoqi@0: aoqi@0: static class CompositeContextDependentTransducedAccessorImpl extends CompositeTransducedAccessorImpl { aoqi@0: public CompositeContextDependentTransducedAccessorImpl(JAXBContextImpl context,Transducer xducer, Accessor acc) { aoqi@0: super(context,xducer,acc); aoqi@0: assert xducer.useNamespace(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean useNamespace() { aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void declareNamespace(BeanT bean, XMLSerializer w) throws AccessorException { aoqi@0: ValueT o = acc.get(bean); aoqi@0: if(o!=null) aoqi@0: xducer.declareNamespace(o,w); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException { aoqi@0: w.startElement(tagName,null); aoqi@0: declareNamespace(o,w); aoqi@0: w.endNamespaceDecls(null); aoqi@0: w.endAttributes(); aoqi@0: xducer.writeText(w,acc.get(o),fieldName); aoqi@0: w.endElement(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Implementation of {@link TransducedAccessor} that aoqi@0: * simply combines a {@link Transducer} and {@link Accessor}. aoqi@0: */ aoqi@0: public static class CompositeTransducedAccessorImpl extends TransducedAccessor { aoqi@0: protected final Transducer xducer; aoqi@0: protected final Accessor acc; aoqi@0: aoqi@0: public CompositeTransducedAccessorImpl(JAXBContextImpl context, Transducer xducer, Accessor acc) { aoqi@0: this.xducer = xducer; aoqi@0: this.acc = acc.optimize(context); aoqi@0: } aoqi@0: aoqi@0: public CharSequence print(BeanT bean) throws AccessorException { aoqi@0: ValueT o = acc.get(bean); aoqi@0: if(o==null) return null; aoqi@0: return xducer.print(o); aoqi@0: } aoqi@0: aoqi@0: public void parse(BeanT bean, CharSequence lexical) throws AccessorException, SAXException { aoqi@0: acc.set(bean,xducer.parse(lexical)); aoqi@0: } aoqi@0: aoqi@0: public boolean hasValue(BeanT bean) throws AccessorException { aoqi@0: return acc.getUnadapted(bean)!=null; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException { aoqi@0: xducer.writeLeafElement(w,tagName,acc.get(o),fieldName); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void writeText(XMLSerializer w, BeanT o, String fieldName) throws AccessorException, SAXException, IOException, XMLStreamException { aoqi@0: xducer.writeText(w,acc.get(o),fieldName); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * {@link TransducedAccessor} for IDREF. aoqi@0: * aoqi@0: * BeanT: the type of the bean that contains this the IDREF field. aoqi@0: * TargetT: the type of the bean pointed by IDREF. aoqi@0: */ aoqi@0: private static final class IDREFTransducedAccessorImpl extends DefaultTransducedAccessor { aoqi@0: private final Accessor acc; aoqi@0: /** aoqi@0: * The object that an IDREF resolves to should be aoqi@0: * assignable to this type. aoqi@0: */ aoqi@0: private final Class targetType; aoqi@0: aoqi@0: public IDREFTransducedAccessorImpl(Accessor acc) { aoqi@0: this.acc = acc; aoqi@0: this.targetType = acc.getValueType(); aoqi@0: } aoqi@0: aoqi@0: public String print(BeanT bean) throws AccessorException, SAXException { aoqi@0: TargetT target = acc.get(bean); aoqi@0: if(target==null) return null; aoqi@0: aoqi@0: XMLSerializer w = XMLSerializer.getInstance(); aoqi@0: try { aoqi@0: String id = w.grammar.getBeanInfo(target,true).getId(target,w); aoqi@0: if(id==null) aoqi@0: w.errorMissingId(target); aoqi@0: return id; aoqi@0: } catch (JAXBException e) { aoqi@0: w.reportError(null,e); aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private void assign( BeanT bean, TargetT t, UnmarshallingContext context ) throws AccessorException { aoqi@0: if(!targetType.isInstance(t)) aoqi@0: context.handleError(Messages.UNASSIGNABLE_TYPE.format(targetType,t.getClass())); aoqi@0: else aoqi@0: acc.set(bean,t); aoqi@0: } aoqi@0: aoqi@0: public void parse(final BeanT bean, CharSequence lexical) throws AccessorException, SAXException { aoqi@0: final String idref = WhiteSpaceProcessor.trim(lexical).toString(); aoqi@0: final UnmarshallingContext context = UnmarshallingContext.getInstance(); aoqi@0: aoqi@0: final Callable callable = context.getObjectFromId(idref,acc.valueType); aoqi@0: if(callable==null) { aoqi@0: // the IDResolver decided to abort it now aoqi@0: context.errorUnresolvedIDREF(bean,idref,context.getLocator()); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: TargetT t; aoqi@0: try { aoqi@0: t = (TargetT)callable.call(); aoqi@0: } catch (SAXException e) {// from callable.call aoqi@0: throw e; aoqi@0: } catch (RuntimeException e) {// from callable.call aoqi@0: throw e; aoqi@0: } catch (Exception e) {// from callable.call aoqi@0: throw new SAXException2(e); aoqi@0: } aoqi@0: if(t!=null) { aoqi@0: assign(bean,t,context); aoqi@0: } else { aoqi@0: // try again later aoqi@0: final LocatorEx loc = new LocatorEx.Snapshot(context.getLocator()); aoqi@0: context.addPatcher(new Patcher() { aoqi@0: public void run() throws SAXException { aoqi@0: try { aoqi@0: TargetT t = (TargetT)callable.call(); aoqi@0: if(t==null) { aoqi@0: context.errorUnresolvedIDREF(bean,idref,loc); aoqi@0: } else { aoqi@0: assign(bean,t,context); aoqi@0: } aoqi@0: } catch (AccessorException e) { aoqi@0: context.handleError(e); aoqi@0: } catch (SAXException e) {// from callable.call aoqi@0: throw e; aoqi@0: } catch (RuntimeException e) {// from callable.call aoqi@0: throw e; aoqi@0: } catch (Exception e) {// from callable.call aoqi@0: throw new SAXException2(e); aoqi@0: } aoqi@0: } aoqi@0: }); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public boolean hasValue(BeanT bean) throws AccessorException { aoqi@0: return acc.get(bean)!=null; aoqi@0: } aoqi@0: } aoqi@0: }