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 Details

  • Constructor Details

  • Method Details

    • setThrowExceptionOnNestedComments

      public static void setThrowExceptionOnNestedComments(boolean bThrowExceptionOnNestedComments)
      Define whether nested XML comments throw an exception or not.
      Parameters:
      bThrowExceptionOnNestedComments - true to throw an exception, false to ignore nested comments.
    • isThrowExceptionOnNestedComments

      public static boolean isThrowExceptionOnNestedComments()
      Returns:
      true if 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. If null is passed, EXMLVersion.XML_10 will be used.
      sEncoding - The encoding to be used for this document. It may be null but it is strongly recommended to write a correct charset.
      eStandalone - if true this is a standalone XML document without a connection to an existing DTD or XML schema
      bWithNewLine - true to add a newline, false if 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 be null.
    • 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 be null.
      eIncorrectCharHandling - The incorrect character handling. May not be null.
      aDocType - The structure document type. May not be null.
      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 be null.
      eIncorrectCharHandling - The incorrect character handling. May not be null.
      sQualifiedName - The qualified element name. May not be null.
      sPublicID - The optional public ID. May be null. If the public ID is not null the system ID must also be set!
      sSystemID - The optional system ID. May be null.
      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 ID
      sSystemID - Document type system ID
    • onProcessingInstruction

      public void onProcessingInstruction(@Nonnull String sTarget, @Nullable String sData)
      On processing instruction
      Parameters:
      sTarget - The target
      sData - 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 array
      nOfs - Offset into the array where to start
      nLen - 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 text
      bEscape - If true the text should be XML masked (the default), false if not. The false case 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 array
      nOfs - Offset into the array where to start
      nLen - Number of chars to use, starting from the provided offset.
      bEscape - If true the text should be XML masked (the default), false if not. The false case 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 be null.
      sTagName - Tag name
      aAttrs - Optional set of attributes.
      eBracketMode - Bracket mode to use. Never null.
    • onElementEnd

      public void onElementEnd(@Nullable String sNamespacePrefix, @Nonnull String sTagName, @Nonnull EXMLSerializeBracketMode eBracketMode)
      End of an element.
      Parameters:
      sNamespacePrefix - Optional namespace prefix. May be null.
      sTagName - Tag name
      eBracketMode - Bracket mode to use. Never null.
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Object