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