Java Doc for XmlElementRef.java in  » 6.0-JDK-Core » xml » javax » xml » bind » annotation » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » xml » javax.xml.bind.annotation 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


javax.xml.bind.annotation.XmlElementRef

XmlElementRef
public @interface XmlElementRef(Code)

Maps a JavaBean property to a XML element derived from property's type.

Usage

@XmlElementRef annotation can be used with a JavaBean property or from within XmlElementRefs

This annotation dynamically associates an XML element name with the JavaBean property. When a JavaBean property is annotated with XmlElement , the XML element name is statically derived from the JavaBean property name. However, when this annotation is used, the XML element name is derived from the instance of the type of the JavaBean property at runtime.

XML Schema substitution group support

XML Schema allows a XML document author to use XML element names that were not statically specified in the content model of a schema using substitution groups. Schema derived code provides support for substitution groups using an element property, (section 5.5.5, "Element Property" of JAXB 2.0 specification). An element property method signature is of the form:

 public void setTerm(JAXBElement<? extends Operator>);
 public JAXBElement<? extends Operator> getTerm();
 

An element factory method annotated with XmlElementDecl is used to create a JAXBElement instance, containing an XML element name. The presence of @XmlElementRef annotation on an element property indicates that the element name from JAXBElement instance be used instead of deriving an XML element name from the JavaBean property name.

The usage is subject to the following constraints:

  • If the collection item type (for collection property) or property type (for single valued property) is javax.xml.bind.JAXBElement , then @XmlElementRef}.name() and @XmlElementRef.namespace() must point an element factory method with an @XmlElementDecl annotation in a class annotated with @XmlRegistry (usually ObjectFactory class generated by the schema compiler) :
    • @XmlElementDecl.name() must equal @XmlElementRef.name()
    • @XmlElementDecl.namespace() must equal @XmlElementRef.namespace().
  • If the collection item type (for collection property) or property type (for single valued property) is not javax.xml.bind.JAXBElement , then the type referenced by the property or field must be annotated with XmlRootElement .
  • This annotation can be used with the following annotations: XmlElementWrapper , XmlJavaTypeAdapter .

See "Package Specification" in javax.xml.bind.package javadoc for additional common information.

Example 1: Ant Task Example

The following Java class hierarchy models an Ant build script. An Ant task corresponds to a class in the class hierarchy. The XML element name of an Ant task is indicated by the @XmlRootElement annotation on its corresponding class.
 @XmlRootElement(name="target")
 class Target {
 // The presence of @XmlElementRef indicates that the XML
 // element name will be derived from the @XmlRootElement 
 // annotation on the type (for e.g. "jar" for JarTask). 
 @XmlElementRef
 List<Task> tasks;
 }
 abstract class Task {
 }
 @XmlRootElement(name="jar")
 class JarTask extends Task {
 ...
 }
 @XmlRootElement(name="javac")
 class JavacTask extends Task {
 ...
 }
 <!-- XML Schema fragment -->
 <xs:element name="target" type="Target">
 <xs:complexType name="Target">
 <xs:sequence>
 <xs:choice maxOccurs="unbounded">
 <xs:element ref="jar">
 <xs:element ref="javac">
 </xs:choice>
 </xs:sequence>
 </xs:complexType>
 

Thus the following code fragment:

 Target target = new Target();
 target.tasks.add(new JarTask());
 target.tasks.add(new JavacTask());
 marshal(target);
 
will produce the following XML output:

 <target>
 <jar>
 ....
 </jar>
 <javac>
 ....
 </javac>
 </target>
 

It is not an error to have a class that extends Task that doesn't have XmlRootElement . But they can't show up in an XML instance (because they don't have XML element names).

Example 2: XML Schema Susbstitution group support

The following example shows the annotations for XML Schema substitution groups. The annotations and the ObjectFactory are derived from the schema.

 @XmlElement
 class Math {
 //  The value of 
XmlElementRef.type() is 
 //  JAXBElement.class , which indicates the XML
 //  element name ObjectFactory - in general a class marked
 //  with @XmlRegistry. (See ObjectFactory below)
 //  
 //  The 
XmlElementRef.name()  is "operator", a pointer to a
 // factory method annotated with a
 //  
XmlElementDecl  with the name "operator". Since
 //  "operator" is the head of a substitution group that
 //  contains elements "add" and "sub" elements, "operator"
 //  element can be substituted in an instance document by
 //  elements "add" or "sub". At runtime, JAXBElement
 //  instance contains the element name that has been
 //  substituted in the XML document.
 // 
 @XmlElementRef(type=JAXBElement.class,name="operator")
 JAXBElement<? extends Operator> term;
 }
 @XmlRegistry
 class ObjectFactory {
 @XmlElementDecl(name="operator")
 JAXBElement<Operator> createOperator(Operator o) {...}
 @XmlElementDecl(name="add",substitutionHeadName="operator")
 JAXBElement<Operator> createAdd(Operator o) {...}
 @XmlElementDecl(name="sub",substitutionHeadName="operator")
 JAXBElement<Operator> createSub(Operator o) {...}
 }
 class Operator {
 ...
 }
 

Thus, the following code fragment

 Math m = new Math();
 m.term = new ObjectFactory().createAdd(new Operator());
 marshal(m);
 
will produce the following XML output:
 <math>
 <add>...</add>
 </math>
 

author:
  
  • Kohsuke Kawaguchi, Sun Microsystems,Inc.
  • Sekhar Vajjhala, Sun Microsystems, Inc.

See Also:   XmlElementRefs
since:
   JAXB2.0


Field Summary
final static  Type DeclarationDEFAULT
     Used in XmlElementRef.type to signal that the type be inferred from the signature of the property.
 Stringname
    
 Stringnamespace
     This parameter and XmlElementRef.name() are used to determine the XML element for the JavaBean property.

If type() is JAXBElement.class , then namespace() and name() point to a factory method with XmlElementDecl .

 Classtype
     The Java type being referenced.



Field Detail
DEFAULT
final static Type Declaration DEFAULT(Code)
Used in XmlElementRef.type to signal that the type be inferred from the signature of the property.



name
String name(Code)

See Also:   XmlElementRef.namespace()



namespace
String namespace(Code)
This parameter and XmlElementRef.name() are used to determine the XML element for the JavaBean property.

If type() is JAXBElement.class , then namespace() and name() point to a factory method with XmlElementDecl . The XML element name is the element name from the factory method's XmlElementDecl annotation or if an element from its substitution group (of which it is a head element) has been substituted in the XML document, then the element name is from the XmlElementDecl on the substituted element.

If XmlElementRef.type() is not JAXBElement.class, then the XML element name is the XML element name statically associated with the type using the annotation XmlRootElement on the type. If the type is not annotated with an XmlElementDecl , then it is an error.

If type() is not JAXBElement.class, then this value must be "".




type
Class type(Code)
The Java type being referenced.

If the value is DEFAULT.class, the type is inferred from the the type of the JavaBean property.






www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.