/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* <b>RTFParser</b> is a subclass of <b>AbstractFilter</b> which understands basic RTF syntax
* indications to its subclass.
*
* Normally programmers will only use <b>RTFFilter</b>, a subclass of this class that knows what to
* do with the tokens this class parses.
*
* @see AbstractFilter
* @see RTFFilter
*/
{
/** The current RTF group nesting level. */
public int level;
private int state;
// read their parameters
private boolean[] savedSpecials;
/** A stream to which to write warnings and debugging information
* while parsing. This is set to <code>System.out</code> to log
* any anomalous information to stdout. */
// value for the 'state' variable
/** Implemented by subclasses to interpret a parameter-less RTF keyword.
* The keyword is passed without the leading '/' or any delimiting
* whitespace. */
/** Implemented by subclasses to interpret a keyword with a parameter.
* @param keyword The keyword, as with <code>handleKeyword(String)</code>.
* @param parameter The parameter following the keyword. */
/** Implemented by subclasses to interpret text from the RTF stream. */
/** Implemented by subclasses to handle the contents of the \bin keyword. */
/** Implemented by subclasses to react to an increase
* in the nesting level. */
public abstract void begingroup();
/** Implemented by subclasses to react to the end of a group. */
public abstract void endgroup();
// table of non-text characters in rtf
static final boolean rtfSpecialsTable[];
static {
rtfSpecialsTable['\n'] = true;
rtfSpecialsTable['\r'] = true;
rtfSpecialsTable['{'] = true;
rtfSpecialsTable['}'] = true;
rtfSpecialsTable['\\'] = true;
}
public RTFParser()
{
currentCharacters = new StringBuffer();
level = 0;
//warnings = System.out;
}
// TODO: Handle wrapup at end of file correctly.
public void writeSpecial(int b)
throws IOException
{
write((char)b);
}
}
}
throws IOException
{
int index = 0;
index ++;
}
return;
}
else
handleText(s);
}
throws IOException
{
boolean ok;
switch (state)
{
case S_text:
break; // unadorned newlines are ignored
} else if (ch == '{') {
currentCharacters = new StringBuffer();
}
level ++;
begingroup();
} else if(ch == '}') {
currentCharacters = new StringBuffer();
}
if (level == 0)
throw new IOException("Too many close-groups in RTF text");
endgroup();
level --;
} else if(ch == '\\') {
currentCharacters = new StringBuffer();
}
} else {
}
break;
case S_backslashed:
if (ch == '\'') {
state = S_aftertick;
break;
}
char newstring[] = new char[1];
}
/* currentCharacters is already an empty stringBuffer */
break;
}
/* FALL THROUGH */
case S_token:
} else {
currentCharacters = new StringBuffer();
// Parameter following?
state = S_parameter;
} else {
if (!ok)
// Non-space delimiters get included in the text
}
}
break;
case S_parameter:
} else {
/* TODO: Test correct behavior of \bin keyword */
else
break;
}
if (!ok)
currentCharacters = new StringBuffer();
// Delimiters here are interpreted as text too
}
break;
case S_aftertick:
else {
}
break;
case S_aftertickc:
{
if (ch != 0)
handleText(ch);
}
break;
case S_inblob:
binaryBytesLeft --;
if (binaryBytesLeft == 0) {
}
}
}
/** Flushes any buffered but not yet written characters.
* Subclasses which override this method should call this
* method <em>before</em> flushing
* any of their own buffers. */
public void flush()
throws IOException
{
super.flush();
currentCharacters = new StringBuffer();
}
}
/** Closes the parser. Currently, this simply does a <code>flush()</code>,
* followed by some minimal consistency checks. */
public void close()
throws IOException
{
flush();
warning("Truncated RTF file.");
/* TODO: any sane way to handle termination in a non-S_text state? */
/* probably not */
/* this will cause subclasses to behave more reasonably
some of the time */
while (level > 0) {
endgroup();
level --;
}
}
super.close();
}
}