| 1 | /* RTFParser.java -- |
| 2 | Copyright (C) 2005 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Classpath. |
| 5 | |
| 6 | GNU Classpath is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GNU Classpath is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the |
| 18 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 | 02110-1301 USA. |
| 20 | |
| 21 | Linking this library statically or dynamically with other modules is |
| 22 | making a combined work based on this library. Thus, the terms and |
| 23 | conditions of the GNU General Public License cover the whole |
| 24 | combination. |
| 25 | |
| 26 | As a special exception, the copyright holders of this library give you |
| 27 | permission to link this library with independent modules to produce an |
| 28 | executable, regardless of the license terms of these independent |
| 29 | modules, and to copy and distribute the resulting executable under |
| 30 | terms of your choice, provided that you also meet, for each linked |
| 31 | independent module, the terms and conditions of the license of that |
| 32 | module. An independent module is a module which is not derived from |
| 33 | or based on this library. If you modify this library, you may extend |
| 34 | this exception to your version of the library, but you are not |
| 35 | obligated to do so. If you do not wish to do so, delete this |
| 36 | exception statement from your version. */ |
| 37 | |
| 38 | |
| 39 | package javax.swing.text.rtf; |
| 40 | |
| 41 | import java.io.IOException; |
| 42 | import java.io.InputStream; |
| 43 | import java.io.Reader; |
| 44 | |
| 45 | import javax.swing.text.BadLocationException; |
| 46 | import javax.swing.text.Document; |
| 47 | |
| 48 | /** |
| 49 | * Parses an RTF file into a {@link Document}. The parser utilizes |
| 50 | * {@link RTFScanner}. |
| 51 | * |
| 52 | * @author Roman Kennke (roman@ontographics.com) |
| 53 | */ |
| 54 | class RTFParser |
| 55 | { |
| 56 | |
| 57 | /** |
| 58 | * Our scanner. |
| 59 | */ |
| 60 | private RTFScanner scanner; |
| 61 | |
| 62 | /** |
| 63 | * The document into which we parse. |
| 64 | */ |
| 65 | private Document doc; |
| 66 | |
| 67 | /** |
| 68 | * The current position. |
| 69 | */ |
| 70 | private int pos; |
| 71 | |
| 72 | /** |
| 73 | * Constructs a new RTFParser for the specified document and position, |
| 74 | * without initializing the scanner. This is only used internally. |
| 75 | * |
| 76 | * @param doc the {@link Document} into which we should parse |
| 77 | * @param pos the position to start |
| 78 | */ |
| 79 | private RTFParser(Document doc, int pos) |
| 80 | { |
| 81 | this.doc = doc; |
| 82 | this.pos = pos; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Constructs a new RTFParser for the specified <code>stream</code>. |
| 87 | * |
| 88 | * @param stream the stream from which we parse |
| 89 | * @param doc the {@link Document} into which we should parse |
| 90 | * @param pos the position to start |
| 91 | */ |
| 92 | public RTFParser(InputStream stream, Document doc, int pos) |
| 93 | { |
| 94 | this(doc, pos); |
| 95 | scanner = new RTFScanner(stream); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Constructs a new RTFParser for the specified <code>reader</code>. |
| 100 | * |
| 101 | * @param reader the reader from which we parse |
| 102 | * @param doc the {@link Document} into which we should parse |
| 103 | * @param pos the position to start |
| 104 | */ |
| 105 | public RTFParser(Reader reader, Document doc, int pos) |
| 106 | { |
| 107 | this(doc, pos); |
| 108 | scanner = new RTFScanner(reader); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the {@link Document} in which we parsed the RTF data. |
| 113 | * |
| 114 | * @return the {@link Document} in which we parsed the RTF data |
| 115 | */ |
| 116 | public Document getDocument() |
| 117 | { |
| 118 | return doc; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Starts the parsing process. |
| 123 | */ |
| 124 | public void parse() |
| 125 | throws IOException, BadLocationException |
| 126 | { |
| 127 | parseFile(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * The parse rules for <file>. |
| 132 | */ |
| 133 | private void parseFile() |
| 134 | throws IOException, BadLocationException |
| 135 | { |
| 136 | Token t1 = scanner.readToken(); |
| 137 | if (t1.type != Token.LCURLY) |
| 138 | throw new RTFParseException("expected left curly braces"); |
| 139 | |
| 140 | parseHeader(); |
| 141 | parseDocument(); |
| 142 | |
| 143 | Token t2 = scanner.peekToken(); |
| 144 | if (t2.type == Token.RCURLY) |
| 145 | { |
| 146 | // Eat the token. |
| 147 | scanner.readToken(); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | // Ignore this for maximum robustness when file is broken. |
| 152 | System.err.println("RTF warning: expected right curly braces"); |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * The parse rules for <header>. |
| 159 | * |
| 160 | * TODO: implement this properly |
| 161 | */ |
| 162 | private void parseHeader() |
| 163 | //throws IOException, BadLocationException |
| 164 | { |
| 165 | // TODO add parse rules here |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * The parse rules for <document>. |
| 171 | * |
| 172 | * TODO: implement this properly |
| 173 | */ |
| 174 | private void parseDocument() |
| 175 | throws IOException, BadLocationException |
| 176 | { |
| 177 | // !!! TODO !!! |
| 178 | // This simply emits every TEXT Token as text to the document |
| 179 | // which is plain stupid |
| 180 | |
| 181 | boolean eof = false; |
| 182 | |
| 183 | do { |
| 184 | Token token = scanner.readToken(); |
| 185 | switch (token.type) |
| 186 | { |
| 187 | case Token.TEXT: |
| 188 | TextToken textToken = (TextToken) token; |
| 189 | doc.insertString(pos, textToken.text, null); |
| 190 | pos += textToken.text.length(); |
| 191 | break; |
| 192 | case Token.EOF: |
| 193 | eof = true; |
| 194 | break; |
| 195 | default: |
| 196 | // FIXME |
| 197 | break; |
| 198 | } |
| 199 | } while (!eof); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | } |