Skip to content

Commit 11d5fdc

Browse files
committed
support setting char encoding on output XML
1 parent 88cc868 commit 11d5fdc

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,22 @@ public ToXmlGenerator createGenerator(OutputStream out, JsonEncoding enc) throws
517517
_generatorFeatures, _xmlGeneratorFeatures,
518518
_objectCodec, _createXmlWriter(ctxt, out), _nameProcessor);
519519
}
520+
521+
/**
522+
* @param out
523+
* @param encoding
524+
* @return
525+
* @throws IOException
526+
* @since 2.16
527+
*/
528+
public ToXmlGenerator createGenerator(OutputStream out, String encoding) throws IOException
529+
{
530+
// false -> we won't manage the stream unless explicitly directed to
531+
final IOContext ctxt = _createContext(_createContentReference(out), false);
532+
return new ToXmlGenerator(ctxt,
533+
_generatorFeatures, _xmlGeneratorFeatures,
534+
_objectCodec, _createXmlWriter(ctxt, out, encoding), _nameProcessor, encoding);
535+
}
520536

521537
@Override
522538
public ToXmlGenerator createGenerator(Writer out) throws IOException
@@ -689,10 +705,15 @@ protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOEx
689705
*/
690706

691707
protected XMLStreamWriter _createXmlWriter(IOContext ctxt, OutputStream out) throws IOException
708+
{
709+
return _createXmlWriter(ctxt, out, "UTF-8");
710+
}
711+
712+
protected XMLStreamWriter _createXmlWriter(IOContext ctxt, OutputStream out, String encoding) throws IOException
692713
{
693714
XMLStreamWriter sw;
694715
try {
695-
sw = _xmlOutputFactory.createXMLStreamWriter(_decorate(ctxt, out), "UTF-8");
716+
sw = _xmlOutputFactory.createXMLStreamWriter(_decorate(ctxt, out), encoding);
696717
} catch (Exception e) {
697718
throw new JsonGenerationException(e.getMessage(), e, null);
698719
}

src/main/java/com/fasterxml/jackson/dataformat/xml/XmlMapper.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.dataformat.xml;
22

33
import java.io.IOException;
4+
import java.io.OutputStream;
45

56
import javax.xml.stream.XMLInputFactory;
67
import javax.xml.stream.XMLOutputFactory;
@@ -9,6 +10,7 @@
910

1011
import com.fasterxml.jackson.core.*;
1112
import com.fasterxml.jackson.core.type.TypeReference;
13+
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
1214
import com.fasterxml.jackson.databind.*;
1315
import com.fasterxml.jackson.databind.cfg.CoercionAction;
1416
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
@@ -392,4 +394,34 @@ public void writeValue(XMLStreamWriter w0, Object value) throws IOException {
392394
// NOTE: above call should do flush(); and we should NOT close here.
393395
// Finally, 'g' has no buffers to release.
394396
}
397+
398+
/**
399+
* Method that can be used to serialize any Java value as
400+
* a byte array.
401+
402+
* @param value value to write as XML bytes
403+
* @param encoding character encoding for the XML output
404+
* @return byte array representing the XML output
405+
* @throws JsonProcessingException
406+
* @since 2.16
407+
*/
408+
public byte[] writeValueAsBytes(Object value, String encoding) throws JsonProcessingException {
409+
try (ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler())) {
410+
_writeValueAndClose(createGenerator(bb, encoding), value);
411+
final byte[] result = bb.toByteArray();
412+
bb.release();
413+
return result;
414+
} catch (JsonProcessingException e) { // to support [JACKSON-758]
415+
throw e;
416+
} catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
417+
throw JsonMappingException.fromUnexpectedIOE(e);
418+
}
419+
}
420+
421+
private JsonGenerator createGenerator(OutputStream out, String encoding) throws IOException {
422+
this._assertNotNull("out", out);
423+
JsonGenerator g = ((XmlFactory) _jsonFactory).createGenerator(out, encoding);
424+
this._serializationConfig.initialize(g);
425+
return g;
426+
}
395427
}

src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.math.BigDecimal;
55
import java.math.BigInteger;
6+
import java.nio.charset.StandardCharsets;
67
import java.util.*;
78

89
import javax.xml.XMLConstants;
@@ -213,6 +214,13 @@ private Feature(boolean defaultState) {
213214
*/
214215
protected XmlNameProcessor.XmlName _nameToEncode = new XmlNameProcessor.XmlName();
215216

217+
/**
218+
* The character encoding for the XML output
219+
*
220+
* @since 2.16
221+
*/
222+
protected final String _encoding;
223+
216224
/*
217225
/**********************************************************
218226
/* Life-cycle
@@ -221,16 +229,24 @@ private Feature(boolean defaultState) {
221229

222230
public ToXmlGenerator(IOContext ctxt, int stdFeatures, int xmlFeatures,
223231
ObjectCodec codec, XMLStreamWriter sw, XmlNameProcessor nameProcessor)
232+
{
233+
this(ctxt, stdFeatures, xmlFeatures, codec, sw, nameProcessor, StandardCharsets.UTF_8.name());
234+
}
235+
236+
public ToXmlGenerator(IOContext ctxt, int stdFeatures, int xmlFeatures,
237+
ObjectCodec codec, XMLStreamWriter sw, XmlNameProcessor nameProcessor,
238+
String encoding)
224239
{
225240
super(stdFeatures, codec);
226241
_formatFeatures = xmlFeatures;
227242
_ioContext = ctxt;
228243
_originalXmlWriter = sw;
244+
_encoding = encoding;
229245
_xmlWriter = Stax2WriterAdapter.wrapIfNecessary(sw);
230246
_stax2Emulation = (_xmlWriter != sw);
231247
_nameProcessor = nameProcessor;
232248
_xmlPrettyPrinter = (_cfgPrettyPrinter instanceof XmlPrettyPrinter) ?
233-
(XmlPrettyPrinter) _cfgPrettyPrinter : null;
249+
(XmlPrettyPrinter) _cfgPrettyPrinter : null;
234250
}
235251

236252
/**
@@ -245,9 +261,9 @@ public void initGenerator() throws IOException
245261
_initialized = true;
246262
try {
247263
if (Feature.WRITE_XML_1_1.enabledIn(_formatFeatures)) {
248-
_xmlWriter.writeStartDocument("UTF-8", "1.1");
264+
_xmlWriter.writeStartDocument(_encoding, "1.1");
249265
} else if (Feature.WRITE_XML_DECLARATION.enabledIn(_formatFeatures)) {
250-
_xmlWriter.writeStartDocument("UTF-8", "1.0");
266+
_xmlWriter.writeStartDocument(_encoding, "1.0");
251267
} else {
252268
return;
253269
}

0 commit comments

Comments
 (0)