Class ToXmlGenerator

java.lang.Object
com.fasterxml.jackson.core.JsonGenerator
com.fasterxml.jackson.core.base.GeneratorBase
com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator
All Implemented Interfaces:
com.fasterxml.jackson.core.Versioned, Closeable, Flushable, AutoCloseable

public class ToXmlGenerator extends com.fasterxml.jackson.core.base.GeneratorBase
JsonGenerator that outputs JAXB-style XML output instead of JSON content. Operation requires calling code (usually either standard Jackson serializers, or in some cases (like BeanSerializer) customized ones) to do additional configuration calls beyond regular JsonGenerator API, mostly to pass namespace information.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Enumeration that defines all togglable extra XML-specific features
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected LinkedList<QName>
    To support proper serialization of arrays it is necessary to keep stack of element names, so that we can "revert" to earlier
    protected int
    Bit flag composed of bits that indicate which ToXmlGenerator.Features are enabled.
    protected boolean
    Marker set when initGenerator() has been called or not.
    Escapes names with invalid XML characters
    Reusable internal value object
    protected boolean
    Marker flag that indicates whether next name to write implies an attribute (true) or element (false)
    protected boolean
    Marker flag used to indicate that the next write of a (property) value should be as CData
    protected boolean
    Marker flag used to indicate that the next write of a (property) value should be done without using surrounding start/end elements.
    protected QName
    Element or attribute name to use for next output call.
    protected final XMLStreamWriter
     
    protected final boolean
    Marker flag set if the underlying stream writer has to emulate Stax2 API: this is problematic if trying to use writeRaw(String) calls.
    protected final com.fasterxml.jackson.core.StreamWriteConstraints
     
    We may need to use XML-specific indentation as well
    protected final org.codehaus.stax2.XMLStreamWriter2
     
    protected static final String
    If we support optional definition of element names, this is the element name to use...

    Fields inherited from class com.fasterxml.jackson.core.base.GeneratorBase

    _cfgNumbersAsStrings, _closed, _features, _ioContext, _objectCodec, _writeContext, DERIVED_FEATURES_MASK, MAX_BIG_DECIMAL_SCALE, SURR1_FIRST, SURR1_LAST, SURR2_FIRST, SURR2_LAST, WRITE_BINARY, WRITE_BOOLEAN, WRITE_NULL, WRITE_NUMBER, WRITE_RAW, WRITE_STRING

    Fields inherited from class com.fasterxml.jackson.core.JsonGenerator

    _cfgPrettyPrinter, DEFAULT_BINARY_WRITE_CAPABILITIES, DEFAULT_TEXTUAL_WRITE_CAPABILITIES, DEFAULT_WRITE_CAPABILITIES
  • Constructor Summary

    Constructors
    Constructor
    Description
    ToXmlGenerator(com.fasterxml.jackson.core.io.IOContext ctxt, int stdFeatures, int xmlFeatures, com.fasterxml.jackson.core.ObjectCodec codec, XMLStreamWriter sw, XmlNameProcessor nameProcessor)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected com.fasterxml.jackson.core.PrettyPrinter
     
    final void
     
    final void
     
    protected void
     
    protected void
    Method called in case access to native Stax2 API implementation is required.
    protected final void
     
    boolean
     
    protected boolean
    Method called to see if unwrapping is required; and if so, clear the flag (so further calls will return 'false' unless state is re-set)
    void
     
    configure(ToXmlGenerator.Feature f, boolean state)
     
     
     
    void
    finishWrappedValue(QName wrapperName, QName wrappedName)
    Method called after a structured collection output has completed
    void
     
    int
     
    int
    Stax2 does not expose buffered content amount, so we can only return -1 from here
     
    Method that allows application direct access to underlying Stax XMLStreamWriter.
    com.fasterxml.jackson.core.util.JacksonFeatureSet<com.fasterxml.jackson.core.StreamWriteCapability>
     
    protected void
     
    void
    Method called before writing any other output, to optionally output XML declaration.
    boolean
     
    final boolean
     
    com.fasterxml.jackson.core.JsonGenerator
    overrideFormatFeatures(int values, int mask)
     
    void
    setNextIsAttribute(boolean isAttribute)
     
    void
    setNextIsCData(boolean isCData)
     
    void
    setNextIsUnwrapped(boolean isUnwrapped)
     
    final void
     
    final boolean
    Method that does same as setNextName(QName), unless a name has already been set.
    com.fasterxml.jackson.core.JsonGenerator
    setPrettyPrinter(com.fasterxml.jackson.core.PrettyPrinter pp)
     
    void
    startWrappedValue(QName wrapperName, QName wrappedName)
    Methdod called when a structured (collection, array, map) is being output.
    com.fasterxml.jackson.core.StreamWriteConstraints
     
    void
    writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant, byte[] data, int offset, int len)
     
    int
    writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant, InputStream data, int dataLength)
     
    void
    writeBoolean(boolean value)
     
    final void
     
    final void
     
    void
    writeFieldName(com.fasterxml.jackson.core.SerializableString name)
     
    final void
     
    void
     
    void
    writeNumber(double d)
     
    void
    writeNumber(float f)
     
    void
    writeNumber(int i)
     
    void
    writeNumber(long l)
     
    void
    writeNumber(String encodedValue)
     
    void
     
    void
     
    void
    writeRaw(char c)
     
    void
    writeRaw(char[] text, int offset, int len)
     
    void
     
    void
    writeRaw(String text, int offset, int len)
     
    void
    writeRawUTF8String(byte[] text, int offset, int length)
     
    void
    writeRawValue(char[] text, int offset, int len)
     
    void
    writeRawValue(com.fasterxml.jackson.core.SerializableString text)
     
    void
     
    void
    writeRawValue(String text, int offset, int len)
     
    void
    Trivial helper method called when to add a replicated wrapper name
    final void
     
    final void
     
    void
    writeString(char[] text, int offset, int len)
     
    void
    writeString(com.fasterxml.jackson.core.SerializableString text)
     
    void
     
    final void
    writeStringField(String fieldName, String value)
     
    void
    writeUTF8String(byte[] text, int offset, int length)
     

    Methods inherited from class com.fasterxml.jackson.core.base.GeneratorBase

    _asString, _checkRangeBoundsForByteArray, _checkRangeBoundsForCharArray, _checkRangeBoundsForString, _checkStdFeatureChanges, _decodeSurrogate, assignCurrentValue, currentValue, disable, enable, getCodec, getFeatureMask, getOutputContext, ioContext, isClosed, isEnabled, overrideStdFeatures, setCodec, setFeatureMask, useDefaultPrettyPrinter, version, writeObject, writeStartObject, writeTree

    Methods inherited from class com.fasterxml.jackson.core.JsonGenerator

    _constructWriteException, _constructWriteException, _constructWriteException, _constructWriteException, _copyCurrentContents, _copyCurrentFloatValue, _copyCurrentFloatValueExact, _copyCurrentIntValue, _copyCurrentStringValue, _reportError, _reportUnsupportedOperation, _reportUnsupportedOperation, _throwInternal, _verifyOffsets, _writeSimpleObject, canOmitFields, canUseSchema, canWriteBinaryNatively, canWriteObjectId, canWriteTypeId, configure, copyCurrentEvent, copyCurrentEventExact, copyCurrentStructure, getCharacterEscapes, getCurrentValue, getHighestEscapedChar, getPrettyPrinter, getSchema, isEnabled, setCharacterEscapes, setCurrentValue, setHighestNonEscapedChar, setRootValueSeparator, setSchema, writeArray, writeArray, writeArray, writeArray, writeArrayFieldStart, writeBinary, writeBinary, writeBinary, writeBinaryField, writeBooleanField, writeEmbeddedObject, writeFieldId, writeNullField, writeNumber, writeNumber, writeNumberField, writeNumberField, writeNumberField, writeNumberField, writeNumberField, writeNumberField, writeNumberField, writeObjectField, writeObjectFieldStart, writeObjectId, writeObjectRef, writeOmittedField, writePOJO, writePOJOField, writeRaw, writeStartArray, writeStartArray, writeStartArray, writeStartObject, writeString, writeTypeId, writeTypePrefix, writeTypeSuffix

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_UNKNOWN_ELEMENT

      protected static final String DEFAULT_UNKNOWN_ELEMENT
      If we support optional definition of element names, this is the element name to use...
      See Also:
    • _xmlWriter

      protected final org.codehaus.stax2.XMLStreamWriter2 _xmlWriter
    • _originalXmlWriter

      protected final XMLStreamWriter _originalXmlWriter
    • _stax2Emulation

      protected final boolean _stax2Emulation
      Marker flag set if the underlying stream writer has to emulate Stax2 API: this is problematic if trying to use writeRaw(String) calls.
    • _streamWriteConstraints

      protected final com.fasterxml.jackson.core.StreamWriteConstraints _streamWriteConstraints
      Since:
      2.16
    • _formatFeatures

      protected int _formatFeatures
      Bit flag composed of bits that indicate which ToXmlGenerator.Features are enabled.
    • _xmlPrettyPrinter

      protected XmlPrettyPrinter _xmlPrettyPrinter
      We may need to use XML-specific indentation as well
    • _nameProcessor

      protected XmlNameProcessor _nameProcessor
      Escapes names with invalid XML characters
      Since:
      2.14
    • _initialized

      protected boolean _initialized
      Marker set when initGenerator() has been called or not.
      Since:
      2.2
    • _nextName

      protected QName _nextName
      Element or attribute name to use for next output call. Assigned by either code that initiates serialization or bean serializer.
    • _nextIsAttribute

      protected boolean _nextIsAttribute
      Marker flag that indicates whether next name to write implies an attribute (true) or element (false)
    • _nextIsUnwrapped

      protected boolean _nextIsUnwrapped
      Marker flag used to indicate that the next write of a (property) value should be done without using surrounding start/end elements. Flag is to be cleared once unwrapping has been triggered once.
    • _nextIsCData

      protected boolean _nextIsCData
      Marker flag used to indicate that the next write of a (property) value should be as CData
    • _elementNameStack

      protected LinkedList<QName> _elementNameStack
      To support proper serialization of arrays it is necessary to keep stack of element names, so that we can "revert" to earlier
    • _nameToEncode

      protected XmlNameProcessor.XmlName _nameToEncode
      Reusable internal value object
      Since:
      2.14
  • Constructor Details

    • ToXmlGenerator

      public ToXmlGenerator(com.fasterxml.jackson.core.io.IOContext ctxt, int stdFeatures, int xmlFeatures, com.fasterxml.jackson.core.ObjectCodec codec, XMLStreamWriter sw, XmlNameProcessor nameProcessor)
  • Method Details

    • initGenerator

      public void initGenerator() throws IOException
      Method called before writing any other output, to optionally output XML declaration.
      Throws:
      IOException
    • _constructDefaultPrettyPrinter

      protected com.fasterxml.jackson.core.PrettyPrinter _constructDefaultPrettyPrinter()
      Overrides:
      _constructDefaultPrettyPrinter in class com.fasterxml.jackson.core.base.GeneratorBase
    • setPrettyPrinter

      public com.fasterxml.jackson.core.JsonGenerator setPrettyPrinter(com.fasterxml.jackson.core.PrettyPrinter pp)
      Overrides:
      setPrettyPrinter in class com.fasterxml.jackson.core.JsonGenerator
    • getOutputTarget

      public Object getOutputTarget()
      Overrides:
      getOutputTarget in class com.fasterxml.jackson.core.JsonGenerator
    • getOutputBuffered

      public int getOutputBuffered()
      Stax2 does not expose buffered content amount, so we can only return -1 from here
      Overrides:
      getOutputBuffered in class com.fasterxml.jackson.core.JsonGenerator
    • getFormatFeatures

      public int getFormatFeatures()
      Overrides:
      getFormatFeatures in class com.fasterxml.jackson.core.JsonGenerator
    • overrideFormatFeatures

      public com.fasterxml.jackson.core.JsonGenerator overrideFormatFeatures(int values, int mask)
      Overrides:
      overrideFormatFeatures in class com.fasterxml.jackson.core.JsonGenerator
    • streamWriteConstraints

      public com.fasterxml.jackson.core.StreamWriteConstraints streamWriteConstraints()
      Overrides:
      streamWriteConstraints in class com.fasterxml.jackson.core.JsonGenerator
    • enable

    • disable

    • isEnabled

      public final boolean isEnabled(ToXmlGenerator.Feature f)
    • configure

      public ToXmlGenerator configure(ToXmlGenerator.Feature f, boolean state)
    • canWriteFormattedNumbers

      public boolean canWriteFormattedNumbers()
      Overrides:
      canWriteFormattedNumbers in class com.fasterxml.jackson.core.JsonGenerator
    • getWriteCapabilities

      public com.fasterxml.jackson.core.util.JacksonFeatureSet<com.fasterxml.jackson.core.StreamWriteCapability> getWriteCapabilities()
      Overrides:
      getWriteCapabilities in class com.fasterxml.jackson.core.JsonGenerator
    • inRoot

      public boolean inRoot()
    • getStaxWriter

      public XMLStreamWriter getStaxWriter()
      Method that allows application direct access to underlying Stax XMLStreamWriter. Note that use of writer is discouraged, and may interfere with processing of this writer; however, occasionally it may be necessary.

      Note: writer instance will always be of type XMLStreamWriter2 (including Typed Access API) so upcasts are safe.

    • setNextIsAttribute

      public void setNextIsAttribute(boolean isAttribute)
    • setNextIsUnwrapped

      public void setNextIsUnwrapped(boolean isUnwrapped)
    • setNextIsCData

      public void setNextIsCData(boolean isCData)
    • setNextName

      public final void setNextName(QName name)
    • setNextNameIfMissing

      public final boolean setNextNameIfMissing(QName name)
      Method that does same as setNextName(QName), unless a name has already been set.
      Since:
      2.1.2
    • startWrappedValue

      public void startWrappedValue(QName wrapperName, QName wrappedName) throws IOException
      Methdod called when a structured (collection, array, map) is being output.
      Parameters:
      wrapperName - Element used as wrapper around elements, if any (null if none)
      wrappedName - Element used around individual content items (can not be null)
      Throws:
      IOException
    • finishWrappedValue

      public void finishWrappedValue(QName wrapperName, QName wrappedName) throws IOException
      Method called after a structured collection output has completed
      Throws:
      IOException
    • writeRepeatedFieldName

      public void writeRepeatedFieldName() throws IOException
      Trivial helper method called when to add a replicated wrapper name
      Throws:
      IOException
      Since:
      2.2
    • writeFieldName

      public void writeFieldName(com.fasterxml.jackson.core.SerializableString name) throws IOException
      Overrides:
      writeFieldName in class com.fasterxml.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • writeFieldName

      public final void writeFieldName(String name) throws IOException
      Specified by:
      writeFieldName in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeStringField

      public final void writeStringField(String fieldName, String value) throws IOException
      Overrides:
      writeStringField in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeStartArray

      public final void writeStartArray() throws IOException
      Specified by:
      writeStartArray in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeEndArray

      public final void writeEndArray() throws IOException
      Specified by:
      writeEndArray in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeStartObject

      public final void writeStartObject() throws IOException
      Specified by:
      writeStartObject in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeEndObject

      public final void writeEndObject() throws IOException
      Specified by:
      writeEndObject in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • _handleStartObject

      public final void _handleStartObject() throws IOException
      Throws:
      IOException
    • _handleEndObject

      public final void _handleEndObject() throws IOException
      Throws:
      IOException
    • writeString

      public void writeString(String text) throws IOException
      Specified by:
      writeString in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeString

      public void writeString(char[] text, int offset, int len) throws IOException
      Specified by:
      writeString in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeString

      public void writeString(com.fasterxml.jackson.core.SerializableString text) throws IOException
      Overrides:
      writeString in class com.fasterxml.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • writeRawUTF8String

      public void writeRawUTF8String(byte[] text, int offset, int length) throws IOException
      Specified by:
      writeRawUTF8String in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeUTF8String

      public void writeUTF8String(byte[] text, int offset, int length) throws IOException
      Specified by:
      writeUTF8String in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeRawValue

      public void writeRawValue(String text) throws IOException
      Overrides:
      writeRawValue in class com.fasterxml.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • writeRawValue

      public void writeRawValue(String text, int offset, int len) throws IOException
      Overrides:
      writeRawValue in class com.fasterxml.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • writeRawValue

      public void writeRawValue(char[] text, int offset, int len) throws IOException
      Overrides:
      writeRawValue in class com.fasterxml.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • writeRawValue

      public void writeRawValue(com.fasterxml.jackson.core.SerializableString text) throws IOException
      Overrides:
      writeRawValue in class com.fasterxml.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • writeRaw

      public void writeRaw(String text) throws IOException
      Specified by:
      writeRaw in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeRaw

      public void writeRaw(String text, int offset, int len) throws IOException
      Specified by:
      writeRaw in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeRaw

      public void writeRaw(char[] text, int offset, int len) throws IOException
      Specified by:
      writeRaw in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeRaw

      public void writeRaw(char c) throws IOException
      Specified by:
      writeRaw in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeBinary

      public void writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant, byte[] data, int offset, int len) throws IOException
      Specified by:
      writeBinary in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeBinary

      public int writeBinary(com.fasterxml.jackson.core.Base64Variant b64variant, InputStream data, int dataLength) throws IOException
      Overrides:
      writeBinary in class com.fasterxml.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • writeBoolean

      public void writeBoolean(boolean value) throws IOException
      Specified by:
      writeBoolean in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeNull

      public void writeNull() throws IOException
      Specified by:
      writeNull in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeNumber

      public void writeNumber(int i) throws IOException
      Specified by:
      writeNumber in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeNumber

      public void writeNumber(long l) throws IOException
      Specified by:
      writeNumber in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeNumber

      public void writeNumber(double d) throws IOException
      Specified by:
      writeNumber in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeNumber

      public void writeNumber(float f) throws IOException
      Specified by:
      writeNumber in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeNumber

      public void writeNumber(BigDecimal dec) throws IOException
      Specified by:
      writeNumber in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeNumber

      public void writeNumber(BigInteger value) throws IOException
      Specified by:
      writeNumber in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
    • writeNumber

      public void writeNumber(String encodedValue) throws IOException, UnsupportedOperationException
      Specified by:
      writeNumber in class com.fasterxml.jackson.core.JsonGenerator
      Throws:
      IOException
      UnsupportedOperationException
    • _verifyValueWrite

      protected final void _verifyValueWrite(String typeMsg) throws IOException
      Specified by:
      _verifyValueWrite in class com.fasterxml.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class com.fasterxml.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class com.fasterxml.jackson.core.base.GeneratorBase
      Throws:
      IOException
    • _releaseBuffers

      protected void _releaseBuffers()
      Specified by:
      _releaseBuffers in class com.fasterxml.jackson.core.base.GeneratorBase
    • checkNextIsUnwrapped

      protected boolean checkNextIsUnwrapped()
      Method called to see if unwrapping is required; and if so, clear the flag (so further calls will return 'false' unless state is re-set)
    • handleMissingName

      protected void handleMissingName()
    • _reportUnimplementedStax2

      protected void _reportUnimplementedStax2(String missingMethod) throws IOException
      Method called in case access to native Stax2 API implementation is required.
      Throws:
      IOException