src/share/jaxws_classes/javax/xml/bind/annotation/XmlAnyElement.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.xml.bind.annotation;
ohair@286 27
ohair@286 28 import org.w3c.dom.Element;
ohair@286 29
ohair@286 30 import javax.xml.bind.JAXBContext;
ohair@286 31 import javax.xml.bind.JAXBElement;
ohair@286 32 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
ohair@286 33 import java.lang.annotation.Retention;
ohair@286 34 import java.lang.annotation.Target;
ohair@286 35 import java.util.List;
ohair@286 36
ohair@286 37 import static java.lang.annotation.ElementType.FIELD;
ohair@286 38 import static java.lang.annotation.ElementType.METHOD;
ohair@286 39 import static java.lang.annotation.RetentionPolicy.RUNTIME;
ohair@286 40
ohair@286 41 /**
ohair@286 42 * Maps a JavaBean property to XML infoset representation and/or JAXB element.
ohair@286 43 *
ohair@286 44 * <p>
ohair@286 45 * This annotation serves as a "catch-all" property while unmarshalling
ohair@286 46 * xml content into a instance of a JAXB annotated class. It typically
ohair@286 47 * annotates a multi-valued JavaBean property, but it can occur on
ohair@286 48 * single value JavaBean property. During unmarshalling, each xml element
ohair@286 49 * that does not match a static &#64;XmlElement or &#64;XmlElementRef
ohair@286 50 * annotation for the other JavaBean properties on the class, is added to this
ohair@286 51 * "catch-all" property.
ohair@286 52 *
ohair@286 53 * <p>
ohair@286 54 * <h2>Usages:</h2>
ohair@286 55 * <pre>
ohair@286 56 * &#64;XmlAnyElement
ohair@286 57 * public {@link Element}[] others;
ohair@286 58 *
ohair@286 59 * // Collection of {@link Element} or JAXB elements.
ohair@286 60 * &#64;XmlAnyElement(lax="true")
ohair@286 61 * public {@link Object}[] others;
ohair@286 62 *
ohair@286 63 * &#64;XmlAnyElement
ohair@286 64 * private List&lt;{@link Element}> nodes;
ohair@286 65 *
ohair@286 66 * &#64;XmlAnyElement
ohair@286 67 * private {@link Element} node;
ohair@286 68 * </pre>
ohair@286 69 *
ohair@286 70 * <h2>Restriction usage constraints</h2>
ohair@286 71 * <p>
ohair@286 72 * This annotation is mutually exclusive with
ohair@286 73 * {@link XmlElement}, {@link XmlAttribute}, {@link XmlValue},
ohair@286 74 * {@link XmlElements}, {@link XmlID}, and {@link XmlIDREF}.
ohair@286 75 *
ohair@286 76 * <p>
ohair@286 77 * There can be only one {@link XmlAnyElement} annotated JavaBean property
ohair@286 78 * in a class and its super classes.
ohair@286 79 *
ohair@286 80 * <h2>Relationship to other annotations</h2>
ohair@286 81 * <p>
ohair@286 82 * This annotation can be used with {@link XmlJavaTypeAdapter}, so that users
ohair@286 83 * can map their own data structure to DOM, which in turn can be composed
ohair@286 84 * into XML.
ohair@286 85 *
ohair@286 86 * <p>
ohair@286 87 * This annotation can be used with {@link XmlMixed} like this:
ohair@286 88 * <pre>
ohair@286 89 * // List of java.lang.String or DOM nodes.
ohair@286 90 * &#64;XmlAnyElement &#64;XmlMixed
ohair@286 91 * List&lt;Object> others;
ohair@286 92 * </pre>
ohair@286 93 *
ohair@286 94 *
ohair@286 95 * <h2>Schema To Java example</h2>
ohair@286 96 *
ohair@286 97 * The following schema would produce the following Java class:
ohair@286 98 * <pre>
ohair@286 99 * &lt;xs:complexType name="foo">
ohair@286 100 * &lt;xs:sequence>
ohair@286 101 * &lt;xs:element name="a" type="xs:int" />
ohair@286 102 * &lt;xs:element name="b" type="xs:int" />
ohair@286 103 * &lt;xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded" />
ohair@286 104 * &lt;/xs:sequence>
ohair@286 105 * &lt;/xs:complexType>
ohair@286 106 * </pre>
ohair@286 107 *
ohair@286 108 * <pre>
ohair@286 109 * class Foo {
ohair@286 110 * int a;
ohair@286 111 * int b;
ohair@286 112 * &#64;{@link XmlAnyElement}
ohair@286 113 * List&lt;Element> any;
ohair@286 114 * }
ohair@286 115 * </pre>
ohair@286 116 *
ohair@286 117 * It can unmarshal instances like
ohair@286 118 *
ohair@286 119 * <pre>
ohair@286 120 * &lt;foo xmlns:e="extra">
ohair@286 121 * &lt;a>1</a>
ohair@286 122 * &lt;e:other /> // this will be bound to DOM, because unmarshalling is orderless
ohair@286 123 * &lt;b>3</b>
ohair@286 124 * &lt;e:other />
ohair@286 125 * &lt;c>5</c> // this will be bound to DOM, because the annotation doesn't remember namespaces.
ohair@286 126 * &lt;/foo>
ohair@286 127 * </pre>
ohair@286 128 *
ohair@286 129 *
ohair@286 130 *
ohair@286 131 * The following schema would produce the following Java class:
ohair@286 132 * <pre>
ohair@286 133 * &lt;xs:complexType name="bar">
ohair@286 134 * &lt;xs:complexContent>
ohair@286 135 * &lt;xs:extension base="foo">
ohair@286 136 * &lt;xs:sequence>
ohair@286 137 * &lt;xs:element name="c" type="xs:int" />
ohair@286 138 * &lt;xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded" />
ohair@286 139 * &lt;/xs:sequence>
ohair@286 140 * &lt;/xs:extension>
ohair@286 141 * &lt;/xs:complexType>
ohair@286 142 * </pre>
ohair@286 143 *
ohair@286 144 * <pre>
ohair@286 145 * class Bar extends Foo {
ohair@286 146 * int c;
ohair@286 147 * // Foo.getAny() also represents wildcard content for type definition bar.
ohair@286 148 * }
ohair@286 149 * </pre>
ohair@286 150 *
ohair@286 151 *
ohair@286 152 * It can unmarshal instances like
ohair@286 153 *
ohair@286 154 * <pre>
ohair@286 155 * &lt;bar xmlns:e="extra">
ohair@286 156 * &lt;a>1</a>
ohair@286 157 * &lt;e:other /> // this will be bound to DOM, because unmarshalling is orderless
ohair@286 158 * &lt;b>3</b>
ohair@286 159 * &lt;e:other />
ohair@286 160 * &lt;c>5</c> // this now goes to Bar.c
ohair@286 161 * &lt;e:other /> // this will go to Foo.any
ohair@286 162 * &lt;/bar>
ohair@286 163 * </pre>
ohair@286 164 *
ohair@286 165 *
ohair@286 166 *
ohair@286 167 *
ohair@286 168 * <h2>Using {@link XmlAnyElement} with {@link XmlElementRef}</h2>
ohair@286 169 * <p>
ohair@286 170 * The {@link XmlAnyElement} annotation can be used with {@link XmlElementRef}s to
ohair@286 171 * designate additional elements that can participate in the content tree.
ohair@286 172 *
ohair@286 173 * <p>
ohair@286 174 * The following schema would produce the following Java class:
ohair@286 175 * <pre>
ohair@286 176 * &lt;xs:complexType name="foo">
ohair@286 177 * &lt;xs:choice maxOccurs="unbounded" minOccurs="0">
ohair@286 178 * &lt;xs:element name="a" type="xs:int" />
ohair@286 179 * &lt;xs:element name="b" type="xs:int" />
ohair@286 180 * &lt;xs:any namespace="##other" processContents="lax" />
ohair@286 181 * &lt;/xs:choice>
ohair@286 182 * &lt;/xs:complexType>
ohair@286 183 * </pre>
ohair@286 184 *
ohair@286 185 * <pre>
ohair@286 186 * class Foo {
ohair@286 187 * &#64;{@link XmlAnyElement}(lax="true")
ohair@286 188 * &#64;{@link XmlElementRefs}({
ohair@286 189 * &#64;{@link XmlElementRef}(name="a", type="JAXBElement.class")
ohair@286 190 * &#64;{@link XmlElementRef}(name="b", type="JAXBElement.class")
ohair@286 191 * })
ohair@286 192 * {@link List}&lt;{@link Object}> others;
ohair@286 193 * }
ohair@286 194 *
ohair@286 195 * &#64;XmlRegistry
ohair@286 196 * class ObjectFactory {
ohair@286 197 * ...
ohair@286 198 * &#64;XmlElementDecl(name = "a", namespace = "", scope = Foo.class)
ohair@286 199 * {@link JAXBElement}&lt;Integer> createFooA( Integer i ) { ... }
ohair@286 200 *
ohair@286 201 * &#64;XmlElementDecl(name = "b", namespace = "", scope = Foo.class)
ohair@286 202 * {@link JAXBElement}&lt;Integer> createFooB( Integer i ) { ... }
ohair@286 203 * </pre>
ohair@286 204 *
ohair@286 205 * It can unmarshal instances like
ohair@286 206 *
ohair@286 207 * <pre>
ohair@286 208 * &lt;foo xmlns:e="extra">
ohair@286 209 * &lt;a>1</a> // this will unmarshal to a {@link JAXBElement} instance whose value is 1.
ohair@286 210 * &lt;e:other /> // this will unmarshal to a DOM {@link Element}.
ohair@286 211 * &lt;b>3</b> // this will unmarshal to a {@link JAXBElement} instance whose value is 1.
ohair@286 212 * &lt;/foo>
ohair@286 213 * </pre>
ohair@286 214 *
ohair@286 215 *
ohair@286 216 *
ohair@286 217 *
ohair@286 218 * <h2>W3C XML Schema "lax" wildcard emulation</h2>
ohair@286 219 * The lax element of the annotation enables the emulation of the "lax" wildcard semantics.
ohair@286 220 * For example, when the Java source code is annotated like this:
ohair@286 221 * <pre>
ohair@286 222 * &#64;{@link XmlRootElement}
ohair@286 223 * class Foo {
ohair@286 224 * &#64;XmlAnyElement(lax=true)
ohair@286 225 * public {@link Object}[] others;
ohair@286 226 * }
ohair@286 227 * </pre>
ohair@286 228 * then the following document will unmarshal like this:
ohair@286 229 * <pre>
ohair@286 230 * &lt;foo>
ohair@286 231 * &lt;unknown />
ohair@286 232 * &lt;foo />
ohair@286 233 * &lt;/foo>
ohair@286 234 *
ohair@286 235 * Foo foo = unmarshal();
ohair@286 236 * // 1 for 'unknown', another for 'foo'
ohair@286 237 * assert foo.others.length==2;
ohair@286 238 * // 'unknown' unmarshals to a DOM element
ohair@286 239 * assert foo.others[0] instanceof Element;
ohair@286 240 * // because of lax=true, the 'foo' element eagerly
ohair@286 241 * // unmarshals to a Foo object.
ohair@286 242 * assert foo.others[1] instanceof Foo;
ohair@286 243 * </pre>
ohair@286 244 *
ohair@286 245 * @author Kohsuke Kawaguchi
ohair@286 246 * @since JAXB2.0
ohair@286 247 */
ohair@286 248 @Retention(RUNTIME)
ohair@286 249 @Target({FIELD,METHOD})
ohair@286 250 public @interface XmlAnyElement {
ohair@286 251
ohair@286 252 /**
ohair@286 253 * Controls the unmarshaller behavior when it sees elements
ohair@286 254 * known to the current {@link JAXBContext}.
ohair@286 255 *
ohair@286 256 * <h3>When false</h3>
ohair@286 257 * <p>
ohair@286 258 * If false, all the elements that match the property will be unmarshalled
ohair@286 259 * to DOM, and the property will only contain DOM elements.
ohair@286 260 *
ohair@286 261 * <h3>When true</h3>
ohair@286 262 * <p>
ohair@286 263 * If true, when an element matches a property marked with {@link XmlAnyElement}
ohair@286 264 * is known to {@link JAXBContext} (for example, there's a class with
ohair@286 265 * {@link XmlRootElement} that has the same tag name, or there's
ohair@286 266 * {@link XmlElementDecl} that has the same tag name),
ohair@286 267 * the unmarshaller will eagerly unmarshal this element to the JAXB object,
ohair@286 268 * instead of unmarshalling it to DOM. Additionally, if the element is
ohair@286 269 * unknown but it has a known xsi:type, the unmarshaller eagerly unmarshals
ohair@286 270 * the element to a {@link JAXBElement}, with the unknown element name and
ohair@286 271 * the JAXBElement value is set to an instance of the JAXB mapping of the
ohair@286 272 * known xsi:type.
ohair@286 273 *
ohair@286 274 * <p>
ohair@286 275 * As a result, after the unmarshalling, the property can become heterogeneous;
ohair@286 276 * it can have both DOM nodes and some JAXB objects at the same time.
ohair@286 277 *
ohair@286 278 * <p>
ohair@286 279 * This can be used to emulate the "lax" wildcard semantics of the W3C XML Schema.
ohair@286 280 */
ohair@286 281 boolean lax() default false;
ohair@286 282
ohair@286 283 /**
ohair@286 284 * Specifies the {@link DomHandler} which is responsible for actually
ohair@286 285 * converting XML from/to a DOM-like data structure.
ohair@286 286 */
ohair@286 287 Class<? extends DomHandler> value() default W3CDomHandler.class;
ohair@286 288 }

mercurial