javax.swing.text.rtf
Class RTFParser

java.lang.Object
  extended byjava.io.OutputStream
      extended byjavax.swing.text.rtf.AbstractFilter
          extended byjavax.swing.text.rtf.RTFParser
Direct Known Subclasses:
RTFReader

abstract class RTFParser
extends AbstractFilter

RTFParser is a subclass of AbstractFilter which understands basic RTF syntax and passes a stream of control words, text, and begin/end group indications to its subclass. Normally programmers will only use RTFFilter, a subclass of this class that knows what to do with the tokens this class parses.

See Also:
AbstractFilter, RTFFilter

Field Summary
(package private) static boolean[] allSpecialsTable
          A specials table which indicates that all characters are special
(package private)  ByteArrayOutputStream binaryBuf
           
private  long binaryBytesLeft
           
private  StringBuffer currentCharacters
           
(package private) static char[] latin1TranslationTable
          A translation table which does ISO Latin-1 (trivial)
 int level
          The current RTF group nesting level.
(package private) static boolean[] noSpecialsTable
          A specials table which indicates that no characters are special
private  int pendingCharacter
           
private  String pendingKeyword
           
(package private) static boolean[] rtfSpecialsTable
           
private  int S_aftertick
           
private  int S_aftertickc
           
private  int S_backslashed
           
private  int S_inblob
           
private  int S_parameter
           
private  int S_text
           
private  int S_token
           
private  boolean[] savedSpecials
           
protected  boolean[] specialsTable
          A table indicating which byte values should be interpreted as characters and which should be treated as formatting codes
private  int state
           
protected  char[] translationTable
          A table mapping bytes to characters
protected  PrintStream warnings
          A stream to which to write warnings and debugging information while parsing.
 
Constructor Summary
RTFParser()
           
 
Method Summary
abstract  void begingroup()
          Implemented by subclasses to react to an increase in the nesting level.
 void close()
          Closes the parser.
abstract  void endgroup()
          Implemented by subclasses to react to the end of a group.
 void flush()
          Flushes any buffered but not yet written characters.
abstract  void handleBinaryBlob(byte[] data)
          Implemented by subclasses to handle the contents of the \bin keyword.
abstract  boolean handleKeyword(String keyword)
          Implemented by subclasses to interpret a parameter-less RTF keyword.
abstract  boolean handleKeyword(String keyword, int parameter)
          Implemented by subclasses to interpret a keyword with a parameter.
 void handleText(char ch)
           
abstract  void handleText(String text)
          Implemented by subclasses to interpret text from the RTF stream.
 void readFromReader(Reader in)
           
 void readFromStream(InputStream in)
          A convenience method that reads text from a FileInputStream and writes it to the receiver.
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

level

public int level
The current RTF group nesting level.


state

private int state

currentCharacters

private StringBuffer currentCharacters

pendingKeyword

private String pendingKeyword

pendingCharacter

private int pendingCharacter

binaryBytesLeft

private long binaryBytesLeft

binaryBuf

ByteArrayOutputStream binaryBuf

savedSpecials

private boolean[] savedSpecials

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.


S_text

private final int S_text
See Also:
Constant Field Values

S_backslashed

private final int S_backslashed
See Also:
Constant Field Values

S_token

private final int S_token
See Also:
Constant Field Values

S_parameter

private final int S_parameter
See Also:
Constant Field Values

S_aftertick

private final int S_aftertick
See Also:
Constant Field Values

S_aftertickc

private final int S_aftertickc
See Also:
Constant Field Values

S_inblob

private final int S_inblob
See Also:
Constant Field Values

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

RTFParser

public RTFParser()
Method Detail

handleKeyword

public abstract boolean handleKeyword(String keyword)
Implemented by subclasses to interpret a parameter-less RTF keyword. The keyword is passed without the leading '/' or any delimiting whitespace.


handleKeyword

public abstract boolean handleKeyword(String keyword,
                                      int parameter)
Implemented by subclasses to interpret a keyword with a parameter.

Parameters:
keyword - The keyword, as with handleKeyword(String).
parameter - The parameter following the keyword.

handleText

public abstract void handleText(String text)
Implemented by subclasses to interpret text from the RTF stream.


handleText

public void handleText(char ch)

handleBinaryBlob

public abstract void handleBinaryBlob(byte[] data)
Implemented by subclasses to handle the contents of the \bin keyword.


begingroup

public abstract void begingroup()
Implemented by subclasses to react to an increase in the nesting level.


endgroup

public abstract void endgroup()
Implemented by subclasses to react to the end of a group.


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.

close

public void close()
           throws IOException
Closes the parser. Currently, this simply does a flush(), followed by some minimal consistency checks.

Overrides:
close 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.