src/share/jaxws_classes/javax/xml/bind/annotation/W3CDomHandler.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 397
b99d7e355d4b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     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  */
    26 package javax.xml.bind.annotation;
    28 import org.w3c.dom.Document;
    29 import org.w3c.dom.DocumentFragment;
    30 import org.w3c.dom.Element;
    31 import org.w3c.dom.Node;
    33 import javax.xml.bind.ValidationEventHandler;
    34 import javax.xml.parsers.DocumentBuilder;
    35 import javax.xml.transform.Source;
    36 import javax.xml.transform.dom.DOMResult;
    37 import javax.xml.transform.dom.DOMSource;
    39 /**
    40  * {@link DomHandler} implementation for W3C DOM (<code>org.w3c.dom</code> package.)
    41  *
    42  * @author Kohsuke Kawaguchi
    43  * @since JAXB2.0
    44  */
    45 public class W3CDomHandler implements DomHandler<Element,DOMResult> {
    47     private DocumentBuilder builder;
    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     }
    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     }
    73     public DocumentBuilder getBuilder() {
    74         return builder;
    75     }
    77     public void setBuilder(DocumentBuilder builder) {
    78         this.builder = builder;
    79     }
    81     public DOMResult createUnmarshaller(ValidationEventHandler errorHandler) {
    82         if(builder==null)
    83             return new DOMResult();
    84         else
    85             return new DOMResult(builder.newDocument());
    86     }
    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);
   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     }
   106     public Source marshal(Element element, ValidationEventHandler errorHandler) {
   107         return new DOMSource(element);
   108     }
   109 }

mercurial