KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > xjc > runtime > ElementWrapper


1 package com.sun.tools.xjc.runtime;
2
3 import javax.xml.bind.Element;
4 import javax.xml.namespace.QName JavaDoc;
5 import org.xml.sax.SAXException JavaDoc;
6 import com.sun.xml.bind.JAXBObject;
7
8 /**
9  * General-purpose element wrapper for marshalling out a type object
10  * as a document.
11  *
12  * <p>
13  * For example, when you have the following schema:
14  * <pre><xmp>
15  * <xs:complexType name="foo">
16  * <xs:sequence>
17  * <xs:element name="aaa" type="..."/>
18  * <xs:element name="bbb" type="..."/>
19  * <xs:element name="ccc" type="..."/>
20  * </xs:sequence>
21  * </xs:complexType>
22  * </xmp></pre>
23  * <p>
24  * Or
25  * <pre><xmp>
26  * <define name="foo">
27  * <element name="aaa"> ... </element>
28  * <element name="bbb"> ... </element>
29  * <element name="ccc"> ... </element>
30  * </define>
31  * </xmp></pre>
32  * <p>
33  * JAXB normally produces a Foo class that cannot be marshalled by itself.
34  * However, it is often convenient (for debugging, logging, etc) if you
35  * can marshal this Foo fragment.
36  *
37  * <p>
38  * {@link ElementWrapper} can be used in cases like this.
39  * To marshal the above Foo object, you can write:
40  * <pre>
41  * Foo foo = ...;
42  * ElementWrapper e = new ElmentWrapper("tagName",foo);
43  * marshaller.marshal(e,System.out);
44  * </pre>
45  * <p>
46  * which produces the following XML:
47  * <pre><xmp>
48  * <tagName>
49  * <aaa>...</aaa>
50  * <bbb>...</bbb>
51  * <ccc>...</ccc>
52  * </tagName>
53  * </xmp></pre>
54  *
55  * <p>
56  * Note that this can be only used with a class generated from the JAXB RI.
57  * You cannot set objects such as {@link Integer} or {@link String}.
58  *
59  * <p>
60  * This is a JAXB RI specific extension.
61  *
62  * @author
63  * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
64  */

65 public class ElementWrapper implements Element, JAXBObject, XMLSerializable {
66     
67     // always non-unll
68
private QName JavaDoc tagName;
69     private Object JavaDoc body;
70     
71     /**
72      * Creates a new {@link ElementWrapper} object by using the given
73      * element name and the type object.
74      *
75      * @param tagName
76      * The name of the outer-most element.
77      * @param body
78      * The object that can be only marshalled as a fragment.
79      */

80     public ElementWrapper( QName JavaDoc tagName, Object JavaDoc body ) {
81         if(tagName==null) throw new IllegalArgumentException JavaDoc("tag name is null");
82         setBody(body);
83         this.tagName = tagName;
84     }
85     
86     
87
88     /**
89      * Gets the tag name of this element.
90      *
91      * @return What you set via {@link #setTagName(QName)} or the constructor.
92      */

93     public QName JavaDoc getTagName() {
94         return tagName;
95     }
96     
97     /**
98      * Sets the tag name of this element.
99      *
100      * <p>
101      * This name is used as the element name when you marshal this object.
102      *
103      */

104     public void setTagName(QName JavaDoc tagName) {
105         if(tagName==null) throw new IllegalArgumentException JavaDoc("tag name is null");
106         this.tagName = tagName;
107     }
108     
109     /**
110      * Gets the body of this element.
111      *
112      * @return What you set via {@link #setBody(XMLSerializable)} or the constructor.
113      */

114     public Object JavaDoc getBody() {
115         return body;
116     }
117     
118     /**
119      * Sets the body of this element.
120      *
121      * @param body
122      * must be non-null.
123      */

124     public void setBody(Object JavaDoc body) {
125         if(body==null) throw new IllegalArgumentException JavaDoc("body is null");
126         if(!(body instanceof JAXBObject))
127             throw new IllegalArgumentException JavaDoc(body.getClass().getName()+" is not a JAXB-generated class");
128         this.body = body;
129     }
130     
131     public void serializeBody(XMLSerializer target) throws SAXException JavaDoc {
132         target.startElement(tagName.getNamespaceURI(),tagName.getLocalPart());
133         target.childAsURIs((JAXBObject)body,"body");
134         target.endNamespaceDecls();
135         target.childAsAttributes((JAXBObject)body,"body");
136         target.endAttributes();
137         target.childAsBody((JAXBObject)body,"body");
138         target.endElement();
139     }
140
141     public void serializeAttributes(XMLSerializer target) throws SAXException JavaDoc {
142         // noop
143
}
144
145     public void serializeURIs(XMLSerializer target) throws SAXException JavaDoc {
146         // noop
147
}
148 }
149
Popular Tags