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