1/*
2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package javax.xml.bind.annotation;
27
28import org.w3c.dom.Document;
29import org.w3c.dom.DocumentFragment;
30import org.w3c.dom.Element;
31import org.w3c.dom.Node;
32
33import javax.xml.bind.ValidationEventHandler;
34import javax.xml.parsers.DocumentBuilder;
35import javax.xml.transform.Source;
36import javax.xml.transform.dom.DOMResult;
37import javax.xml.transform.dom.DOMSource;
38
39/**
40 * {@link DomHandler} implementation for W3C DOM (<code>org.w3c.dom</code> package.)
41 *
42 * @author Kohsuke Kawaguchi
43 * @since 1.6, JAXB 2.0
44 */
45public class W3CDomHandler implements DomHandler<Element,DOMResult> {
46
47    private DocumentBuilder builder;
48
49    /**
50     * Default constructor.
51     *
52     * It is up to a JAXB provider to decide which DOM implementation
53     * to use or how that is configured.
54     */
55    public W3CDomHandler() {
56        this.builder = null;
57    }
58
59    /**
60     * Constructor that allows applications to specify which DOM implementation
61     * to be used.
62     *
63     * @param builder
64     *      must not be null. JAXB uses this {@link DocumentBuilder} to create
65     *      a new element.
66     */
67    public W3CDomHandler(DocumentBuilder builder) {
68        if(builder==null)
69            throw new IllegalArgumentException();
70        this.builder = builder;
71    }
72
73    public DocumentBuilder getBuilder() {
74        return builder;
75    }
76
77    public void setBuilder(DocumentBuilder builder) {
78        this.builder = builder;
79    }
80
81    public DOMResult createUnmarshaller(ValidationEventHandler errorHandler) {
82        if(builder==null)
83            return new DOMResult();
84        else
85            return new DOMResult(builder.newDocument());
86    }
87
88    public Element getElement(DOMResult r) {
89        // JAXP spec is ambiguous about what really happens in this case,
90        // so work defensively
91        Node n = r.getNode();
92        if( n instanceof Document ) {
93            return ((Document)n).getDocumentElement();
94        }
95        if( n instanceof Element )
96            return (Element)n;
97        if( n instanceof DocumentFragment )
98            return (Element)n.getChildNodes().item(0);
99
100        // if the result object contains something strange,
101        // it is not a user problem, but it is a JAXB provider's problem.
102        // That's why we throw a runtime exception.
103        throw new IllegalStateException(n.toString());
104    }
105
106    public Source marshal(Element element, ValidationEventHandler errorHandler) {
107        return new DOMSource(element);
108    }
109}
110