XmlAnyElement.java :  » REST » alc-rest » javax » xml » bind » annotation » Java Open Source

Java Open Source » REST » alc rest 
alc rest » javax » xml » bind » annotation » XmlAnyElement.java
/*
 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.xml.bind.annotation;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.w3c.dom.Element;

/**
 * Maps a JavaBean property to XML infoset representation and/or JAXB element.
 * 
 * <p>
 * This annotation serves as a "catch-all" property while unmarshalling xml
 * content into a instance of a JAXB annotated class. It typically annotates a
 * multi-valued JavaBean property, but it can occur on single value JavaBean
 * property. During unmarshalling, each xml element that does not match a static
 * &#64;XmlElement or &#64;XmlElementRef annotation for the other JavaBean
 * properties on the class, is added to this "catch-all" property.
 * 
 * <p>
 * <h2>Usages:</h2>
 * 
 * <pre>
 * &#064;XmlAnyElement
 * public {@link Element}[] others;
 * 
 * // Collection of {@link Element} or JAXB elements.
 * &#064;XmlAnyElement(lax=&quot;true&quot;)
 * public {@link Object}[] others;
 * &#064;XmlAnyElement
 * private List&lt;{@link Element}&gt; nodes;
 * &#064;XmlAnyElement
 * private {@link Element} node;
 * </pre>
 * 
 * <h2>Restriction usage constraints</h2>
 * <p>
 * This annotation is mutually exclusive with {@link XmlElement},
 * {@link XmlAttribute}, {@link XmlValue}, {@link XmlElements}, {@link XmlID},
 * and {@link XmlIDREF}.
 * 
 * <p>
 * There can be only one {@link XmlAnyElement} annotated JavaBean property in a
 * class and its super classes.
 * 
 * <h2>Relationship to other annotations</h2>
 * <p>
 * This annotation can be used with {@link XmlJavaTypeAdapter}, so that users
 * can map their own data structure to DOM, which in turn can be composed into
 * XML.
 * 
 * <p>
 * This annotation can be used with {@link XmlMixed} like this:
 * 
 * <pre>
 * // List of java.lang.String or DOM nodes.
 * &#064;XmlAnyElement
 * &#064;XmlMixed
 * List&lt;Object&gt; others;
 * </pre>
 * 
 * 
 * <h2>Schema To Java example</h2>
 * 
 * The following schema would produce the following Java class:
 * 
 * <pre>
 * &lt;xmp&gt;
 * &lt;xs:complexType name=&quot;foo&quot;&gt;
 *   &lt;xs:sequence&gt;
 *     &lt;xs:element name=&quot;a&quot; type=&quot;xs:int&quot; /&gt;
 *     &lt;xs:element name=&quot;b&quot; type=&quot;xs:int&quot; /&gt;
 *     &lt;xs:any namespace=&quot;##other&quot; processContents=&quot;lax&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot; /&gt;
 *   &lt;/xs:sequence&gt;
 * &lt;/xs:complexType&gt;
 * &lt;/xmp&gt;
 * </pre>
 * 
 * <pre>
 * class Foo {
 *   int a;
 *   int b;
 *   &#064;{@link XmlAnyElement}
 *   List&lt;Element&gt; any;
 * }
 * </pre>
 * 
 * It can unmarshal instances like
 * 
 * <pre>
 * &lt;xmp&gt;
 * &lt;foo xmlns:e=&quot;extra&quot;&gt;
 *   &lt;a&gt;1&lt;/a&gt;
 *   &lt;e:other /&gt;  // this will be bound to DOM, because unmarshalling is orderless
 *   &lt;b&gt;3&lt;/b&gt;
 *   &lt;e:other /&gt;
 *   &lt;c&gt;5&lt;/c&gt;     // this will be bound to DOM, because the annotation doesn't remember namespaces.
 * &lt;/foo&gt;
 * &lt;/xmp&gt;
 * </pre>
 * 
 * 
 * 
 * The following schema would produce the following Java class:
 * 
 * <pre>
 * &lt;xmp&gt;
 * &lt;xs:complexType name=&quot;bar&quot;&gt;
 *   &lt;xs:complexContent&gt;
 *   &lt;xs:extension base=&quot;foo&quot;&gt;
 *     &lt;xs:sequence&gt;
 *       &lt;xs:element name=&quot;c&quot; type=&quot;xs:int&quot; /&gt;
 *       &lt;xs:any namespace=&quot;##other&quot; processContents=&quot;lax&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot; /&gt;
 *     &lt;/xs:sequence&gt;
 *   &lt;/xs:extension&gt;
 * &lt;/xs:complexType&gt;
 * &lt;/xmp&gt;
 * </pre>
 * 
 * <pre>
 * &lt;xmp&gt;
 * class Bar extends Foo {
 *   int c;
 *   // Foo.getAny() also represents wildcard content for type definition bar.
 * }
 * &lt;/xmp&gt;
 * </pre>
 * 
 * 
 * It can unmarshal instances like
 * 
 * <pre>
 * &lt;xmp&gt;
 * &lt;bar xmlns:e=&quot;extra&quot;&gt;
 *   &lt;a&gt;1&lt;/a&gt;
 *   &lt;e:other /&gt;  // this will be bound to DOM, because unmarshalling is orderless
 *   &lt;b&gt;3&lt;/b&gt;
 *   &lt;e:other /&gt;
 *   &lt;c&gt;5&lt;/c&gt;     // this now goes to Bar.c
 *   &lt;e:other /&gt;  // this will go to Foo.any
 * &lt;/bar&gt;
 * &lt;/xmp&gt;
 * </pre>
 * 
 * 
 * 
 * 
 * <h2>Using {@link XmlAnyElement} with {@link XmlElementRef}</h2>
 * <p>
 * The {@link XmlAnyElement} annotation can be used with {@link XmlElementRef}s
 * to designate additional elements that can participate in the content tree.
 * 
 * <p>
 * The following schema would produce the following Java class:
 * 
 * <pre>
 * &lt;xmp&gt;
 * &lt;xs:complexType name=&quot;foo&quot;&gt;
 *   &lt;xs:choice maxOccurs=&quot;unbounded&quot; minOccurs=&quot;0&quot;&gt;
 *     &lt;xs:element name=&quot;a&quot; type=&quot;xs:int&quot; /&gt;
 *     &lt;xs:element name=&quot;b&quot; type=&quot;xs:int&quot; /&gt;
 *     &lt;xs:any namespace=&quot;##other&quot; processContents=&quot;lax&quot; /&gt;
 *   &lt;/xs:choice&gt;
 * &lt;/xs:complexType&gt;
 * &lt;/xmp&gt;
 * </pre>
 * 
 * <pre>
 * class Foo {
 *   &#064;{@link XmlAnyElement}(lax=&quot;true&quot;)
 *   &#064;{@link XmlElementRefs}({
 *     &#064;{@link XmlElementRef}(name=&quot;a&quot;, type=&quot;JAXBElement.class&quot;)
 *     &#064;{@link XmlElementRef}(name=&quot;b&quot;, type=&quot;JAXBElement.class&quot;)
 *   })
 *   {@link List}&lt;{@link Object}&gt; others;
 * }
 * &#064;XmlRegistry
 * class ObjectFactory {
 *   ...
 *   &#064;XmlElementDecl(name = &quot;a&quot;, namespace = &quot;&quot;, scope = Foo.class)
 *   {@link JAXBElement}&lt;Integer&gt; createFooA( Integer i ) { ... }
 *   &#064;XmlElementDecl(name = &quot;b&quot;, namespace = &quot;&quot;, scope = Foo.class)
 *   {@link JAXBElement}&lt;Integer&gt; createFooB( Integer i ) { ... }
 * </pre>
 * 
 * It can unmarshal instances like
 * 
 * <pre>
 * &lt;xmp&gt;
 * &lt;foo xmlns:e=&quot;extra&quot;&gt;
 *   &lt;a&gt;1&lt;/a&gt;     // this will unmarshal to a {@link JAXBElement} instance whose value is 1.
 *   &lt;e:other /&gt;  // this will unmarshal to a DOM {@link Element}.
 *   &lt;b&gt;3&lt;/b&gt;     // this will unmarshal to a {@link JAXBElement} instance whose value is 1.
 * &lt;/foo&gt;
 * &lt;/xmp&gt;
 * </pre>
 * 
 * 
 * 
 * 
 * <h2>W3C XML Schema "lax" wildcard emulation</h2>
 * The lax element of the annotation enables the emulation of the "lax" wildcard
 * semantics. For example, when the Java source code is annotated like this:
 * 
 * <pre>
 * &#064;{@link XmlRootElement}
 * class Foo {
 *   &#064;XmlAnyElement(lax=true)
 *   public {@link Object}[] others;
 * }
 * </pre>
 * 
 * then the following document will unmarshal like this:
 * 
 * <pre>
 * &lt;xmp&gt;
 * &lt;foo&gt;
 *   &lt;unknown /&gt;
 *   &lt;foo /&gt;
 * &lt;/foo&gt;
 * Foo foo = unmarshal();
 * // 1 for 'unknown', another for 'foo'
 * assert foo.others.length==2;
 * // 'unknown' unmarshals to a DOM element
 * assert foo.others[0] instanceof Element;
 * // because of lax=true, the 'foo' element eagerly
 * // unmarshals to a Foo object.
 * assert foo.others[1] instanceof Foo;
 * &lt;/xmp&gt;
 * </pre>
 * 
 * 
 * @author Kohsuke Kawaguchi
 * @since JAXB2.0
 */
