1 50 package com.lowagie.text.rtf.direct; 51 52 import java.awt.Color ; 53 import java.io.IOException ; 54 import java.io.Reader ; 55 import java.util.Iterator ; 56 57 import com.lowagie.text.rtf.document.RtfDocument; 58 59 68 public class RtfParser { 69 72 private static final int PARSER_IN_HEADER = 0; 73 76 private static final int PARSER_IN_FONT_TABLE = 1; 77 80 private static final int PARSER_IN_COLOR_TABLE = 2; 81 84 private static final int PARSER_IN_INFO_GROUP = 4; 85 88 private static final int PARSER_IN_DOCUMENT = 8; 89 90 93 private RtfDocument rtfDoc = null; 94 97 private RtfTokeniser tokeniser = null; 98 101 private RtfImportHeader importHeader = null; 102 105 private RtfFontTableParser fontTableParser = null; 106 109 private RtfColorTableParser colorTableParser = null; 110 113 private int state = PARSER_IN_HEADER; 114 115 122 public void importRtfDocument(Reader reader, RtfDocument rtfDoc) throws IOException { 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 140 public void importRtfFragment(Reader reader, RtfDocument rtfDoc, RtfImportMappings importMappings) throws IOException { 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 157 private void handleImportMappings(RtfImportMappings importMappings) { 158 Iterator it = importMappings.getFontMappings().keySet().iterator(); 159 while(it.hasNext()) { 160 String fontNr = (String ) it.next(); 161 this.importHeader.importFont(fontNr, (String ) importMappings.getFontMappings().get(fontNr)); 162 } 163 it = importMappings.getColorMappings().keySet().iterator(); 164 while(it.hasNext()) { 165 String colorNr = (String ) it.next(); 166 this.importHeader.importColor(colorNr, (Color ) importMappings.getColorMappings().get(colorNr)); 167 } 168 } 169 170 175 public void handleOpenGroup(int groupLevel) { 176 if(this.state == PARSER_IN_DOCUMENT) { 177 this.rtfDoc.add(new RtfDirectContent("{")); 178 } 179 } 180 181 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 208 public void handleCtrlCharacter(String ctrlCharacter, int groupLevel) { 209 if(this.state == PARSER_IN_DOCUMENT) { 210 this.rtfDoc.add(new RtfDirectContent(ctrlCharacter)); 211 } 212 } 213 214 223 public void handleCtrlWord(String 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 256 public void handleText(String 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 |