1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
73 * The keyword is passed without the leading '/' or any delimiting
74 * whitespace. */
75 public abstract boolean handleKeyword(String keyword);
76 /** Implemented by subclasses to interpret a keyword with a parameter.
77 * @param keyword The keyword, as with <code>handleKeyword(String)</code>.
78 * @param parameter The parameter following the keyword. */
79 public abstract boolean handleKeyword(String keyword, int parameter);
80 /** Implemented by subclasses to interpret text from the RTF stream. */
81 public abstract void handleText(String text);
82 public void handleText(char ch)
83 { handleText(String.valueOf(ch)); }
84 /** Implemented by subclasses to handle the contents of the \bin keyword. */
85 public abstract void handleBinaryBlob(byte[] data);
86 /** Implemented by subclasses to react to an increase
87 * in the nesting level. */
88 public abstract void begingroup();
89 /** Implemented by subclasses to react to the end of a group. */
90 public abstract void endgroup();
91
92 // table of non-text characters in rtf
93 static final boolean rtfSpecialsTable[];
94 static {
95 rtfSpecialsTable = noSpecialsTable.clone();
96 rtfSpecialsTable['\n'] = true;
97 rtfSpecialsTable['\r'] = true;
98 rtfSpecialsTable['{'] = true;
99 rtfSpecialsTable['}'] = true;
100 rtfSpecialsTable['\\'] = true;
101 }
102
103 public RTFParser()
104 {
105 currentCharacters = new StringBuffer();
106 state = S_text;
107 pendingKeyword = null;
108 level = 0;
109 //warnings = System.out;
110
111 specialsTable = rtfSpecialsTable;
112 }
113
174 if (level == 0)
175 throw new IOException("Too many close-groups in RTF text");
176 endgroup();
177 level --;
178 } else if(ch == '\\') {
179 if (currentCharacters.length() > 0) {
180 handleText(currentCharacters.toString());
181 currentCharacters = new StringBuffer();
182 }
183 state = S_backslashed;
184 } else {
185 currentCharacters.append(ch);
186 }
187 break;
188 case S_backslashed:
189 if (ch == '\'') {
190 state = S_aftertick;
191 break;
192 }
193 if (!Character.isLetter(ch)) {
194 char newstring[] = new char[1];
195 newstring[0] = ch;
196 if (!handleKeyword(new String(newstring))) {
197 warning("Unknown keyword: " + newstring + " (" + (byte)ch + ")");
198 }
199 state = S_text;
200 pendingKeyword = null;
201 /* currentCharacters is already an empty stringBuffer */
202 break;
203 }
204
205 state = S_token;
206 /* FALL THROUGH */
207 case S_token:
208 if (Character.isLetter(ch)) {
209 currentCharacters.append(ch);
210 } else {
211 pendingKeyword = currentCharacters.toString();
212 currentCharacters = new StringBuffer();
213
214 // Parameter following?
|
1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
73 * The keyword is passed without the leading '/' or any delimiting
74 * whitespace. */
75 public abstract boolean handleKeyword(String keyword);
76 /** Implemented by subclasses to interpret a keyword with a parameter.
77 * @param keyword The keyword, as with <code>handleKeyword(String)</code>.
78 * @param parameter The parameter following the keyword. */
79 public abstract boolean handleKeyword(String keyword, int parameter);
80 /** Implemented by subclasses to interpret text from the RTF stream. */
81 public abstract void handleText(String text);
82 public void handleText(char ch)
83 { handleText(String.valueOf(ch)); }
84 /** Implemented by subclasses to handle the contents of the \bin keyword. */
85 public abstract void handleBinaryBlob(byte[] data);
86 /** Implemented by subclasses to react to an increase
87 * in the nesting level. */
88 public abstract void begingroup();
89 /** Implemented by subclasses to react to the end of a group. */
90 public abstract void endgroup();
91
92 // table of non-text characters in rtf
93 static final boolean[] rtfSpecialsTable;
94 static {
95 rtfSpecialsTable = noSpecialsTable.clone();
96 rtfSpecialsTable['\n'] = true;
97 rtfSpecialsTable['\r'] = true;
98 rtfSpecialsTable['{'] = true;
99 rtfSpecialsTable['}'] = true;
100 rtfSpecialsTable['\\'] = true;
101 }
102
103 public RTFParser()
104 {
105 currentCharacters = new StringBuffer();
106 state = S_text;
107 pendingKeyword = null;
108 level = 0;
109 //warnings = System.out;
110
111 specialsTable = rtfSpecialsTable;
112 }
113
174 if (level == 0)
175 throw new IOException("Too many close-groups in RTF text");
176 endgroup();
177 level --;
178 } else if(ch == '\\') {
179 if (currentCharacters.length() > 0) {
180 handleText(currentCharacters.toString());
181 currentCharacters = new StringBuffer();
182 }
183 state = S_backslashed;
184 } else {
185 currentCharacters.append(ch);
186 }
187 break;
188 case S_backslashed:
189 if (ch == '\'') {
190 state = S_aftertick;
191 break;
192 }
193 if (!Character.isLetter(ch)) {
194 char[] newstring = new char[1];
195 newstring[0] = ch;
196 if (!handleKeyword(new String(newstring))) {
197 warning("Unknown keyword: " + newstring + " (" + (byte)ch + ")");
198 }
199 state = S_text;
200 pendingKeyword = null;
201 /* currentCharacters is already an empty stringBuffer */
202 break;
203 }
204
205 state = S_token;
206 /* FALL THROUGH */
207 case S_token:
208 if (Character.isLetter(ch)) {
209 currentCharacters.append(ch);
210 } else {
211 pendingKeyword = currentCharacters.toString();
212 currentCharacters = new StringBuffer();
213
214 // Parameter following?
|