aoqi@0: /* aoqi@0: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package javax.xml.bind.annotation; aoqi@0: aoqi@0: import org.w3c.dom.Document; aoqi@0: import org.w3c.dom.DocumentFragment; aoqi@0: import org.w3c.dom.Element; aoqi@0: import org.w3c.dom.Node; aoqi@0: aoqi@0: import javax.xml.bind.ValidationEventHandler; aoqi@0: import javax.xml.parsers.DocumentBuilder; aoqi@0: import javax.xml.transform.Source; aoqi@0: import javax.xml.transform.dom.DOMResult; aoqi@0: import javax.xml.transform.dom.DOMSource; aoqi@0: aoqi@0: /** aoqi@0: * {@link DomHandler} implementation for W3C DOM (org.w3c.dom package.) aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: * @since JAXB2.0 aoqi@0: */ aoqi@0: public class W3CDomHandler implements DomHandler { aoqi@0: aoqi@0: private DocumentBuilder builder; aoqi@0: aoqi@0: /** aoqi@0: * Default constructor. aoqi@0: * aoqi@0: * It is up to a JAXB provider to decide which DOM implementation aoqi@0: * to use or how that is configured. aoqi@0: */ aoqi@0: public W3CDomHandler() { aoqi@0: this.builder = null; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Constructor that allows applications to specify which DOM implementation aoqi@0: * to be used. aoqi@0: * aoqi@0: * @param builder aoqi@0: * must not be null. JAXB uses this {@link DocumentBuilder} to create aoqi@0: * a new element. aoqi@0: */ aoqi@0: public W3CDomHandler(DocumentBuilder builder) { aoqi@0: if(builder==null) aoqi@0: throw new IllegalArgumentException(); aoqi@0: this.builder = builder; aoqi@0: } aoqi@0: aoqi@0: public DocumentBuilder getBuilder() { aoqi@0: return builder; aoqi@0: } aoqi@0: aoqi@0: public void setBuilder(DocumentBuilder builder) { aoqi@0: this.builder = builder; aoqi@0: } aoqi@0: aoqi@0: public DOMResult createUnmarshaller(ValidationEventHandler errorHandler) { aoqi@0: if(builder==null) aoqi@0: return new DOMResult(); aoqi@0: else aoqi@0: return new DOMResult(builder.newDocument()); aoqi@0: } aoqi@0: aoqi@0: public Element getElement(DOMResult r) { aoqi@0: // JAXP spec is ambiguous about what really happens in this case, aoqi@0: // so work defensively aoqi@0: Node n = r.getNode(); aoqi@0: if( n instanceof Document ) { aoqi@0: return ((Document)n).getDocumentElement(); aoqi@0: } aoqi@0: if( n instanceof Element ) aoqi@0: return (Element)n; aoqi@0: if( n instanceof DocumentFragment ) aoqi@0: return (Element)n.getChildNodes().item(0); aoqi@0: aoqi@0: // if the result object contains something strange, aoqi@0: // it is not a user problem, but it is a JAXB provider's problem. aoqi@0: // That's why we throw a runtime exception. aoqi@0: throw new IllegalStateException(n.toString()); aoqi@0: } aoqi@0: aoqi@0: public Source marshal(Element element, ValidationEventHandler errorHandler) { aoqi@0: return new DOMSource(element); aoqi@0: } aoqi@0: }