Class XmlGeneratorInitializer
- All Implemented Interfaces:
tools.jackson.databind.cfg.GeneratorInitializer
GeneratorInitializer implementation to use with
ToXmlGenerator, registered via
ObjectWriter.with(GeneratorInitializer).
It allows output of various document-level things such as
- Document Type Declarations (DTD); that is "<!DOCTYPE>" directive
- Comments (in Document prolog, before the root element)
- Namespace bindings (prefix to URI mappings)
- Processing Instructions (PIs; in Document prolog, before the root element)
- XML Declaration (with custom version, encoding and/or standalone value)
NOTE: instances are mutable, not thread-safe.
- Since:
- 3.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected List<PrologDirective>Prolog Directives to pass for generator to write.protected booleanprotected List<NamespaceBinding>Namespace bindings (prefix to URI) to register with generator.protected List<RootAttribute>Attributes to add to root element (if any).protected XmlDeclarationCustom XML declaration to write. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected XmlGeneratorInitializeraddComment(String commentContent) Method for adding XML comment; to be written at position added relative to other directives (but always after XML Declaration which must come before any other output; and before Document Root element)addDefaultNamespace(String namespaceURI) Method for specifying namespace URI to preferentially bind to the "default namespace" (one used when element has no prefix).Convenience method that constructsDTDout of arguments and callsaddDTD(DTD).Method for adding Document Type Declaration (DTD) directive; to be written at position added relative to other directives (but always after XML Declaration which must come before any other output; and before Document Root element)addNamespace(String prefix, String namespaceURI) Method for adding a mapping (binding) between given prefix and matching namespace URI.Method for adding XML Processing Instruction (PI); to be written at position added relative to other directives (but always after XML Declaration which must come before any other output; and before Document Root element)addRootAttribute(String localName, String value) Convenience overload ofaddRootAttribute(QName, String)for adding non-namespaced attribute by local name.addRootAttribute(QName name, String value) Method for adding an attribute to be written on the root element of the output document.addXmlDeclaration(String version, String encoding) Method for specifying custom XML declaration to write.addXmlDeclaration(String version, String encoding, boolean standalone) Method for specifying custom XML declaration to write.addXmlDeclaration(XmlDeclaration xmlDeclaration) Method for specifying custom XML declaration to write.voidinitialize(tools.jackson.databind.SerializationConfig config, tools.jackson.core.JsonGenerator g) linefeedsBetweenPrologDirectives(boolean addLFs) Method to change whether line-feeds are to be added between Prolog directives or not: default being they are (enabled).
-
Field Details
-
_directives
Prolog Directives to pass for generator to write. -
_namespaceBindings
Namespace bindings (prefix to URI) to register with generator. -
_rootAttributes
Attributes to add to root element (if any).- Since:
- 3.2
-
_xmlDeclaration
Custom XML declaration to write. -
_addLfBetweenPrologDirectives
protected boolean _addLfBetweenPrologDirectives -
_hasDTD
protected boolean _hasDTD
-
-
Constructor Details
-
XmlGeneratorInitializer
public XmlGeneratorInitializer()
-
-
Method Details
-
initialize
public void initialize(tools.jackson.databind.SerializationConfig config, tools.jackson.core.JsonGenerator g) throws tools.jackson.core.JacksonException - Specified by:
initializein interfacetools.jackson.databind.cfg.GeneratorInitializer- Throws:
tools.jackson.core.JacksonException
-
linefeedsBetweenPrologDirectives
Method to change whether line-feeds are to be added between Prolog directives or not: default being they are (enabled).- Parameters:
addLFs- Whether line-feeds are to be added or not (default:true)- Returns:
- This initializer for call chaining
-
addComment
Method for adding XML comment; to be written at position added relative to other directives (but always after XML Declaration which must come before any other output; and before Document Root element)- Parameters:
commentContent- (optional) Comment content to include- Returns:
- This initializer for call chaining
-
addDTD
public XmlGeneratorInitializer addDTD(String rootName, String systemId, String publicId, String internalSubset) Convenience method that constructsDTDout of arguments and callsaddDTD(DTD).- Parameters:
rootName- (required) Root name for DTDsystemId- (optional) System Id for DTDpublicId- (optional) Public Id for DTDinternalSubset- (optional) Internal subset for DTD (not including surrounding brackets- Returns:
- This initializer for call chaining
-
addDTD
Method for adding Document Type Declaration (DTD) directive; to be written at position added relative to other directives (but always after XML Declaration which must come before any other output; and before Document Root element)- Parameters:
dtd- DTD to write- Returns:
- This initializer for call chaining
-
addPI
Method for adding XML Processing Instruction (PI); to be written at position added relative to other directives (but always after XML Declaration which must come before any other output; and before Document Root element)- Parameters:
target- Processing Instruction target: must not benullor empty Stringdata- (optional) Processing Instruction data part, if any, separated by a space from target (if not null)- Returns:
- This initializer for call chaining
-
addDefaultNamespace
Method for specifying namespace URI to preferentially bind to the "default namespace" (one used when element has no prefix). This will guide underlying generator to add necessary declarations when actually writing elements with matching namespace URI.- Parameters:
namespaceURI- URI of the default namespace- Returns:
- This initializer for call chaining
-
addNamespace
Method for adding a mapping (binding) between given prefix and matching namespace URI. This will guide underlying generator to add necessary declarations when actually writing namespaced attributes and elements.- Parameters:
prefix- Prefix to use for namespacenamespaceURI- URI of the namespace- Returns:
- This initializer for call chaining
-
addRootAttribute
Method for adding an attribute to be written on the root element of the output document. Attributes are emitted in the order added, after the root element's start tag is written and before any content from the value being serialized.Typical use case is adding XML Schema instance attributes such as
xsi:schemaLocationorxsi:noNamespaceSchemaLocation.NOTE: root attributes are only emitted when the root value being serialized produces a structured (object) start element; scalar root values do not currently get root attributes attached.
- Parameters:
name- Attribute name (with optional namespace and prefix)value- Attribute value (null is coerced to empty String)- Returns:
- This initializer for call chaining
- Since:
- 3.2
-
addRootAttribute
Convenience overload ofaddRootAttribute(QName, String)for adding non-namespaced attribute by local name.- Parameters:
localName- Attribute local name (must be non-empty) in default namespace (one with URI of "")value- Attribute value (ifnull, coerced to empty String)- Returns:
- This initializer for call chaining
- Since:
- 3.2
-
addXmlDeclaration
Method for specifying custom XML declaration to write.When a custom XML declaration is registered it fully replaces output that would otherwise be produced by
XmlWriteFeature.WRITE_XML_DECLARATION,XmlWriteFeature.WRITE_XML_1_1andXmlWriteFeature.WRITE_STANDALONE_YES_TO_XML_DECLARATION: those format features are ignored. Note that caller is responsible for ensuring the declared encoding matches the encoding the underlyingWriterorOutputStreamactually uses.- Parameters:
version- XML version: either "1.0" or "1.1"encoding-encodingcontent will be encoded in: usually "UTF-8"- Returns:
- This initializer for call chaining
-
addXmlDeclaration
public XmlGeneratorInitializer addXmlDeclaration(String version, String encoding, boolean standalone) Method for specifying custom XML declaration to write.See
addXmlDeclaration(String, String)for details on how this interacts withXmlWriteFeatureflags.- Parameters:
version- XML version: either "1.0" or "1.1"encoding-encodingcontent will be encoded in: usually "UTF-8"standalone-standalonepseudo-attribute value to write- Returns:
- This initializer for call chaining
-
addXmlDeclaration
Method for specifying custom XML declaration to write.See
addXmlDeclaration(String, String)for details on how this interacts withXmlWriteFeatureflags.- Parameters:
xmlDeclaration- declaration to write- Returns:
- This initializer for call chaining
-
_add
-