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.unmarshaller; aoqi@0: aoqi@0: import java.util.Collection; aoqi@0: aoqi@0: import javax.xml.bind.JAXBElement; aoqi@0: import javax.xml.namespace.QName; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.DatatypeConverterImpl; aoqi@0: import com.sun.xml.internal.bind.api.AccessorException; aoqi@0: import com.sun.xml.internal.bind.v2.WellKnownNamespace; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; aoqi@0: aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * Looks for xsi:nil='true' and sets the target to null. aoqi@0: * Otherwise delegate to another handler. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public class XsiNilLoader extends ProxyLoader { aoqi@0: aoqi@0: private final Loader defaultLoader; aoqi@0: aoqi@0: public XsiNilLoader(Loader defaultLoader) { aoqi@0: this.defaultLoader = defaultLoader; aoqi@0: assert defaultLoader!=null; aoqi@0: } aoqi@0: aoqi@0: protected Loader selectLoader(UnmarshallingContext.State state, TagName ea) throws SAXException { aoqi@0: int idx = ea.atts.getIndex(WellKnownNamespace.XML_SCHEMA_INSTANCE,"nil"); aoqi@0: aoqi@0: if (idx!=-1) { aoqi@0: Boolean b = DatatypeConverterImpl._parseBoolean(ea.atts.getValue(idx)); aoqi@0: aoqi@0: if (b != null && b) { aoqi@0: onNil(state); aoqi@0: boolean hasOtherAttributes = (ea.atts.getLength() - 1) > 0; aoqi@0: // see issues 6759703 and 565 - need to preserve attributes even if the element is nil; only when the type is stored in JAXBElement aoqi@0: if (!(hasOtherAttributes && (state.prev.target instanceof JAXBElement))) { aoqi@0: return Discarder.INSTANCE; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return defaultLoader; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Collection getExpectedChildElements() { aoqi@0: return defaultLoader.getExpectedChildElements(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Collection getExpectedAttributes() { aoqi@0: return defaultLoader.getExpectedAttributes(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Called when xsi:nil='true' was found. aoqi@0: */ aoqi@0: protected void onNil(UnmarshallingContext.State state) throws SAXException { aoqi@0: } aoqi@0: aoqi@0: public static final class Single extends XsiNilLoader { aoqi@0: private final Accessor acc; aoqi@0: public Single(Loader l, Accessor acc) { aoqi@0: super(l); aoqi@0: this.acc = acc; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: protected void onNil(UnmarshallingContext.State state) throws SAXException { aoqi@0: try { aoqi@0: acc.set(state.prev.target,null); aoqi@0: state.prev.nil = true; aoqi@0: } catch (AccessorException e) { aoqi@0: handleGenericException(e,true); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: public static final class Array extends XsiNilLoader { aoqi@0: public Array(Loader core) { aoqi@0: super(core); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: protected void onNil(UnmarshallingContext.State state) { aoqi@0: // let the receiver add this to the lister aoqi@0: state.target = null; aoqi@0: } aoqi@0: } aoqi@0: }