1 /*
2 * Copyright (c) 1997, 2013, 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
44 * @author Kohsuke Kawaguchi
45 */
46 public class XsiTypeLoader extends Loader {
47
48 /**
49 * Use this when no @xsi:type was found.
50 */
51 private final JaxBeanInfo defaultBeanInfo;
52
53 public XsiTypeLoader(JaxBeanInfo defaultBeanInfo) {
54 super(true);
55 this.defaultBeanInfo = defaultBeanInfo;
56 }
57
58 public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
59 JaxBeanInfo beanInfo = parseXsiType(state,ea,defaultBeanInfo);
60 if(beanInfo==null)
61 beanInfo = defaultBeanInfo;
62
63 Loader loader = beanInfo.getLoader(null,false);
64 state.loader = loader;
65 loader.startElement(state,ea);
66 }
67
68 /*pacakge*/ static JaxBeanInfo parseXsiType(UnmarshallingContext.State state, TagName ea, @Nullable JaxBeanInfo defaultBeanInfo) throws SAXException {
69 UnmarshallingContext context = state.getContext();
70 JaxBeanInfo beanInfo = null;
71
72 // look for @xsi:type
73 Attributes atts = ea.atts;
74 int idx = atts.getIndex(WellKnownNamespace.XML_SCHEMA_INSTANCE,"type");
75
76 if(idx>=0) {
77 // we'll consume the value only when it's a recognized value,
78 // so don't consume it just yet.
79 String value = atts.getValue(idx);
80
81 QName type = DatatypeConverterImpl._parseQName(value,context);
82 if(type==null) {
83 reportError(Messages.NOT_A_QNAME.format(value),true);
84 } else {
|
1 /*
2 * Copyright (c) 1997, 2014, 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
44 * @author Kohsuke Kawaguchi
45 */
46 public class XsiTypeLoader extends Loader {
47
48 /**
49 * Use this when no @xsi:type was found.
50 */
51 private final JaxBeanInfo defaultBeanInfo;
52
53 public XsiTypeLoader(JaxBeanInfo defaultBeanInfo) {
54 super(true);
55 this.defaultBeanInfo = defaultBeanInfo;
56 }
57
58 public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
59 JaxBeanInfo beanInfo = parseXsiType(state,ea,defaultBeanInfo);
60 if(beanInfo==null)
61 beanInfo = defaultBeanInfo;
62
63 Loader loader = beanInfo.getLoader(null,false);
64 state.setLoader(loader);
65 loader.startElement(state,ea);
66 }
67
68 /*pacakge*/ static JaxBeanInfo parseXsiType(UnmarshallingContext.State state, TagName ea, @Nullable JaxBeanInfo defaultBeanInfo) throws SAXException {
69 UnmarshallingContext context = state.getContext();
70 JaxBeanInfo beanInfo = null;
71
72 // look for @xsi:type
73 Attributes atts = ea.atts;
74 int idx = atts.getIndex(WellKnownNamespace.XML_SCHEMA_INSTANCE,"type");
75
76 if(idx>=0) {
77 // we'll consume the value only when it's a recognized value,
78 // so don't consume it just yet.
79 String value = atts.getValue(idx);
80
81 QName type = DatatypeConverterImpl._parseQName(value,context);
82 if(type==null) {
83 reportError(Messages.NOT_A_QNAME.format(value),true);
84 } else {
|