0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text.rtf;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.lang.*;
0N/A
0N/A/**
0N/A * <b>RTFParser</b> is a subclass of <b>AbstractFilter</b> which understands basic RTF syntax
0N/A * and passes a stream of control words, text, and begin/end group
0N/A * indications to its subclass.
0N/A *
0N/A * Normally programmers will only use <b>RTFFilter</b>, a subclass of this class that knows what to
0N/A * do with the tokens this class parses.
0N/A *
0N/A * @see AbstractFilter
0N/A * @see RTFFilter
0N/A */
0N/Aabstract class RTFParser extends AbstractFilter
0N/A{
0N/A /** The current RTF group nesting level. */
0N/A public int level;
0N/A
0N/A private int state;
0N/A private StringBuffer currentCharacters;
0N/A private String pendingKeyword; // where keywords go while we
0N/A // read their parameters
0N/A private int pendingCharacter; // for the \'xx construct
0N/A
0N/A private long binaryBytesLeft; // in a \bin blob?
0N/A ByteArrayOutputStream binaryBuf;
0N/A private boolean[] savedSpecials;
0N/A
0N/A /** A stream to which to write warnings and debugging information
0N/A * while parsing. This is set to <code>System.out</code> to log
0N/A * any anomalous information to stdout. */
0N/A protected PrintStream warnings;
0N/A
0N/A // value for the 'state' variable
0N/A private final int S_text = 0; // reading random text
0N/A private final int S_backslashed = 1; // read a backslash, waiting for next
0N/A private final int S_token = 2; // reading a multicharacter token
0N/A private final int S_parameter = 3; // reading a token's parameter
0N/A
0N/A private final int S_aftertick = 4; // after reading \'
0N/A private final int S_aftertickc = 5; // after reading \'x
0N/A
0N/A private final int S_inblob = 6; // in a \bin blob
0N/A
0N/A /** Implemented by subclasses to interpret a parameter-less RTF keyword.
0N/A * The keyword is passed without the leading '/' or any delimiting
0N/A * whitespace. */
0N/A public abstract boolean handleKeyword(String keyword);
0N/A /** Implemented by subclasses to interpret a keyword with a parameter.
0N/A * @param keyword The keyword, as with <code>handleKeyword(String)</code>.
0N/A * @param parameter The parameter following the keyword. */
0N/A public abstract boolean handleKeyword(String keyword, int parameter);
0N/A /** Implemented by subclasses to interpret text from the RTF stream. */
0N/A public abstract void handleText(String text);
0N/A public void handleText(char ch)
0N/A { handleText(String.valueOf(ch)); }
0N/A /** Implemented by subclasses to handle the contents of the \bin keyword. */
0N/A public abstract void handleBinaryBlob(byte[] data);
0N/A /** Implemented by subclasses to react to an increase
0N/A * in the nesting level. */
0N/A public abstract void begingroup();
0N/A /** Implemented by subclasses to react to the end of a group. */
0N/A public abstract void endgroup();
0N/A
0N/A // table of non-text characters in rtf
0N/A static final boolean rtfSpecialsTable[];
0N/A static {
611N/A rtfSpecialsTable = noSpecialsTable.clone();
0N/A rtfSpecialsTable['\n'] = true;
0N/A rtfSpecialsTable['\r'] = true;
0N/A rtfSpecialsTable['{'] = true;
0N/A rtfSpecialsTable['}'] = true;
0N/A rtfSpecialsTable['\\'] = true;
0N/A }
0N/A
0N/A public RTFParser()
0N/A {
0N/A currentCharacters = new StringBuffer();
0N/A state = S_text;
0N/A pendingKeyword = null;
0N/A level = 0;
0N/A //warnings = System.out;
0N/A
0N/A specialsTable = rtfSpecialsTable;
0N/A }
0N/A
0N/A // TODO: Handle wrapup at end of file correctly.
0N/A
0N/A public void writeSpecial(int b)
0N/A throws IOException
0N/A {
0N/A write((char)b);
0N/A }
0N/A
0N/A protected void warning(String s) {
0N/A if (warnings != null) {
0N/A warnings.println(s);
0N/A }
0N/A }
0N/A
0N/A public void write(String s)
0N/A throws IOException
0N/A {
0N/A if (state != S_text) {
0N/A int index = 0;
0N/A int length = s.length();
0N/A while(index < length && state != S_text) {
0N/A write(s.charAt(index));
0N/A index ++;
0N/A }
0N/A
0N/A if(index >= length)
0N/A return;
0N/A
0N/A s = s.substring(index);
0N/A }
0N/A
0N/A if (currentCharacters.length() > 0)
0N/A currentCharacters.append(s);
0N/A else
0N/A handleText(s);
0N/A }
0N/A
0N/A public void write(char ch)
0N/A throws IOException
0N/A {
0N/A boolean ok;
0N/A
0N/A switch (state)
0N/A {
0N/A case S_text:
0N/A if (ch == '\n' || ch == '\r') {
0N/A break; // unadorned newlines are ignored
0N/A } else if (ch == '{') {
0N/A if (currentCharacters.length() > 0) {
0N/A handleText(currentCharacters.toString());
0N/A currentCharacters = new StringBuffer();
0N/A }
0N/A level ++;
0N/A begingroup();
0N/A } else if(ch == '}') {
0N/A if (currentCharacters.length() > 0) {
0N/A handleText(currentCharacters.toString());
0N/A currentCharacters = new StringBuffer();
0N/A }
0N/A if (level == 0)
0N/A throw new IOException("Too many close-groups in RTF text");
0N/A endgroup();
0N/A level --;
0N/A } else if(ch == '\\') {
0N/A if (currentCharacters.length() > 0) {
0N/A handleText(currentCharacters.toString());
0N/A currentCharacters = new StringBuffer();
0N/A }
0N/A state = S_backslashed;
0N/A } else {
0N/A currentCharacters.append(ch);
0N/A }
0N/A break;
0N/A case S_backslashed:
0N/A if (ch == '\'') {
0N/A state = S_aftertick;
0N/A break;
0N/A }
0N/A if (!Character.isLetter(ch)) {
0N/A char newstring[] = new char[1];
0N/A newstring[0] = ch;
0N/A if (!handleKeyword(new String(newstring))) {
0N/A warning("Unknown keyword: " + newstring + " (" + (byte)ch + ")");
0N/A }
0N/A state = S_text;
0N/A pendingKeyword = null;
0N/A /* currentCharacters is already an empty stringBuffer */
0N/A break;
0N/A }
0N/A
0N/A state = S_token;
0N/A /* FALL THROUGH */
0N/A case S_token:
0N/A if (Character.isLetter(ch)) {
0N/A currentCharacters.append(ch);
0N/A } else {
0N/A pendingKeyword = currentCharacters.toString();
0N/A currentCharacters = new StringBuffer();
0N/A
0N/A // Parameter following?
0N/A if (Character.isDigit(ch) || (ch == '-')) {
0N/A state = S_parameter;
0N/A currentCharacters.append(ch);
0N/A } else {
0N/A ok = handleKeyword(pendingKeyword);
0N/A if (!ok)
0N/A warning("Unknown keyword: " + pendingKeyword);
0N/A pendingKeyword = null;
0N/A state = S_text;
0N/A
0N/A // Non-space delimiters get included in the text
0N/A if (!Character.isWhitespace(ch))
0N/A write(ch);
0N/A }
0N/A }
0N/A break;
0N/A case S_parameter:
0N/A if (Character.isDigit(ch)) {
0N/A currentCharacters.append(ch);
0N/A } else {
0N/A /* TODO: Test correct behavior of \bin keyword */
0N/A if (pendingKeyword.equals("bin")) { /* magic layer-breaking kwd */
0N/A long parameter = Long.parseLong(currentCharacters.toString());
0N/A pendingKeyword = null;
0N/A state = S_inblob;
0N/A binaryBytesLeft = parameter;
0N/A if (binaryBytesLeft > Integer.MAX_VALUE)
0N/A binaryBuf = new ByteArrayOutputStream(Integer.MAX_VALUE);
0N/A else
0N/A binaryBuf = new ByteArrayOutputStream((int)binaryBytesLeft);
0N/A savedSpecials = specialsTable;
0N/A specialsTable = allSpecialsTable;
0N/A break;
0N/A }
0N/A
0N/A int parameter = Integer.parseInt(currentCharacters.toString());
0N/A ok = handleKeyword(pendingKeyword, parameter);
0N/A if (!ok)
0N/A warning("Unknown keyword: " + pendingKeyword +
0N/A " (param " + currentCharacters + ")");
0N/A pendingKeyword = null;
0N/A currentCharacters = new StringBuffer();
0N/A state = S_text;
0N/A
0N/A // Delimiters here are interpreted as text too
0N/A if (!Character.isWhitespace(ch))
0N/A write(ch);
0N/A }
0N/A break;
0N/A case S_aftertick:
0N/A if (Character.digit(ch, 16) == -1)
0N/A state = S_text;
0N/A else {
0N/A pendingCharacter = Character.digit(ch, 16);
0N/A state = S_aftertickc;
0N/A }
0N/A break;
0N/A case S_aftertickc:
0N/A state = S_text;
0N/A if (Character.digit(ch, 16) != -1)
0N/A {
0N/A pendingCharacter = pendingCharacter * 16 + Character.digit(ch, 16);
0N/A ch = translationTable[pendingCharacter];
0N/A if (ch != 0)
0N/A handleText(ch);
0N/A }
0N/A break;
0N/A case S_inblob:
0N/A binaryBuf.write(ch);
0N/A binaryBytesLeft --;
0N/A if (binaryBytesLeft == 0) {
0N/A state = S_text;
0N/A specialsTable = savedSpecials;
0N/A savedSpecials = null;
0N/A handleBinaryBlob(binaryBuf.toByteArray());
0N/A binaryBuf = null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /** Flushes any buffered but not yet written characters.
0N/A * Subclasses which override this method should call this
0N/A * method <em>before</em> flushing
0N/A * any of their own buffers. */
0N/A public void flush()
0N/A throws IOException
0N/A {
0N/A super.flush();
0N/A
0N/A if (state == S_text && currentCharacters.length() > 0) {
0N/A handleText(currentCharacters.toString());
0N/A currentCharacters = new StringBuffer();
0N/A }
0N/A }
0N/A
0N/A /** Closes the parser. Currently, this simply does a <code>flush()</code>,
0N/A * followed by some minimal consistency checks. */
0N/A public void close()
0N/A throws IOException
0N/A {
0N/A flush();
0N/A
0N/A if (state != S_text || level > 0) {
0N/A warning("Truncated RTF file.");
0N/A
0N/A /* TODO: any sane way to handle termination in a non-S_text state? */
0N/A /* probably not */
0N/A
0N/A /* this will cause subclasses to behave more reasonably
0N/A some of the time */
0N/A while (level > 0) {
0N/A endgroup();
0N/A level --;
0N/A }
0N/A }
0N/A
0N/A super.close();
0N/A }
0N/A
0N/A}