Thu, 12 Oct 2017 19:44:07 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, 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 com.sun.xml.internal.bind.v2.runtime; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.BufferedWriter; |
aoqi@0 | 29 | import java.io.Closeable; |
aoqi@0 | 30 | import java.io.FileOutputStream; |
aoqi@0 | 31 | import java.io.Flushable; |
aoqi@0 | 32 | import java.io.IOException; |
aoqi@0 | 33 | import java.io.OutputStream; |
aoqi@0 | 34 | import java.io.OutputStreamWriter; |
aoqi@0 | 35 | import java.io.UnsupportedEncodingException; |
aoqi@0 | 36 | import java.io.Writer; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import java.net.URI; |
aoqi@0 | 39 | import javax.xml.bind.JAXBException; |
aoqi@0 | 40 | import javax.xml.bind.MarshalException; |
aoqi@0 | 41 | import javax.xml.bind.Marshaller; |
aoqi@0 | 42 | import javax.xml.bind.PropertyException; |
aoqi@0 | 43 | import javax.xml.bind.ValidationEvent; |
aoqi@0 | 44 | import javax.xml.bind.ValidationEventHandler; |
aoqi@0 | 45 | import javax.xml.bind.annotation.adapters.XmlAdapter; |
aoqi@0 | 46 | import javax.xml.bind.attachment.AttachmentMarshaller; |
aoqi@0 | 47 | import javax.xml.bind.helpers.AbstractMarshallerImpl; |
aoqi@0 | 48 | import javax.xml.stream.XMLEventWriter; |
aoqi@0 | 49 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 50 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 51 | import javax.xml.transform.Result; |
aoqi@0 | 52 | import javax.xml.transform.dom.DOMResult; |
aoqi@0 | 53 | import javax.xml.transform.sax.SAXResult; |
aoqi@0 | 54 | import javax.xml.transform.stream.StreamResult; |
aoqi@0 | 55 | import javax.xml.validation.Schema; |
aoqi@0 | 56 | import javax.xml.validation.ValidatorHandler; |
aoqi@0 | 57 | import javax.xml.namespace.NamespaceContext; |
aoqi@0 | 58 | |
aoqi@0 | 59 | import com.sun.xml.internal.bind.api.JAXBRIContext; |
aoqi@0 | 60 | import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; |
aoqi@0 | 61 | import com.sun.xml.internal.bind.marshaller.DataWriter; |
aoqi@0 | 62 | import com.sun.xml.internal.bind.marshaller.DumbEscapeHandler; |
aoqi@0 | 63 | import com.sun.xml.internal.bind.marshaller.MinimumEscapeHandler; |
aoqi@0 | 64 | import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper; |
aoqi@0 | 65 | import com.sun.xml.internal.bind.marshaller.NioEscapeHandler; |
aoqi@0 | 66 | import com.sun.xml.internal.bind.marshaller.SAX2DOMEx; |
aoqi@0 | 67 | import com.sun.xml.internal.bind.marshaller.XMLWriter; |
aoqi@0 | 68 | import com.sun.xml.internal.bind.v2.runtime.output.C14nXmlOutput; |
aoqi@0 | 69 | import com.sun.xml.internal.bind.v2.runtime.output.Encoded; |
aoqi@0 | 70 | import com.sun.xml.internal.bind.v2.runtime.output.ForkXmlOutput; |
aoqi@0 | 71 | import com.sun.xml.internal.bind.v2.runtime.output.IndentingUTF8XmlOutput; |
aoqi@0 | 72 | import com.sun.xml.internal.bind.v2.runtime.output.NamespaceContextImpl; |
aoqi@0 | 73 | import com.sun.xml.internal.bind.v2.runtime.output.SAXOutput; |
aoqi@0 | 74 | import com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput; |
aoqi@0 | 75 | import com.sun.xml.internal.bind.v2.runtime.output.XMLEventWriterOutput; |
aoqi@0 | 76 | import com.sun.xml.internal.bind.v2.runtime.output.XMLStreamWriterOutput; |
aoqi@0 | 77 | import com.sun.xml.internal.bind.v2.runtime.output.XmlOutput; |
aoqi@0 | 78 | import com.sun.xml.internal.bind.v2.util.FatalAdapter; |
aoqi@0 | 79 | |
aoqi@0 | 80 | import java.net.URISyntaxException; |
aoqi@0 | 81 | import org.w3c.dom.Document; |
aoqi@0 | 82 | import org.w3c.dom.Node; |
aoqi@0 | 83 | import org.xml.sax.SAXException; |
aoqi@0 | 84 | import org.xml.sax.helpers.XMLFilterImpl; |
aoqi@0 | 85 | |
aoqi@0 | 86 | /** |
aoqi@0 | 87 | * Implementation of {@link Marshaller} interface for the JAXB RI. |
aoqi@0 | 88 | * |
aoqi@0 | 89 | * <p> |
aoqi@0 | 90 | * Eventually all the {@link #marshal} methods call into |
aoqi@0 | 91 | * the {@link #write} method. |
aoqi@0 | 92 | * |
aoqi@0 | 93 | * @author Kohsuke Kawaguchi |
aoqi@0 | 94 | * @author Vivek Pandey |
aoqi@0 | 95 | */ |
aoqi@0 | 96 | public /*to make unit tests happy*/ final class MarshallerImpl extends AbstractMarshallerImpl implements ValidationEventHandler |
aoqi@0 | 97 | { |
aoqi@0 | 98 | /** Indentation string. Default is four whitespaces. */ |
aoqi@0 | 99 | private String indent = " "; |
aoqi@0 | 100 | |
aoqi@0 | 101 | /** Used to assign prefixes to namespace URIs. */ |
aoqi@0 | 102 | private NamespacePrefixMapper prefixMapper = null; |
aoqi@0 | 103 | |
aoqi@0 | 104 | /** Object that handles character escaping. */ |
aoqi@0 | 105 | private CharacterEscapeHandler escapeHandler = null; |
aoqi@0 | 106 | |
aoqi@0 | 107 | /** XML BLOB written after the XML declaration. */ |
aoqi@0 | 108 | private String header=null; |
aoqi@0 | 109 | |
aoqi@0 | 110 | /** reference to the context that created this object */ |
aoqi@0 | 111 | final JAXBContextImpl context; |
aoqi@0 | 112 | |
aoqi@0 | 113 | protected final XMLSerializer serializer; |
aoqi@0 | 114 | |
aoqi@0 | 115 | /** |
aoqi@0 | 116 | * Non-null if we do the marshal-time validation. |
aoqi@0 | 117 | */ |
aoqi@0 | 118 | private Schema schema; |
aoqi@0 | 119 | |
aoqi@0 | 120 | /** Marshaller.Listener */ |
aoqi@0 | 121 | private Listener externalListener = null; |
aoqi@0 | 122 | |
aoqi@0 | 123 | /** Configured for c14n? */ |
aoqi@0 | 124 | private boolean c14nSupport; |
aoqi@0 | 125 | |
aoqi@0 | 126 | // while createing XmlOutput those values may be set. |
aoqi@0 | 127 | // if these are non-null they need to be cleaned up |
aoqi@0 | 128 | private Flushable toBeFlushed; |
aoqi@0 | 129 | private Closeable toBeClosed; |
aoqi@0 | 130 | |
aoqi@0 | 131 | /** |
aoqi@0 | 132 | * @param assoc |
aoqi@0 | 133 | * non-null if the marshaller is working inside {@link BinderImpl}. |
aoqi@0 | 134 | */ |
aoqi@0 | 135 | public MarshallerImpl( JAXBContextImpl c, AssociationMap assoc ) { |
aoqi@0 | 136 | context = c; |
aoqi@0 | 137 | serializer = new XMLSerializer(this); |
aoqi@0 | 138 | c14nSupport = context.c14nSupport; |
aoqi@0 | 139 | |
aoqi@0 | 140 | try { |
aoqi@0 | 141 | setEventHandler(this); |
aoqi@0 | 142 | } catch (JAXBException e) { |
aoqi@0 | 143 | throw new AssertionError(e); // impossible |
aoqi@0 | 144 | } |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | public JAXBContextImpl getContext() { |
aoqi@0 | 148 | return context; |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | /** |
aoqi@0 | 152 | * Marshals to {@link OutputStream} with the given in-scope namespaces |
aoqi@0 | 153 | * taken into account. |
aoqi@0 | 154 | * |
aoqi@0 | 155 | * @since 2.1.5 |
aoqi@0 | 156 | */ |
aoqi@0 | 157 | public void marshal(Object obj, OutputStream out, NamespaceContext inscopeNamespace) throws JAXBException { |
aoqi@0 | 158 | write(obj, createWriter(out), new StAXPostInitAction(inscopeNamespace,serializer)); |
aoqi@0 | 159 | } |
aoqi@0 | 160 | |
aoqi@0 | 161 | @Override |
aoqi@0 | 162 | public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException { |
aoqi@0 | 163 | write(obj, XMLStreamWriterOutput.create(writer,context), new StAXPostInitAction(writer,serializer)); |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | @Override |
aoqi@0 | 167 | public void marshal(Object obj, XMLEventWriter writer) throws JAXBException { |
aoqi@0 | 168 | write(obj, new XMLEventWriterOutput(writer), new StAXPostInitAction(writer,serializer)); |
aoqi@0 | 169 | } |
aoqi@0 | 170 | |
aoqi@0 | 171 | public void marshal(Object obj, XmlOutput output) throws JAXBException { |
aoqi@0 | 172 | write(obj, output, null ); |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | /** |
aoqi@0 | 176 | * Creates {@link XmlOutput} from the given {@link Result} object. |
aoqi@0 | 177 | */ |
aoqi@0 | 178 | final XmlOutput createXmlOutput(Result result) throws JAXBException { |
aoqi@0 | 179 | if (result instanceof SAXResult) |
aoqi@0 | 180 | return new SAXOutput(((SAXResult) result).getHandler()); |
aoqi@0 | 181 | |
aoqi@0 | 182 | if (result instanceof DOMResult) { |
aoqi@0 | 183 | final Node node = ((DOMResult) result).getNode(); |
aoqi@0 | 184 | |
aoqi@0 | 185 | if (node == null) { |
aoqi@0 | 186 | Document doc = JAXBContextImpl.createDom(getContext().disableSecurityProcessing); |
aoqi@0 | 187 | ((DOMResult) result).setNode(doc); |
aoqi@0 | 188 | return new SAXOutput(new SAX2DOMEx(doc)); |
aoqi@0 | 189 | } else { |
aoqi@0 | 190 | return new SAXOutput(new SAX2DOMEx(node)); |
aoqi@0 | 191 | } |
aoqi@0 | 192 | } |
aoqi@0 | 193 | if (result instanceof StreamResult) { |
aoqi@0 | 194 | StreamResult sr = (StreamResult) result; |
aoqi@0 | 195 | |
aoqi@0 | 196 | if (sr.getWriter() != null) |
aoqi@0 | 197 | return createWriter(sr.getWriter()); |
aoqi@0 | 198 | else if (sr.getOutputStream() != null) |
aoqi@0 | 199 | return createWriter(sr.getOutputStream()); |
aoqi@0 | 200 | else if (sr.getSystemId() != null) { |
aoqi@0 | 201 | String fileURL = sr.getSystemId(); |
aoqi@0 | 202 | |
aoqi@0 | 203 | try { |
aoqi@0 | 204 | fileURL = new URI(fileURL).getPath(); |
aoqi@0 | 205 | } catch (URISyntaxException use) { |
aoqi@0 | 206 | // otherwise assume that it's a file name |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | try { |
aoqi@0 | 210 | FileOutputStream fos = new FileOutputStream(fileURL); |
aoqi@0 | 211 | assert toBeClosed==null; |
aoqi@0 | 212 | toBeClosed = fos; |
aoqi@0 | 213 | return createWriter(fos); |
aoqi@0 | 214 | } catch (IOException e) { |
aoqi@0 | 215 | throw new MarshalException(e); |
aoqi@0 | 216 | } |
aoqi@0 | 217 | } |
aoqi@0 | 218 | } |
aoqi@0 | 219 | |
aoqi@0 | 220 | // unsupported parameter type |
aoqi@0 | 221 | throw new MarshalException(Messages.UNSUPPORTED_RESULT.format()); |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | /** |
aoqi@0 | 225 | * Creates an appropriate post-init action object. |
aoqi@0 | 226 | */ |
aoqi@0 | 227 | final Runnable createPostInitAction(Result result) { |
aoqi@0 | 228 | if (result instanceof DOMResult) { |
aoqi@0 | 229 | Node node = ((DOMResult) result).getNode(); |
aoqi@0 | 230 | return new DomPostInitAction(node,serializer); |
aoqi@0 | 231 | } |
aoqi@0 | 232 | return null; |
aoqi@0 | 233 | } |
aoqi@0 | 234 | |
aoqi@0 | 235 | public void marshal(Object target,Result result) throws JAXBException { |
aoqi@0 | 236 | write(target, createXmlOutput(result), createPostInitAction(result)); |
aoqi@0 | 237 | } |
aoqi@0 | 238 | |
aoqi@0 | 239 | |
aoqi@0 | 240 | /** |
aoqi@0 | 241 | * Used by {@link BridgeImpl} to write an arbitrary object as a fragment. |
aoqi@0 | 242 | */ |
aoqi@0 | 243 | protected final <T> void write(Name rootTagName, JaxBeanInfo<T> bi, T obj, XmlOutput out,Runnable postInitAction) throws JAXBException { |
aoqi@0 | 244 | try { |
aoqi@0 | 245 | try { |
aoqi@0 | 246 | prewrite(out, true, postInitAction); |
aoqi@0 | 247 | serializer.startElement(rootTagName,null); |
aoqi@0 | 248 | if(bi.jaxbType==Void.class || bi.jaxbType==void.class) { |
aoqi@0 | 249 | // special case for void |
aoqi@0 | 250 | serializer.endNamespaceDecls(null); |
aoqi@0 | 251 | serializer.endAttributes(); |
aoqi@0 | 252 | } else { // normal cases |
aoqi@0 | 253 | if(obj==null) |
aoqi@0 | 254 | serializer.writeXsiNilTrue(); |
aoqi@0 | 255 | else |
aoqi@0 | 256 | serializer.childAsXsiType(obj,"root",bi, false); |
aoqi@0 | 257 | } |
aoqi@0 | 258 | serializer.endElement(); |
aoqi@0 | 259 | postwrite(); |
aoqi@0 | 260 | } catch( SAXException e ) { |
aoqi@0 | 261 | throw new MarshalException(e); |
aoqi@0 | 262 | } catch (IOException e) { |
aoqi@0 | 263 | throw new MarshalException(e); |
aoqi@0 | 264 | } catch (XMLStreamException e) { |
aoqi@0 | 265 | throw new MarshalException(e); |
aoqi@0 | 266 | } finally { |
aoqi@0 | 267 | serializer.close(); |
aoqi@0 | 268 | } |
aoqi@0 | 269 | } finally { |
aoqi@0 | 270 | cleanUp(); |
aoqi@0 | 271 | } |
aoqi@0 | 272 | } |
aoqi@0 | 273 | |
aoqi@0 | 274 | /** |
aoqi@0 | 275 | * All the marshal method invocation eventually comes down to this call. |
aoqi@0 | 276 | */ |
aoqi@0 | 277 | private void write(Object obj, XmlOutput out, Runnable postInitAction) throws JAXBException { |
aoqi@0 | 278 | try { |
aoqi@0 | 279 | if( obj == null ) |
aoqi@0 | 280 | throw new IllegalArgumentException(Messages.NOT_MARSHALLABLE.format()); |
aoqi@0 | 281 | |
aoqi@0 | 282 | if( schema!=null ) { |
aoqi@0 | 283 | // send the output to the validator as well |
aoqi@0 | 284 | ValidatorHandler validator = schema.newValidatorHandler(); |
aoqi@0 | 285 | validator.setErrorHandler(new FatalAdapter(serializer)); |
aoqi@0 | 286 | // work around a bug in JAXP validator in Tiger |
aoqi@0 | 287 | XMLFilterImpl f = new XMLFilterImpl() { |
aoqi@0 | 288 | @Override |
aoqi@0 | 289 | public void startPrefixMapping(String prefix, String uri) throws SAXException { |
aoqi@0 | 290 | super.startPrefixMapping(prefix.intern(), uri.intern()); |
aoqi@0 | 291 | } |
aoqi@0 | 292 | }; |
aoqi@0 | 293 | f.setContentHandler(validator); |
aoqi@0 | 294 | out = new ForkXmlOutput( new SAXOutput(f) { |
aoqi@0 | 295 | @Override |
aoqi@0 | 296 | public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws SAXException, IOException, XMLStreamException { |
aoqi@0 | 297 | super.startDocument(serializer, false, nsUriIndex2prefixIndex, nsContext); |
aoqi@0 | 298 | } |
aoqi@0 | 299 | @Override |
aoqi@0 | 300 | public void endDocument(boolean fragment) throws SAXException, IOException, XMLStreamException { |
aoqi@0 | 301 | super.endDocument(false); |
aoqi@0 | 302 | } |
aoqi@0 | 303 | }, out ); |
aoqi@0 | 304 | } |
aoqi@0 | 305 | |
aoqi@0 | 306 | try { |
aoqi@0 | 307 | prewrite(out,isFragment(),postInitAction); |
aoqi@0 | 308 | serializer.childAsRoot(obj); |
aoqi@0 | 309 | postwrite(); |
aoqi@0 | 310 | } catch( SAXException e ) { |
aoqi@0 | 311 | throw new MarshalException(e); |
aoqi@0 | 312 | } catch (IOException e) { |
aoqi@0 | 313 | throw new MarshalException(e); |
aoqi@0 | 314 | } catch (XMLStreamException e) { |
aoqi@0 | 315 | throw new MarshalException(e); |
aoqi@0 | 316 | } finally { |
aoqi@0 | 317 | serializer.close(); |
aoqi@0 | 318 | } |
aoqi@0 | 319 | } finally { |
aoqi@0 | 320 | cleanUp(); |
aoqi@0 | 321 | } |
aoqi@0 | 322 | } |
aoqi@0 | 323 | |
aoqi@0 | 324 | private void cleanUp() { |
aoqi@0 | 325 | if(toBeFlushed!=null) |
aoqi@0 | 326 | try { |
aoqi@0 | 327 | toBeFlushed.flush(); |
aoqi@0 | 328 | } catch (IOException e) { |
aoqi@0 | 329 | // ignore |
aoqi@0 | 330 | } |
aoqi@0 | 331 | if(toBeClosed!=null) |
aoqi@0 | 332 | try { |
aoqi@0 | 333 | toBeClosed.close(); |
aoqi@0 | 334 | } catch (IOException e) { |
aoqi@0 | 335 | // ignore |
aoqi@0 | 336 | } |
aoqi@0 | 337 | toBeFlushed = null; |
aoqi@0 | 338 | toBeClosed = null; |
aoqi@0 | 339 | } |
aoqi@0 | 340 | |
aoqi@0 | 341 | // common parts between two write methods. |
aoqi@0 | 342 | |
aoqi@0 | 343 | private void prewrite(XmlOutput out, boolean fragment, Runnable postInitAction) throws IOException, SAXException, XMLStreamException { |
aoqi@0 | 344 | serializer.startDocument(out,fragment,getSchemaLocation(),getNoNSSchemaLocation()); |
aoqi@0 | 345 | if(postInitAction!=null) postInitAction.run(); |
aoqi@0 | 346 | if(prefixMapper!=null) { |
aoqi@0 | 347 | // be defensive as we work with the user's code |
aoqi@0 | 348 | String[] decls = prefixMapper.getContextualNamespaceDecls(); |
aoqi@0 | 349 | if(decls!=null) { // defensive check |
aoqi@0 | 350 | for( int i=0; i<decls.length; i+=2 ) { |
aoqi@0 | 351 | String prefix = decls[i]; |
aoqi@0 | 352 | String nsUri = decls[i+1]; |
aoqi@0 | 353 | if(nsUri!=null && prefix!=null) // defensive check |
aoqi@0 | 354 | serializer.addInscopeBinding(nsUri,prefix); |
aoqi@0 | 355 | } |
aoqi@0 | 356 | } |
aoqi@0 | 357 | } |
aoqi@0 | 358 | serializer.setPrefixMapper(prefixMapper); |
aoqi@0 | 359 | } |
aoqi@0 | 360 | |
aoqi@0 | 361 | private void postwrite() throws IOException, SAXException, XMLStreamException { |
aoqi@0 | 362 | serializer.endDocument(); |
aoqi@0 | 363 | serializer.reconcileID(); // extra check |
aoqi@0 | 364 | } |
aoqi@0 | 365 | |
aoqi@0 | 366 | |
aoqi@0 | 367 | // |
aoqi@0 | 368 | // |
aoqi@0 | 369 | // create XMLWriter by specifing various type of output. |
aoqi@0 | 370 | // |
aoqi@0 | 371 | // |
aoqi@0 | 372 | |
aoqi@0 | 373 | protected CharacterEscapeHandler createEscapeHandler( String encoding ) { |
aoqi@0 | 374 | if( escapeHandler!=null ) |
aoqi@0 | 375 | // user-specified one takes precedence. |
aoqi@0 | 376 | return escapeHandler; |
aoqi@0 | 377 | |
aoqi@0 | 378 | if( encoding.startsWith("UTF") ) |
aoqi@0 | 379 | // no need for character reference. Use the handler |
aoqi@0 | 380 | // optimized for that pattern. |
aoqi@0 | 381 | return MinimumEscapeHandler.theInstance; |
aoqi@0 | 382 | |
aoqi@0 | 383 | // otherwise try to find one from the encoding |
aoqi@0 | 384 | try { |
aoqi@0 | 385 | // try new JDK1.4 NIO |
aoqi@0 | 386 | return new NioEscapeHandler( getJavaEncoding(encoding) ); |
aoqi@0 | 387 | } catch( Throwable e ) { |
aoqi@0 | 388 | // if that fails, fall back to the dumb mode |
aoqi@0 | 389 | return DumbEscapeHandler.theInstance; |
aoqi@0 | 390 | } |
aoqi@0 | 391 | } |
aoqi@0 | 392 | |
aoqi@0 | 393 | public XmlOutput createWriter( Writer w, String encoding ) { |
aoqi@0 | 394 | // XMLWriter doesn't do buffering, so do it here if it looks like a good idea |
aoqi@0 | 395 | if(!(w instanceof BufferedWriter)) |
aoqi@0 | 396 | w = new BufferedWriter(w); |
aoqi@0 | 397 | |
aoqi@0 | 398 | assert toBeFlushed==null; |
aoqi@0 | 399 | toBeFlushed = w; |
aoqi@0 | 400 | |
aoqi@0 | 401 | CharacterEscapeHandler ceh = createEscapeHandler(encoding); |
aoqi@0 | 402 | XMLWriter xw; |
aoqi@0 | 403 | |
aoqi@0 | 404 | if(isFormattedOutput()) { |
aoqi@0 | 405 | DataWriter d = new DataWriter(w,encoding,ceh); |
aoqi@0 | 406 | d.setIndentStep(indent); |
aoqi@0 | 407 | xw=d; |
aoqi@0 | 408 | } else |
aoqi@0 | 409 | xw = new XMLWriter(w,encoding,ceh); |
aoqi@0 | 410 | |
aoqi@0 | 411 | xw.setXmlDecl(!isFragment()); |
aoqi@0 | 412 | xw.setHeader(header); |
aoqi@0 | 413 | return new SAXOutput(xw); // TODO: don't we need a better writer? |
aoqi@0 | 414 | } |
aoqi@0 | 415 | |
aoqi@0 | 416 | public XmlOutput createWriter(Writer w) { |
aoqi@0 | 417 | return createWriter(w, getEncoding()); |
aoqi@0 | 418 | } |
aoqi@0 | 419 | |
aoqi@0 | 420 | public XmlOutput createWriter( OutputStream os ) throws JAXBException { |
aoqi@0 | 421 | return createWriter(os, getEncoding()); |
aoqi@0 | 422 | } |
aoqi@0 | 423 | |
aoqi@0 | 424 | public XmlOutput createWriter( OutputStream os, String encoding ) throws JAXBException { |
aoqi@0 | 425 | // UTF8XmlOutput does buffering on its own, and |
aoqi@0 | 426 | // otherwise createWriter(Writer) inserts a buffering, |
aoqi@0 | 427 | // so no point in doing a buffering here. |
aoqi@0 | 428 | |
aoqi@0 | 429 | if(encoding.equals("UTF-8")) { |
aoqi@0 | 430 | Encoded[] table = context.getUTF8NameTable(); |
aoqi@0 | 431 | final UTF8XmlOutput out; |
aoqi@0 | 432 | if(isFormattedOutput()) |
aoqi@0 | 433 | out = new IndentingUTF8XmlOutput(os, indent, table, escapeHandler); |
aoqi@0 | 434 | else { |
aoqi@0 | 435 | if(c14nSupport) |
aoqi@0 | 436 | out = new C14nXmlOutput(os, table, context.c14nSupport, escapeHandler); |
aoqi@0 | 437 | else |
aoqi@0 | 438 | out = new UTF8XmlOutput(os, table, escapeHandler); |
aoqi@0 | 439 | } |
aoqi@0 | 440 | if(header!=null) |
aoqi@0 | 441 | out.setHeader(header); |
aoqi@0 | 442 | return out; |
aoqi@0 | 443 | } |
aoqi@0 | 444 | |
aoqi@0 | 445 | try { |
aoqi@0 | 446 | return createWriter( |
aoqi@0 | 447 | new OutputStreamWriter(os,getJavaEncoding(encoding)), |
aoqi@0 | 448 | encoding ); |
aoqi@0 | 449 | } catch( UnsupportedEncodingException e ) { |
aoqi@0 | 450 | throw new MarshalException( |
aoqi@0 | 451 | Messages.UNSUPPORTED_ENCODING.format(encoding), |
aoqi@0 | 452 | e ); |
aoqi@0 | 453 | } |
aoqi@0 | 454 | } |
aoqi@0 | 455 | |
aoqi@0 | 456 | |
aoqi@0 | 457 | @Override |
aoqi@0 | 458 | public Object getProperty(String name) throws PropertyException { |
aoqi@0 | 459 | if( INDENT_STRING.equals(name) ) |
aoqi@0 | 460 | return indent; |
aoqi@0 | 461 | if( ENCODING_HANDLER.equals(name) || ENCODING_HANDLER2.equals(name) ) |
aoqi@0 | 462 | return escapeHandler; |
aoqi@0 | 463 | if( PREFIX_MAPPER.equals(name) ) |
aoqi@0 | 464 | return prefixMapper; |
aoqi@0 | 465 | if( XMLDECLARATION.equals(name) ) |
aoqi@0 | 466 | return !isFragment(); |
aoqi@0 | 467 | if( XML_HEADERS.equals(name) ) |
aoqi@0 | 468 | return header; |
aoqi@0 | 469 | if( C14N.equals(name) ) |
aoqi@0 | 470 | return c14nSupport; |
aoqi@0 | 471 | if ( OBJECT_IDENTITY_CYCLE_DETECTION.equals(name)) |
aoqi@0 | 472 | return serializer.getObjectIdentityCycleDetection(); |
aoqi@0 | 473 | |
aoqi@0 | 474 | return super.getProperty(name); |
aoqi@0 | 475 | } |
aoqi@0 | 476 | |
aoqi@0 | 477 | @Override |
aoqi@0 | 478 | public void setProperty(String name, Object value) throws PropertyException { |
aoqi@0 | 479 | if( INDENT_STRING.equals(name) ) { |
aoqi@0 | 480 | checkString(name, value); |
aoqi@0 | 481 | indent = (String)value; |
aoqi@0 | 482 | return; |
aoqi@0 | 483 | } |
aoqi@0 | 484 | if( ENCODING_HANDLER.equals(name) || ENCODING_HANDLER2.equals(name)) { |
aoqi@0 | 485 | if(!(value instanceof CharacterEscapeHandler)) |
aoqi@0 | 486 | throw new PropertyException( |
aoqi@0 | 487 | Messages.MUST_BE_X.format( |
aoqi@0 | 488 | name, |
aoqi@0 | 489 | CharacterEscapeHandler.class.getName(), |
aoqi@0 | 490 | value.getClass().getName() ) ); |
aoqi@0 | 491 | escapeHandler = (CharacterEscapeHandler)value; |
aoqi@0 | 492 | return; |
aoqi@0 | 493 | } |
aoqi@0 | 494 | if( PREFIX_MAPPER.equals(name) ) { |
aoqi@0 | 495 | if(!(value instanceof NamespacePrefixMapper)) |
aoqi@0 | 496 | throw new PropertyException( |
aoqi@0 | 497 | Messages.MUST_BE_X.format( |
aoqi@0 | 498 | name, |
aoqi@0 | 499 | NamespacePrefixMapper.class.getName(), |
aoqi@0 | 500 | value.getClass().getName() ) ); |
aoqi@0 | 501 | prefixMapper = (NamespacePrefixMapper)value; |
aoqi@0 | 502 | return; |
aoqi@0 | 503 | } |
aoqi@0 | 504 | if( XMLDECLARATION.equals(name) ) { |
aoqi@0 | 505 | checkBoolean(name, value); |
aoqi@0 | 506 | // com.sun.xml.internal.bind.xmlDeclaration is an alias for JAXB_FRAGMENT |
aoqi@0 | 507 | // setting it to false is treated the same as setting fragment to true. |
aoqi@0 | 508 | super.setProperty(JAXB_FRAGMENT, !(Boolean)value); |
aoqi@0 | 509 | return; |
aoqi@0 | 510 | } |
aoqi@0 | 511 | if( XML_HEADERS.equals(name) ) { |
aoqi@0 | 512 | checkString(name, value); |
aoqi@0 | 513 | header = (String)value; |
aoqi@0 | 514 | return; |
aoqi@0 | 515 | } |
aoqi@0 | 516 | if( C14N.equals(name) ) { |
aoqi@0 | 517 | checkBoolean(name,value); |
aoqi@0 | 518 | c14nSupport = (Boolean)value; |
aoqi@0 | 519 | return; |
aoqi@0 | 520 | } |
aoqi@0 | 521 | if (OBJECT_IDENTITY_CYCLE_DETECTION.equals(name)) { |
aoqi@0 | 522 | checkBoolean(name,value); |
aoqi@0 | 523 | serializer.setObjectIdentityCycleDetection((Boolean)value); |
aoqi@0 | 524 | return; |
aoqi@0 | 525 | } |
aoqi@0 | 526 | |
aoqi@0 | 527 | super.setProperty(name, value); |
aoqi@0 | 528 | } |
aoqi@0 | 529 | |
aoqi@0 | 530 | /* |
aoqi@0 | 531 | * assert that the given object is a Boolean |
aoqi@0 | 532 | */ |
aoqi@0 | 533 | private void checkBoolean( String name, Object value ) throws PropertyException { |
aoqi@0 | 534 | if(!(value instanceof Boolean)) |
aoqi@0 | 535 | throw new PropertyException( |
aoqi@0 | 536 | Messages.MUST_BE_X.format( |
aoqi@0 | 537 | name, |
aoqi@0 | 538 | Boolean.class.getName(), |
aoqi@0 | 539 | value.getClass().getName() ) ); |
aoqi@0 | 540 | } |
aoqi@0 | 541 | |
aoqi@0 | 542 | /* |
aoqi@0 | 543 | * assert that the given object is a String |
aoqi@0 | 544 | */ |
aoqi@0 | 545 | private void checkString( String name, Object value ) throws PropertyException { |
aoqi@0 | 546 | if(!(value instanceof String)) |
aoqi@0 | 547 | throw new PropertyException( |
aoqi@0 | 548 | Messages.MUST_BE_X.format( |
aoqi@0 | 549 | name, |
aoqi@0 | 550 | String.class.getName(), |
aoqi@0 | 551 | value.getClass().getName() ) ); |
aoqi@0 | 552 | } |
aoqi@0 | 553 | |
aoqi@0 | 554 | @Override |
aoqi@0 | 555 | public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) { |
aoqi@0 | 556 | if(type==null) |
aoqi@0 | 557 | throw new IllegalArgumentException(); |
aoqi@0 | 558 | serializer.putAdapter(type,adapter); |
aoqi@0 | 559 | } |
aoqi@0 | 560 | |
aoqi@0 | 561 | @Override |
aoqi@0 | 562 | public <A extends XmlAdapter> A getAdapter(Class<A> type) { |
aoqi@0 | 563 | if(type==null) |
aoqi@0 | 564 | throw new IllegalArgumentException(); |
aoqi@0 | 565 | if(serializer.containsAdapter(type)) |
aoqi@0 | 566 | // so as not to create a new instance when this method is called |
aoqi@0 | 567 | return serializer.getAdapter(type); |
aoqi@0 | 568 | else |
aoqi@0 | 569 | return null; |
aoqi@0 | 570 | } |
aoqi@0 | 571 | |
aoqi@0 | 572 | @Override |
aoqi@0 | 573 | public void setAttachmentMarshaller(AttachmentMarshaller am) { |
aoqi@0 | 574 | serializer.attachmentMarshaller = am; |
aoqi@0 | 575 | } |
aoqi@0 | 576 | |
aoqi@0 | 577 | @Override |
aoqi@0 | 578 | public AttachmentMarshaller getAttachmentMarshaller() { |
aoqi@0 | 579 | return serializer.attachmentMarshaller; |
aoqi@0 | 580 | } |
aoqi@0 | 581 | |
aoqi@0 | 582 | @Override |
aoqi@0 | 583 | public Schema getSchema() { |
aoqi@0 | 584 | return schema; |
aoqi@0 | 585 | } |
aoqi@0 | 586 | |
aoqi@0 | 587 | @Override |
aoqi@0 | 588 | public void setSchema(Schema s) { |
aoqi@0 | 589 | this.schema = s; |
aoqi@0 | 590 | } |
aoqi@0 | 591 | |
aoqi@0 | 592 | /** |
aoqi@0 | 593 | * Default error handling behavior fot {@link Marshaller}. |
aoqi@0 | 594 | */ |
aoqi@0 | 595 | public boolean handleEvent(ValidationEvent event) { |
aoqi@0 | 596 | // draconian by default |
aoqi@0 | 597 | return false; |
aoqi@0 | 598 | } |
aoqi@0 | 599 | |
aoqi@0 | 600 | @Override |
aoqi@0 | 601 | public Listener getListener() { |
aoqi@0 | 602 | return externalListener; |
aoqi@0 | 603 | } |
aoqi@0 | 604 | |
aoqi@0 | 605 | @Override |
aoqi@0 | 606 | public void setListener(Listener listener) { |
aoqi@0 | 607 | externalListener = listener; |
aoqi@0 | 608 | } |
aoqi@0 | 609 | |
aoqi@0 | 610 | // features supported |
aoqi@0 | 611 | protected static final String INDENT_STRING = "com.sun.xml.internal.bind.indentString"; |
aoqi@0 | 612 | protected static final String PREFIX_MAPPER = "com.sun.xml.internal.bind.namespacePrefixMapper"; |
aoqi@0 | 613 | protected static final String ENCODING_HANDLER = "com.sun.xml.internal.bind.characterEscapeHandler"; |
aoqi@0 | 614 | protected static final String ENCODING_HANDLER2 = "com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler"; |
aoqi@0 | 615 | protected static final String XMLDECLARATION = "com.sun.xml.internal.bind.xmlDeclaration"; |
aoqi@0 | 616 | protected static final String XML_HEADERS = "com.sun.xml.internal.bind.xmlHeaders"; |
aoqi@0 | 617 | protected static final String C14N = JAXBRIContext.CANONICALIZATION_SUPPORT; |
aoqi@0 | 618 | protected static final String OBJECT_IDENTITY_CYCLE_DETECTION = "com.sun.xml.internal.bind.objectIdentitityCycleDetection"; |
aoqi@0 | 619 | } |