|
1 /* |
|
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.bind.v2.runtime.unmarshaller; |
|
27 |
|
28 import java.util.Collection; |
|
29 |
|
30 import javax.xml.bind.JAXBElement; |
|
31 import javax.xml.namespace.QName; |
|
32 |
|
33 import com.sun.xml.internal.bind.DatatypeConverterImpl; |
|
34 import com.sun.xml.internal.bind.api.AccessorException; |
|
35 import com.sun.xml.internal.bind.v2.WellKnownNamespace; |
|
36 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; |
|
37 |
|
38 import org.xml.sax.SAXException; |
|
39 |
|
40 /** |
|
41 * Looks for xsi:nil='true' and sets the target to null. |
|
42 * Otherwise delegate to another handler. |
|
43 * |
|
44 * @author Kohsuke Kawaguchi |
|
45 */ |
|
46 public class XsiNilLoader extends ProxyLoader { |
|
47 |
|
48 private final Loader defaultLoader; |
|
49 |
|
50 public XsiNilLoader(Loader defaultLoader) { |
|
51 this.defaultLoader = defaultLoader; |
|
52 assert defaultLoader!=null; |
|
53 } |
|
54 |
|
55 protected Loader selectLoader(UnmarshallingContext.State state, TagName ea) throws SAXException { |
|
56 int idx = ea.atts.getIndex(WellKnownNamespace.XML_SCHEMA_INSTANCE,"nil"); |
|
57 |
|
58 if (idx!=-1) { |
|
59 Boolean b = DatatypeConverterImpl._parseBoolean(ea.atts.getValue(idx)); |
|
60 |
|
61 if (b != null && b) { |
|
62 onNil(state); |
|
63 boolean hasOtherAttributes = (ea.atts.getLength() - 1) > 0; |
|
64 // see issues 6759703 and 565 - need to preserve attributes even if the element is nil; only when the type is stored in JAXBElement |
|
65 if (!(hasOtherAttributes && (state.prev.target instanceof JAXBElement))) { |
|
66 return Discarder.INSTANCE; |
|
67 } |
|
68 } |
|
69 } |
|
70 return defaultLoader; |
|
71 } |
|
72 |
|
73 @Override |
|
74 public Collection<QName> getExpectedChildElements() { |
|
75 return defaultLoader.getExpectedChildElements(); |
|
76 } |
|
77 |
|
78 @Override |
|
79 public Collection<QName> getExpectedAttributes() { |
|
80 return defaultLoader.getExpectedAttributes(); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Called when xsi:nil='true' was found. |
|
85 */ |
|
86 protected void onNil(UnmarshallingContext.State state) throws SAXException { |
|
87 } |
|
88 |
|
89 public static final class Single extends XsiNilLoader { |
|
90 private final Accessor acc; |
|
91 public Single(Loader l, Accessor acc) { |
|
92 super(l); |
|
93 this.acc = acc; |
|
94 } |
|
95 |
|
96 @Override |
|
97 protected void onNil(UnmarshallingContext.State state) throws SAXException { |
|
98 try { |
|
99 acc.set(state.prev.target,null); |
|
100 state.prev.nil = true; |
|
101 } catch (AccessorException e) { |
|
102 handleGenericException(e,true); |
|
103 } |
|
104 } |
|
105 |
|
106 } |
|
107 |
|
108 public static final class Array extends XsiNilLoader { |
|
109 public Array(Loader core) { |
|
110 super(core); |
|
111 } |
|
112 |
|
113 @Override |
|
114 protected void onNil(UnmarshallingContext.State state) { |
|
115 // let the receiver add this to the lister |
|
116 state.target = null; |
|
117 } |
|
118 } |
|
119 } |