Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package javax.xml.bind.util; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import javax.xml.bind.JAXBContext; |
aoqi@0 | 29 | import javax.xml.bind.JAXBException; |
aoqi@0 | 30 | import javax.xml.bind.Unmarshaller; |
aoqi@0 | 31 | import javax.xml.bind.UnmarshallerHandler; |
aoqi@0 | 32 | import javax.xml.transform.sax.SAXResult; |
aoqi@0 | 33 | |
aoqi@0 | 34 | /** |
aoqi@0 | 35 | * JAXP {@link javax.xml.transform.Result} implementation |
aoqi@0 | 36 | * that unmarshals a JAXB object. |
aoqi@0 | 37 | * |
aoqi@0 | 38 | * <p> |
aoqi@0 | 39 | * This utility class is useful to combine JAXB with |
aoqi@0 | 40 | * other Java/XML technologies. |
aoqi@0 | 41 | * |
aoqi@0 | 42 | * <p> |
aoqi@0 | 43 | * The following example shows how to use JAXB to unmarshal a document |
aoqi@0 | 44 | * resulting from an XSLT transformation. |
aoqi@0 | 45 | * |
aoqi@0 | 46 | * <blockquote> |
aoqi@0 | 47 | * <pre> |
aoqi@0 | 48 | * JAXBResult result = new JAXBResult( |
aoqi@0 | 49 | * JAXBContext.newInstance("org.acme.foo") ); |
aoqi@0 | 50 | * |
aoqi@0 | 51 | * // set up XSLT transformation |
aoqi@0 | 52 | * TransformerFactory tf = TransformerFactory.newInstance(); |
aoqi@0 | 53 | * Transformer t = tf.newTransformer(new StreamSource("test.xsl")); |
aoqi@0 | 54 | * |
aoqi@0 | 55 | * // run transformation |
aoqi@0 | 56 | * t.transform(new StreamSource("document.xml"),result); |
aoqi@0 | 57 | * |
aoqi@0 | 58 | * // obtain the unmarshalled content tree |
aoqi@0 | 59 | * Object o = result.getResult(); |
aoqi@0 | 60 | * </pre> |
aoqi@0 | 61 | * </blockquote> |
aoqi@0 | 62 | * |
aoqi@0 | 63 | * <p> |
aoqi@0 | 64 | * The fact that JAXBResult derives from SAXResult is an implementation |
aoqi@0 | 65 | * detail. Thus in general applications are strongly discouraged from |
aoqi@0 | 66 | * accessing methods defined on SAXResult. |
aoqi@0 | 67 | * |
aoqi@0 | 68 | * <p> |
aoqi@0 | 69 | * In particular it shall never attempt to call the setHandler, |
aoqi@0 | 70 | * setLexicalHandler, and setSystemId methods. |
aoqi@0 | 71 | * |
aoqi@0 | 72 | * @author |
aoqi@0 | 73 | * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | public class JAXBResult extends SAXResult { |
aoqi@0 | 76 | |
aoqi@0 | 77 | /** |
aoqi@0 | 78 | * Creates a new instance that uses the specified |
aoqi@0 | 79 | * JAXBContext to unmarshal. |
aoqi@0 | 80 | * |
aoqi@0 | 81 | * @param context The JAXBContext that will be used to create the |
aoqi@0 | 82 | * necessary Unmarshaller. This parameter must not be null. |
aoqi@0 | 83 | * @exception JAXBException if an error is encountered while creating the |
aoqi@0 | 84 | * JAXBResult or if the context parameter is null. |
aoqi@0 | 85 | */ |
aoqi@0 | 86 | public JAXBResult( JAXBContext context ) throws JAXBException { |
aoqi@0 | 87 | this( ( context == null ) ? assertionFailed() : context.createUnmarshaller() ); |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | /** |
aoqi@0 | 91 | * Creates a new instance that uses the specified |
aoqi@0 | 92 | * Unmarshaller to unmarshal an object. |
aoqi@0 | 93 | * |
aoqi@0 | 94 | * <p> |
aoqi@0 | 95 | * This JAXBResult object will use the specified Unmarshaller |
aoqi@0 | 96 | * instance. It is the caller's responsibility not to use the |
aoqi@0 | 97 | * same Unmarshaller for other purposes while it is being |
aoqi@0 | 98 | * used by this object. |
aoqi@0 | 99 | * |
aoqi@0 | 100 | * <p> |
aoqi@0 | 101 | * The primary purpose of this method is to allow the client |
aoqi@0 | 102 | * to configure Unmarshaller. Unless you know what you are doing, |
aoqi@0 | 103 | * it's easier and safer to pass a JAXBContext. |
aoqi@0 | 104 | * |
aoqi@0 | 105 | * @param _unmarshaller the unmarshaller. This parameter must not be null. |
aoqi@0 | 106 | * @throws JAXBException if an error is encountered while creating the |
aoqi@0 | 107 | * JAXBResult or the Unmarshaller parameter is null. |
aoqi@0 | 108 | */ |
aoqi@0 | 109 | public JAXBResult( Unmarshaller _unmarshaller ) throws JAXBException { |
aoqi@0 | 110 | if( _unmarshaller == null ) |
aoqi@0 | 111 | throw new JAXBException( |
aoqi@0 | 112 | Messages.format( Messages.RESULT_NULL_UNMARSHALLER ) ); |
aoqi@0 | 113 | |
aoqi@0 | 114 | this.unmarshallerHandler = _unmarshaller.getUnmarshallerHandler(); |
aoqi@0 | 115 | |
aoqi@0 | 116 | super.setHandler(unmarshallerHandler); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | /** |
aoqi@0 | 120 | * Unmarshaller that will be used to unmarshal |
aoqi@0 | 121 | * the input documents. |
aoqi@0 | 122 | */ |
aoqi@0 | 123 | private final UnmarshallerHandler unmarshallerHandler; |
aoqi@0 | 124 | |
aoqi@0 | 125 | /** |
aoqi@0 | 126 | * Gets the unmarshalled object created by the transformation. |
aoqi@0 | 127 | * |
aoqi@0 | 128 | * @return |
aoqi@0 | 129 | * Always return a non-null object. |
aoqi@0 | 130 | * |
aoqi@0 | 131 | * @exception IllegalStateException |
aoqi@0 | 132 | * if this method is called before an object is unmarshalled. |
aoqi@0 | 133 | * |
aoqi@0 | 134 | * @exception JAXBException |
aoqi@0 | 135 | * if there is any unmarshalling error. |
aoqi@0 | 136 | * Note that the implementation is allowed to throw SAXException |
aoqi@0 | 137 | * during the parsing when it finds an error. |
aoqi@0 | 138 | */ |
aoqi@0 | 139 | public Object getResult() throws JAXBException { |
aoqi@0 | 140 | return unmarshallerHandler.getResult(); |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | /** |
aoqi@0 | 144 | * Hook to throw exception from the middle of a contructor chained call |
aoqi@0 | 145 | * to this |
aoqi@0 | 146 | */ |
aoqi@0 | 147 | private static Unmarshaller assertionFailed() throws JAXBException { |
aoqi@0 | 148 | throw new JAXBException( Messages.format( Messages.RESULT_NULL_CONTEXT ) ); |
aoqi@0 | 149 | } |
aoqi@0 | 150 | } |