Class XMLWriterSettings

java.lang.Object
com.helger.xml.serialize.write.XMLWriterSettings
All Implemented Interfaces:
com.helger.commons.lang.ICloneable<XMLWriterSettings>, IXMLWriterSettings

@NotThreadSafe public class XMLWriterSettings extends Object implements IXMLWriterSettings, com.helger.commons.lang.ICloneable<XMLWriterSettings>
Default implementation of the IXMLWriterSettings interface.
Describes the export settings for the MicroWriter. Defaults to indented and aligned XML in the UTF-8 charset.
Author:
Philip Helger
  • Field Details

    • DEFAULT_XML_SERIALIZE_VERSION

      public static final EXMLSerializeVersion DEFAULT_XML_SERIALIZE_VERSION
      Default default XML version is 1.0
    • DEFAULT_SERIALIZE_XML_DECLARATION

      public static final EXMLSerializeXMLDeclaration DEFAULT_SERIALIZE_XML_DECLARATION
      By default all fields of the XML declaration are emitted
    • DEFAULT_NEW_LINE_AFTER_XML_DECLARATION

      public static final boolean DEFAULT_NEW_LINE_AFTER_XML_DECLARATION
      By default a newline character is emitted afterwards
      See Also:
    • DEFAULT_SERIALIZE_DOC_TYPE

      public static final EXMLSerializeDocType DEFAULT_SERIALIZE_DOC_TYPE
      By default the document type is emitted
    • DEFAULT_SERIALIZE_COMMENTS

      public static final EXMLSerializeComments DEFAULT_SERIALIZE_COMMENTS
      By default comments are emitted
    • DEFAULT_SERIALIZE_INDENT

      public static final EXMLSerializeIndent DEFAULT_SERIALIZE_INDENT
      By default the output is indented and aligned (newlines are added)
    • DEFAULT_INCORRECT_CHARACTER_HANDLING

      public static final EXMLIncorrectCharacterHandling DEFAULT_INCORRECT_CHARACTER_HANDLING
      By default invalid characters are not written and a log message is emitted
    • DEFAULT_XML_CHARSET_OBJ

      public static final Charset DEFAULT_XML_CHARSET_OBJ
      The default charset is UTF-8
    • DEFAULT_XML_CHARSET

      public static final String DEFAULT_XML_CHARSET
      The default charset is UTF-8
    • DEFAULT_USE_DOUBLE_QUOTES_FOR_ATTRIBUTES

      public static final boolean DEFAULT_USE_DOUBLE_QUOTES_FOR_ATTRIBUTES
      By default double quotes are used to wrap attribute values
      See Also:
    • DEFAULT_SPACE_ON_SELF_CLOSED_ELEMENT

      public static final boolean DEFAULT_SPACE_ON_SELF_CLOSED_ELEMENT
      By default a leading space is inserted before a self closed element (e.g. <b /> in contrast to <b/>).
      See Also:
    • DEFAULT_INDENTATION_STRING

      public static final String DEFAULT_INDENTATION_STRING
      By default indentation happens with 2 spaces
      See Also:
    • DEFAULT_EMIT_NAMESPACES

      public static final boolean DEFAULT_EMIT_NAMESPACES
      By default namespaces are written.
      See Also:
    • DEFAULT_PUT_NAMESPACE_CONTEXT_PREFIXES_IN_ROOT

      public static final boolean DEFAULT_PUT_NAMESPACE_CONTEXT_PREFIXES_IN_ROOT
      By default namespace context prefixes are put inside the root element
      See Also:
    • DEFAULT_WRITE_CDATA_AS_TEXT

      public static final boolean DEFAULT_WRITE_CDATA_AS_TEXT
      By default CDATA children are emitted as CDATA
      See Also:
    • DEFAULT_ORDER_ATTRIBUTES_AND_NAMESPACES

      public static final boolean DEFAULT_ORDER_ATTRIBUTES_AND_NAMESPACES
      By default the insertion order of attributes is maintained
      See Also:
    • DEFAULT_XML_SETTINGS

      public static final IXMLWriterSettings DEFAULT_XML_SETTINGS
      The default settings to use - last constant
  • Constructor Details

    • XMLWriterSettings

      public XMLWriterSettings()
      Creates a default settings object with the following settings:
      • XML version 1.0
      • with XML declaration
      • with document type
      • with comments
      • Indented and aligned
      • Writing invalid characters to the file as is - may result in invalid XML files
      • Default character set UTF-8
      • No namespace context
    • XMLWriterSettings

      public XMLWriterSettings(@Nonnull IXMLWriterSettings aOther)
      Copy constructor.
      Parameters:
      aOther - The object to copy the settings from. May not be null.
  • Method Details

    • getXMLVersion

      @Nonnull public EXMLVersion getXMLVersion()
      Specified by:
      getXMLVersion in interface IXMLWriterSettings
      Returns:
      The XML version to use. Default is 1.0
    • getSerializeVersion

      @Nonnull public EXMLSerializeVersion getSerializeVersion()
      Specified by:
      getSerializeVersion in interface IXMLWriterSettings
      Returns:
      The XML serialize version to use. Default is XML 1.0
    • setSerializeVersion

      @Nonnull public final XMLWriterSettings setSerializeVersion(@Nonnull EXMLSerializeVersion eSerializeVersion)
      Set the preferred XML version to use.
      Parameters:
      eSerializeVersion - The XML serialize version. May not be null.
      Returns:
      this
    • getSerializeXMLDeclaration

      @Nonnull public EXMLSerializeXMLDeclaration getSerializeXMLDeclaration()
      Specified by:
      getSerializeXMLDeclaration in interface IXMLWriterSettings
      Returns:
      Write XML declaration? Default is true.
    • setSerializeXMLDeclaration

      @Nonnull public final XMLWriterSettings setSerializeXMLDeclaration(@Nonnull EXMLSerializeXMLDeclaration eSerializeXMLDecl)
      Set the way how to handle the XML declaration.
      Parameters:
      eSerializeXMLDecl - XML declaration handling. May not be null.
      Returns:
      this
    • isNewLineAfterXMLDeclaration

      public boolean isNewLineAfterXMLDeclaration()
      Specified by:
      isNewLineAfterXMLDeclaration in interface IXMLWriterSettings
      Returns:
      true if a newline should be added after the XML declaration or not. Defaults to true.
    • setNewLineAfterXMLDeclaration

      @Nonnull public final XMLWriterSettings setNewLineAfterXMLDeclaration(boolean bNewLineAfterXMLDeclaration)
      Change whether a newline should be printed after the XML declaration or not.
      Parameters:
      bNewLineAfterXMLDeclaration - true to print it, false to not print a new line.
      Returns:
      this for chaining
    • getSerializeDocType

      @Nonnull public EXMLSerializeDocType getSerializeDocType()
      Specified by:
      getSerializeDocType in interface IXMLWriterSettings
      Returns:
      Write document type? Default is true.
    • setSerializeDocType

      @Nonnull public final XMLWriterSettings setSerializeDocType(@Nonnull EXMLSerializeDocType eSerializeDocType)
      Set the way how to handle the doc type.
      Parameters:
      eSerializeDocType - Doc type handling. May not be null.
      Returns:
      this
    • getSerializeComments

      @Nonnull public EXMLSerializeComments getSerializeComments()
      Specified by:
      getSerializeComments in interface IXMLWriterSettings
      Returns:
      Write comments? Default is true.
    • setSerializeComments

      @Nonnull public final XMLWriterSettings setSerializeComments(@Nonnull EXMLSerializeComments eSerializeComments)
      Set the way how comments should be handled.
      Parameters:
      eSerializeComments - The comment handling. May not be null.
      Returns:
      this
    • getIndent

      @Nonnull public EXMLSerializeIndent getIndent()
      Specified by:
      getIndent in interface IXMLWriterSettings
      Returns:
      Indent and/or align code? Default is indent and aligned.
    • setIndent

      @Nonnull public final XMLWriterSettings setIndent(@Nonnull EXMLSerializeIndent eIndent)
      Set the way how to indent/align
      Parameters:
      eIndent - Indent and align definition. May not be null.
      Returns:
      this
    • getIndentDeterminator

      @Nonnull public IXMLIndentDeterminator getIndentDeterminator()
      Specified by:
      getIndentDeterminator in interface IXMLWriterSettings
      Returns:
      The dynamic (per-element) indentation determinator. This must be changed when e.g. serializing HTML. Never null.
    • setIndentDeterminator

      @Nonnull public final XMLWriterSettings setIndentDeterminator(@Nonnull IXMLIndentDeterminator aIndentDeterminator)
      Set the dynamic (per-element) indent determinator to be used.
      Parameters:
      aIndentDeterminator - The object to use. May not be null.
      Returns:
      this
    • getIncorrectCharacterHandling

      @Nonnull public EXMLIncorrectCharacterHandling getIncorrectCharacterHandling()
      Specified by:
      getIncorrectCharacterHandling in interface IXMLWriterSettings
      Returns:
      The incorrect character handling to be used. May not be null.
    • setIncorrectCharacterHandling

      @Nonnull public final XMLWriterSettings setIncorrectCharacterHandling(@Nonnull EXMLIncorrectCharacterHandling eIncorrectCharacterHandling)
      Set the way how to handle invalid characters.
      Parameters:
      eIncorrectCharacterHandling - The invalid character handling. May not be null.
      Returns:
      this
    • getCharset

      @Nonnull public Charset getCharset()
      Specified by:
      getCharset in interface IXMLWriterSettings
      Returns:
      The charset to use. May never be null.
    • setCharset

      @Nonnull public final XMLWriterSettings setCharset(@Nonnull Charset aCharset)
      Set the serialization charset.
      Parameters:
      aCharset - The charset to be used. May not be null.
      Returns:
      this
    • getNamespaceContext

      @Nonnull public NamespaceContext getNamespaceContext()
      Specified by:
      getNamespaceContext in interface IXMLWriterSettings
      Returns:
      The namespace context to be used. Never null.
    • setNamespaceContext

      @Nonnull public final XMLWriterSettings setNamespaceContext(@Nullable NamespaceContext aNamespaceContext)
      Set the namespace context to be used.
      Parameters:
      aNamespaceContext - The namespace context to be used. May be null.
      Returns:
      this
    • isUseDoubleQuotesForAttributes

      public boolean isUseDoubleQuotesForAttributes()
      Specified by:
      isUseDoubleQuotesForAttributes in interface IXMLWriterSettings
      Returns:
      true if attribute values should be enclosed in double quotes, false if single quotes should be used.
    • setUseDoubleQuotesForAttributes

      @Nonnull public final XMLWriterSettings setUseDoubleQuotesForAttributes(boolean bUseDoubleQuotesForAttributes)
    • getBracketModeDeterminator

      @Nonnull public IXMLBracketModeDeterminator getBracketModeDeterminator()
      Specified by:
      getBracketModeDeterminator in interface IXMLWriterSettings
      Returns:
      The dynamic (per-element) bracket mode determinator to use. This must be changed when e.g. serializing HTML. Never null .
    • setBracketModeDeterminator

      @Nonnull public final XMLWriterSettings setBracketModeDeterminator(@Nonnull IXMLBracketModeDeterminator aBracketModeDeterminator)
    • isSpaceOnSelfClosedElement

      public boolean isSpaceOnSelfClosedElement()
      Description copied from interface: IXMLWriterSettings
      Determine if a self closed element (an element having no children) should be emitted with a space at the end (<br />) or not ( <br/>)
      Specified by:
      isSpaceOnSelfClosedElement in interface IXMLWriterSettings
      Returns:
      true if a space should be emitted, false if no space should be emitted.
    • setSpaceOnSelfClosedElement

      @Nonnull public final XMLWriterSettings setSpaceOnSelfClosedElement(boolean bSpaceOnSelfClosedElement)
    • getNewLineMode

      @Nonnull public com.helger.commons.system.ENewLineMode getNewLineMode()
      Specified by:
      getNewLineMode in interface IXMLWriterSettings
      Returns:
      The newline mode to be used. By default it is the platform specific new line mode. Never null.
    • setNewLineMode

      @Nonnull public final XMLWriterSettings setNewLineMode(@Nonnull com.helger.commons.system.ENewLineMode eNewLineMode)
    • getIndentationString

      @Nonnull @Nonempty public String getIndentationString()
      Specified by:
      getIndentationString in interface IXMLWriterSettings
      Returns:
      The string to be used for indentation of a single level. By default it is 2 spaces.
    • setIndentationString

      @Nonnull public final XMLWriterSettings setIndentationString(@Nonnull @Nonempty String sIndentationString)
    • isEmitNamespaces

      public boolean isEmitNamespaces()
      Description copied from interface: IXMLWriterSettings
      Determine if namespaces should be emitted or not.
      Specified by:
      isEmitNamespaces in interface IXMLWriterSettings
      Returns:
      true if namespaces should be emitted, false if all created elements should reside in the default namespace.
    • setEmitNamespaces

      @Nonnull public final XMLWriterSettings setEmitNamespaces(boolean bEmitNamespaces)
    • isPutNamespaceContextPrefixesInRoot

      public boolean isPutNamespaceContextPrefixesInRoot()
      Description copied from interface: IXMLWriterSettings
      Check if the flag for putting all namespace prefixes specified in the namespace context should be put in the root document. This setting only has an effect if a namespace context is defined and if it implements the IIterableNamespaceContext interface!
      Specified by:
      isPutNamespaceContextPrefixesInRoot in interface IXMLWriterSettings
      Returns:
      true if it is enabled, false if not.
    • setPutNamespaceContextPrefixesInRoot

      @Nonnull public final XMLWriterSettings setPutNamespaceContextPrefixesInRoot(boolean bPutNamespaceContextPrefixesInRoot)
    • isWriteCDATAAsText

      public boolean isWriteCDATAAsText()
      Description copied from interface: IXMLWriterSettings
      Check if all CDATA children should be emitted as normal text or not. This is required for XML canonicalization.
      Specified by:
      isWriteCDATAAsText in interface IXMLWriterSettings
      Returns:
      true if CDATA should be written as text, false to keep CDATA as it is.
    • setWriteCDATAAsText

      @Nonnull public final XMLWriterSettings setWriteCDATAAsText(boolean bWriteCDATAAsText)
    • isOrderAttributesAndNamespaces

      public boolean isOrderAttributesAndNamespaces()
      Description copied from interface: IXMLWriterSettings
      Determine if attributes of elements and namespaces should be emitted in alphabetical order or not. This is required for XML canonicalization.
      Specified by:
      isOrderAttributesAndNamespaces in interface IXMLWriterSettings
      Returns:
      true to order attributes, false to keep the original order.
    • setOrderAttributesAndNamespaces

      @Nonnull public final XMLWriterSettings setOrderAttributesAndNamespaces(boolean bOrderAttributesAndNamespaces)
    • getClone

      @Nonnull public XMLWriterSettings getClone()
      Specified by:
      getClone in interface com.helger.commons.lang.ICloneable<XMLWriterSettings>
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • createForHTML4

      @Nonnull @ReturnsMutableCopy public static XMLWriterSettings createForHTML4()
    • createForXHTML

      @Nonnull @ReturnsMutableCopy public static XMLWriterSettings createForXHTML()
    • createForHTML5

      @Nonnull @ReturnsMutableCopy public static XMLWriterSettings createForHTML5()
    • createForCanonicalization

      @Nonnull @ReturnsMutableCopy public static XMLWriterSettings createForCanonicalization()
      The canonical form of an XML document is physical representation of the document produced by the method described in this specification. The changes are summarized in the following list:
      • The document is encoded in UTF-8
      • Line breaks normalized to #xA on input, before parsing
      • Attribute values are normalized, as if by a validating processor
      • Character and parsed entity references are replaced
      • CDATA sections are replaced with their character content
      • The XML declaration and document type declaration are removed
      • Empty elements are converted to start-end tag pairs
      • Whitespace outside of the document element and within start and end tags is normalized
      • All whitespace in character content is retained (excluding characters removed during line feed normalization)
      • Attribute value delimiters are set to quotation marks (double quotes)
      • Special characters in attribute values and character content are replaced by character references
      • Superfluous namespace declarations are removed from each element
      • Default attributes are added to each element
      • Fixup of xml:base attributes [C14N-Issues] is performed
      • Lexicographic order is imposed on the namespace declarations and attributes of each element
      Returns:
      XMLWriterSettings for canonicalization