Package com.helger.xml.serialize.write
Class XMLWriterSettings
- java.lang.Object
-
- com.helger.xml.serialize.write.XMLWriterSettings
-
- All Implemented Interfaces:
com.helger.commons.lang.ICloneable<XMLWriterSettings>,IXMLWriterSettings,Serializable
@NotThreadSafe public class XMLWriterSettings extends Object implements IXMLWriterSettings, com.helger.commons.lang.ICloneable<XMLWriterSettings>
Default implementation of theIXMLWriterSettingsinterface.
Describes the export settings for the MicroWriter. Defaults to indented and aligned XML in the UTF-8 charset.- Author:
- Philip Helger
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEFAULT_EMIT_NAMESPACESBy default namespaces are written.static EXMLIncorrectCharacterHandlingDEFAULT_INCORRECT_CHARACTER_HANDLINGBy default invalid characters are not written and a log message is emittedstatic StringDEFAULT_INDENTATION_STRINGBy default indentation happens with 2 spacesstatic booleanDEFAULT_NEW_LINE_AFTER_XML_DECLARATIONBy default a newline character is emitted afterwardsstatic booleanDEFAULT_ORDER_ATTRIBUTES_AND_NAMESPACESBy default the insertion order of attributes is maintainedstatic booleanDEFAULT_PUT_NAMESPACE_CONTEXT_PREFIXES_IN_ROOTBy default namespace context prefixes are put inside the root elementstatic EXMLSerializeCommentsDEFAULT_SERIALIZE_COMMENTSBy default comments are emittedstatic EXMLSerializeDocTypeDEFAULT_SERIALIZE_DOC_TYPEBy default the document type is emittedstatic EXMLSerializeIndentDEFAULT_SERIALIZE_INDENTBy default the output is indented and aligned (newlines are added)static EXMLSerializeXMLDeclarationDEFAULT_SERIALIZE_XML_DECLARATIONBy default all fields of the XML declaration are emittedstatic booleanDEFAULT_SPACE_ON_SELF_CLOSED_ELEMENTBy default a leading space is inserted before a self closed element (e.g.static booleanDEFAULT_USE_DOUBLE_QUOTES_FOR_ATTRIBUTESBy default double quotes are used to wrap attribute valuesstatic booleanDEFAULT_WRITE_CDATA_AS_TEXTBy default CDATA children are emitted as CDATAstatic StringDEFAULT_XML_CHARSETThe default charset is UTF-8static CharsetDEFAULT_XML_CHARSET_OBJThe default charset is UTF-8static EXMLSerializeVersionDEFAULT_XML_SERIALIZE_VERSIONDefault default XML version is 1.0static IXMLWriterSettingsDEFAULT_XML_SETTINGSThe default settings to use - last constant
-
Constructor Summary
Constructors Constructor Description 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 contextXMLWriterSettings(IXMLWriterSettings aOther)Copy constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static XMLWriterSettingscreateForCanonicalization()The canonical form of an XML document is physical representation of the document produced by the method described in this specification.static XMLWriterSettingscreateForHTML4()static XMLWriterSettingscreateForHTML5()static XMLWriterSettingscreateForXHTML()booleanequals(Object o)IXMLBracketModeDeterminatorgetBracketModeDeterminator()CharsetgetCharset()XMLWriterSettingsgetClone()EXMLIncorrectCharacterHandlinggetIncorrectCharacterHandling()EXMLSerializeIndentgetIndent()StringgetIndentationString()IXMLIndentDeterminatorgetIndentDeterminator()INamespaceContextgetNamespaceContext()com.helger.commons.system.ENewLineModegetNewLineMode()EXMLSerializeCommentsgetSerializeComments()EXMLSerializeDocTypegetSerializeDocType()EXMLSerializeVersiongetSerializeVersion()EXMLSerializeXMLDeclarationgetSerializeXMLDeclaration()EXMLVersiongetXMLVersion()inthashCode()booleanisEmitNamespaces()Determine if namespaces should be emitted or not.booleanisNewLineAfterXMLDeclaration()booleanisOrderAttributesAndNamespaces()Determine if attributes of elements and namespaces should be emitted in alphabetical order or not.booleanisPutNamespaceContextPrefixesInRoot()Check if the flag for putting all namespace prefixes specified in the namespace context should be put in the root document.booleanisSpaceOnSelfClosedElement()Determine if a self closed element (an element having no children) should be emitted with a space at the end (<br />) or not (<br/>)booleanisUseDoubleQuotesForAttributes()booleanisWriteCDATAAsText()Check if all CDATA children should be emitted as normal text or not.XMLWriterSettingssetBracketModeDeterminator(IXMLBracketModeDeterminator aBracketModeDeterminator)XMLWriterSettingssetCharset(Charset aCharset)Set the serialization charset.XMLWriterSettingssetEmitNamespaces(boolean bEmitNamespaces)XMLWriterSettingssetIncorrectCharacterHandling(EXMLIncorrectCharacterHandling eIncorrectCharacterHandling)Set the way how to handle invalid characters.XMLWriterSettingssetIndent(EXMLSerializeIndent eIndent)Set the way how to indent/alignXMLWriterSettingssetIndentationString(String sIndentationString)XMLWriterSettingssetIndentDeterminator(IXMLIndentDeterminator aIndentDeterminator)Set the dynamic (per-element) indent determinator to be used.XMLWriterSettingssetNamespaceContext(INamespaceContext aNamespaceContext)Set the namespace context to be used.XMLWriterSettingssetNewLineAfterXMLDeclaration(boolean bNewLineAfterXMLDeclaration)Change whether a newline should be printed after the XML declaration or not.XMLWriterSettingssetNewLineMode(com.helger.commons.system.ENewLineMode eNewLineMode)XMLWriterSettingssetOrderAttributesAndNamespaces(boolean bOrderAttributesAndNamespaces)XMLWriterSettingssetPutNamespaceContextPrefixesInRoot(boolean bPutNamespaceContextPrefixesInRoot)XMLWriterSettingssetSerializeComments(EXMLSerializeComments eSerializeComments)Set the way how comments should be handled.XMLWriterSettingssetSerializeDocType(EXMLSerializeDocType eSerializeDocType)Set the way how to handle the doc type.XMLWriterSettingssetSerializeVersion(EXMLSerializeVersion eSerializeVersion)Set the preferred XML version to use.XMLWriterSettingssetSerializeXMLDeclaration(EXMLSerializeXMLDeclaration eSerializeXMLDecl)Set the way how to handle the XML declaration.XMLWriterSettingssetSpaceOnSelfClosedElement(boolean bSpaceOnSelfClosedElement)XMLWriterSettingssetUseDoubleQuotesForAttributes(boolean bUseDoubleQuotesForAttributes)XMLWriterSettingssetWriteCDATAAsText(boolean bWriteCDATAAsText)StringtoString()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.helger.xml.serialize.write.IXMLWriterSettings
getNewLineString
-
-
-
-
Field Detail
-
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:
- Constant Field Values
-
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:
- Constant Field Values
-
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:
- Constant Field Values
-
DEFAULT_INDENTATION_STRING
public static final String DEFAULT_INDENTATION_STRING
By default indentation happens with 2 spaces- See Also:
- Constant Field Values
-
DEFAULT_EMIT_NAMESPACES
public static final boolean DEFAULT_EMIT_NAMESPACES
By default namespaces are written.- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
DEFAULT_WRITE_CDATA_AS_TEXT
public static final boolean DEFAULT_WRITE_CDATA_AS_TEXT
By default CDATA children are emitted as CDATA- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
DEFAULT_XML_SETTINGS
public static final IXMLWriterSettings DEFAULT_XML_SETTINGS
The default settings to use - last constant
-
-
Constructor Detail
-
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 benull.
-
-
Method Detail
-
getXMLVersion
@Nonnull public EXMLVersion getXMLVersion()
- Specified by:
getXMLVersionin interfaceIXMLWriterSettings- Returns:
- The XML version to use. Default is
1.0
-
getSerializeVersion
@Nonnull public EXMLSerializeVersion getSerializeVersion()
- Specified by:
getSerializeVersionin interfaceIXMLWriterSettings- 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 benull.- Returns:
- this
-
getSerializeXMLDeclaration
@Nonnull public EXMLSerializeXMLDeclaration getSerializeXMLDeclaration()
- Specified by:
getSerializeXMLDeclarationin interfaceIXMLWriterSettings- 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 benull.- Returns:
- this
-
isNewLineAfterXMLDeclaration
public boolean isNewLineAfterXMLDeclaration()
- Specified by:
isNewLineAfterXMLDeclarationin interfaceIXMLWriterSettings- Returns:
trueif a newline should be added after the XML declaration or not. Defaults totrue.
-
setNewLineAfterXMLDeclaration
@Nonnull public final XMLWriterSettings setNewLineAfterXMLDeclaration(boolean bNewLineAfterXMLDeclaration)
Change whether a newline should be printed after the XML declaration or not.- Parameters:
bNewLineAfterXMLDeclaration-trueto print it,falseto not print a new line.- Returns:
- this for chaining
-
getSerializeDocType
@Nonnull public EXMLSerializeDocType getSerializeDocType()
- Specified by:
getSerializeDocTypein interfaceIXMLWriterSettings- 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 benull.- Returns:
- this
-
getSerializeComments
@Nonnull public EXMLSerializeComments getSerializeComments()
- Specified by:
getSerializeCommentsin interfaceIXMLWriterSettings- 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 benull.- Returns:
- this
-
getIndent
@Nonnull public EXMLSerializeIndent getIndent()
- Specified by:
getIndentin interfaceIXMLWriterSettings- 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 benull.- Returns:
- this
-
getIndentDeterminator
@Nonnull public IXMLIndentDeterminator getIndentDeterminator()
- Specified by:
getIndentDeterminatorin interfaceIXMLWriterSettings- 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 benull.- Returns:
- this
-
getIncorrectCharacterHandling
@Nonnull public EXMLIncorrectCharacterHandling getIncorrectCharacterHandling()
- Specified by:
getIncorrectCharacterHandlingin interfaceIXMLWriterSettings- 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 benull.- Returns:
- this
-
getCharset
@Nonnull public Charset getCharset()
- Specified by:
getCharsetin interfaceIXMLWriterSettings- 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 benull.- Returns:
- this
-
getNamespaceContext
@Nonnull public INamespaceContext getNamespaceContext()
- Specified by:
getNamespaceContextin interfaceIXMLWriterSettings- Returns:
- The namespace context to be used. Never
null.
-
setNamespaceContext
@Nonnull public final XMLWriterSettings setNamespaceContext(@Nullable INamespaceContext aNamespaceContext)
Set the namespace context to be used.- Parameters:
aNamespaceContext- The namespace context to be used. May benull.- Returns:
- this
-
isUseDoubleQuotesForAttributes
public boolean isUseDoubleQuotesForAttributes()
- Specified by:
isUseDoubleQuotesForAttributesin interfaceIXMLWriterSettings- Returns:
trueif attribute values should be enclosed in double quotes,falseif single quotes should be used.
-
setUseDoubleQuotesForAttributes
@Nonnull public final XMLWriterSettings setUseDoubleQuotesForAttributes(boolean bUseDoubleQuotesForAttributes)
-
getBracketModeDeterminator
@Nonnull public IXMLBracketModeDeterminator getBracketModeDeterminator()
- Specified by:
getBracketModeDeterminatorin interfaceIXMLWriterSettings- 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:IXMLWriterSettingsDetermine 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:
isSpaceOnSelfClosedElementin interfaceIXMLWriterSettings- Returns:
trueif a space should be emitted,falseif no space should be emitted.
-
setSpaceOnSelfClosedElement
@Nonnull public final XMLWriterSettings setSpaceOnSelfClosedElement(boolean bSpaceOnSelfClosedElement)
-
getNewLineMode
@Nonnull public com.helger.commons.system.ENewLineMode getNewLineMode()
- Specified by:
getNewLineModein interfaceIXMLWriterSettings- 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:
getIndentationStringin interfaceIXMLWriterSettings- 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:IXMLWriterSettingsDetermine if namespaces should be emitted or not.- Specified by:
isEmitNamespacesin interfaceIXMLWriterSettings- Returns:
trueif namespaces should be emitted,falseif 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:IXMLWriterSettingsCheck 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 theIIterableNamespaceContextinterface!- Specified by:
isPutNamespaceContextPrefixesInRootin interfaceIXMLWriterSettings- Returns:
trueif it is enabled,falseif not.
-
setPutNamespaceContextPrefixesInRoot
@Nonnull public final XMLWriterSettings setPutNamespaceContextPrefixesInRoot(boolean bPutNamespaceContextPrefixesInRoot)
-
isWriteCDATAAsText
public boolean isWriteCDATAAsText()
Description copied from interface:IXMLWriterSettingsCheck if all CDATA children should be emitted as normal text or not. This is required for XML canonicalization.- Specified by:
isWriteCDATAAsTextin interfaceIXMLWriterSettings- Returns:
trueif CDATA should be written as text,falseto keep CDATA as it is.
-
setWriteCDATAAsText
@Nonnull public final XMLWriterSettings setWriteCDATAAsText(boolean bWriteCDATAAsText)
-
isOrderAttributesAndNamespaces
public boolean isOrderAttributesAndNamespaces()
Description copied from interface:IXMLWriterSettingsDetermine if attributes of elements and namespaces should be emitted in alphabetical order or not. This is required for XML canonicalization.- Specified by:
isOrderAttributesAndNamespacesin interfaceIXMLWriterSettings- Returns:
trueto order attributes,falseto keep the original order.
-
setOrderAttributesAndNamespaces
@Nonnull public final XMLWriterSettings setOrderAttributesAndNamespaces(boolean bOrderAttributesAndNamespaces)
-
getClone
@Nonnull public XMLWriterSettings getClone()
- Specified by:
getClonein interfacecom.helger.commons.lang.ICloneable<XMLWriterSettings>
-
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:
XMLWriterSettingsfor canonicalization
-
-