src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/property/ArrayERProperty.java

Sun, 31 Aug 2014 16:14:36 +0400

author
aefimov
date
Sun, 31 Aug 2014 16:14:36 +0400
changeset 707
31893650acaf
parent 650
121e938cb9c3
permissions
-rw-r--r--

8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos

aoqi@0 1 /*
aefimov@650 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.bind.v2.runtime.property;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.util.Collection;
aoqi@0 30
aoqi@0 31 import javax.xml.namespace.QName;
aoqi@0 32 import javax.xml.stream.XMLStreamException;
aoqi@0 33
aoqi@0 34 import com.sun.xml.internal.bind.api.AccessorException;
aoqi@0 35 import com.sun.xml.internal.bind.v2.util.QNameMap;
aoqi@0 36 import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
aoqi@0 37 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
aoqi@0 38 import com.sun.xml.internal.bind.v2.runtime.Name;
aoqi@0 39 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
aoqi@0 40 import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
aoqi@0 41 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
aoqi@0 42 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
aoqi@0 43 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName;
aoqi@0 44 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
aoqi@0 45 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver;
aoqi@0 46 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope;
aoqi@0 47 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
aoqi@0 48 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
aoqi@0 49
aoqi@0 50 import org.xml.sax.SAXException;
aoqi@0 51
aoqi@0 52 /**
aoqi@0 53 * Commonality between {@link ArrayElementProperty} and {@link ArrayReferenceNodeProperty}.
aoqi@0 54 *
aoqi@0 55 * Mostly handles the unmarshalling of the wrapper element.
aoqi@0 56 *
aoqi@0 57 * @author Kohsuke Kawaguchi
aoqi@0 58 */
aoqi@0 59 abstract class ArrayERProperty<BeanT,ListT,ItemT> extends ArrayProperty<BeanT,ListT,ItemT> {
aoqi@0 60
aoqi@0 61 /**
aoqi@0 62 * Wrapper tag name if any, or null.
aoqi@0 63 */
aoqi@0 64 protected final Name wrapperTagName;
aoqi@0 65
aoqi@0 66 /**
aoqi@0 67 * True if the wrapper tag name is nillable.
aoqi@0 68 * Always false if {@link #wrapperTagName}==null.
aoqi@0 69 */
aoqi@0 70 protected final boolean isWrapperNillable;
aoqi@0 71
aoqi@0 72 protected ArrayERProperty(JAXBContextImpl grammar, RuntimePropertyInfo prop, QName tagName, boolean isWrapperNillable) {
aoqi@0 73 super(grammar,prop);
aoqi@0 74 if(tagName==null)
aoqi@0 75 this.wrapperTagName = null;
aoqi@0 76 else
aoqi@0 77 this.wrapperTagName = grammar.nameBuilder.createElementName(tagName);
aoqi@0 78 this.isWrapperNillable = isWrapperNillable;
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * Used to handle the collection wrapper element.
aoqi@0 83 */
aoqi@0 84 private static final class ItemsLoader extends Loader {
aoqi@0 85
aoqi@0 86 private final Accessor acc;
aoqi@0 87 private final Lister lister;
aoqi@0 88
aoqi@0 89 public ItemsLoader(Accessor acc, Lister lister, QNameMap<ChildLoader> children) {
aoqi@0 90 super(false);
aoqi@0 91 this.acc = acc;
aoqi@0 92 this.lister = lister;
aoqi@0 93 this.children = children;
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 @Override
aoqi@0 97 public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
aoqi@0 98 UnmarshallingContext context = state.getContext();
aoqi@0 99 context.startScope(1);
aoqi@0 100 // inherit the target so that our children can access its target
aefimov@650 101 state.setTarget(state.getPrev().getTarget());
aoqi@0 102
aoqi@0 103 // start it now, so that even if there's no children we can still return empty collection
aoqi@0 104 context.getScope(0).start(acc,lister);
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 private final QNameMap<ChildLoader> children;
aoqi@0 108
aoqi@0 109 @Override
aoqi@0 110 public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
aoqi@0 111 ChildLoader child = children.get(ea.uri,ea.local);
aoqi@0 112 if (child == null) {
aoqi@0 113 child = children.get(CATCH_ALL);
aoqi@0 114 }
aoqi@0 115 if (child == null) {
aoqi@0 116 super.childElement(state,ea);
aoqi@0 117 return;
aoqi@0 118 }
aefimov@650 119 state.setLoader(child.loader);
aefimov@650 120 state.setReceiver(child.receiver);
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 @Override
aoqi@0 124 public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
aoqi@0 125 state.getContext().endScope(1);
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 @Override
aoqi@0 129 public Collection<QName> getExpectedChildElements() {
aoqi@0 130 return children.keySet();
aoqi@0 131 }
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 public final void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException {
aoqi@0 135 ListT list = acc.get(o);
aoqi@0 136
aoqi@0 137 if(list!=null) {
aoqi@0 138 if(wrapperTagName!=null) {
aoqi@0 139 w.startElement(wrapperTagName,null);
aoqi@0 140 w.endNamespaceDecls(list);
aoqi@0 141 w.endAttributes();
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 serializeListBody(o,w,list);
aoqi@0 145
aoqi@0 146 if(wrapperTagName!=null)
aoqi@0 147 w.endElement();
aoqi@0 148 } else {
aoqi@0 149 // list is null
aoqi@0 150 if(isWrapperNillable) {
aoqi@0 151 w.startElement(wrapperTagName,null);
aoqi@0 152 w.writeXsiNilTrue();
aoqi@0 153 w.endElement();
aoqi@0 154 } // otherwise don't print the wrapper tag name
aoqi@0 155 }
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 /**
aoqi@0 159 * Serializes the items of the list.
aoqi@0 160 * This method is invoked after the necessary wrapper tag is produced (if necessary.)
aoqi@0 161 *
aoqi@0 162 * @param list
aoqi@0 163 * always non-null.
aoqi@0 164 */
aoqi@0 165 protected abstract void serializeListBody(BeanT o, XMLSerializer w, ListT list) throws IOException, XMLStreamException, SAXException, AccessorException;
aoqi@0 166
aoqi@0 167 /**
aoqi@0 168 * Creates the unmarshaler to unmarshal the body.
aoqi@0 169 */
aoqi@0 170 protected abstract void createBodyUnmarshaller(UnmarshallerChain chain, QNameMap<ChildLoader> loaders);
aoqi@0 171
aoqi@0 172
aoqi@0 173 public final void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> loaders) {
aoqi@0 174 if(wrapperTagName!=null) {
aoqi@0 175 UnmarshallerChain c = new UnmarshallerChain(chain.context);
aoqi@0 176 QNameMap<ChildLoader> m = new QNameMap<ChildLoader>();
aoqi@0 177 createBodyUnmarshaller(c,m);
aoqi@0 178 Loader loader = new ItemsLoader(acc, lister, m);
aoqi@0 179 if(isWrapperNillable || chain.context.allNillable)
aoqi@0 180 loader = new XsiNilLoader(loader);
aoqi@0 181 loaders.put(wrapperTagName,new ChildLoader(loader,null));
aoqi@0 182 } else {
aoqi@0 183 createBodyUnmarshaller(chain,loaders);
aoqi@0 184 }
aoqi@0 185 }
aoqi@0 186
aoqi@0 187 /**
aoqi@0 188 * {@link Receiver} that puts the child object into the {@link Scope} object.
aoqi@0 189 */
aoqi@0 190 protected final class ReceiverImpl implements Receiver {
aoqi@0 191 private final int offset;
aoqi@0 192
aoqi@0 193 protected ReceiverImpl(int offset) {
aoqi@0 194 this.offset = offset;
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 public void receive(UnmarshallingContext.State state, Object o) throws SAXException {
aoqi@0 198 state.getContext().getScope(offset).add(acc,lister,o);
aoqi@0 199 }
aoqi@0 200 }}

mercurial