KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > annotation > W3CDomHandler


1 /*
2  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5
6 package javax.xml.bind.annotation;
7
8 import org.w3c.dom.Document JavaDoc;
9 import org.w3c.dom.DocumentFragment JavaDoc;
10 import org.w3c.dom.Element JavaDoc;
11 import org.w3c.dom.Node JavaDoc;
12
13 import javax.xml.bind.ValidationEventHandler;
14 import javax.xml.parsers.DocumentBuilder JavaDoc;
15 import javax.xml.transform.Source JavaDoc;
16 import javax.xml.transform.dom.DOMResult JavaDoc;
17 import javax.xml.transform.dom.DOMSource JavaDoc;
18
19 /**
20  * {@link DomHandler} implementation for W3C DOM (<code>org.w3c.dom</code> package.)
21  *
22  * @author Kohsuke Kawaguchi
23  * @since JAXB2.0
24  */

25 public class W3CDomHandler implements DomHandler<Element JavaDoc,DOMResult JavaDoc> {
26
27     private DocumentBuilder JavaDoc builder;
28
29     /**
30      * Default constructor.
31      *
32      * It is up to a JAXB provider to decide which DOM implementation
33      * to use or how that is configured.
34      */

35     public W3CDomHandler() {
36         this.builder = null;
37     }
38
39     /**
40      * Constructor that allows applications to specify which DOM implementation
41      * to be used.
42      *
43      * @param builder
44      * must not be null. JAXB uses this {@link DocumentBuilder} to create
45      * a new element.
46      */

47     public W3CDomHandler(DocumentBuilder JavaDoc builder) {
48         if(builder==null)
49             throw new IllegalArgumentException JavaDoc();
50         this.builder = builder;
51     }
52
53     public DocumentBuilder JavaDoc getBuilder() {
54         return builder;
55     }
56
57     public void setBuilder(DocumentBuilder JavaDoc builder) {
58         this.builder = builder;
59     }
60
61     public DOMResult JavaDoc createUnmarshaller(ValidationEventHandler errorHandler) {
62         if(builder==null)
63             return new DOMResult JavaDoc();
64         else
65             return new DOMResult JavaDoc(builder.newDocument());
66     }
67
68     public Element JavaDoc getElement(DOMResult JavaDoc r) {
69         // JAXP spec is ambiguous about what really happens in this case,
70
// so work defensively
71
Node JavaDoc n = r.getNode();
72         if( n instanceof Document JavaDoc ) {
73             return ((Document JavaDoc)n).getDocumentElement();
74         }
75         if( n instanceof Element JavaDoc )
76             return (Element JavaDoc)n;
77         if( n instanceof DocumentFragment JavaDoc )
78             return (Element JavaDoc)n.getChildNodes().item(0);
79
80         // if the result object contains something strange,
81
// it is not a user problem, but it is a JAXB provider's problem.
82
// That's why we throw a runtime exception.
83
throw new IllegalStateException JavaDoc(n.toString());
84     }
85
86     public Source JavaDoc marshal(Element JavaDoc element, ValidationEventHandler errorHandler) {
87         return new DOMSource JavaDoc(element);
88     }
89 }
90
Popular Tags