javax.swing.text.rtf
Class RTFReader

java.lang.Object
  extended byjava.io.OutputStream
      extended byjavax.swing.text.rtf.AbstractFilter
          extended byjavax.swing.text.rtf.RTFParser
              extended byjavax.swing.text.rtf.RTFReader

class RTFReader
extends RTFParser

Takes a sequence of RTF tokens and text and appends the text described by the RTF to a StyledDocument (the target). The RTF is lexed from the character stream by the RTFParser which is this class's superclass. This class is an indirect subclass of OutputStream. It must be closed in order to guarantee that all of the text has been sent to the text acceptor.

See Also:
RTFParser, OutputStream

Nested Class Summary
(package private)  class RTFReader.AttributeTrackingDestination
          RTFReader.TextHandlingDestination is an abstract RTF destination which simply tracks the attributes specified by the RTF control words in internal form and can produce acceptable AttributeSets for the current character, paragraph, and section attributes.
(package private)  class RTFReader.ColortblDestination
          Reads the colortbl group.
(package private) static interface RTFReader.Destination
          An interface (could be an entirely abstract class) describing a destination.
(package private)  class RTFReader.DiscardingDestination
          This data-sink class is used to implement ignored destinations (e.g.
(package private)  class RTFReader.DocumentDestination
          RTFReader.DocumentDestination is a concrete subclass of TextHandlingDestination which appends the text to the StyledDocument given by the target ivar of the containing RTFReader.
(package private)  class RTFReader.FonttblDestination
          Reads the fonttbl group, inserting fonts into the RTFReader's fontTable dictionary.
(package private)  class RTFReader.InfoDestination
          Handles the info group.
(package private)  class RTFReader.StylesheetDestination
          Handles the stylesheet keyword.
(package private)  class RTFReader.TextHandlingDestination
          RTFReader.TextHandlingDestination provides basic text handling functionality.
 
Field Summary
(package private) static boolean[] allSpecialsTable
          A specials table which indicates that all characters are special
(package private)  ByteArrayOutputStream binaryBuf
           
(package private) static Dictionary characterSets
           
(package private)  Style[] characterStyles
          This array maps character style numbers to Style objects.
(package private)  Color[] colorTable
          This array maps color indices to Color objects.
(package private)  MutableAttributeSet documentAttributes
          This holds the current document attributes.
(package private)  Dictionary fontTable
          This Dictionary maps Integer font numbers to String font names.
(package private)  boolean ignoreGroupIfUnknownKeyword
          true to indicate that if the next keyword is unknown, the containing group should be ignored.
(package private) static char[] latin1TranslationTable
          A translation table which does ISO Latin-1 (trivial)
 int level
          The current RTF group nesting level.
private  MockAttributeSet mockery
           
(package private) static boolean[] noSpecialsTable
          A specials table which indicates that no characters are special
(package private)  Style[] paragraphStyles
          This array maps paragraph style numbers to Style objects.
(package private)  Dictionary parserState
          Miscellaneous information about the parser's state.
(package private)  RTFReader.Destination rtfDestination
          This is the "dst" item from parserState. rtfDestination is the current rtf destination.
(package private) static boolean[] rtfSpecialsTable
           
(package private)  int rtfversion
          This is the RTF version number, extracted from the \rtf keyword.
(package private)  Style[] sectionStyles
          This array maps section style numbers to Style objects.
(package private)  int skippingCharacters
          The parameter of the most recently parsed \\ucN keyword, used for skipping alternative representations after a Unicode character.
protected  boolean[] specialsTable
          A table indicating which byte values should be interpreted as characters and which should be treated as formatting codes
private static Dictionary straightforwardAttributes
           
(package private) static String TabAlignmentKey
           
(package private) static String TabLeaderKey
           
(package private)  StyledDocument target
          The object to which the parsed text is sent.
(package private) static Dictionary textKeywords
          textKeywords maps RTF keywords to single-character strings, for those keywords which simply insert some text.
protected  char[] translationTable
          A table mapping bytes to characters
(package private) static boolean useNeXTForAnsi
           
protected  PrintStream warnings
          A stream to which to write warnings and debugging information while parsing.
 
Constructor Summary
RTFReader(StyledDocument destination)
          Creates a new RTFReader instance.
 
Method Summary
 void begingroup()
          Called by the superclass when a new RTF group is begun.
 void close()
          Called by the user when there is no more input (i.e.
(package private)  Color defaultColor()
          The default color for text which has no specified color.
static void defineCharacterSet(String name, char[] table)
          Adds a character set to the RTFReader's list of known character sets
 void endgroup()
          Called by the superclass when the current RTF group is closed.
 void flush()
          Flushes any buffered but not yet written characters.
static Object getCharacterSet(String name)
          Looks up a named character set.
 void handleBinaryBlob(byte[] data)
          Called when the RTFParser encounters a bin keyword in the RTF stream.
 boolean handleKeyword(String keyword)
          Handles a parameterless RTF keyword.
 boolean handleKeyword(String keyword, int parameter)
          Handles an RTF keyword and its integer parameter.
 void handleText(char ch)
           
 void handleText(String text)
          Handles any pure text (containing no control characters) in the input stream.
(package private) static char[] readCharset(InputStream strm)
          Parses a character set from an InputStream.
(package private) static char[] readCharset(URL href)
           
 void readFromReader(Reader in)
           
 void readFromStream(InputStream in)
          A convenience method that reads text from a FileInputStream and writes it to the receiver.
 void setCharacterSet(String name)
          setCharacterSet sets the current translation table to correspond with the named character set.
protected  void setRTFDestination(RTFReader.Destination newDestination)
           
private  void setTargetAttribute(String name, Object value)
           
protected  void warning(String s)
           
 void write(byte[] buf, int off, int len)
          Implements the buffer-at-a-time write method for greater efficiency.
 void write(char ch)
          Subclasses must provide an implementation of this method which accepts a single (non-special) character.
 void write(int b)
          Implements the abstract method of OutputStream, of which this class is a subclass.
 void write(String s)
          Hopefully, all subclasses will override this method to accept strings of text, but if they don't, AbstractFilter's implementation will spoon-feed them via write(char).
 void writeSpecial(int b)
          Subclasses must provide an implementation of this method which accepts a single special byte.
 
Methods inherited from class java.io.OutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

target

StyledDocument target
The object to which the parsed text is sent.


parserState

Dictionary parserState
Miscellaneous information about the parser's state. This dictionary is saved and restored when an RTF group begins or ends.


rtfDestination

RTFReader.Destination rtfDestination
This is the "dst" item from parserState. rtfDestination is the current rtf destination. It is cached in an instance variable for speed.


documentAttributes

MutableAttributeSet documentAttributes
This holds the current document attributes.


fontTable

Dictionary fontTable
This Dictionary maps Integer font numbers to String font names.


colorTable

Color[] colorTable
This array maps color indices to Color objects.


characterStyles

Style[] characterStyles
This array maps character style numbers to Style objects.


paragraphStyles

Style[] paragraphStyles
This array maps paragraph style numbers to Style objects.


sectionStyles

Style[] sectionStyles
This array maps section style numbers to Style objects.


rtfversion

int rtfversion
This is the RTF version number, extracted from the \rtf keyword. The version information is currently not used.


ignoreGroupIfUnknownKeyword

boolean ignoreGroupIfUnknownKeyword
true to indicate that if the next keyword is unknown, the containing group should be ignored.


skippingCharacters

int skippingCharacters
The parameter of the most recently parsed \\ucN keyword, used for skipping alternative representations after a Unicode character.


straightforwardAttributes

private static Dictionary straightforwardAttributes

mockery

private MockAttributeSet mockery

textKeywords

static Dictionary textKeywords
textKeywords maps RTF keywords to single-character strings, for those keywords which simply insert some text.


TabAlignmentKey

static final String TabAlignmentKey
See Also:
Constant Field Values

TabLeaderKey

static final String TabLeaderKey
See Also:
Constant Field Values

characterSets

static Dictionary characterSets

useNeXTForAnsi

static boolean useNeXTForAnsi

level

public int level
The current RTF group nesting level.


binaryBuf

ByteArrayOutputStream binaryBuf

warnings

protected PrintStream warnings
A stream to which to write warnings and debugging information while parsing. This is set to System.out to log any anomalous information to stdout.


rtfSpecialsTable

static final boolean[] rtfSpecialsTable

translationTable

protected char[] translationTable
A table mapping bytes to characters


specialsTable

protected boolean[] specialsTable
A table indicating which byte values should be interpreted as characters and which should be treated as formatting codes


latin1TranslationTable

static final char[] latin1TranslationTable
A translation table which does ISO Latin-1 (trivial)


noSpecialsTable

static final boolean[] noSpecialsTable
A specials table which indicates that no characters are special


allSpecialsTable

static final boolean[] allSpecialsTable
A specials table which indicates that all characters are special

Constructor Detail

RTFReader

public RTFReader(StyledDocument destination)
Creates a new RTFReader instance. Text will be sent to the specified TextAcceptor.

Parameters:
destination - The TextAcceptor which is to receive the text.
Method Detail

handleBinaryBlob

public void handleBinaryBlob(byte[] data)
Called when the RTFParser encounters a bin keyword in the RTF stream.

Specified by:
handleBinaryBlob in class RTFParser
See Also:
RTFParser

handleText

public void handleText(String text)
Handles any pure text (containing no control characters) in the input stream. Called by the superclass.

Specified by:
handleText in class RTFParser

defaultColor

Color defaultColor()
The default color for text which has no specified color.


begingroup

public void begingroup()
Called by the superclass when a new RTF group is begun. This implementation saves the current parserState, and gives the current destination a chance to save its own state.

Specified by:
begingroup in class RTFParser
See Also:
RTFParser.begingroup()

endgroup

public void endgroup()
Called by the superclass when the current RTF group is closed. This restores the parserState saved by begingroup() as well as invoking the endgroup method of the current destination.

Specified by:
endgroup in class RTFParser
See Also:
RTFParser.endgroup()

setRTFDestination

protected void setRTFDestination(RTFReader.Destination newDestination)

close

public void close()
           throws IOException
Called by the user when there is no more input (i.e., at the end of the RTF file.)

Overrides:
close in class RTFParser
Throws:
IOException
See Also:
OutputStream.close()

handleKeyword

public boolean handleKeyword(String keyword)
Handles a parameterless RTF keyword. This is called by the superclass (RTFParser) when a keyword is found in the input stream.

Specified by:
handleKeyword in class RTFParser
See Also:
RTFParser.handleKeyword(java.lang.String)

handleKeyword

public boolean handleKeyword(String keyword,
                             int parameter)
Handles an RTF keyword and its integer parameter. This is called by the superclass (RTFParser) when a keyword is found in the input stream.

Specified by:
handleKeyword in class RTFParser
Parameters:
keyword - The keyword, as with handleKeyword(String).
parameter - The parameter following the keyword.
See Also:
RTFParser.handleKeyword(java.lang.String)

setTargetAttribute

private void setTargetAttribute(String name,
                                Object value)

setCharacterSet

public void setCharacterSet(String name)
setCharacterSet sets the current translation table to correspond with the named character set. The character set is loaded if necessary.

See Also:
AbstractFilter

defineCharacterSet

public static void defineCharacterSet(String name,
                                      char[] table)
Adds a character set to the RTFReader's list of known character sets


getCharacterSet

public static Object getCharacterSet(String name)
                              throws IOException
Looks up a named character set. A character set is a 256-entry array of characters, mapping unsigned byte values to their Unicode equivalents. The character set is loaded if necessary.

Throws:
IOException

readCharset

static char[] readCharset(InputStream strm)
                   throws IOException
Parses a character set from an InputStream. The character set must contain 256 decimal integers, separated by whitespace, with no punctuation. B- and C- style comments are allowed.

Throws:
IOException

readCharset

static char[] readCharset(URL href)
                   throws IOException
Throws:
IOException

handleText

public void handleText(char ch)

writeSpecial

public void writeSpecial(int b)
                  throws IOException
Description copied from class: AbstractFilter
Subclasses must provide an implementation of this method which accepts a single special byte. No translation is performed on specials.

Specified by:
writeSpecial in class AbstractFilter
Parameters:
b - The byte written to the OutputStream.
Throws:
IOException

warning

protected void warning(String s)

write

public void write(String s)
           throws IOException
Description copied from class: AbstractFilter
Hopefully, all subclasses will override this method to accept strings of text, but if they don't, AbstractFilter's implementation will spoon-feed them via write(char).

Overrides:
write in class AbstractFilter
Parameters:
s - The string of non-special characters written to the OutputStream.
Throws:
IOException

write

public void write(char ch)
           throws IOException
Description copied from class: AbstractFilter
Subclasses must provide an implementation of this method which accepts a single (non-special) character.

Specified by:
write in class AbstractFilter
Parameters:
ch - The character written to the OutputStream.
Throws:
IOException

flush

public void flush()
           throws IOException
Flushes any buffered but not yet written characters. Subclasses which override this method should call this method before flushing any of their own buffers.

Overrides:
flush in class OutputStream
Throws:
IOException - if an I/O error occurs.

readFromStream

public void readFromStream(InputStream in)
                    throws IOException
A convenience method that reads text from a FileInputStream and writes it to the receiver. The format in which the file is read is determined by the concrete subclass of AbstractFilter to which this method is sent.

This method does not close the receiver after reaching EOF on the input stream. The user must call close() to ensure that all data are processed.

Parameters:
in - An InputStream providing text.
Throws:
IOException

readFromReader

public void readFromReader(Reader in)
                    throws IOException
Throws:
IOException

write

public void write(int b)
           throws IOException
Implements the abstract method of OutputStream, of which this class is a subclass.

Specified by:
write in class OutputStream
Parameters:
b - the byte.
Throws:
IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.

write

public void write(byte[] buf,
                  int off,
                  int len)
           throws IOException
Implements the buffer-at-a-time write method for greater efficiency.

PENDING: Does write(byte[]) call write(byte[], int, int) or is it the other way around?

Overrides:
write in class OutputStream
Parameters:
buf - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
IOException - if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.