Package com.helger.xml.serialize.write
Class XMLEmitter
- java.lang.Object
-
- com.helger.xml.serialize.write.XMLEmitter
-
- All Implemented Interfaces:
Flushable,AutoCloseable
@NotThreadSafe public class XMLEmitter extends Object implements AutoCloseable, Flushable
Converts XML constructs into a string representation.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static StringCDATA_ENDstatic StringCDATA_STARTstatic StringCOMMENT_ENDstatic StringCOMMENT_STARTstatic booleanDEFAULT_THROW_EXCEPTION_ON_NESTED_COMMENTSBy default an exception is thrown for nested commentsstatic charER_ENDstatic charER_STARTstatic StringPI_ENDstatic StringPI_START
-
Constructor Summary
Constructors Constructor Description XMLEmitter(Writer aWriter, IXMLWriterSettings aSettings)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()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.IXMLWriterSettingsgetXMLWriterSettings()static booleanisThrowExceptionOnNestedComments()XMLEmitternewLine()voidonCDATA(String sText)CDATA node.voidonComment(String sComment)Comment node.voidonContentElementWhitespace(CharSequence aWhitespaces)Ignorable whitespace characters.voidonDocumentType(String sQualifiedElementName, String sPublicID, String sSystemID)On XML document type.voidonDTD(String sDTD)Write 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.voidonText(String sText)XML text node.voidonText(String sText, boolean bEscape)Text 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.StringtoString()
-
-
-
Field Detail
-
DEFAULT_THROW_EXCEPTION_ON_NESTED_COMMENTS
public static final boolean DEFAULT_THROW_EXCEPTION_ON_NESTED_COMMENTS
By default an exception is thrown for nested comments- See Also:
- Constant Field Values
-
CDATA_START
public static final String CDATA_START
- See Also:
- Constant Field Values
-
CDATA_END
public static final String CDATA_END
- See Also:
- Constant Field Values
-
COMMENT_START
public static final String COMMENT_START
- See Also:
- Constant Field Values
-
COMMENT_END
public static final String COMMENT_END
- See Also:
- Constant Field Values
-
ER_START
public static final char ER_START
- See Also:
- Constant Field Values
-
ER_END
public static final char ER_END
- See Also:
- Constant Field Values
-
PI_START
public static final String PI_START
- See Also:
- Constant Field Values
-
PI_END
public static final String PI_END
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
XMLEmitter
public XMLEmitter(@Nonnull @WillNotClose Writer aWriter, @Nonnull IXMLWriterSettings aSettings)
-
-
Method Detail
-
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
@Nonnull public IXMLWriterSettings getXMLWriterSettings()
- Returns:
- The XML writer settings as provided in the constructor. Never
null.
-
newLine
@Nonnull public XMLEmitter 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
public void onDTD(@Nonnull String sDTD)
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
public void onProcessingInstruction(@Nonnull String sTarget, @Nullable String sData)
On processing instruction- Parameters:
sTarget- The targetsData- The data (attributes as a string)
-
onEntityReference
public void onEntityReference(@Nonnull String sEntityRef)
On entity reference.- Parameters:
sEntityRef- The reference (without '&' and ';' !!)
-
onContentElementWhitespace
public void onContentElementWhitespace(@Nullable CharSequence aWhitespaces)
Ignorable whitespace characters.- Parameters:
aWhitespaces- The whitespace character sequence
-
onComment
public void onComment(@Nullable String sComment)
Comment node.- Parameters:
sComment- The comment text
-
onText
public void onText(@Nullable String sText)
XML text node.- Parameters:
sText- The contained text
-
onText
public void onText(@Nonnull char[] aText, @Nonnegative int nOfs, @Nonnegative int nLen)
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
public void onText(@Nullable String sText, boolean bEscape)
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
public void onCDATA(@Nullable String sText)
CDATA node.- Parameters:
sText- The contained text
-
elementStartOpen
public void elementStartOpen(@Nullable String sNamespacePrefix, @Nonnull String sTagName)
-
elementAttr
public void elementAttr(@Nullable String sAttrNamespacePrefix, @Nonnull String sAttrName, @Nonnull String sAttrValue)
-
elementStartClose
public void elementStartClose(@Nonnull EXMLSerializeBracketMode eBracketMode)
-
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
public void flush() throws IOException- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-
-