1 /*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package javax.xml.bind;
27
28 import javax.xml.bind.annotation.XmlRootElement;
29 import javax.xml.bind.annotation.adapters.XmlAdapter;
30 import javax.xml.bind.attachment.AttachmentMarshaller;
31 import javax.xml.validation.Schema;
32
33 /**
34 * <p>
35 * The <tt>Marshaller</tt> class is responsible for governing the process
36 * of serializing Java content trees back into XML data. It provides the basic
37 * marshalling methods:
38 *
39 * <p>
40 * <i>Assume the following setup code for all following code fragments:</i>
41 * <blockquote>
42 * <pre>
43 * JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
44 * Unmarshaller u = jc.createUnmarshaller();
45 * Object element = u.unmarshal( new File( "foo.xml" ) );
46 * Marshaller m = jc.createMarshaller();
47 * </pre>
48 * </blockquote>
49 *
50 * <p>
51 * Marshalling to a File:
52 * <blockquote>
53 * <pre>
54 * OutputStream os = new FileOutputStream( "nosferatu.xml" );
55 * m.marshal( element, os );
56 * </pre>
57 * </blockquote>
58 *
59 * <p>
60 * Marshalling to a SAX ContentHandler:
61 * <blockquote>
62 * <pre>
63 * // assume MyContentHandler instanceof ContentHandler
64 * m.marshal( element, new MyContentHandler() );
65 * </pre>
66 * </blockquote>
67 *
68 * <p>
69 * Marshalling to a DOM Node:
70 * <blockquote>
71 * <pre>
72 * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
73 * dbf.setNamespaceAware(true);
74 * DocumentBuilder db = dbf.newDocumentBuilder();
75 * Document doc = db.newDocument();
76 *
77 * m.marshal( element, doc );
78 * </pre>
79 * </blockquote>
80 *
81 * <p>
82 * Marshalling to a java.io.OutputStream:
83 * <blockquote>
84 * <pre>
85 * m.marshal( element, System.out );
86 * </pre>
87 * </blockquote>
88 *
89 * <p>
90 * Marshalling to a java.io.Writer:
91 * <blockquote>
92 * <pre>
93 * m.marshal( element, new PrintWriter( System.out ) );
94 * </pre>
95 * </blockquote>
96 *
97 * <p>
98 * Marshalling to a javax.xml.transform.SAXResult:
99 * <blockquote>
100 * <pre>
101 * // assume MyContentHandler instanceof ContentHandler
102 * SAXResult result = new SAXResult( new MyContentHandler() );
103 *
104 * m.marshal( element, result );
105 * </pre>
106 * </blockquote>
107 *
108 * <p>
109 * Marshalling to a javax.xml.transform.DOMResult:
110 * <blockquote>
111 * <pre>
112 * DOMResult result = new DOMResult();
113 *
114 * m.marshal( element, result );
115 * </pre>
116 * </blockquote>
117 *
118 * <p>
119 * Marshalling to a javax.xml.transform.StreamResult:
120 * <blockquote>
121 * <pre>
122 * StreamResult result = new StreamResult( System.out );
123 *
124 * m.marshal( element, result );
125 * </pre>
126 * </blockquote>
127 *
128 * <p>
129 * Marshalling to a javax.xml.stream.XMLStreamWriter:
130 * <blockquote>
131 * <pre>
132 * XMLStreamWriter xmlStreamWriter =
133 * XMLOutputFactory.newInstance().createXMLStreamWriter( ... );
134 *
135 * m.marshal( element, xmlStreamWriter );
136 * </pre>
137 * </blockquote>
138 *
139 * <p>
140 * Marshalling to a javax.xml.stream.XMLEventWriter:
141 * <blockquote>
142 * <pre>
143 * XMLEventWriter xmlEventWriter =
144 * XMLOutputFactory.newInstance().createXMLEventWriter( ... );
145 *
146 * m.marshal( element, xmlEventWriter );
147 * </pre>
148 * </blockquote>
149 *
150 * <p>
151 * <a name="elementMarshalling"></a>
152 * <b>Marshalling content tree rooted by a JAXB element</b><br>
153 * <blockquote>
154 * The first parameter of the overloaded
155 * <tt>Marshaller.marshal(java.lang.Object, ...)</tt> methods must be a
156 * JAXB element as computed by
157 * {@link JAXBIntrospector#isElement(java.lang.Object)};
158 * otherwise, a <tt>Marshaller.marshal</tt> method must throw a
159 * {@link MarshalException}. There exist two mechanisms
160 * to enable marshalling an instance that is not a JAXB element.
161 * One method is to wrap the instance as a value of a {@link JAXBElement},
162 * and pass the wrapper element as the first parameter to
163 * a <tt>Marshaller.marshal</tt> method. For java to schema binding, it
164 * is also possible to simply annotate the instance's class with
165 * @{@link XmlRootElement}.
166 * </blockquote>
167 *
168 * <p>
169 * <b>Encoding</b><br>
170 * <blockquote>
171 * By default, the Marshaller will use UTF-8 encoding when generating XML data
172 * to a <tt>java.io.OutputStream</tt>, or a <tt>java.io.Writer</tt>. Use the
173 * {@link #setProperty(String,Object) setProperty} API to change the output
174 * encoding used during these marshal operations. Client applications are
175 * expected to supply a valid character encoding name as defined in the
176 * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#charencoding">W3C XML 1.0
177 * Recommendation</a> and supported by your
178 * <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">
179 * Java Platform</a>.
180 * </blockquote>
181 *
182 * <p>
183 * <b>Validation and Well-Formedness</b><br>
184 * <blockquote>
185 * <p>
186 * Client applications are not required to validate the Java content tree prior
187 * to calling any of the marshal API's. Furthermore, there is no requirement
188 * that the Java content tree be valid with respect to its original schema in
189 * order to marshal it back into XML data. Different JAXB Providers will
190 * support marshalling invalid Java content trees at varying levels, however
191 * all JAXB Providers must be able to marshal a valid content tree back to
192 * XML data. A JAXB Provider must throw a <tt>MarshalException</tt> when it
193 * is unable to complete the marshal operation due to invalid content. Some
194 * JAXB Providers will fully allow marshalling invalid content, others will fail
195 * on the first validation error.
196 * <p>
197 * Even when schema validation is not explictly enabled for the marshal operation,
198 * it is possible that certain types of validation events will be detected
199 * during the operation. Validation events will be reported to the registered
200 * event handler. If the client application has not registered an event handler
201 * prior to invoking one of the marshal API's, then events will be delivered to
202 * a default event handler which will terminate the marshal operation after
203 * encountering the first error or fatal error. Note that for JAXB 2.0 and
204 * later versions, {@link javax.xml.bind.helpers.DefaultValidationEventHandler} is
205 * no longer used.
206 *
207 * </blockquote>
208 *
209 * <p>
210 * <a name="supportedProps"></a>
211 * <b>Supported Properties</b><br>
212 * <blockquote>
213 * <p>
214 * All JAXB Providers are required to support the following set of properties.
215 * Some providers may support additional properties.
216 * <dl>
217 * <dt><tt>jaxb.encoding</tt> - value must be a java.lang.String</dd>
218 * <dd>The output encoding to use when marshalling the XML data. The
219 * Marshaller will use "UTF-8" by default if this property is not
220 * specified.</dd>
221 * <dt><tt>jaxb.formatted.output</tt> - value must be a java.lang.Boolean</dd>
222 * <dd>This property controls whether or not the Marshaller will format
223 * the resulting XML data with line breaks and indentation. A
224 * true value for this property indicates human readable indented
225 * xml data, while a false value indicates unformatted xml data.
226 * The Marshaller will default to false (unformatted) if this
227 * property is not specified.</dd>
228 * <dt><tt>jaxb.schemaLocation</tt> - value must be a java.lang.String</dd>
229 * <dd>This property allows the client application to specify an
230 * xsi:schemaLocation attribute in the generated XML data. The format of
231 * the schemaLocation attribute value is discussed in an easy to
232 * understand, non-normative form in
233 * <a href="http://www.w3.org/TR/xmlschema-0/#schemaLocation">Section 5.6
234 * of the W3C XML Schema Part 0: Primer</a> and specified in
235 * <a href="http://www.w3.org/TR/xmlschema-1/#Instance_Document_Constructions">
236 * Section 2.6 of the W3C XML Schema Part 1: Structures</a>.</dd>
237 * <dt><tt>jaxb.noNamespaceSchemaLocation</tt> - value must be a java.lang.String</dd>
238 * <dd>This property allows the client application to specify an
239 * xsi:noNamespaceSchemaLocation attribute in the generated XML
240 * data. The format of the schemaLocation attribute value is discussed in
241 * an easy to understand, non-normative form in
242 * <a href="http://www.w3.org/TR/xmlschema-0/#schemaLocation">Section 5.6
243 * of the W3C XML Schema Part 0: Primer</a> and specified in
244 * <a href="http://www.w3.org/TR/xmlschema-1/#Instance_Document_Constructions">
245 * Section 2.6 of the W3C XML Schema Part 1: Structures</a>.</dd>
246 * <dt><tt>jaxb.fragment</tt> - value must be a java.lang.Boolean</dd>
247 * <dd>This property determines whether or not document level events will be
248 * generated by the Marshaller. If the property is not specified, the
249 * default is <tt>false</tt>. This property has different implications depending
250 * on which marshal api you are using - when this property is set to true:<br>
251 * <ul>
252 * <li>{@link #marshal(Object,org.xml.sax.ContentHandler) marshal(Object,ContentHandler)} - the Marshaller won't
253 * invoke {@link org.xml.sax.ContentHandler#startDocument()} and
254 * {@link org.xml.sax.ContentHandler#endDocument()}.</li>
255 * <li>{@link #marshal(Object,org.w3c.dom.Node) marshal(Object,Node)} - the property has no effect on this
256 * API.</li>
257 * <li>{@link #marshal(Object,java.io.OutputStream) marshal(Object,OutputStream)} - the Marshaller won't
258 * generate an xml declaration.</li>
259 * <li>{@link #marshal(Object,java.io.Writer) marshal(Object,Writer)} - the Marshaller won't
260 * generate an xml declaration.</li>
261 * <li>{@link #marshal(Object,javax.xml.transform.Result) marshal(Object,Result)} - depends on the kind of
262 * Result object, see semantics for Node, ContentHandler, and Stream APIs</li>
263 * <li>{@link #marshal(Object,javax.xml.stream.XMLEventWriter) marshal(Object,XMLEventWriter)} - the
264 * Marshaller will not generate {@link javax.xml.stream.events.XMLEvent#START_DOCUMENT} and
265 * {@link javax.xml.stream.events.XMLEvent#END_DOCUMENT} events.</li>
266 * <li>{@link #marshal(Object,javax.xml.stream.XMLStreamWriter) marshal(Object,XMLStreamWriter)} - the
267 * Marshaller will not generate {@link javax.xml.stream.events.XMLEvent#START_DOCUMENT} and
268 * {@link javax.xml.stream.events.XMLEvent#END_DOCUMENT} events.</li>
269 * </ul>
270 * </dd>
271 * </dl>
272 * </blockquote>
273 *
274 * <p>
275 * <a name="marshalEventCallback"></a>
276 * <b>Marshal Event Callbacks</b><br>
277 * <blockquote>
278 * "The {@link Marshaller} provides two styles of callback mechanisms
279 * that allow application specific processing during key points in the
280 * unmarshalling process. In 'class defined' event callbacks, application
281 * specific code placed in JAXB mapped classes is triggered during
282 * marshalling. 'External listeners' allow for centralized processing
283 * of marshal events in one callback method rather than by type event callbacks.
284 *
285 * <p>
286 * Class defined event callback methods allow any JAXB mapped class to specify
287 * its own specific callback methods by defining methods with the following method signatures:
288 * <blockquote>
289 * <pre>
290 * // Invoked by Marshaller after it has created an instance of this object.
291 * boolean beforeMarshal(Marshaller, Object parent);
292 *
293 * // Invoked by Marshaller after it has marshalled all properties of this object.
294 * void afterMmarshal(Marshaller, Object parent);
295 * </pre>
296 * </blockquote>
297 * The class defined event callback methods should be used when the callback method requires
298 * access to non-public methods and/or fields of the class.
299 * <p>
300 * The external listener callback mechanism enables the registration of a {@link Listener}
301 * instance with a {@link Marshaller#setListener(Listener)}. The external listener receives all callback events,
302 * allowing for more centralized processing than per class defined callback methods.
303 * <p>
304 * The 'class defined' and external listener event callback methods are independent of each other,
305 * both can be called for one event. The invocation ordering when both listener callback methods exist is
306 * defined in {@link Listener#beforeMarshal(Object)} and {@link Listener#afterMarshal(Object)}.
307 * <p>
308 * An event callback method throwing an exception terminates the current marshal process.
309 * </blockquote>
310 *
311 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
312 * @see JAXBContext
313 * @see Validator
314 * @see Unmarshaller
315 * @since JAXB1.0
316 */
317 public interface Marshaller {
318
319 /**
320 * The name of the property used to specify the output encoding in
321 * the marshalled XML data.
322 */
323 public static final String JAXB_ENCODING =
324 "jaxb.encoding";
325
326 /**
327 * The name of the property used to specify whether or not the marshalled
328 * XML data is formatted with linefeeds and indentation.
329 */
330 public static final String JAXB_FORMATTED_OUTPUT =
331 "jaxb.formatted.output";
332
333 /**
334 * The name of the property used to specify the xsi:schemaLocation
335 * attribute value to place in the marshalled XML output.
336 */
337 public static final String JAXB_SCHEMA_LOCATION =
338 "jaxb.schemaLocation";
339
340 /**
341 * The name of the property used to specify the
342 * xsi:noNamespaceSchemaLocation attribute value to place in the marshalled
343 * XML output.
344 */
345 public static final String JAXB_NO_NAMESPACE_SCHEMA_LOCATION =
346 "jaxb.noNamespaceSchemaLocation";
347
348 /**
349 * The name of the property used to specify whether or not the marshaller
350 * will generate document level events (ie calling startDocument or endDocument).
351 */
352 public static final String JAXB_FRAGMENT =
353 "jaxb.fragment";
354
355 /**
356 * Marshal the content tree rooted at <tt>jaxbElement</tt> into the specified
357 * <tt>javax.xml.transform.Result</tt>.
358 *
359 * <p>
360 * All JAXB Providers must at least support
361 * {@link javax.xml.transform.dom.DOMResult},
362 * {@link javax.xml.transform.sax.SAXResult}, and
363 * {@link javax.xml.transform.stream.StreamResult}. It can
364 * support other derived classes of <tt>Result</tt> as well.
365 *
366 * @param jaxbElement
367 * The root of content tree to be marshalled.
368 * @param result
369 * XML will be sent to this Result
370 *
371 * @throws JAXBException
372 * If any unexpected problem occurs during the marshalling.
373 * @throws MarshalException
374 * If the {@link ValidationEventHandler ValidationEventHandler}
375 * returns false from its <tt>handleEvent</tt> method or the
376 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
377 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
378 * Marshalling a JAXB element</a>.
379 * @throws IllegalArgumentException
380 * If any of the method parameters are null
381 */
382 public void marshal( Object jaxbElement, javax.xml.transform.Result result )
383 throws JAXBException;
384
385 /**
386 * Marshal the content tree rooted at <tt>jaxbElement</tt> into an output stream.
387 *
388 * @param jaxbElement
389 * The root of content tree to be marshalled.
390 * @param os
391 * XML will be added to this stream.
392 *
393 * @throws JAXBException
394 * If any unexpected problem occurs during the marshalling.
395 * @throws MarshalException
396 * If the {@link ValidationEventHandler ValidationEventHandler}
397 * returns false from its <tt>handleEvent</tt> method or the
398 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
399 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
400 * Marshalling a JAXB element</a>.
401 * @throws IllegalArgumentException
402 * If any of the method parameters are null
403 */
404 public void marshal( Object jaxbElement, java.io.OutputStream os )
405 throws JAXBException;
406
407 /**
408 * Marshal the content tree rooted at <tt>jaxbElement</tt> into a Writer.
409 *
410 * @param jaxbElement
411 * The root of content tree to be marshalled.
412 * @param writer
413 * XML will be sent to this writer.
414 *
415 * @throws JAXBException
416 * If any unexpected problem occurs during the marshalling.
417 * @throws MarshalException
418 * If the {@link ValidationEventHandler ValidationEventHandler}
419 * returns false from its <tt>handleEvent</tt> method or the
420 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
421 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
422 * Marshalling a JAXB element</a>.
423 * @throws IllegalArgumentException
424 * If any of the method parameters are null
425 */
426 public void marshal( Object jaxbElement, java.io.Writer writer )
427 throws JAXBException;
428
429 /**
430 * Marshal the content tree rooted at <tt>jaxbElement</tt> into SAX2 events.
431 *
432 * @param jaxbElement
433 * The root of content tree to be marshalled.
434 * @param handler
435 * XML will be sent to this handler as SAX2 events.
436 *
437 * @throws JAXBException
438 * If any unexpected problem occurs during the marshalling.
439 * @throws MarshalException
440 * If the {@link ValidationEventHandler ValidationEventHandler}
441 * returns false from its <tt>handleEvent</tt> method or the
442 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
443 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
444 * Marshalling a JAXB element</a>.
445 * @throws IllegalArgumentException
446 * If any of the method parameters are null
447 */
448 public void marshal( Object jaxbElement, org.xml.sax.ContentHandler handler )
449 throws JAXBException;
450
451 /**
452 * Marshal the content tree rooted at <tt>jaxbElement</tt> into a DOM tree.
453 *
454 * @param jaxbElement
455 * The content tree to be marshalled.
456 * @param node
457 * DOM nodes will be added as children of this node.
458 * This parameter must be a Node that accepts children
459 * ({@link org.w3c.dom.Document},
460 * {@link org.w3c.dom.DocumentFragment}, or
461 * {@link org.w3c.dom.Element})
462 *
463 * @throws JAXBException
464 * If any unexpected problem occurs during the marshalling.
465 * @throws MarshalException
466 * If the {@link ValidationEventHandler ValidationEventHandler}
467 * returns false from its <tt>handleEvent</tt> method or the
468 * <tt>Marshaller</tt> is unable to marshal <tt>jaxbElement</tt> (or any
469 * object reachable from <tt>jaxbElement</tt>). See <a href="#elementMarshalling">
470 * Marshalling a JAXB element</a>.
471 * @throws IllegalArgumentException
472 * If any of the method parameters are null
473 */
474 public void marshal( Object jaxbElement, org.w3c.dom.Node node )
475 throws JAXBException;
476
477 /**
478 * Marshal the content tree rooted at <tt>jaxbElement</tt> into a
479 * {@link javax.xml.stream.XMLStreamWriter}.
480 *
481 * @param jaxbElement
482 * The content tree to be marshalled.
483 * @param writer
484 * XML will be sent to this writer.
485 *
486 * @throws JAXBException
487 * If any unexpected problem occurs during the marshalling.
488 * @throws MarshalException
489 * If the {@link ValidationEventHandler ValidationEventHandler}
490 * returns false from its <tt>handleEvent</tt> method or the
491 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
492 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
493 * Marshalling a JAXB element</a>.
494 * @throws IllegalArgumentException
495 * If any of the method parameters are null
496 * @since JAXB 2.0
497 */
498 public void marshal( Object jaxbElement, javax.xml.stream.XMLStreamWriter writer )
499 throws JAXBException;
500
501 /**
502 * Marshal the content tree rooted at <tt>jaxbElement</tt> into a
503 * {@link javax.xml.stream.XMLEventWriter}.
504 *
505 * @param jaxbElement
506 * The content tree rooted at jaxbElement to be marshalled.
507 * @param writer
508 * XML will be sent to this writer.
509 *
510 * @throws JAXBException
511 * If any unexpected problem occurs during the marshalling.
512 * @throws MarshalException
513 * If the {@link ValidationEventHandler ValidationEventHandler}
514 * returns false from its <tt>handleEvent</tt> method or the
515 * <tt>Marshaller</tt> is unable to marshal <tt>obj</tt> (or any
516 * object reachable from <tt>obj</tt>). See <a href="#elementMarshalling">
517 * Marshalling a JAXB element</a>.
518 * @throws IllegalArgumentException
519 * If any of the method parameters are null
520 * @since JAXB 2.0
521 */
522 public void marshal( Object jaxbElement, javax.xml.stream.XMLEventWriter writer )
523 throws JAXBException;
524
525 /**
526 * Get a DOM tree view of the content tree(Optional).
527 *
528 * If the returned DOM tree is updated, these changes are also
529 * visible in the content tree.
530 * Use {@link #marshal(Object, org.w3c.dom.Node)} to force
531 * a deep copy of the content tree to a DOM representation.
532 *
533 * @param contentTree - JAXB Java representation of XML content
534 *
535 * @return the DOM tree view of the contentTree
536 *
537 * @throws UnsupportedOperationException
538 * If the JAXB provider implementation does not support a
539 * DOM view of the content tree
540 *
541 * @throws IllegalArgumentException
542 * If any of the method parameters are null
543 *
544 * @throws JAXBException
545 * If any unexpected problem occurs
546 *
547 */
548 public org.w3c.dom.Node getNode( java.lang.Object contentTree )
549 throws JAXBException;
550
551 /**
552 * Set the particular property in the underlying implementation of
553 * <tt>Marshaller</tt>. This method can only be used to set one of
554 * the standard JAXB defined properties above or a provider specific
555 * property. Attempting to set an undefined property will result in
556 * a PropertyException being thrown. See <a href="#supportedProps">
557 * Supported Properties</a>.
558 *
559 * @param name the name of the property to be set. This value can either
560 * be specified using one of the constant fields or a user
561 * supplied string.
562 * @param value the value of the property to be set
563 *
564 * @throws PropertyException when there is an error processing the given
565 * property or value
566 * @throws IllegalArgumentException
567 * If the name parameter is null
568 */
569 public void setProperty( String name, Object value )
570 throws PropertyException;
571
572 /**
573 * Get the particular property in the underlying implementation of
574 * <tt>Marshaller</tt>. This method can only be used to get one of
575 * the standard JAXB defined properties above or a provider specific
576 * property. Attempting to get an undefined property will result in
577 * a PropertyException being thrown. See <a href="#supportedProps">
578 * Supported Properties</a>.
579 *
580 * @param name the name of the property to retrieve
581 * @return the value of the requested property
582 *
583 * @throws PropertyException
584 * when there is an error retrieving the given property or value
585 * property name
586 * @throws IllegalArgumentException
587 * If the name parameter is null
588 */
589 public Object getProperty( String name ) throws PropertyException;
590
591 /**
592 * Allow an application to register a validation event handler.
593 * <p>
594 * The validation event handler will be called by the JAXB Provider if any
595 * validation errors are encountered during calls to any of the marshal
596 * API's. If the client application does not register a validation event
597 * handler before invoking one of the marshal methods, then validation
598 * events will be handled by the default event handler which will terminate
599 * the marshal operation after the first error or fatal error is encountered.
600 * <p>
601 * Calling this method with a null parameter will cause the Marshaller
602 * to revert back to the default default event handler.
603 *
604 * @param handler the validation event handler
605 * @throws JAXBException if an error was encountered while setting the
606 * event handler
607 */
608 public void setEventHandler( ValidationEventHandler handler )
609 throws JAXBException;
610
611 /**
612 * Return the current event handler or the default event handler if one
613 * hasn't been set.
614 *
615 * @return the current ValidationEventHandler or the default event handler
616 * if it hasn't been set
617 * @throws JAXBException if an error was encountered while getting the
618 * current event handler
619 */
620 public ValidationEventHandler getEventHandler()
621 throws JAXBException;
622
623
624
625 /**
626 * Associates a configured instance of {@link XmlAdapter} with this marshaller.
627 *
628 * <p>
629 * This is a convenience method that invokes <code>setAdapter(adapter.getClass(),adapter);</code>.
630 *
631 * @see #setAdapter(Class,XmlAdapter)
632 * @throws IllegalArgumentException
633 * if the adapter parameter is null.
634 * @throws UnsupportedOperationException
635 * if invoked agains a JAXB 1.0 implementation.
636 * @since JAXB 2.0
637 */
638 public void setAdapter( XmlAdapter adapter );
639
640 /**
641 * Associates a configured instance of {@link XmlAdapter} with this marshaller.
642 *
643 * <p>
644 * Every marshaller internally maintains a
645 * {@link java.util.Map}<{@link Class},{@link XmlAdapter}>,
646 * which it uses for marshalling classes whose fields/methods are annotated
647 * with {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter}.
648 *
649 * <p>
650 * This method allows applications to use a configured instance of {@link XmlAdapter}.
651 * When an instance of an adapter is not given, a marshaller will create
652 * one by invoking its default constructor.
653 *
654 * @param type
655 * The type of the adapter. The specified instance will be used when
656 * {@link javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter#value()}
657 * refers to this type.
658 * @param adapter
659 * The instance of the adapter to be used. If null, it will un-register
660 * the current adapter set for this type.
661 * @throws IllegalArgumentException
662 * if the type parameter is null.
663 * @throws UnsupportedOperationException
664 * if invoked agains a JAXB 1.0 implementation.
665 * @since JAXB 2.0
666 */
667 public <A extends XmlAdapter> void setAdapter( Class<A> type, A adapter );
668
669 /**
670 * Gets the adapter associated with the specified type.
671 *
672 * This is the reverse operation of the {@link #setAdapter} method.
673 *
674 * @throws IllegalArgumentException
675 * if the type parameter is null.
676 * @throws UnsupportedOperationException
677 * if invoked agains a JAXB 1.0 implementation.
678 * @since JAXB 2.0
679 */
680 public <A extends XmlAdapter> A getAdapter( Class<A> type );
681
682
683 /**
684 * <p>Associate a context that enables binary data within an XML document
685 * to be transmitted as XML-binary optimized attachment.
686 * The attachment is referenced from the XML document content model
687 * by content-id URIs(cid) references stored within the xml document.
688 *
689 * @throws IllegalStateException if attempt to concurrently call this
690 * method during a marshal operation.
691 */
692 void setAttachmentMarshaller(AttachmentMarshaller am);
693
694 AttachmentMarshaller getAttachmentMarshaller();
695
696 /**
697 * Specify the JAXP 1.3 {@link javax.xml.validation.Schema Schema}
698 * object that should be used to validate subsequent marshal operations
699 * against. Passing null into this method will disable validation.
700 *
701 * <p>
702 * This method allows the caller to validate the marshalled XML as it's marshalled.
703 *
704 * <p>
705 * Initially this property is set to <tt>null</tt>.
706 *
707 * @param schema Schema object to validate marshal operations against or null to disable validation
708 * @throws UnsupportedOperationException could be thrown if this method is
709 * invoked on an Marshaller created from a JAXBContext referencing
710 * JAXB 1.0 mapped classes
711 * @since JAXB2.0
712 */
713 public void setSchema( Schema schema );
714
715 /**
716 * Get the JAXP 1.3 {@link javax.xml.validation.Schema Schema} object
717 * being used to perform marshal-time validation. If there is no
718 * Schema set on the marshaller, then this method will return null
719 * indicating that marshal-time validation will not be performed.
720 *
721 * @return the Schema object being used to perform marshal-time
722 * validation or null if not present.
723 * @throws UnsupportedOperationException could be thrown if this method is
724 * invoked on an Marshaller created from a JAXBContext referencing
725 * JAXB 1.0 mapped classes
726 * @since JAXB2.0
727 */
728 public Schema getSchema();
729
730 /**
731 * <p/>
732 * Register an instance of an implementation of this class with a {@link Marshaller} to externally listen
733 * for marshal events.
734 * <p/>
735 * <p/>
736 * This class enables pre and post processing of each marshalled object.
737 * The event callbacks are called when marshalling from an instance that maps to an xml element or
738 * complex type definition. The event callbacks are not called when marshalling from an instance of a
739 * Java datatype that represents a simple type definition.
740 * <p/>
741 * <p/>
742 * External listener is one of two different mechanisms for defining marshal event callbacks.
743 * See <a href="Marshaller.html#marshalEventCallback">Marshal Event Callbacks</a> for an overview.
744 *
745 * @see Marshaller#setListener(Listener)
746 * @see Marshaller#getListener()
747 * @since JAXB2.0
748 */
749 public static abstract class Listener {
750 /**
751 * <p/>
752 * Callback method invoked before marshalling from <tt>source</tt> to XML.
753 * <p/>
754 * <p/>
755 * This method is invoked just before marshalling process starts to marshal <tt>source</tt>.
756 * Note that if the class of <tt>source</tt> defines its own <tt>beforeMarshal</tt> method,
757 * the class specific callback method is invoked just before this method is invoked.
758 *
759 * @param source instance of JAXB mapped class prior to marshalling from it.
760 */
761 public void beforeMarshal(Object source) {
762 }
763
764 /**
765 * <p/>
766 * Callback method invoked after marshalling <tt>source</tt> to XML.
767 * <p/>
768 * <p/>
769 * This method is invoked after <tt>source</tt> and all its descendants have been marshalled.
770 * Note that if the class of <tt>source</tt> defines its own <tt>afterMarshal</tt> method,
771 * the class specific callback method is invoked just before this method is invoked.
772 *
773 * @param source instance of JAXB mapped class after marshalling it.
774 */
775 public void afterMarshal(Object source) {
776 }
777 }
778
779 /**
780 * <p>
781 * Register marshal event callback {@link Listener} with this {@link Marshaller}.
782 *
783 * <p>
784 * There is only one Listener per Marshaller. Setting a Listener replaces the previous set Listener.
785 * One can unregister current Listener by setting listener to <tt>null</tt>.
786 *
787 * @param listener an instance of a class that implements {@link Listener}
788 * @since JAXB2.0
789 */
790 public void setListener(Listener listener);
791
792 /**
793 * <p>Return {@link Listener} registered with this {@link Marshaller}.
794 *
795 * @return registered {@link Listener} or <code>null</code> if no Listener is registered with this Marshaller.
796 * @since JAXB2.0
797 */
798 public Listener getListener();
799 }