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
148 throw new AssertionError(e); // impossible
149 }
150 }
151
152 public JAXBContextImpl getContext() {
153 return context;
154 }
155
156 /**
157 * Marshals to {@link OutputStream} with the given in-scope namespaces
158 * taken into account.
159 *
160 * @since 2.1.5
161 */
162 public void marshal(Object obj, OutputStream out, NamespaceContext inscopeNamespace) throws JAXBException {
163 write(obj, createWriter(out), new StAXPostInitAction(inscopeNamespace,serializer));
164 }
165
166 @Override
167 public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException {
168 write(obj, XMLStreamWriterOutput.create(writer,context), new StAXPostInitAction(writer,serializer));
169 }
170
171 @Override
172 public void marshal(Object obj, XMLEventWriter writer) throws JAXBException {
173 write(obj, new XMLEventWriterOutput(writer), new StAXPostInitAction(writer,serializer));
174 }
175
176 public void marshal(Object obj, XmlOutput output) throws JAXBException {
177 write(obj, output, null );
178 }
179
180 /**
181 * Creates {@link XmlOutput} from the given {@link Result} object.
182 */
183 final XmlOutput createXmlOutput(Result result) throws JAXBException {
184 if (result instanceof SAXResult)
185 return new SAXOutput(((SAXResult) result).getHandler());
186
187 if (result instanceof DOMResult) {
188 final Node node = ((DOMResult) result).getNode();
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 }
363 }
364 }
365 serializer.setPrefixMapper(prefixMapper);
366 }
367
368 private void postwrite() throws IOException, SAXException, XMLStreamException {
369 serializer.endDocument();
370 serializer.reconcileID(); // extra check
371 }
372
373
374 //
375 //
376 // create XMLWriter by specifing various type of output.
377 //
378 //
379
380 protected CharacterEscapeHandler createEscapeHandler( String encoding ) {
381 if( escapeHandler!=null )
382 // user-specified one takes precedence.
383 return escapeHandler;
384
385 if( encoding.startsWith("UTF") )
386 // no need for character reference. Use the handler
387 // optimized for that pattern.
388 return MinimumEscapeHandler.theInstance;
389
390 // otherwise try to find one from the encoding
391 try {
392 // try new JDK1.4 NIO
393 return new NioEscapeHandler( getJavaEncoding(encoding) );
419 xw.setHeader(header);
420 return new SAXOutput(xw); // TODO: don't we need a better writer?
421 }
422
423 public XmlOutput createWriter(Writer w) {
424 return createWriter(w, getEncoding());
425 }
426
427 public XmlOutput createWriter( OutputStream os ) throws JAXBException {
428 return createWriter(os, getEncoding());
429 }
430
431 public XmlOutput createWriter( OutputStream os, String encoding ) throws JAXBException {
432 // UTF8XmlOutput does buffering on its own, and
433 // otherwise createWriter(Writer) inserts a buffering,
434 // so no point in doing a buffering here.
435
436 if(encoding.equals("UTF-8")) {
437 Encoded[] table = context.getUTF8NameTable();
438 final UTF8XmlOutput out;
439 if(isFormattedOutput())
440 out = new IndentingUTF8XmlOutput(os, indent, table, escapeHandler);
441 else {
442 if(c14nSupport)
443 out = new C14nXmlOutput(os, table, context.c14nSupport, escapeHandler);
444 else
445 out = new UTF8XmlOutput(os, table, escapeHandler);
446 }
447 if(header!=null)
448 out.setHeader(header);
449 return out;
450 }
451
452 try {
453 return createWriter(
454 new OutputStreamWriter(os,getJavaEncoding(encoding)),
455 encoding );
456 } catch( UnsupportedEncodingException e ) {
457 throw new MarshalException(
458 Messages.UNSUPPORTED_ENCODING.format(encoding),
459 e );
460 }
461 }
462
463
464 @Override
465 public Object getProperty(String name) throws PropertyException {
|
1 /*
2 * Copyright (c) 1997, 2017, 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
148 throw new AssertionError(e); // impossible
149 }
150 }
151
152 public JAXBContextImpl getContext() {
153 return context;
154 }
155
156 /**
157 * Marshals to {@link OutputStream} with the given in-scope namespaces
158 * taken into account.
159 *
160 * @since 2.1.5
161 */
162 public void marshal(Object obj, OutputStream out, NamespaceContext inscopeNamespace) throws JAXBException {
163 write(obj, createWriter(out), new StAXPostInitAction(inscopeNamespace,serializer));
164 }
165
166 @Override
167 public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException {
168 write(obj, XMLStreamWriterOutput.create(writer,context, escapeHandler), new StAXPostInitAction(writer,serializer));
169 }
170
171 @Override
172 public void marshal(Object obj, XMLEventWriter writer) throws JAXBException {
173 write(obj, new XMLEventWriterOutput(writer), new StAXPostInitAction(writer,serializer));
174 }
175
176 public void marshal(Object obj, XmlOutput output) throws JAXBException {
177 write(obj, output, null );
178 }
179
180 /**
181 * Creates {@link XmlOutput} from the given {@link Result} object.
182 */
183 final XmlOutput createXmlOutput(Result result) throws JAXBException {
184 if (result instanceof SAXResult)
185 return new SAXOutput(((SAXResult) result).getHandler());
186
187 if (result instanceof DOMResult) {
188 final Node node = ((DOMResult) result).getNode();
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 }
363 }
364 }
365 serializer.setPrefixMapper(prefixMapper);
366 }
367
368 private void postwrite() throws IOException, SAXException, XMLStreamException {
369 serializer.endDocument();
370 serializer.reconcileID(); // extra check
371 }
372
373
374 /**
375 * Returns escape handler provided with JAXB context parameters.
376 *
377 * @return escape handler
378 */
379 CharacterEscapeHandler getEscapeHandler() {
380 return escapeHandler;
381 }
382
383 //
384 //
385 // create XMLWriter by specifing various type of output.
386 //
387 //
388
389 protected CharacterEscapeHandler createEscapeHandler( String encoding ) {
390 if( escapeHandler!=null )
391 // user-specified one takes precedence.
392 return escapeHandler;
393
394 if( encoding.startsWith("UTF") )
395 // no need for character reference. Use the handler
396 // optimized for that pattern.
397 return MinimumEscapeHandler.theInstance;
398
399 // otherwise try to find one from the encoding
400 try {
401 // try new JDK1.4 NIO
402 return new NioEscapeHandler( getJavaEncoding(encoding) );
428 xw.setHeader(header);
429 return new SAXOutput(xw); // TODO: don't we need a better writer?
430 }
431
432 public XmlOutput createWriter(Writer w) {
433 return createWriter(w, getEncoding());
434 }
435
436 public XmlOutput createWriter( OutputStream os ) throws JAXBException {
437 return createWriter(os, getEncoding());
438 }
439
440 public XmlOutput createWriter( OutputStream os, String encoding ) throws JAXBException {
441 // UTF8XmlOutput does buffering on its own, and
442 // otherwise createWriter(Writer) inserts a buffering,
443 // so no point in doing a buffering here.
444
445 if(encoding.equals("UTF-8")) {
446 Encoded[] table = context.getUTF8NameTable();
447 final UTF8XmlOutput out;
448 CharacterEscapeHandler ceh = createEscapeHandler(encoding);
449 if(isFormattedOutput())
450 out = new IndentingUTF8XmlOutput(os, indent, table, ceh);
451 else {
452 if(c14nSupport)
453 out = new C14nXmlOutput(os, table, context.c14nSupport, ceh);
454 else
455 out = new UTF8XmlOutput(os, table, ceh);
456 }
457 if(header!=null)
458 out.setHeader(header);
459 return out;
460 }
461
462 try {
463 return createWriter(
464 new OutputStreamWriter(os,getJavaEncoding(encoding)),
465 encoding );
466 } catch( UnsupportedEncodingException e ) {
467 throw new MarshalException(
468 Messages.UNSUPPORTED_ENCODING.format(encoding),
469 e );
470 }
471 }
472
473
474 @Override
475 public Object getProperty(String name) throws PropertyException {
|