Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2004, 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 java.lang.annotation.Target; |
aoqi@0 | 29 | import java.lang.annotation.Retention; |
aoqi@0 | 30 | import static java.lang.annotation.ElementType.*; |
aoqi@0 | 31 | import static java.lang.annotation.RetentionPolicy.*; |
aoqi@0 | 32 | |
aoqi@0 | 33 | /** |
aoqi@0 | 34 | * <p> |
aoqi@0 | 35 | * Maps a JavaBean property to XML IDREF. |
aoqi@0 | 36 | * |
aoqi@0 | 37 | * <p> |
aoqi@0 | 38 | * To preserve referential integrity of an object graph across XML |
aoqi@0 | 39 | * serialization followed by a XML deserialization, requires an object |
aoqi@0 | 40 | * reference to be marshalled by reference or containment |
aoqi@0 | 41 | * appropriately. Annotations <tt>@XmlID</tt> and <tt>@XmlIDREF</tt> |
aoqi@0 | 42 | * together allow a customized mapping of a JavaBean property's |
aoqi@0 | 43 | * type by containment or reference. |
aoqi@0 | 44 | * |
aoqi@0 | 45 | * <p><b>Usage</b> </p> |
aoqi@0 | 46 | * The <tt>@XmlIDREF</tt> annotation can be used with the following |
aoqi@0 | 47 | * program elements: |
aoqi@0 | 48 | * <ul> |
aoqi@0 | 49 | * <li> a JavaBean property </li> |
aoqi@0 | 50 | * <li> non static, non transient field </li> |
aoqi@0 | 51 | * </ul> |
aoqi@0 | 52 | * |
aoqi@0 | 53 | * <p>See "Package Specification" in javax.xml.bind.package javadoc for |
aoqi@0 | 54 | * additional common information.</p> |
aoqi@0 | 55 | * |
aoqi@0 | 56 | * <p> The usage is subject to the following constraints: |
aoqi@0 | 57 | * <ul> |
aoqi@0 | 58 | * |
aoqi@0 | 59 | * <li> If the type of the field or property is a collection type, |
aoqi@0 | 60 | * then the collection item type must contain a property or |
aoqi@0 | 61 | * field annotated with <tt>@XmlID</tt>. </li> |
aoqi@0 | 62 | * <li> If the field or property is single valued, then the type of |
aoqi@0 | 63 | * the property or field must contain a property or field |
aoqi@0 | 64 | * annotated with <tt>@XmlID</tt>. |
aoqi@0 | 65 | * <p>Note: If the collection item type or the type of the |
aoqi@0 | 66 | * property (for non collection type) is java.lang.Object, then |
aoqi@0 | 67 | * the instance must contain a property/field annotated with |
aoqi@0 | 68 | * <tt>@XmlID</tt> attribute. |
aoqi@0 | 69 | * </li> |
aoqi@0 | 70 | * <li> This annotation can be used with the following annotations: |
aoqi@0 | 71 | * {@link XmlElement}, {@link XmlAttribute}, {@link XmlList}, |
aoqi@0 | 72 | * and {@link XmlElements}.</li> |
aoqi@0 | 73 | * |
aoqi@0 | 74 | * </ul> |
aoqi@0 | 75 | * <p><b>Example:</b> Map a JavaBean property to <tt>xs:IDREF</tt> |
aoqi@0 | 76 | * (i.e. by reference rather than by containment)</p> |
aoqi@0 | 77 | * <pre> |
aoqi@0 | 78 | * |
aoqi@0 | 79 | * //EXAMPLE: Code fragment |
aoqi@0 | 80 | * public class Shipping { |
aoqi@0 | 81 | * @XmlIDREF public Customer getCustomer(); |
aoqi@0 | 82 | * public void setCustomer(Customer customer); |
aoqi@0 | 83 | * .... |
aoqi@0 | 84 | * } |
aoqi@0 | 85 | * |
aoqi@0 | 86 | * <!-- Example: XML Schema fragment --> |
aoqi@0 | 87 | * <xs:complexType name="Shipping"> |
aoqi@0 | 88 | * <xs:complexContent> |
aoqi@0 | 89 | * <xs:sequence> |
aoqi@0 | 90 | * <xs:element name="customer" type="xs:IDREF"/> |
aoqi@0 | 91 | * .... |
aoqi@0 | 92 | * </xs:sequence> |
aoqi@0 | 93 | * </xs:complexContent> |
aoqi@0 | 94 | * </xs:complexType> |
aoqi@0 | 95 | * |
aoqi@0 | 96 | * </pre> |
aoqi@0 | 97 | * |
aoqi@0 | 98 | * |
aoqi@0 | 99 | * <p><b>Example 2: </b> The following is a complete example of |
aoqi@0 | 100 | * containment versus reference. |
aoqi@0 | 101 | * |
aoqi@0 | 102 | * <pre> |
aoqi@0 | 103 | * // By default, Customer maps to complex type <tt>xs:Customer</tt> |
aoqi@0 | 104 | * public class Customer { |
aoqi@0 | 105 | * |
aoqi@0 | 106 | * // map JavaBean property type to <tt>xs:ID</tt> |
aoqi@0 | 107 | * @XmlID public String getCustomerID(); |
aoqi@0 | 108 | * public void setCustomerID(String id); |
aoqi@0 | 109 | * |
aoqi@0 | 110 | * // .... other properties not shown |
aoqi@0 | 111 | * } |
aoqi@0 | 112 | * |
aoqi@0 | 113 | * |
aoqi@0 | 114 | * // By default, Invoice maps to a complex type <tt>xs:Invoice</tt> |
aoqi@0 | 115 | * public class Invoice { |
aoqi@0 | 116 | * |
aoqi@0 | 117 | * // map by reference |
aoqi@0 | 118 | * @XmlIDREF public Customer getCustomer(); |
aoqi@0 | 119 | * public void setCustomer(Customer customer); |
aoqi@0 | 120 | * |
aoqi@0 | 121 | * // .... other properties not shown here |
aoqi@0 | 122 | * } |
aoqi@0 | 123 | * |
aoqi@0 | 124 | * // By default, Shipping maps to complex type <tt>xs:Shipping</tt> |
aoqi@0 | 125 | * public class Shipping { |
aoqi@0 | 126 | * |
aoqi@0 | 127 | * // map by reference |
aoqi@0 | 128 | * @XmlIDREF public Customer getCustomer(); |
aoqi@0 | 129 | * public void setCustomer(Customer customer); |
aoqi@0 | 130 | * } |
aoqi@0 | 131 | * |
aoqi@0 | 132 | * // at least one class must reference Customer by containment; |
aoqi@0 | 133 | * // Customer instances won't be marshalled. |
aoqi@0 | 134 | * @XmlElement(name="CustomerData") |
aoqi@0 | 135 | * public class CustomerData { |
aoqi@0 | 136 | * // map reference to Customer by containment by default. |
aoqi@0 | 137 | * public Customer getCustomer(); |
aoqi@0 | 138 | * |
aoqi@0 | 139 | * // maps reference to Shipping by containment by default. |
aoqi@0 | 140 | * public Shipping getShipping(); |
aoqi@0 | 141 | * |
aoqi@0 | 142 | * // maps reference to Invoice by containment by default. |
aoqi@0 | 143 | * public Invoice getInvoice(); |
aoqi@0 | 144 | * } |
aoqi@0 | 145 | * |
aoqi@0 | 146 | * <!-- XML Schema mapping for above code frament --> |
aoqi@0 | 147 | * |
aoqi@0 | 148 | * <xs:complexType name="Invoice"> |
aoqi@0 | 149 | * <xs:complexContent> |
aoqi@0 | 150 | * <xs:sequence> |
aoqi@0 | 151 | * <xs:element name="customer" type="xs:IDREF"/> |
aoqi@0 | 152 | * .... |
aoqi@0 | 153 | * </xs:sequence> |
aoqi@0 | 154 | * </xs:complexContent> |
aoqi@0 | 155 | * </xs:complexType> |
aoqi@0 | 156 | * |
aoqi@0 | 157 | * <xs:complexType name="Shipping"> |
aoqi@0 | 158 | * <xs:complexContent> |
aoqi@0 | 159 | * <xs:sequence> |
aoqi@0 | 160 | * <xs:element name="customer" type="xs:IDREF"/> |
aoqi@0 | 161 | * .... |
aoqi@0 | 162 | * </xs:sequence> |
aoqi@0 | 163 | * </xs:complexContent> |
aoqi@0 | 164 | * </xs:complexType> |
aoqi@0 | 165 | * |
aoqi@0 | 166 | * <xs:complexType name="Customer"> |
aoqi@0 | 167 | * <xs:complexContent> |
aoqi@0 | 168 | * <xs:sequence> |
aoqi@0 | 169 | * .... |
aoqi@0 | 170 | * </xs:sequence> |
aoqi@0 | 171 | * <xs:attribute name="CustomerID" type="xs:ID"/> |
aoqi@0 | 172 | * </xs:complexContent> |
aoqi@0 | 173 | * </xs:complexType> |
aoqi@0 | 174 | * |
aoqi@0 | 175 | * <xs:complexType name="CustomerData"> |
aoqi@0 | 176 | * <xs:complexContent> |
aoqi@0 | 177 | * <xs:sequence> |
aoqi@0 | 178 | * <xs:element name="customer" type="xs:Customer"/> |
aoqi@0 | 179 | * <xs:element name="shipping" type="xs:Shipping"/> |
aoqi@0 | 180 | * <xs:element name="invoice" type="xs:Invoice"/> |
aoqi@0 | 181 | * </xs:sequence> |
aoqi@0 | 182 | * </xs:complexContent> |
aoqi@0 | 183 | * </xs:complexType> |
aoqi@0 | 184 | * |
aoqi@0 | 185 | * <xs:element name"customerData" type="xs:CustomerData"/> |
aoqi@0 | 186 | * |
aoqi@0 | 187 | * <!-- Instance document conforming to the above XML Schema --> |
aoqi@0 | 188 | * <customerData> |
aoqi@0 | 189 | * <customer customerID="Alice"> |
aoqi@0 | 190 | * .... |
aoqi@0 | 191 | * </customer> |
aoqi@0 | 192 | * |
aoqi@0 | 193 | * <shipping customer="Alice"> |
aoqi@0 | 194 | * .... |
aoqi@0 | 195 | * </shipping> |
aoqi@0 | 196 | * |
aoqi@0 | 197 | * <invoice customer="Alice"> |
aoqi@0 | 198 | * .... |
aoqi@0 | 199 | * </invoice> |
aoqi@0 | 200 | * </customerData> |
aoqi@0 | 201 | * |
aoqi@0 | 202 | * </pre> |
aoqi@0 | 203 | * |
aoqi@0 | 204 | * <p><b>Example 3: </b> Mapping List to repeating element of type IDREF |
aoqi@0 | 205 | * <pre> |
aoqi@0 | 206 | * // Code fragment |
aoqi@0 | 207 | * public class Shipping { |
aoqi@0 | 208 | * @XmlIDREF |
aoqi@0 | 209 | * @XmlElement(name="Alice") |
aoqi@0 | 210 | * public List customers; |
aoqi@0 | 211 | * } |
aoqi@0 | 212 | * |
aoqi@0 | 213 | * <!-- XML schema fragment --> |
aoqi@0 | 214 | * <xs:complexType name="Shipping"> |
aoqi@0 | 215 | * <xs:sequence> |
aoqi@0 | 216 | * <xs:choice minOccurs="0" maxOccurs="unbounded"> |
aoqi@0 | 217 | * <xs:element name="Alice" type="xs:IDREF"/> |
aoqi@0 | 218 | * </xs:choice> |
aoqi@0 | 219 | * </xs:sequence> |
aoqi@0 | 220 | * </xs:complexType> |
aoqi@0 | 221 | * </pre> |
aoqi@0 | 222 | * |
aoqi@0 | 223 | * <p><b>Example 4: </b> Mapping a List to a list of elements of type IDREF. |
aoqi@0 | 224 | * <pre> |
aoqi@0 | 225 | * //Code fragment |
aoqi@0 | 226 | * public class Shipping { |
aoqi@0 | 227 | * @XmlIDREF |
aoqi@0 | 228 | * @XmlElements( |
aoqi@0 | 229 | * @XmlElement(name="Alice", type="Customer.class") |
aoqi@0 | 230 | * @XmlElement(name="John", type="InternationalCustomer.class") |
aoqi@0 | 231 | * public List customers; |
aoqi@0 | 232 | * } |
aoqi@0 | 233 | * |
aoqi@0 | 234 | * <!-- XML Schema fragment --> |
aoqi@0 | 235 | * <xs:complexType name="Shipping"> |
aoqi@0 | 236 | * <xs:sequence> |
aoqi@0 | 237 | * <xs:choice minOccurs="0" maxOccurs="unbounded"> |
aoqi@0 | 238 | * <xs:element name="Alice" type="xs:IDREF"/> |
aoqi@0 | 239 | * <xs:element name="John" type="xs:IDREF"/> |
aoqi@0 | 240 | * </xs:choice> |
aoqi@0 | 241 | * </xs:sequence> |
aoqi@0 | 242 | * </xs:complexType> |
aoqi@0 | 243 | * </pre> |
aoqi@0 | 244 | * @author Sekhar Vajjhala, Sun Microsystems, Inc. |
aoqi@0 | 245 | * @see XmlID |
aoqi@0 | 246 | * @since JAXB2.0 |
aoqi@0 | 247 | */ |
aoqi@0 | 248 | |
aoqi@0 | 249 | @Retention(RUNTIME) @Target({FIELD, METHOD}) |
aoqi@0 | 250 | public @interface XmlIDREF {} |