Package com.inet.html

Class InetHtmlWriter


public class InetHtmlWriter extends AbstractWriter
Write the InetHtmlDocument as text/plain. Some protected method have the same signature like the Sun javax.swing.text.html.HTMLWriter to make it easer to port to this implementation.
As a 'special feature' this writer can replace image source links, which is useful for e-mails for instance.
  • Constructor Details

    • InetHtmlWriter

      public InetHtmlWriter(Writer out, Element element)
      Create a new InetHtmlWriter, which writes the contents of a certain element.
      Parameters:
      out - any Writer
      element - the element to be written, must not be null
      Since:
      1.05
    • InetHtmlWriter

      public InetHtmlWriter(Writer out, Document doc, int pos, int len)
      Create a new InetHtmlWriter
      Parameters:
      out - any Writer
      doc - a HTML4Document
      pos - the start position of the document that should be written.
      len - the length of data that should be written
      Since:
      1.05
  • Method Details

    • write

      public void write() throws BadLocationException, IOException
      Specified by:
      write in class AbstractWriter
      Throws:
      BadLocationException
      IOException
    • emptyTag

      protected void emptyTag(Element elem) throws BadLocationException, IOException
      Write a single Element that have never childs like <BR>. This method is API compatible with javax.swing.text.html.HTMLWriter.
      Parameters:
      elem - the Element that should be write
      Throws:
      BadLocationException - should not occur
      IOException - on any I/O error with the Writer
      Since:
      1.05
    • isBlockTag

      protected boolean isBlockTag(Element elem)
      Test if the current Element is a block element.
      Parameters:
      elem - the element to check
      Returns:
      true if it is a block.
      Since:
      1.05
    • startTag

      protected boolean startTag(Element elem) throws IOException
      Write the start tag of an element that can span multiple elements.
      Parameters:
      elem - the Element that should be write
      Returns:
      true, if the start-tag was HEAD and writeHead was called, false otherwise
      Throws:
      IOException - on any I/O error with the Writer
      Since:
      1.05
    • endTag

      protected void endTag(Element elem) throws IOException
      Write the start tag of an element that can span multiple elements. This method is API compatible with javax.swing.text.html.HTMLWriter.
      Parameters:
      elem - the Element that should be write
      Throws:
      IOException - on any I/O error with the Writer
      Since:
      1.05
    • writeAttributes

      protected void writeAttributes(AttributeSet attrs) throws IOException
      Write the Attributes inside of a tag.
      Overrides:
      writeAttributes in class AbstractWriter
      Parameters:
      attrs - the attributes of the Element
      Throws:
      IOException - on any I/O error with the Writer
      Since:
      1.05
    • output

      protected void output(char[] chars, int start, int length) throws IOException
      Overrides:
      output in class AbstractWriter
      Throws:
      IOException
    • registerImageSRCreplace

      public void registerImageSRCreplace(Map<String,String> imageReplaceMap)
      Registers an image SRC replace map at this writer. If the SRC-location of an image element is registered in this map it will be replaced before it is written. Useful to redirect image locations for embedded images.
      Parameters:
      imageReplaceMap - Key=original location, Value=new location
      Since:
      1.05
    • registerLinkHREFreplace

      public void registerLinkHREFreplace(Map<String,String> hrefReplaceMap)
      Registers an <a>-href replace map at this writer. If the HREF-location of an element is registered in this map it will be replaced before it is written. Useful to redirect links to local documents.
      Parameters:
      hrefReplaceMap - Key=original location, Value=new location
      Since:
      1.12
    • registerAttributeFilter

      public void registerAttributeFilter(InetHtmlWriter.AttributeFilter filter)
      Registers the filter to be used when writing CSS and HTML attributes
      Parameters:
      filter - the filter to be used, null for none
      Since:
      1.12
    • getEndOffset

      public int getEndOffset()
      Overrides:
      getEndOffset in class AbstractWriter
    • setSorted

      public static void setSorted(boolean sorted)
      Enable or disable the attribute sorting of the writer. Since writers are create internally on demand this option is static for all InetHtmlWriter on the current VM! Note: This will
      Parameters:
      sorted - true, if the attributes should be sorted
      Since:
      1.05
    • setInlineMode

      public void setInlineMode(boolean inlineMode)
      Sets the writer to inline-CSS mode. In this mode all linked stylesheets and the script tag will be dropped. Instead the effective styles will be written inline into each element. That way the writer generates an monolithic but style-reference free html output.
      Parameters:
      inlineMode - true to activate the inline mode, false to write a normal html content
      Since:
      1.05
    • setAbsolutePathMode

      public void setAbsolutePathMode(boolean absolutePathMode)
      Advised the writer to convert all relative pathes in the document to absolute pathes. This will allow external HTML renderers to resolve the URL as well. But use this mode wiht caution: if the URLs point to a local file or localhost, the output will not be portable anymore.
      Requires a base-URL to be set for the document to work!
      Parameters:
      absolutePathMode - if true, all relative URLs will be written as absulte ones
      Since:
      1.05
    • setAllowStyleSpan

      public void setAllowStyleSpan(boolean allowStyleSpan)
      Allows or prohibits the style SPAN on partial or element based write operation. Default is true.
      By default: in case of a partial copy the style inheritance and CSS selector hierarchy is broken. To get the correct styles in this case, a SPAN is added as root of the copied elements to replace the missing parent and to allow a correct inheritance of styles.
      Parameters:
      allowStyleSpan - true to allow the root SPAN, false to get the row element structure
      Since:
      1.05
    • setWriteSelectedElementOnly

      public void setWriteSelectedElementOnly(boolean writeSelectedElementOnly)
      Defines whether or not to write only the selected element or a full HTML document with the selected element as content.
      NOTE: Although this flag can be used in a any case, it's most useful when combined with inline mode and style container element to write a independent and complete HTML snippet.
      Parameters:
      writeSelectedElementOnly - if true only the selected element will be written, no implicit implicit HTML, HEAD or BODY element will be created unless it's the element to write. If false the HTML, HEAD and BODY elements will always be written(or created) to create a valid HTML document. Default is false
      Since:
      1.13
    • setWriteIfNoTextContent

      public void setWriteIfNoTextContent(boolean writeIfNoTextContent)
      Defines whether or not to write the full DOM in case there is no actual text content. E.g. the document may have an empty DIV element which is styles to display a background image. This would have a rendering appearance but since the InetHtmlDocument.getLength() would yield 0, the writer would write nothing. This flag defines whether or not do apply this optimization.
      NOTE: A completely empty BODY element will result in nothing to be written in any case
      The default is false.
      Parameters:
      writeIfNoTextContent - true to write DOM with no text content, false to write nothing if InetHtmlDocument.getLength() is 0.
      Since:
      1.13
    • setStyleContainer

      public void setStyleContainer(HTML.Tag styleContainer)
      Defines the style container element to be used when writing with inline mode. Default is HTML.Tag.SPAN
      By default: in case of a partial copy the style inheritance and CSS selector hierarchy is broken. To get the correct styles in this case, a container element is added as root of the copied elements to replace the missing parent and to allow a correct inheritance of styles.
      NOTE: There is a major difference between inline an block level elements for the container! While it has no effect on the inheritance of styles, it will affect the rendering. Since the line-height is taken from the latest block level element, an inline element as style container will not set the correct line height for anonymous elements. So if inline is not strictly required, use a HTML.Tag.DIV here.
      Parameters:
      styleContainer - the container element to be used, null to skip the artificial container element
      Since:
      1.12
    • getTagWritingOptions

      public Map<HTML.Tag,Boolean> getTagWritingOptions()
      Returns the map with the Tags we do not want written
      Returns:
      the map with the Tags we do not want written
      Since:
      1.09
    • setTagWritingOptions

      public void setTagWritingOptions(Map<HTML.Tag,Boolean> tagWritingOptionsMap)
      Set the map with the Tags we do not want written (if null everything will be written)
      Parameters:
      tagWritingOptionsMap - he map with the Tags we do not want written
      Since:
      1.09
    • setTrustedImagePath

      public void setTrustedImagePath(boolean trustedImagePath)
      Sets whether the image paths in the image map of registerImageSRCreplace(Map) already encoded; So if true the paths will not be URL path encoded. This may lead to a corrupted file in case the paths are not properly encoded. When in doubt, set to false or don't use this method.
      Parameters:
      trustedImagePath - true if paths are already encoded, hence false
    • setEmptyBlockFiller

      public void setEmptyBlockFiller(String emptyBlockFiller)
      Sets the string to be written to an empty block (if activated by the document configuration). By default the fill string is ' ' but some HTML editors use '
      ' instead.
      Parameters:
      emptyBlockFiller - the string to write to an empty block, null will be interpreted as an empty string
      Since:
      1.12