Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
ohair@286 | 1 | /* |
ohair@286 | 2 | * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package javax.xml.bind.util; |
ohair@286 | 27 | |
ohair@286 | 28 | import org.xml.sax.ContentHandler; |
ohair@286 | 29 | import org.xml.sax.DTDHandler; |
ohair@286 | 30 | import org.xml.sax.EntityResolver; |
ohair@286 | 31 | import org.xml.sax.ErrorHandler; |
ohair@286 | 32 | import org.xml.sax.InputSource; |
ohair@286 | 33 | import org.xml.sax.SAXException; |
ohair@286 | 34 | import org.xml.sax.SAXNotRecognizedException; |
ohair@286 | 35 | import org.xml.sax.SAXParseException; |
ohair@286 | 36 | import org.xml.sax.XMLReader; |
ohair@286 | 37 | import org.xml.sax.ext.LexicalHandler; |
ohair@286 | 38 | import org.xml.sax.helpers.XMLFilterImpl; |
ohair@286 | 39 | |
ohair@286 | 40 | import javax.xml.bind.JAXBContext; |
ohair@286 | 41 | import javax.xml.bind.JAXBException; |
ohair@286 | 42 | import javax.xml.bind.Marshaller; |
ohair@286 | 43 | import javax.xml.transform.sax.SAXSource; |
ohair@286 | 44 | |
ohair@286 | 45 | /** |
ohair@286 | 46 | * JAXP {@link javax.xml.transform.Source} implementation |
ohair@286 | 47 | * that marshals a JAXB-generated object. |
ohair@286 | 48 | * |
ohair@286 | 49 | * <p> |
ohair@286 | 50 | * This utility class is useful to combine JAXB with |
ohair@286 | 51 | * other Java/XML technologies. |
ohair@286 | 52 | * |
ohair@286 | 53 | * <p> |
ohair@286 | 54 | * The following example shows how to use JAXB to marshal a document |
ohair@286 | 55 | * for transformation by XSLT. |
ohair@286 | 56 | * |
ohair@286 | 57 | * <blockquote> |
ohair@286 | 58 | * <pre> |
ohair@286 | 59 | * MyObject o = // get JAXB content tree |
ohair@286 | 60 | * |
ohair@286 | 61 | * // jaxbContext is a JAXBContext object from which 'o' is created. |
ohair@286 | 62 | * JAXBSource source = new JAXBSource( jaxbContext, o ); |
ohair@286 | 63 | * |
ohair@286 | 64 | * // set up XSLT transformation |
ohair@286 | 65 | * TransformerFactory tf = TransformerFactory.newInstance(); |
ohair@286 | 66 | * Transformer t = tf.newTransformer(new StreamSource("test.xsl")); |
ohair@286 | 67 | * |
ohair@286 | 68 | * // run transformation |
ohair@286 | 69 | * t.transform(source,new StreamResult(System.out)); |
ohair@286 | 70 | * </pre> |
ohair@286 | 71 | * </blockquote> |
ohair@286 | 72 | * |
ohair@286 | 73 | * <p> |
ohair@286 | 74 | * The fact that JAXBSource derives from SAXSource is an implementation |
ohair@286 | 75 | * detail. Thus in general applications are strongly discouraged from |
ohair@286 | 76 | * accessing methods defined on SAXSource. In particular, |
ohair@286 | 77 | * the setXMLReader and setInputSource methods shall never be called. |
ohair@286 | 78 | * The XMLReader object obtained by the getXMLReader method shall |
ohair@286 | 79 | * be used only for parsing the InputSource object returned by |
ohair@286 | 80 | * the getInputSource method. |
ohair@286 | 81 | * |
ohair@286 | 82 | * <p> |
ohair@286 | 83 | * Similarly the InputSource object obtained by the getInputSource |
ohair@286 | 84 | * method shall be used only for being parsed by the XMLReader object |
ohair@286 | 85 | * returned by the getXMLReader. |
ohair@286 | 86 | * |
ohair@286 | 87 | * @author |
ohair@286 | 88 | * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |
ohair@286 | 89 | */ |
ohair@286 | 90 | public class JAXBSource extends SAXSource { |
ohair@286 | 91 | |
ohair@286 | 92 | /** |
ohair@286 | 93 | * Creates a new {@link javax.xml.transform.Source} for the given content object. |
ohair@286 | 94 | * |
ohair@286 | 95 | * @param context |
ohair@286 | 96 | * JAXBContext that was used to create |
ohair@286 | 97 | * <code>contentObject</code>. This context is used |
ohair@286 | 98 | * to create a new instance of marshaller and must not be null. |
ohair@286 | 99 | * @param contentObject |
ohair@286 | 100 | * An instance of a JAXB-generated class, which will be |
ohair@286 | 101 | * used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must |
ohair@286 | 102 | * not be null. |
ohair@286 | 103 | * @throws JAXBException if an error is encountered while creating the |
ohair@286 | 104 | * JAXBSource or if either of the parameters are null. |
ohair@286 | 105 | */ |
ohair@286 | 106 | public JAXBSource( JAXBContext context, Object contentObject ) |
ohair@286 | 107 | throws JAXBException { |
ohair@286 | 108 | |
ohair@286 | 109 | this( |
ohair@286 | 110 | ( context == null ) ? |
ohair@286 | 111 | assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTEXT ) ) : |
ohair@286 | 112 | context.createMarshaller(), |
ohair@286 | 113 | |
ohair@286 | 114 | ( contentObject == null ) ? |
ohair@286 | 115 | assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTENT ) ) : |
ohair@286 | 116 | contentObject); |
ohair@286 | 117 | } |
ohair@286 | 118 | |
ohair@286 | 119 | /** |
ohair@286 | 120 | * Creates a new {@link javax.xml.transform.Source} for the given content object. |
ohair@286 | 121 | * |
ohair@286 | 122 | * @param marshaller |
ohair@286 | 123 | * A marshaller instance that will be used to marshal |
ohair@286 | 124 | * <code>contentObject</code> into XML. This must be |
ohair@286 | 125 | * created from a JAXBContext that was used to build |
ohair@286 | 126 | * <code>contentObject</code> and must not be null. |
ohair@286 | 127 | * @param contentObject |
ohair@286 | 128 | * An instance of a JAXB-generated class, which will be |
ohair@286 | 129 | * used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must |
ohair@286 | 130 | * not be null. |
ohair@286 | 131 | * @throws JAXBException if an error is encountered while creating the |
ohair@286 | 132 | * JAXBSource or if either of the parameters are null. |
ohair@286 | 133 | */ |
ohair@286 | 134 | public JAXBSource( Marshaller marshaller, Object contentObject ) |
ohair@286 | 135 | throws JAXBException { |
ohair@286 | 136 | |
ohair@286 | 137 | if( marshaller == null ) |
ohair@286 | 138 | throw new JAXBException( |
ohair@286 | 139 | Messages.format( Messages.SOURCE_NULL_MARSHALLER ) ); |
ohair@286 | 140 | |
ohair@286 | 141 | if( contentObject == null ) |
ohair@286 | 142 | throw new JAXBException( |
ohair@286 | 143 | Messages.format( Messages.SOURCE_NULL_CONTENT ) ); |
ohair@286 | 144 | |
ohair@286 | 145 | this.marshaller = marshaller; |
ohair@286 | 146 | this.contentObject = contentObject; |
ohair@286 | 147 | |
ohair@286 | 148 | super.setXMLReader(pseudoParser); |
ohair@286 | 149 | // pass a dummy InputSource. We don't care |
ohair@286 | 150 | super.setInputSource(new InputSource()); |
ohair@286 | 151 | } |
ohair@286 | 152 | |
ohair@286 | 153 | private final Marshaller marshaller; |
ohair@286 | 154 | private final Object contentObject; |
ohair@286 | 155 | |
ohair@286 | 156 | // this object will pretend as an XMLReader. |
ohair@286 | 157 | // no matter what parameter is specified to the parse method, |
ohair@286 | 158 | // it just parse the contentObject. |
ohair@286 | 159 | private final XMLReader pseudoParser = new XMLReader() { |
ohair@286 | 160 | public boolean getFeature(String name) throws SAXNotRecognizedException { |
ohair@286 | 161 | if(name.equals("http://xml.org/sax/features/namespaces")) |
ohair@286 | 162 | return true; |
ohair@286 | 163 | if(name.equals("http://xml.org/sax/features/namespace-prefixes")) |
ohair@286 | 164 | return false; |
ohair@286 | 165 | throw new SAXNotRecognizedException(name); |
ohair@286 | 166 | } |
ohair@286 | 167 | |
ohair@286 | 168 | public void setFeature(String name, boolean value) throws SAXNotRecognizedException { |
ohair@286 | 169 | if(name.equals("http://xml.org/sax/features/namespaces") && value) |
ohair@286 | 170 | return; |
ohair@286 | 171 | if(name.equals("http://xml.org/sax/features/namespace-prefixes") && !value) |
ohair@286 | 172 | return; |
ohair@286 | 173 | throw new SAXNotRecognizedException(name); |
ohair@286 | 174 | } |
ohair@286 | 175 | |
ohair@286 | 176 | public Object getProperty(String name) throws SAXNotRecognizedException { |
ohair@286 | 177 | if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { |
ohair@286 | 178 | return lexicalHandler; |
ohair@286 | 179 | } |
ohair@286 | 180 | throw new SAXNotRecognizedException(name); |
ohair@286 | 181 | } |
ohair@286 | 182 | |
ohair@286 | 183 | public void setProperty(String name, Object value) throws SAXNotRecognizedException { |
ohair@286 | 184 | if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { |
ohair@286 | 185 | this.lexicalHandler = (LexicalHandler)value; |
ohair@286 | 186 | return; |
ohair@286 | 187 | } |
ohair@286 | 188 | throw new SAXNotRecognizedException(name); |
ohair@286 | 189 | } |
ohair@286 | 190 | |
ohair@286 | 191 | private LexicalHandler lexicalHandler; |
ohair@286 | 192 | |
ohair@286 | 193 | // we will store this value but never use it by ourselves. |
ohair@286 | 194 | private EntityResolver entityResolver; |
ohair@286 | 195 | public void setEntityResolver(EntityResolver resolver) { |
ohair@286 | 196 | this.entityResolver = resolver; |
ohair@286 | 197 | } |
ohair@286 | 198 | public EntityResolver getEntityResolver() { |
ohair@286 | 199 | return entityResolver; |
ohair@286 | 200 | } |
ohair@286 | 201 | |
ohair@286 | 202 | private DTDHandler dtdHandler; |
ohair@286 | 203 | public void setDTDHandler(DTDHandler handler) { |
ohair@286 | 204 | this.dtdHandler = handler; |
ohair@286 | 205 | } |
ohair@286 | 206 | public DTDHandler getDTDHandler() { |
ohair@286 | 207 | return dtdHandler; |
ohair@286 | 208 | } |
ohair@286 | 209 | |
ohair@286 | 210 | // SAX allows ContentHandler to be changed during the parsing, |
ohair@286 | 211 | // but JAXB doesn't. So this repeater will sit between those |
ohair@286 | 212 | // two components. |
ohair@286 | 213 | private XMLFilterImpl repeater = new XMLFilterImpl(); |
ohair@286 | 214 | |
ohair@286 | 215 | public void setContentHandler(ContentHandler handler) { |
ohair@286 | 216 | repeater.setContentHandler(handler); |
ohair@286 | 217 | } |
ohair@286 | 218 | public ContentHandler getContentHandler() { |
ohair@286 | 219 | return repeater.getContentHandler(); |
ohair@286 | 220 | } |
ohair@286 | 221 | |
ohair@286 | 222 | private ErrorHandler errorHandler; |
ohair@286 | 223 | public void setErrorHandler(ErrorHandler handler) { |
ohair@286 | 224 | this.errorHandler = handler; |
ohair@286 | 225 | } |
ohair@286 | 226 | public ErrorHandler getErrorHandler() { |
ohair@286 | 227 | return errorHandler; |
ohair@286 | 228 | } |
ohair@286 | 229 | |
ohair@286 | 230 | public void parse(InputSource input) throws SAXException { |
ohair@286 | 231 | parse(); |
ohair@286 | 232 | } |
ohair@286 | 233 | |
ohair@286 | 234 | public void parse(String systemId) throws SAXException { |
ohair@286 | 235 | parse(); |
ohair@286 | 236 | } |
ohair@286 | 237 | |
ohair@286 | 238 | public void parse() throws SAXException { |
ohair@286 | 239 | // parses a content object by using the given marshaller |
ohair@286 | 240 | // SAX events will be sent to the repeater, and the repeater |
ohair@286 | 241 | // will further forward it to an appropriate component. |
ohair@286 | 242 | try { |
ohair@286 | 243 | marshaller.marshal( contentObject, repeater ); |
ohair@286 | 244 | } catch( JAXBException e ) { |
ohair@286 | 245 | // wrap it to a SAXException |
ohair@286 | 246 | SAXParseException se = |
ohair@286 | 247 | new SAXParseException( e.getMessage(), |
ohair@286 | 248 | null, null, -1, -1, e ); |
ohair@286 | 249 | |
ohair@286 | 250 | // if the consumer sets an error handler, it is our responsibility |
ohair@286 | 251 | // to notify it. |
ohair@286 | 252 | if(errorHandler!=null) |
ohair@286 | 253 | errorHandler.fatalError(se); |
ohair@286 | 254 | |
ohair@286 | 255 | // this is a fatal error. Even if the error handler |
ohair@286 | 256 | // returns, we will abort anyway. |
ohair@286 | 257 | throw se; |
ohair@286 | 258 | } |
ohair@286 | 259 | } |
ohair@286 | 260 | }; |
ohair@286 | 261 | |
ohair@286 | 262 | /** |
ohair@286 | 263 | * Hook to throw exception from the middle of a contructor chained call |
ohair@286 | 264 | * to this |
ohair@286 | 265 | */ |
ohair@286 | 266 | private static Marshaller assertionFailed( String message ) |
ohair@286 | 267 | throws JAXBException { |
ohair@286 | 268 | |
ohair@286 | 269 | throw new JAXBException( message ); |
ohair@286 | 270 | } |
ohair@286 | 271 | } |