ohair@286: /*
ohair@286: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286: *
ohair@286: * This code is free software; you can redistribute it and/or modify it
ohair@286: * under the terms of the GNU General Public License version 2 only, as
ohair@286: * published by the Free Software Foundation. Oracle designates this
ohair@286: * particular file as subject to the "Classpath" exception as provided
ohair@286: * by Oracle in the LICENSE file that accompanied this code.
ohair@286: *
ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286: * version 2 for more details (a copy is included in the LICENSE file that
ohair@286: * accompanied this code).
ohair@286: *
ohair@286: * You should have received a copy of the GNU General Public License version
ohair@286: * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286: *
ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286: * or visit www.oracle.com if you need additional information or have any
ohair@286: * questions.
ohair@286: */
ohair@286:
ohair@286: package com.sun.xml.internal.bind.v2.runtime.reflect;
ohair@286:
ohair@286: import java.io.IOException;
ohair@286: import java.util.concurrent.Callable;
ohair@286:
ohair@286: import javax.xml.bind.JAXBException;
ohair@286: import javax.xml.bind.annotation.XmlValue;
ohair@286: import javax.xml.stream.XMLStreamException;
ohair@286:
ohair@286: import com.sun.istack.internal.NotNull;
ohair@286: import com.sun.istack.internal.Nullable;
ohair@286: import com.sun.istack.internal.SAXException2;
ohair@286: import com.sun.xml.internal.bind.WhiteSpaceProcessor;
ohair@286: import com.sun.xml.internal.bind.api.AccessorException;
ohair@286: import com.sun.xml.internal.bind.v2.model.core.ID;
ohair@286: import com.sun.xml.internal.bind.v2.model.impl.RuntimeModelBuilder;
ohair@286: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElementRef;
ohair@286: import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.Name;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.Transducer;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedTransducedAccessorFactory;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Patcher;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LocatorEx;
ohair@286:
ohair@286: import org.xml.sax.SAXException;
ohair@286:
ohair@286: /**
ohair@286: * {@link Accessor} and {@link Transducer} combined into one object.
ohair@286: *
ohair@286: *
ohair@286: * This allows efficient conversions between primitive values and
ohair@286: * String without using boxing.
ohair@286: *
ohair@286: *
ohair@286: * This abstraction only works for a single-value property.
ohair@286: *
ohair@286: *
ohair@286: * An instance of {@link TransducedAccessor} implicitly holds a
ohair@286: * field of the {@code BeanT} that the accessors access.
ohair@286: *
ohair@286: * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286: */
ohair@286: public abstract class TransducedAccessor {
ohair@286:
ohair@286: /**
ohair@286: * @see Transducer#useNamespace()
ohair@286: */
ohair@286: public boolean useNamespace() {
ohair@286: return false;
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Obtain the value of the field and declares the namespace URIs used in
ohair@286: * the value.
ohair@286: *
ohair@286: * @see Transducer#declareNamespace(Object, XMLSerializer)
ohair@286: */
ohair@286: public void declareNamespace( BeanT o, XMLSerializer w ) throws AccessorException, SAXException {
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Prints the responsible field of the given bean to the writer.
ohair@286: *
ohair@286: *
ohair@286: * Use {@link XMLSerializer#getInstance()} to access to the namespace bindings
ohair@286: *
ohair@286: * @return
ohair@286: * if the accessor didn't yield a value, return null.
ohair@286: */
ohair@286: public abstract @Nullable CharSequence print(@NotNull BeanT o) throws AccessorException, SAXException;
ohair@286:
ohair@286: /**
ohair@286: * Parses the text value into the responsible field of the given bean.
ohair@286: *
ohair@286: *
ohair@286: * Use {@link UnmarshallingContext#getInstance()} to access to the namespace bindings
ohair@286: *
ohair@286: * @throws AccessorException
ohair@286: * if the transducer is used to parse an user bean that uses {@link XmlValue},
ohair@286: * then this exception may occur when it tries to set the leaf value to the bean.
ohair@286: * @throws RuntimeException
ohair@286: * if the lexical form is incorrect. The method may throw a RuntimeException,
ohair@286: * but it shouldn't cause the entire unmarshalling to fail.
ohair@286: * @throws SAXException
ohair@286: * if the parse method found an error, the error is reported, and then
ohair@286: * the processing is aborted.
ohair@286: */
ohair@286: public abstract void parse(BeanT o, CharSequence lexical) throws AccessorException, SAXException;
ohair@286:
ohair@286: /**
ohair@286: * Checks if the field has a value.
ohair@286: */
ohair@286: public abstract boolean hasValue(BeanT o) throws AccessorException;
ohair@286:
ohair@286:
ohair@286:
ohair@286:
ohair@286:
ohair@286:
ohair@286:
ohair@286:
ohair@286:
ohair@286:
ohair@286:
ohair@286: /**
ohair@286: * Gets the {@link TransducedAccessor} appropriately configured for
ohair@286: * the given property.
ohair@286: *
ohair@286: *
ohair@286: * This allows the implementation to use an optimized code.
ohair@286: */
ohair@286: public static TransducedAccessor get(JAXBContextImpl context, RuntimeNonElementRef ref) {
ohair@286: Transducer xducer = RuntimeModelBuilder.createTransducer(ref);
ohair@286: RuntimePropertyInfo prop = ref.getSource();
ohair@286:
ohair@286: if(prop.isCollection()) {
ohair@286: return new ListTransducedAccessorImpl(xducer,prop.getAccessor(),
mkos@450: Lister.create(Utils.REFLECTION_NAVIGATOR.erasure(prop.getRawType()), prop.id(), prop.getAdapter()));
ohair@286: }
ohair@286:
ohair@286: if(prop.id()==ID.IDREF)
ohair@286: return new IDREFTransducedAccessorImpl(prop.getAccessor());
ohair@286:
ohair@286: if(xducer.isDefault() && context != null && !context.fastBoot) {
ohair@286: TransducedAccessor xa = OptimizedTransducedAccessorFactory.get(prop);
ohair@286: if(xa!=null) return xa;
ohair@286: }
ohair@286:
ohair@286: if(xducer.useNamespace())
ohair@286: return new CompositeContextDependentTransducedAccessorImpl( context, xducer, prop.getAccessor() );
ohair@286: else
ohair@286: return new CompositeTransducedAccessorImpl( context, xducer, prop.getAccessor() );
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Convenience method to write the value as a text inside an element
ohair@286: * without any attributes.
ohair@286: * Can be overridden for improved performance.
ohair@286: *
ohair@286: *
ohair@286: * The callee assumes that there's an associated value in the field.
ohair@286: * No @xsi:type handling is expected.
ohair@286: */
ohair@286: public abstract void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException;
ohair@286:
ohair@286: /**
ohair@286: * Invokes one of the {@link XMLSerializer#text(String, String)} method
ohair@286: * with the representation of data bested suited for this transduced accessor.
ohair@286: */
ohair@286: public abstract void writeText(XMLSerializer w, BeanT o, String fieldName) throws AccessorException, SAXException, IOException, XMLStreamException;
ohair@286:
ohair@286: static class CompositeContextDependentTransducedAccessorImpl extends CompositeTransducedAccessorImpl {
ohair@286: public CompositeContextDependentTransducedAccessorImpl(JAXBContextImpl context,Transducer xducer, Accessor acc) {
ohair@286: super(context,xducer,acc);
ohair@286: assert xducer.useNamespace();
ohair@286: }
ohair@286:
ohair@286: @Override
ohair@286: public boolean useNamespace() {
ohair@286: return true;
ohair@286: }
ohair@286:
ohair@286: @Override
ohair@286: public void declareNamespace(BeanT bean, XMLSerializer w) throws AccessorException {
ohair@286: ValueT o = acc.get(bean);
ohair@286: if(o!=null)
ohair@286: xducer.declareNamespace(o,w);
ohair@286: }
ohair@286:
ohair@286: @Override
ohair@286: public void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException {
ohair@286: w.startElement(tagName,null);
ohair@286: declareNamespace(o,w);
ohair@286: w.endNamespaceDecls(null);
ohair@286: w.endAttributes();
ohair@286: xducer.writeText(w,acc.get(o),fieldName);
ohair@286: w.endElement();
ohair@286: }
ohair@286: }
ohair@286:
ohair@286:
ohair@286: /**
ohair@286: * Implementation of {@link TransducedAccessor} that
ohair@286: * simply combines a {@link Transducer} and {@link Accessor}.
ohair@286: */
ohair@286: public static class CompositeTransducedAccessorImpl extends TransducedAccessor {
ohair@286: protected final Transducer xducer;
ohair@286: protected final Accessor acc;
ohair@286:
ohair@286: public CompositeTransducedAccessorImpl(JAXBContextImpl context, Transducer xducer, Accessor acc) {
ohair@286: this.xducer = xducer;
ohair@286: this.acc = acc.optimize(context);
ohair@286: }
ohair@286:
ohair@286: public CharSequence print(BeanT bean) throws AccessorException {
ohair@286: ValueT o = acc.get(bean);
ohair@286: if(o==null) return null;
ohair@286: return xducer.print(o);
ohair@286: }
ohair@286:
ohair@286: public void parse(BeanT bean, CharSequence lexical) throws AccessorException, SAXException {
ohair@286: acc.set(bean,xducer.parse(lexical));
ohair@286: }
ohair@286:
ohair@286: public boolean hasValue(BeanT bean) throws AccessorException {
ohair@286: return acc.getUnadapted(bean)!=null;
ohair@286: }
ohair@286:
ohair@286: @Override
ohair@286: public void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws SAXException, AccessorException, IOException, XMLStreamException {
ohair@286: xducer.writeLeafElement(w,tagName,acc.get(o),fieldName);
ohair@286: }
ohair@286:
ohair@286: @Override
ohair@286: public void writeText(XMLSerializer w, BeanT o, String fieldName) throws AccessorException, SAXException, IOException, XMLStreamException {
ohair@286: xducer.writeText(w,acc.get(o),fieldName);
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * {@link TransducedAccessor} for IDREF.
ohair@286: *
ohair@286: * BeanT: the type of the bean that contains this the IDREF field.
ohair@286: * TargetT: the type of the bean pointed by IDREF.
ohair@286: */
ohair@286: private static final class IDREFTransducedAccessorImpl extends DefaultTransducedAccessor {
ohair@286: private final Accessor acc;
ohair@286: /**
ohair@286: * The object that an IDREF resolves to should be
ohair@286: * assignable to this type.
ohair@286: */
ohair@286: private final Class targetType;
ohair@286:
ohair@286: public IDREFTransducedAccessorImpl(Accessor acc) {
ohair@286: this.acc = acc;
ohair@286: this.targetType = acc.getValueType();
ohair@286: }
ohair@286:
ohair@286: public String print(BeanT bean) throws AccessorException, SAXException {
ohair@286: TargetT target = acc.get(bean);
ohair@286: if(target==null) return null;
ohair@286:
ohair@286: XMLSerializer w = XMLSerializer.getInstance();
ohair@286: try {
ohair@286: String id = w.grammar.getBeanInfo(target,true).getId(target,w);
ohair@286: if(id==null)
ohair@286: w.errorMissingId(target);
ohair@286: return id;
ohair@286: } catch (JAXBException e) {
ohair@286: w.reportError(null,e);
ohair@286: return null;
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: private void assign( BeanT bean, TargetT t, UnmarshallingContext context ) throws AccessorException {
ohair@286: if(!targetType.isInstance(t))
ohair@286: context.handleError(Messages.UNASSIGNABLE_TYPE.format(targetType,t.getClass()));
ohair@286: else
ohair@286: acc.set(bean,t);
ohair@286: }
ohair@286:
ohair@286: public void parse(final BeanT bean, CharSequence lexical) throws AccessorException, SAXException {
ohair@286: final String idref = WhiteSpaceProcessor.trim(lexical).toString();
ohair@286: final UnmarshallingContext context = UnmarshallingContext.getInstance();
ohair@286:
ohair@286: final Callable callable = context.getObjectFromId(idref,acc.valueType);
ohair@286: if(callable==null) {
ohair@286: // the IDResolver decided to abort it now
ohair@286: context.errorUnresolvedIDREF(bean,idref,context.getLocator());
ohair@286: return;
ohair@286: }
ohair@286:
ohair@286: TargetT t;
ohair@286: try {
ohair@286: t = (TargetT)callable.call();
ohair@286: } catch (SAXException e) {// from callable.call
ohair@286: throw e;
ohair@286: } catch (RuntimeException e) {// from callable.call
ohair@286: throw e;
ohair@286: } catch (Exception e) {// from callable.call
ohair@286: throw new SAXException2(e);
ohair@286: }
ohair@286: if(t!=null) {
ohair@286: assign(bean,t,context);
ohair@286: } else {
ohair@286: // try again later
ohair@286: final LocatorEx loc = new LocatorEx.Snapshot(context.getLocator());
ohair@286: context.addPatcher(new Patcher() {
ohair@286: public void run() throws SAXException {
ohair@286: try {
ohair@286: TargetT t = (TargetT)callable.call();
ohair@286: if(t==null) {
ohair@286: context.errorUnresolvedIDREF(bean,idref,loc);
ohair@286: } else {
ohair@286: assign(bean,t,context);
ohair@286: }
ohair@286: } catch (AccessorException e) {
ohair@286: context.handleError(e);
ohair@286: } catch (SAXException e) {// from callable.call
ohair@286: throw e;
ohair@286: } catch (RuntimeException e) {// from callable.call
ohair@286: throw e;
ohair@286: } catch (Exception e) {// from callable.call
ohair@286: throw new SAXException2(e);
ohair@286: }
ohair@286: }
ohair@286: });
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: public boolean hasValue(BeanT bean) throws AccessorException {
ohair@286: return acc.get(bean)!=null;
ohair@286: }
ohair@286: }
ohair@286: }