Class Serializer
- java.lang.Object
-
- eu.maveniverse.domtrip.Serializer
-
public class Serializer extends java.lang.ObjectSerializes XML node trees back to XML string format with configurable formatting options and lossless preservation for unmodified content.The Serializer class is responsible for converting DomTrip's internal XML node tree back into XML text format. It provides intelligent formatting preservation that maintains the original formatting for unchanged content while applying new formatting rules to modified sections.
Serialization Features:
- Selective Preservation - Preserves formatting only for unmodified content
- Pretty Printing - Configurable indentation and formatting
- Raw Mode - Completely unformatted output (no line breaks or indentation)
- Automatic Detection - Intelligently detects and preserves existing formatting patterns
- Attribute Formatting - Preserves quote styles and attribute order
- Namespace Handling - Serialization of namespace declarations
Serialization Modes:
- Preservation Mode (
prettyPrint = false, default) - Maintains original formatting for unchanged content. Automatically detects formatting patterns in existing XML and preserves them when adding new content. - Pretty Print Mode (
prettyPrint = true) - Applies consistent indentation and formatting using configured settings. WhenlineEnding = ""andindentString = "", produces raw mode output.
Raw Mode:
Raw mode produces XML with no line breaks or indentation, resulting in a single continuous line. This is useful for minimizing file size or when formatting is not desired. Raw mode can be achieved by setting both
lineEndingandindentStringto empty strings, or by usingDomTripConfig.raw().Automatic Formatting Detection:
When parsing existing XML documents, the system automatically detects the formatting style:
- Raw formatting - XML with no line breaks or custom spacing is detected and preserved when adding new content
- Pretty formatting - XML with consistent indentation is detected and preserved
- Custom formatting - XML with custom spacing patterns is detected and preserved
Usage Examples:
// Basic serialization with preservation (default) Serializer serializer = new Serializer(); String xml = serializer.serialize(document); // Pretty printing with custom indentation Serializer prettySerializer = new Serializer(); prettySerializer.setPrettyPrint(true); prettySerializer.setIndentString(" "); // 4 spaces prettySerializer.setLineEnding("\n"); String prettyXml = prettySerializer.serialize(document); // Raw mode (no formatting) Serializer rawSerializer = new Serializer(DomTripConfig.raw()); String rawXml = rawSerializer.serialize(document); // Result: <root><child>content</child></root> // Manual raw mode configuration Serializer manualRaw = new Serializer(); manualRaw.setPrettyPrint(true); manualRaw.setIndentString(""); // No indentation manualRaw.setLineEnding(""); // No line endings // Serialize to OutputStream with encoding OutputStream outputStream = new FileOutputStream("output.xml"); serializer.serialize(document, outputStream); // Using configuration DomTripConfig config = DomTripConfig.prettyPrint() .withIndentString("\t") .withLineEnding("\r\n"); Serializer configSerializer = new Serializer(config);Performance Considerations:
The Serializer is optimized for performance:
- Uses StringBuilder for efficient string building
- Avoids re-serializing unmodified content when possible
- Provides both string and StringBuilder output methods
- See Also:
Parser,DomTripConfig,DomTripConfig.raw(),Document,Node
-
-
Constructor Summary
Constructors Constructor Description Serializer()Creates a new Serializer with default settings.Serializer(DomTripConfig config)Creates a new Serializer with the specified configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description EmptyElementStyleemptyElementStyle()Gets the empty element style used for serialization.EmptyElementStylegetEmptyElementStyle()Deprecated.UseemptyElementStyle()instead.java.lang.StringgetIndentString()Deprecated.UseindentString()instead.java.lang.StringgetLineEnding()Deprecated.UselineEnding()instead.java.lang.StringindentString()Gets the indentation string used for pretty printing.booleanisPrettyPrint()Checks if pretty printing is enabled.java.lang.StringlineEnding()Gets the line ending string used for pretty printing.java.lang.Stringserialize(Document document)Serializes an XML document to string using this serializer's configuration.java.lang.Stringserialize(Document document, DomTripConfig config)Serializes an XML document to string with custom configuration.voidserialize(Document document, java.io.OutputStream outputStream)Serializes an XML document to an OutputStream using the document's encoding.voidserialize(Document document, java.io.OutputStream outputStream, java.lang.String encoding)Serializes an XML document to an OutputStream using the specified encoding.voidserialize(Document document, java.io.OutputStream outputStream, java.nio.charset.Charset charset)Serializes an XML document to an OutputStream using the specified charset.java.lang.Stringserialize(Node node)Serializes a single node to string.voidserialize(Node node, java.io.OutputStream outputStream)Serializes a single node to an OutputStream using UTF-8 encoding.voidserialize(Node node, java.io.OutputStream outputStream, java.lang.String encoding)Serializes a single node to an OutputStream using the specified encoding.voidserialize(Node node, java.io.OutputStream outputStream, java.nio.charset.Charset charset)Serializes a single node to an OutputStream using the specified charset.voidsetEmptyElementStyle(EmptyElementStyle emptyElementStyle)Sets the empty element style for serialization.voidsetIndentString(java.lang.String indentString)Sets the indentation string for pretty printing.voidsetLineEnding(java.lang.String lineEnding)Sets the line ending string for pretty printing.voidsetPrettyPrint(boolean prettyPrint)Sets whether to enable pretty printing.
-
-
-
Constructor Detail
-
Serializer
public Serializer()
Creates a new Serializer with default settings.Default settings include two-space indentation, pretty printing disabled (which preserves original formatting), and standard line endings.
-
Serializer
public Serializer(DomTripConfig config)
Creates a new Serializer with the specified configuration.- Parameters:
config- the DomTripConfig to use for serialization settings
-
-
Method Detail
-
indentString
public java.lang.String indentString()
Gets the indentation string used for pretty printing.- Returns:
- the indentation string
-
getIndentString
@Deprecated public java.lang.String getIndentString()
Deprecated.UseindentString()instead.Gets the indentation string used for pretty printing.- Returns:
- the indentation string
-
setIndentString
public void setIndentString(java.lang.String indentString)
Sets the indentation string for pretty printing.- Parameters:
indentString- the indentation string, or null for default
-
isPrettyPrint
public boolean isPrettyPrint()
Checks if pretty printing is enabled.- Returns:
- true if pretty printing is enabled, false otherwise
-
setPrettyPrint
public void setPrettyPrint(boolean prettyPrint)
Sets whether to enable pretty printing.- Parameters:
prettyPrint- true to enable pretty printing, false otherwise
-
lineEnding
public java.lang.String lineEnding()
Gets the line ending string used for pretty printing.- Returns:
- the line ending string
-
getLineEnding
@Deprecated public java.lang.String getLineEnding()
Deprecated.UselineEnding()instead.Gets the line ending string used for pretty printing.- Returns:
- the line ending string
-
setLineEnding
public void setLineEnding(java.lang.String lineEnding)
Sets the line ending string for pretty printing.- Parameters:
lineEnding- the line ending string, or null for default
-
emptyElementStyle
public EmptyElementStyle emptyElementStyle()
Gets the empty element style used for serialization.- Returns:
- the empty element style
-
getEmptyElementStyle
@Deprecated public EmptyElementStyle getEmptyElementStyle()
Deprecated.UseemptyElementStyle()instead.Gets the empty element style used for serialization.- Returns:
- the empty element style
-
setEmptyElementStyle
public void setEmptyElementStyle(EmptyElementStyle emptyElementStyle)
Sets the empty element style for serialization.- Parameters:
emptyElementStyle- the empty element style, or null for default
-
serialize
public java.lang.String serialize(Document document, DomTripConfig config)
Serializes an XML document to string with custom configuration.Creates a temporary serializer with the specified configuration and uses it to serialize the document. This allows one-time use of different serialization settings without modifying this serializer.
- Parameters:
document- the document to serializeconfig- the configuration to use for serialization- Returns:
- the serialized XML string, or empty string if document is null
-
serialize
public java.lang.String serialize(Document document)
Serializes an XML document to string using this serializer's configuration.If pretty printing is disabled and the document is unmodified, the original formatting will be preserved. Otherwise, the document will be serialized according to this serializer's configuration.
- Parameters:
document- the document to serialize- Returns:
- the serialized XML string, or empty string if document is null
-
serialize
public void serialize(Document document, java.io.OutputStream outputStream) throws DomTripException
Serializes an XML document to an OutputStream using the document's encoding.This method uses the document's encoding property to determine the character encoding for the output stream. If the document has no encoding specified, UTF-8 is used as the default.
- Parameters:
document- the document to serializeoutputStream- the OutputStream to write to- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
public void serialize(Document document, java.io.OutputStream outputStream, java.nio.charset.Charset charset) throws DomTripException
Serializes an XML document to an OutputStream using the specified charset.This method allows explicit control over the character encoding used for serialization, regardless of the document's encoding property.
- Parameters:
document- the document to serializeoutputStream- the OutputStream to write tocharset- the character encoding to use- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
public void serialize(Document document, java.io.OutputStream outputStream, java.lang.String encoding) throws DomTripException
Serializes an XML document to an OutputStream using the specified encoding.This method allows explicit control over the character encoding used for serialization, regardless of the document's encoding property.
- Parameters:
document- the document to serializeoutputStream- the OutputStream to write toencoding- the character encoding name to use- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
public void serialize(Node node, java.io.OutputStream outputStream) throws DomTripException
Serializes a single node to an OutputStream using UTF-8 encoding.- Parameters:
node- the node to serializeoutputStream- the OutputStream to write to- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
public void serialize(Node node, java.io.OutputStream outputStream, java.nio.charset.Charset charset) throws DomTripException
Serializes a single node to an OutputStream using the specified charset.- Parameters:
node- the node to serializeoutputStream- the OutputStream to write tocharset- the character encoding to use- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
public void serialize(Node node, java.io.OutputStream outputStream, java.lang.String encoding) throws DomTripException
Serializes a single node to an OutputStream using the specified encoding.- Parameters:
node- the node to serializeoutputStream- the OutputStream to write toencoding- the character encoding name to use- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
public java.lang.String serialize(Node node)
Serializes a single node to string.If pretty printing is disabled and the node is unmodified, the original formatting will be preserved. Otherwise, the node will be serialized according to this serializer's configuration.
- Parameters:
node- the node to serialize- Returns:
- the serialized XML string, or empty string if node is null
-
-