1 /*
2 * Copyright (c) 1997, 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
61 import com.sun.xml.internal.bind.marshaller.DataWriter;
62 import com.sun.xml.internal.bind.marshaller.DumbEscapeHandler;
63 import com.sun.xml.internal.bind.marshaller.MinimumEscapeHandler;
64 import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper;
65 import com.sun.xml.internal.bind.marshaller.NioEscapeHandler;
66 import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
67 import com.sun.xml.internal.bind.marshaller.XMLWriter;
68 import com.sun.xml.internal.bind.v2.runtime.output.C14nXmlOutput;
69 import com.sun.xml.internal.bind.v2.runtime.output.Encoded;
70 import com.sun.xml.internal.bind.v2.runtime.output.ForkXmlOutput;
71 import com.sun.xml.internal.bind.v2.runtime.output.IndentingUTF8XmlOutput;
72 import com.sun.xml.internal.bind.v2.runtime.output.NamespaceContextImpl;
73 import com.sun.xml.internal.bind.v2.runtime.output.SAXOutput;
74 import com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput;
75 import com.sun.xml.internal.bind.v2.runtime.output.XMLEventWriterOutput;
76 import com.sun.xml.internal.bind.v2.runtime.output.XMLStreamWriterOutput;
77 import com.sun.xml.internal.bind.v2.runtime.output.XmlOutput;
78 import com.sun.xml.internal.bind.v2.util.FatalAdapter;
79
80 import java.net.URISyntaxException;
81 import org.w3c.dom.Document;
82 import org.w3c.dom.Node;
83 import org.xml.sax.SAXException;
84 import org.xml.sax.helpers.XMLFilterImpl;
85
86 /**
87 * Implementation of {@link Marshaller} interface for the JAXB RI.
88 *
89 * <p>
90 * Eventually all the {@link #marshal} methods call into
91 * the {@link #write} method.
92 *
93 * @author Kohsuke Kawaguchi
94 * @author Vivek Pandey
95 */
96 public /*to make unit tests happy*/ final class MarshallerImpl extends AbstractMarshallerImpl implements ValidationEventHandler
97 {
98 /** Indentation string. Default is four whitespaces. */
99 private String indent = " ";
100
101 /** Used to assign prefixes to namespace URIs. */
102 private NamespacePrefixMapper prefixMapper = null;
103
104 /** Object that handles character escaping. */
105 private CharacterEscapeHandler escapeHandler = null;
106
107 /** XML BLOB written after the XML declaration. */
108 private String header=null;
109
110 /** reference to the context that created this object */
111 final JAXBContextImpl context;
112
113 protected final XMLSerializer serializer;
114
115 /**
116 * Non-null if we do the marshal-time validation.
117 */
310 } catch( SAXException e ) {
311 throw new MarshalException(e);
312 } catch (IOException e) {
313 throw new MarshalException(e);
314 } catch (XMLStreamException e) {
315 throw new MarshalException(e);
316 } finally {
317 serializer.close();
318 }
319 } finally {
320 cleanUp();
321 }
322 }
323
324 private void cleanUp() {
325 if(toBeFlushed!=null)
326 try {
327 toBeFlushed.flush();
328 } catch (IOException e) {
329 // ignore
330 }
331 if(toBeClosed!=null)
332 try {
333 toBeClosed.close();
334 } catch (IOException e) {
335 // ignore
336 }
337 toBeFlushed = null;
338 toBeClosed = null;
339 }
340
341 // common parts between two write methods.
342
343 private void prewrite(XmlOutput out, boolean fragment, Runnable postInitAction) throws IOException, SAXException, XMLStreamException {
344 serializer.startDocument(out,fragment,getSchemaLocation(),getNoNSSchemaLocation());
345 if(postInitAction!=null) postInitAction.run();
346 if(prefixMapper!=null) {
347 // be defensive as we work with the user's code
348 String[] decls = prefixMapper.getContextualNamespaceDecls();
349 if(decls!=null) { // defensive check
350 for( int i=0; i<decls.length; i+=2 ) {
351 String prefix = decls[i];
352 String nsUri = decls[i+1];
353 if(nsUri!=null && prefix!=null) // defensive check
354 serializer.addInscopeBinding(nsUri,prefix);
355 }
|
1 /*
2 * Copyright (c) 1997, 2015, 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
61 import com.sun.xml.internal.bind.marshaller.DataWriter;
62 import com.sun.xml.internal.bind.marshaller.DumbEscapeHandler;
63 import com.sun.xml.internal.bind.marshaller.MinimumEscapeHandler;
64 import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper;
65 import com.sun.xml.internal.bind.marshaller.NioEscapeHandler;
66 import com.sun.xml.internal.bind.marshaller.SAX2DOMEx;
67 import com.sun.xml.internal.bind.marshaller.XMLWriter;
68 import com.sun.xml.internal.bind.v2.runtime.output.C14nXmlOutput;
69 import com.sun.xml.internal.bind.v2.runtime.output.Encoded;
70 import com.sun.xml.internal.bind.v2.runtime.output.ForkXmlOutput;
71 import com.sun.xml.internal.bind.v2.runtime.output.IndentingUTF8XmlOutput;
72 import com.sun.xml.internal.bind.v2.runtime.output.NamespaceContextImpl;
73 import com.sun.xml.internal.bind.v2.runtime.output.SAXOutput;
74 import com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput;
75 import com.sun.xml.internal.bind.v2.runtime.output.XMLEventWriterOutput;
76 import com.sun.xml.internal.bind.v2.runtime.output.XMLStreamWriterOutput;
77 import com.sun.xml.internal.bind.v2.runtime.output.XmlOutput;
78 import com.sun.xml.internal.bind.v2.util.FatalAdapter;
79
80 import java.net.URISyntaxException;
81 import java.util.logging.Level;
82 import java.util.logging.Logger;
83
84 import org.w3c.dom.Document;
85 import org.w3c.dom.Node;
86 import org.xml.sax.SAXException;
87 import org.xml.sax.helpers.XMLFilterImpl;
88
89 /**
90 * Implementation of {@link Marshaller} interface for the JAXB RI.
91 *
92 * <p>
93 * Eventually all the {@link #marshal} methods call into
94 * the {@link #write} method.
95 *
96 * @author Kohsuke Kawaguchi
97 * @author Vivek Pandey
98 */
99 public /*to make unit tests happy*/ final class MarshallerImpl extends AbstractMarshallerImpl implements ValidationEventHandler
100 {
101 private static final Logger LOGGER = Logger.getLogger(MarshallerImpl.class.getName());
102
103 /** Indentation string. Default is four whitespaces. */
104 private String indent = " ";
105
106 /** Used to assign prefixes to namespace URIs. */
107 private NamespacePrefixMapper prefixMapper = null;
108
109 /** Object that handles character escaping. */
110 private CharacterEscapeHandler escapeHandler = null;
111
112 /** XML BLOB written after the XML declaration. */
113 private String header=null;
114
115 /** reference to the context that created this object */
116 final JAXBContextImpl context;
117
118 protected final XMLSerializer serializer;
119
120 /**
121 * Non-null if we do the marshal-time validation.
122 */
315 } catch( SAXException e ) {
316 throw new MarshalException(e);
317 } catch (IOException e) {
318 throw new MarshalException(e);
319 } catch (XMLStreamException e) {
320 throw new MarshalException(e);
321 } finally {
322 serializer.close();
323 }
324 } finally {
325 cleanUp();
326 }
327 }
328
329 private void cleanUp() {
330 if(toBeFlushed!=null)
331 try {
332 toBeFlushed.flush();
333 } catch (IOException e) {
334 // ignore
335 LOGGER.log(Level.SEVERE, e.getMessage(), e);
336 }
337 if(toBeClosed!=null)
338 try {
339 toBeClosed.close();
340 } catch (IOException e) {
341 // ignore
342 LOGGER.log(Level.SEVERE, e.getMessage(), e);
343 }
344 toBeFlushed = null;
345 toBeClosed = null;
346 }
347
348 // common parts between two write methods.
349
350 private void prewrite(XmlOutput out, boolean fragment, Runnable postInitAction) throws IOException, SAXException, XMLStreamException {
351 serializer.startDocument(out,fragment,getSchemaLocation(),getNoNSSchemaLocation());
352 if(postInitAction!=null) postInitAction.run();
353 if(prefixMapper!=null) {
354 // be defensive as we work with the user's code
355 String[] decls = prefixMapper.getContextualNamespaceDecls();
356 if(decls!=null) { // defensive check
357 for( int i=0; i<decls.length; i+=2 ) {
358 String prefix = decls[i];
359 String nsUri = decls[i+1];
360 if(nsUri!=null && prefix!=null) // defensive check
361 serializer.addInscopeBinding(nsUri,prefix);
362 }
|