@Retention(RUNTIME)
@Target( { FIELD, METHOD })
public @interface XmlAnyElement {

  /**
   * Controls the unmarshaller behavior when it sees elements known to the
   * current {@link JAXBContext}.
   * 
   * <h3>When false</h3>
   * <p>
   * If false, all the elements that match the property will be unmarshalled
   * to DOM, and the property will only contain DOM elements.
   * 
   * <h3>When true</h3>
   * <p>
   * If true, when an element matches a property marked with
   * {@link XmlAnyElement} is known to {@link JAXBContext} (for example,
   * there's a class with {@link XmlRootElement} that has the same tag name,
   * or there's {@link XmlElementDecl} that has the same tag name), the
   * unmarshaller will eagerly unmarshal this element to the JAXB object,
   * instead of unmarshalling it to DOM. Additionally, if the element is
   * unknown but it has a known xsi:type, the unmarshaller eagerly unmarshals
   * the element to a {@link JAXBElement}, with the unknown element name and
   * the JAXBElement value is set to an instance of the JAXB mapping of the
   * known xsi:type.
   * 
   * <p>
   * As a result, after the unmarshalling, the property can become
   * heterogeneous; it can have both DOM nodes and some JAXB objects at the
   * same time.
   * 
   * <p>
   * This can be used to emulate the "lax" wildcard semantics of the W3C XML
   * Schema.
   */
  boolean lax() default false;

  /**
   * Specifies the {@link DomHandler} which is responsible for actually
   * converting XML from/to a DOM-like data structure.
   */
  Class<? extends DomHandler> value() default W3CDomHandler.class;
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.