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