KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > direct > RtfParser


1 /**
2  * $Id: RtfParser.java 2377 2006-09-15 07:51:05Z xlv $
3  * $Name$
4  *
5  * Copyright 2006 by Mark Hall
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999-2006 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000-2006 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50 package com.lowagie.text.rtf.direct;
51
52 import java.awt.Color JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.io.Reader JavaDoc;
55 import java.util.Iterator JavaDoc;
56
57 import com.lowagie.text.rtf.document.RtfDocument;
58
59 /**
60  * The RtfParser allows the importing of RTF documents or
61  * RTF document fragments. The RTF document or fragment is tokenised,
62  * font and color definitions corrected and then added to
63  * the document being written.
64  *
65  * @version $Revision: 2377 $
66  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
67  */

68 public class RtfParser {
69     /**
70      * Currently the RTF document header is being parsed.
71      */

72     private static final int PARSER_IN_HEADER = 0;
73     /**
74      * Currently the RTF font table is being parsed.
75      */

76     private static final int PARSER_IN_FONT_TABLE = 1;
77     /**
78      * Currently the RTF color table is being parsed.
79      */

80     private static final int PARSER_IN_COLOR_TABLE = 2;
81     /**
82      * Currently the RTF info group is being parsed.
83      */

84     private static final int PARSER_IN_INFO_GROUP = 4;
85     /**
86      * Currently the RTF document content is being parsed.
87      */

88     private static final int PARSER_IN_DOCUMENT = 8;
89     
90     /**
91      * The RtfDocument to add the RTF document or fragment to.
92      */

93     private RtfDocument rtfDoc = null;
94     /**
95      * The RtfTokeniser to use for tokenising the RTF document or fragment.
96      */

97     private RtfTokeniser tokeniser = null;
98     /**
99      * The RtfImportHeader to store imported font and color mappings in.
100      */

101     private RtfImportHeader importHeader = null;
102     /**
103      * The RtfFontTableParser to use for parsing the font table.
104      */

105     private RtfFontTableParser fontTableParser = null;
106     /**
107      * The RtfColorTableParser to use for parsing the color table.
108      */

109     private RtfColorTableParser colorTableParser = null;
110     /**
111      * The current parser state.
112      */

113     private int state = PARSER_IN_HEADER;
114     
115     /**
116      * Imports a complete RTF document.
117      *
118      * @param reader The Reader to read the RTF document from.
119      * @param rtfDoc The RtfDocument to add the imported document to.
120      * @throws IOException On I/O errors.
121      */

122     public void importRtfDocument(Reader JavaDoc reader, RtfDocument rtfDoc) throws IOException JavaDoc {
123         this.rtfDoc = rtfDoc;
124         this.state = PARSER_IN_HEADER;
125         this.importHeader = new RtfImportHeader(this.rtfDoc);
126         this.fontTableParser = new RtfFontTableParser(this.importHeader);
127         this.colorTableParser = new RtfColorTableParser(this.importHeader);
128         this.tokeniser = new RtfTokeniser(this, 0);
129         this.tokeniser.tokenise(reader);
130     }
131     
132     /**
133      * Imports an RTF fragment.
134      *
135      * @param reader The Reader to read the RTF fragment from.
136      * @param rtfDoc The RTF document to add the RTF fragment to.
137      * @param importMappings The RtfImportMappings defining font and color mappings for the fragment.
138      * @throws IOException On I/O errors.
139      */

140     public void importRtfFragment(Reader JavaDoc reader, RtfDocument rtfDoc, RtfImportMappings importMappings) throws IOException JavaDoc {
141         this.rtfDoc = rtfDoc;
142         this.state = PARSER_IN_DOCUMENT;
143         this.importHeader = new RtfImportHeader(this.rtfDoc);
144         this.fontTableParser = new RtfFontTableParser(this.importHeader);
145         this.colorTableParser = new RtfColorTableParser(this.importHeader);
146         handleImportMappings(importMappings);
147         this.tokeniser = new RtfTokeniser(this, 1);
148         this.tokeniser.tokenise(reader);
149     }
150
151     /**
152      * Imports the mappings defined in the RtfImportMappings into the
153      * RtfImportHeader of this RtfParser.
154      *
155      * @param importMappings The RtfImportMappings to import.
156      */

157     private void handleImportMappings(RtfImportMappings importMappings) {
158         Iterator JavaDoc it = importMappings.getFontMappings().keySet().iterator();
159         while(it.hasNext()) {
160             String JavaDoc fontNr = (String JavaDoc) it.next();
161             this.importHeader.importFont(fontNr, (String JavaDoc) importMappings.getFontMappings().get(fontNr));
162         }
163         it = importMappings.getColorMappings().keySet().iterator();
164         while(it.hasNext()) {
165             String JavaDoc colorNr = (String JavaDoc) it.next();
166             this.importHeader.importColor(colorNr, (Color JavaDoc) importMappings.getColorMappings().get(colorNr));
167         }
168     }
169     
170     /**
171      * Handles open group tokens.
172      *
173      * @param groupLevel The current group nesting level.
174      */

175     public void handleOpenGroup(int groupLevel) {
176         if(this.state == PARSER_IN_DOCUMENT) {
177             this.rtfDoc.add(new RtfDirectContent("{"));
178         }
179     }
180     
181     /**
182      * Handles close group tokens. Depending on what is currently
183      * being parsed the parse state may change.
184      *
185      * @param groupLevel The current group nesting level.
186      */

187     public void handleCloseGroup(int groupLevel) {
188         if(this.state == PARSER_IN_DOCUMENT && groupLevel > 1) {
189             this.rtfDoc.add(new RtfDirectContent("}"));
190         } else if(this.state == PARSER_IN_INFO_GROUP && groupLevel == 2) {
191             this.state = PARSER_IN_DOCUMENT;
192         } else if(this.state == PARSER_IN_FONT_TABLE) {
193             this.fontTableParser.handleCloseGroup(groupLevel);
194             if(groupLevel == 2) {
195                 this.state = PARSER_IN_HEADER;
196             }
197         } else if(this.state == PARSER_IN_COLOR_TABLE) {
198             this.state = PARSER_IN_HEADER;
199         }
200     }
201     
202     /**
203      * Handles single control character tokens.
204      *
205      * @param ctrlCharacter The control character to handle.
206      * @param groupLevel The current group nesting level.
207      */

208     public void handleCtrlCharacter(String JavaDoc ctrlCharacter, int groupLevel) {
209         if(this.state == PARSER_IN_DOCUMENT) {
210             this.rtfDoc.add(new RtfDirectContent(ctrlCharacter));
211         }
212     }
213     
214     /**
215      * Handles control word tokens. Depending on the current
216      * state a control word can lead to a state change. When
217      * parsing the actual document contents, The font number,
218      * color number and background color number are remapped.
219      *
220      * @param ctrlWord The control word to handle.
221      * @param groupLevel The current group nesting level.
222      */

223     public void handleCtrlWord(String JavaDoc ctrlWord, int groupLevel) {
224         if(this.state == PARSER_IN_DOCUMENT) {
225             if(RtfColorTableParser.stringMatches(ctrlWord, "\\f")) {
226                 ctrlWord = "\\f" + this.importHeader.mapFontNr(ctrlWord.substring(2));
227             } else if(RtfColorTableParser.stringMatches(ctrlWord, "\\cf")) {
228                 ctrlWord = "\\cf" + this.importHeader.mapColorNr(ctrlWord.substring(3));
229             } else if(RtfColorTableParser.stringMatches(ctrlWord, "\\cb")) {
230                 ctrlWord = "\\cb" + this.importHeader.mapColorNr(ctrlWord.substring(3));
231             }
232             this.rtfDoc.add(new RtfDirectContent(ctrlWord));
233         } else if(this.state == PARSER_IN_FONT_TABLE) {
234             this.fontTableParser.handleCtrlWord(ctrlWord, groupLevel);
235         } else if(this.state == PARSER_IN_COLOR_TABLE) {
236             this.colorTableParser.handleCtrlWord(ctrlWord, groupLevel);
237         } else if(this.state == PARSER_IN_HEADER) {
238             if(ctrlWord.equals("\\info")) {
239                 this.state = PARSER_IN_INFO_GROUP;
240             } else if(ctrlWord.equals("\\fonttbl")) {
241                 this.state = PARSER_IN_FONT_TABLE;
242             } else if(ctrlWord.equals("\\colortbl")) {
243                 this.state = PARSER_IN_COLOR_TABLE;
244             }
245         }
246     }
247     
248     /**
249      * Handles text tokens. These are either handed on to the
250      * RtfColorTableParser or RtfFontTableParser or added directly
251      * to the document.
252      *
253      * @param text The text token to handle.
254      * @param groupLevel The current group nesting level.
255      */

256     public void handleText(String JavaDoc text, int groupLevel) {
257         if(this.state == PARSER_IN_DOCUMENT) {
258             this.rtfDoc.add(new RtfDirectContent(text));
259         } else if(this.state == PARSER_IN_FONT_TABLE) {
260             this.fontTableParser.handleText(text, groupLevel);
261         } else if(this.state == PARSER_IN_COLOR_TABLE) {
262             this.colorTableParser.handleText(text, groupLevel);
263         }
264     }
265 }
266
Popular Tags