Package com.helger.xml.serialize.write
Class XMLEmitter
java.lang.Object
com.helger.xml.serialize.write.XMLEmitter
- All Implemented Interfaces:
Flushable,AutoCloseable
Converts XML constructs into a string representation.
- Author:
- Philip Helger
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidelementAttr(String sAttrNamespacePrefix, String sAttrName, String sAttrValue) voidelementStartClose(EXMLSerializeBracketMode eBracketMode) voidelementStartOpen(String sNamespacePrefix, String sTagName) voidflush()static StringgetDocTypeHTMLRepresentation(EXMLSerializeVersion eXMLVersion, EXMLIncorrectCharacterHandling eIncorrectCharHandling, IMicroDocumentType aDocType) Get the XML representation of a document type.static StringgetDocTypeXMLRepresentation(EXMLSerializeVersion eXMLVersion, EXMLIncorrectCharacterHandling eIncorrectCharHandling, String sQualifiedName, String sPublicID, String sSystemID) Get the XML representation of a document type.static booleannewLine()voidCDATA node.voidComment node.voidonContentElementWhitespace(CharSequence aWhitespaces) Ignorable whitespace characters.voidonDocumentType(String sQualifiedElementName, String sPublicID, String sSystemID) On XML document type.voidWrite a DTD section.voidonElementEnd(String sNamespacePrefix, String sTagName, EXMLSerializeBracketMode eBracketMode) End of an element.voidonElementStart(String sNamespacePrefix, String sTagName, Map<QName, String> aAttrs, EXMLSerializeBracketMode eBracketMode) Start of an element.voidonEntityReference(String sEntityRef) On entity reference.voidonProcessingInstruction(String sTarget, String sData) On processing instructionvoidonText(char[] aText, int nOfs, int nLen) XML text node.voidonText(char[] aText, int nOfs, int nLen, boolean bEscape) Text node.voidXML text node.voidText node.voidonXMLDeclaration(EXMLVersion eXMLVersion, String sEncoding, com.helger.commons.state.ETriState eStandalone, boolean bWithNewLine) At the very beginning of the document (XML declaration).static voidsetThrowExceptionOnNestedComments(boolean bThrowExceptionOnNestedComments) Define whether nested XML comments throw an exception or not.toString()
-
Field Details
-
DEFAULT_THROW_EXCEPTION_ON_NESTED_COMMENTS
public static final boolean DEFAULT_THROW_EXCEPTION_ON_NESTED_COMMENTSBy default an exception is thrown for nested comments- See Also:
-
CDATA_START
- See Also:
-
CDATA_END
- See Also:
-
COMMENT_START
- See Also:
-
COMMENT_END
- See Also:
-
ER_START
public static final char ER_START- See Also:
-
ER_END
public static final char ER_END- See Also:
-
PI_START
- See Also:
-
PI_END
- See Also:
-
-
Constructor Details
-
XMLEmitter
-
-
Method Details
-
setThrowExceptionOnNestedComments
public static void setThrowExceptionOnNestedComments(boolean bThrowExceptionOnNestedComments) Define whether nested XML comments throw an exception or not.- Parameters:
bThrowExceptionOnNestedComments-trueto throw an exception,falseto ignore nested comments.
-
isThrowExceptionOnNestedComments
public static boolean isThrowExceptionOnNestedComments()- Returns:
trueif nested XML comments will throw an error. Default is true.
-
getXMLWriterSettings
- Returns:
- The XML writer settings as provided in the constructor. Never
null.
-
newLine
-
onXMLDeclaration
public void onXMLDeclaration(@Nullable EXMLVersion eXMLVersion, @Nullable String sEncoding, @Nonnull com.helger.commons.state.ETriState eStandalone, boolean bWithNewLine) At the very beginning of the document (XML declaration).- Parameters:
eXMLVersion- The XML version to use. Ifnullis passed,EXMLVersion.XML_10will be used.sEncoding- The encoding to be used for this document. It may benullbut it is strongly recommended to write a correct charset.eStandalone- iftruethis is a standalone XML document without a connection to an existing DTD or XML schemabWithNewLine-trueto add a newline,falseif not- Since:
- 9.2.1
-
onDTD
Write a DTD section. This string represents the entire doctypedecl production from the XML 1.0 specification.- Parameters:
sDTD- the DTD to be written. May not benull.
-
getDocTypeHTMLRepresentation
@Nonnull public static String getDocTypeHTMLRepresentation(@Nonnull EXMLSerializeVersion eXMLVersion, @Nonnull EXMLIncorrectCharacterHandling eIncorrectCharHandling, @Nonnull IMicroDocumentType aDocType) Get the XML representation of a document type.- Parameters:
eXMLVersion- The XML version to use. May not benull.eIncorrectCharHandling- The incorrect character handling. May not benull.aDocType- The structure document type. May not benull.- Returns:
- The string DOCTYPE representation.
-
getDocTypeXMLRepresentation
@Nonnull public static String getDocTypeXMLRepresentation(@Nonnull EXMLSerializeVersion eXMLVersion, @Nonnull EXMLIncorrectCharacterHandling eIncorrectCharHandling, @Nonnull String sQualifiedName, @Nullable String sPublicID, @Nullable String sSystemID) Get the XML representation of a document type.- Parameters:
eXMLVersion- The XML version to use. May not benull.eIncorrectCharHandling- The incorrect character handling. May not benull.sQualifiedName- The qualified element name. May not benull.sPublicID- The optional public ID. May benull. If the public ID is notnullthe system ID must also be set!sSystemID- The optional system ID. May benull.- Returns:
- The string DOCTYPE representation.
-
onDocumentType
public void onDocumentType(@Nonnull String sQualifiedElementName, @Nullable String sPublicID, @Nullable String sSystemID) On XML document type.- Parameters:
sQualifiedElementName- Qualified name of the root element.sPublicID- Document type public IDsSystemID- Document type system ID
-
onProcessingInstruction
On processing instruction- Parameters:
sTarget- The targetsData- The data (attributes as a string)
-
onEntityReference
On entity reference.- Parameters:
sEntityRef- The reference (without '&' and ';' !!)
-
onContentElementWhitespace
Ignorable whitespace characters.- Parameters:
aWhitespaces- The whitespace character sequence
-
onComment
Comment node.- Parameters:
sComment- The comment text
-
onText
XML text node.- Parameters:
sText- The contained text
-
onText
XML text node.- Parameters:
aText- The contained text arraynOfs- Offset into the array where to startnLen- Number of chars to use, starting from the provided offset.
-
onText
Text node.- Parameters:
sText- The contained textbEscape- Iftruethe text should be XML masked (the default),falseif not. Thefalsecase is especially interesting for HTML inline JS and CSS code.
-
onText
public void onText(@Nonnull char[] aText, @Nonnegative int nOfs, @Nonnegative int nLen, boolean bEscape) Text node.- Parameters:
aText- The contained text arraynOfs- Offset into the array where to startnLen- Number of chars to use, starting from the provided offset.bEscape- Iftruethe text should be XML masked (the default),falseif not. Thefalsecase is especially interesting for HTML inline JS and CSS code.
-
onCDATA
CDATA node.- Parameters:
sText- The contained text
-
elementStartOpen
-
elementAttr
-
elementStartClose
-
onElementStart
public void onElementStart(@Nullable String sNamespacePrefix, @Nonnull String sTagName, @Nullable Map<QName, String> aAttrs, @Nonnull EXMLSerializeBracketMode eBracketMode) Start of an element.- Parameters:
sNamespacePrefix- Optional namespace prefix. May benull.sTagName- Tag nameaAttrs- Optional set of attributes.eBracketMode- Bracket mode to use. Nevernull.
-
onElementEnd
public void onElementEnd(@Nullable String sNamespacePrefix, @Nonnull String sTagName, @Nonnull EXMLSerializeBracketMode eBracketMode) End of an element.- Parameters:
sNamespacePrefix- Optional namespace prefix. May benull.sTagName- Tag nameeBracketMode- Bracket mode to use. Nevernull.
-
flush
- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-
toString
-