Class RtfToText


  • final class RtfToText
    extends java.lang.Object
    Gets the plain text version of RTF documents.

    This is used by StyledTextTransferable to return the plain text version of the transferable when the receiver does not support RTF.

    Version:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int blockCount  
      private java.lang.StringBuilder controlWord  
      private boolean inControlWord  
      private java.io.Reader r  
      private java.lang.StringBuilder sb  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private RtfToText​(java.io.Reader r)
      Private constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.lang.String convert()
      Converts the RTF text read from this converter's Reader into plain text.
      private void endControlWord()
      Ends a control word.
      static java.lang.String getPlainText​(byte[] rtf)
      Converts the contents of the specified byte array representing an RTF document into plain text.
      static java.lang.String getPlainText​(java.io.File file)
      Converts the contents of the specified RTF file to plain text.
      static java.lang.String getPlainText​(java.io.InputStream in)
      Converts the contents of the specified input stream to plain text.
      private static java.lang.String getPlainText​(java.io.Reader r)
      Converts the contents of the specified Reader to plain text.
      static java.lang.String getPlainText​(java.lang.String rtf)
      Converts the contents of the specified String to plain text.
      private static boolean isUnicodeEscape​(java.lang.String controlWord)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • r

        private java.io.Reader r
      • sb

        private java.lang.StringBuilder sb
      • controlWord

        private java.lang.StringBuilder controlWord
      • blockCount

        private int blockCount
      • inControlWord

        private boolean inControlWord
    • Constructor Detail

      • RtfToText

        private RtfToText​(java.io.Reader r)
        Private constructor.
        Parameters:
        r - The reader to read RTF text from.
    • Method Detail

      • convert

        private java.lang.String convert()
                                  throws java.io.IOException
        Converts the RTF text read from this converter's Reader into plain text. It is the caller's responsibility to close the reader after this method is called.
        Returns:
        The plain text.
        Throws:
        java.io.IOException - If an IO error occurs.
      • endControlWord

        private void endControlWord()
        Ends a control word. Checks whether it is a common one that affects the plain text output (such as "par" or "tab") and updates the text buffer accordingly.
      • isUnicodeEscape

        private static boolean isUnicodeEscape​(java.lang.String controlWord)
      • getPlainText

        public static java.lang.String getPlainText​(byte[] rtf)
                                             throws java.io.IOException
        Converts the contents of the specified byte array representing an RTF document into plain text.
        Parameters:
        rtf - The byte array representing an RTF document.
        Returns:
        The contents of the RTF document, in plain text.
        Throws:
        java.io.IOException - If an IO error occurs.
      • getPlainText

        public static java.lang.String getPlainText​(java.io.File file)
                                             throws java.io.IOException
        Converts the contents of the specified RTF file to plain text.
        Parameters:
        file - The RTF file to convert.
        Returns:
        The contents of the file, in plain text.
        Throws:
        java.io.IOException - If an IO error occurs.
      • getPlainText

        public static java.lang.String getPlainText​(java.io.InputStream in)
                                             throws java.io.IOException
        Converts the contents of the specified input stream to plain text. The input stream will be closed when this method returns.
        Parameters:
        in - The input stream to convert. This will be closed when this method returns.
        Returns:
        The contents of the stream, in plain text.
        Throws:
        java.io.IOException - If an IO error occurs.
      • getPlainText

        private static java.lang.String getPlainText​(java.io.Reader r)
                                              throws java.io.IOException
        Converts the contents of the specified Reader to plain text.
        Parameters:
        r - The Reader. This will be closed when this method returns.
        Returns:
        The contents of the Reader, in plain text.
        Throws:
        java.io.IOException - If an IO error occurs.
      • getPlainText

        public static java.lang.String getPlainText​(java.lang.String rtf)
                                             throws java.io.IOException
        Converts the contents of the specified String to plain text.
        Parameters:
        rtf - A string whose contents represent an RTF document.
        Returns:
        The contents of the String, in plain text.
        Throws:
        java.io.IOException - If an IO error occurs.