1 50 51 package com.lowagie.text.rtf.direct; 52 53 import java.io.IOException ; 54 import java.io.Reader ; 55 56 73 public class RtfTokeniser { 74 77 private static final int TOKENISER_STATE_READY = 0; 78 81 private static final int TOKENISER_STATE_SLASH = 1; 82 85 private static final int TOKENISER_STATE_IN_CTRL_WORD = 2; 86 89 private static final int TOKENISER_STATE_IN_TEXT = 4; 90 91 94 private int state = TOKENISER_STATE_READY; 95 98 private int groupLevel = 0; 99 102 private RtfParser rtfParser = null; 103 104 112 public RtfTokeniser(RtfParser rtfParser, int startGroupLevel) { 113 this.rtfParser = rtfParser; 114 this.groupLevel = startGroupLevel; 115 } 116 117 123 public void tokenise(Reader reader) throws IOException { 124 char[] nextChar = new char[1]; 125 StringBuffer temp = new StringBuffer (); 126 this.state = TOKENISER_STATE_READY; 127 this.groupLevel = 0; 128 while(reader.read(nextChar) != -1) { 129 if(this.state == TOKENISER_STATE_READY) { if(nextChar[0] == '{') { this.rtfParser.handleOpenGroup(this.groupLevel); 132 groupLevel++; 133 } else if(nextChar[0] == '}') { this.rtfParser.handleCloseGroup(this.groupLevel); 135 groupLevel--; 136 } else if(nextChar[0] == '\\') { 137 this.state = TOKENISER_STATE_SLASH; 138 temp = new StringBuffer (); 139 } else { 140 this.state = TOKENISER_STATE_IN_TEXT; 141 temp.append(nextChar[0]); 142 } 143 } else if((this.state & TOKENISER_STATE_SLASH) == TOKENISER_STATE_SLASH) { if(nextChar[0] == '{') { 145 this.state = TOKENISER_STATE_IN_TEXT; 146 temp.append("\\{"); 147 } else if(nextChar[0] == '}') { 148 this.state = TOKENISER_STATE_IN_TEXT; 149 temp.append("\\}"); 150 } else if(nextChar[0] == '\\') { 151 this.state = TOKENISER_STATE_IN_TEXT; 152 temp.append("\\\\"); 153 } else { 154 if((this.state & TOKENISER_STATE_IN_TEXT) == TOKENISER_STATE_IN_TEXT) { this.rtfParser.handleText(temp.toString(), this.groupLevel); 156 temp = new StringBuffer (); 157 } 158 if(nextChar[0] == '|') { 159 this.state = TOKENISER_STATE_READY; 160 this.rtfParser.handleCtrlCharacter("\\|", this.groupLevel); 161 } else if(nextChar[0] == '~') { 162 this.state = TOKENISER_STATE_READY; 163 this.rtfParser.handleCtrlCharacter("\\~", this.groupLevel); 164 } else if(nextChar[0] == '-') { 165 this.state = TOKENISER_STATE_READY; 166 this.rtfParser.handleCtrlCharacter("\\-", this.groupLevel); 167 } else if(nextChar[0] == '_') { 168 this.state = TOKENISER_STATE_READY; 169 this.rtfParser.handleCtrlCharacter("\\_", this.groupLevel); 170 } else if(nextChar[0] == ':') { 171 this.state = TOKENISER_STATE_READY; 172 this.rtfParser.handleCtrlCharacter("\\:", this.groupLevel); 173 } else if(nextChar[0] == '*') { 174 this.state = TOKENISER_STATE_READY; 175 this.rtfParser.handleCtrlCharacter("\\*", this.groupLevel); 176 } else { 177 this.state = TOKENISER_STATE_IN_CTRL_WORD; 178 temp = new StringBuffer ("\\"); 179 temp.append(nextChar[0]); 180 } 181 } 182 } else if(this.state == TOKENISER_STATE_IN_CTRL_WORD) { if(nextChar[0] == '\n' || nextChar[0] == '\r') { 184 nextChar[0] = ' '; 185 } 186 if(nextChar[0] == '{') { 187 this.rtfParser.handleCtrlWord(temp.toString(), this.groupLevel); 188 this.rtfParser.handleOpenGroup(this.groupLevel); 189 groupLevel++; 190 this.state = TOKENISER_STATE_READY; 191 temp = new StringBuffer (); 192 } else if(nextChar[0] == '}') { 193 this.rtfParser.handleCtrlWord(temp.toString(), this.groupLevel); 194 this.rtfParser.handleCloseGroup(this.groupLevel); 195 groupLevel--; 196 this.state = TOKENISER_STATE_READY; 197 temp = new StringBuffer (); 198 } else if(nextChar[0] == '\\') { 199 this.rtfParser.handleCtrlWord(temp.toString(), this.groupLevel); 200 this.state = TOKENISER_STATE_SLASH; 201 temp = new StringBuffer (); 202 } else if(nextChar[0] == ' ') { 203 this.rtfParser.handleCtrlWord(temp.toString(), this.groupLevel); 204 this.rtfParser.handleText(" ", this.groupLevel); 205 this.state = TOKENISER_STATE_READY; 206 temp = new StringBuffer (); 207 } else if(nextChar[0] == ';') { 208 this.rtfParser.handleCtrlWord(temp.toString(), this.groupLevel); 209 this.rtfParser.handleText(";", this.groupLevel); 210 this.state = TOKENISER_STATE_READY; 211 temp = new StringBuffer (); 212 } else { 213 temp.append(nextChar[0]); 214 } 215 } else if(this.state == TOKENISER_STATE_IN_TEXT) { if(nextChar[0] == '{') { 217 this.rtfParser.handleText(temp.toString(), this.groupLevel); 218 this.rtfParser.handleOpenGroup(this.groupLevel); 219 groupLevel++; 220 this.state = TOKENISER_STATE_READY; 221 temp = new StringBuffer (); 222 } else if(nextChar[0] == '}') { 223 this.rtfParser.handleText(temp.toString(), this.groupLevel); 224 this.rtfParser.handleCloseGroup(this.groupLevel); 225 groupLevel--; 226 this.state = TOKENISER_STATE_READY; 227 temp = new StringBuffer (); 228 } else if(nextChar[0] == '\\') { 229 this.state = TOKENISER_STATE_IN_TEXT | TOKENISER_STATE_SLASH; 230 } else { 231 temp.append(nextChar[0]); 232 } 233 } 234 } 235 if((this.state & TOKENISER_STATE_IN_TEXT) == TOKENISER_STATE_IN_TEXT && !temp.toString().equals("")) { this.rtfParser.handleText(temp.toString(), this.groupLevel); 237 } 238 } 239 } 240 | Popular Tags |