1 18 package org.apache.batik.parser; 19 20 import java.io.IOException ; 21 22 29 public class LengthParser extends AbstractParser { 30 31 34 protected LengthHandler lengthHandler; 35 36 39 public LengthParser() { 40 lengthHandler = DefaultLengthHandler.INSTANCE; 41 } 42 43 54 public void setLengthHandler(LengthHandler handler) { 55 lengthHandler = handler; 56 } 57 58 61 public LengthHandler getLengthHandler() { 62 return lengthHandler; 63 } 64 65 protected void doParse() throws ParseException, IOException { 66 lengthHandler.startLength(); 67 68 current = reader.read(); 69 skipSpaces(); 70 71 parseLength(); 72 73 skipSpaces(); 74 if (current != -1) { 75 reportError("end.of.stream.expected", 76 new Object [] { new Integer (current) }); 77 } 78 lengthHandler.endLength(); 79 } 80 81 84 protected void parseLength() throws ParseException, IOException { 85 int mant = 0; 86 int mantDig = 0; 87 boolean mantPos = true; 88 boolean mantRead = false; 89 90 int exp = 0; 91 int expDig = 0; 92 int expAdj = 0; 93 boolean expPos = true; 94 95 int unitState = 0; 96 97 switch (current) { 98 case '-': 99 mantPos = false; 100 case '+': 101 current = reader.read(); 102 } 103 104 m1: switch (current) { 105 default: 106 reportError("character.unexpected", 107 new Object [] { new Integer (current) }); 108 return; 109 110 case '.': 111 break; 112 113 case '0': 114 mantRead = true; 115 l: for (;;) { 116 current = reader.read(); 117 switch (current) { 118 case '1': case '2': case '3': case '4': 119 case '5': case '6': case '7': case '8': case '9': 120 break l; 121 default: 122 break m1; 123 case '0': 124 } 125 } 126 127 case '1': case '2': case '3': case '4': 128 case '5': case '6': case '7': case '8': case '9': 129 mantRead = true; 130 l: for (;;) { 131 if (mantDig < 9) { 132 mantDig++; 133 mant = mant * 10 + (current - '0'); 134 } else { 135 expAdj++; 136 } 137 current = reader.read(); 138 switch (current) { 139 default: 140 break l; 141 case '0': case '1': case '2': case '3': case '4': 142 case '5': case '6': case '7': case '8': case '9': 143 } 144 } 145 } 146 147 if (current == '.') { 148 current = reader.read(); 149 m2: switch (current) { 150 default: 151 case 'e': case 'E': 152 if (!mantRead) { 153 reportError("character.unexpected", 154 new Object [] { new Integer (current) }); 155 return; 156 } 157 break; 158 159 case '0': 160 if (mantDig == 0) { 161 l: for (;;) { 162 current = reader.read(); 163 expAdj--; 164 switch (current) { 165 case '1': case '2': case '3': case '4': 166 case '5': case '6': case '7': case '8': case '9': 167 break l; 168 default: 169 break m2; 170 case '0': 171 } 172 } 173 } 174 case '1': case '2': case '3': case '4': 175 case '5': case '6': case '7': case '8': case '9': 176 l: for (;;) { 177 if (mantDig < 9) { 178 mantDig++; 179 mant = mant * 10 + (current - '0'); 180 expAdj--; 181 } 182 current = reader.read(); 183 switch (current) { 184 default: 185 break l; 186 case '0': case '1': case '2': case '3': case '4': 187 case '5': case '6': case '7': case '8': case '9': 188 } 189 } 190 } 191 } 192 193 boolean le = false; 194 es: switch (current) { 195 case 'e': 196 le = true; 197 case 'E': 198 current = reader.read(); 199 switch (current) { 200 default: 201 reportError("character.unexpected", 202 new Object [] { new Integer (current) }); 203 return; 204 case 'm': 205 if (!le) { 206 reportError("character.unexpected", 207 new Object [] { new Integer (current) }); 208 return; 209 } 210 unitState = 1; 211 break es; 212 case 'x': 213 if (!le) { 214 reportError("character.unexpected", 215 new Object [] { new Integer (current) }); 216 return; 217 } 218 unitState = 2; 219 break es; 220 case '-': 221 expPos = false; 222 case '+': 223 current = reader.read(); 224 switch (current) { 225 default: 226 reportError("character.unexpected", 227 new Object [] { new Integer (current) }); 228 return; 229 case '0': case '1': case '2': case '3': case '4': 230 case '5': case '6': case '7': case '8': case '9': 231 } 232 case '0': case '1': case '2': case '3': case '4': 233 case '5': case '6': case '7': case '8': case '9': 234 } 235 236 en: switch (current) { 237 case '0': 238 l: for (;;) { 239 current = reader.read(); 240 switch (current) { 241 case '1': case '2': case '3': case '4': 242 case '5': case '6': case '7': case '8': case '9': 243 break l; 244 default: 245 break en; 246 case '0': 247 } 248 } 249 250 case '1': case '2': case '3': case '4': 251 case '5': case '6': case '7': case '8': case '9': 252 l: for (;;) { 253 if (expDig < 3) { 254 expDig++; 255 exp = exp * 10 + (current - '0'); 256 } 257 current = reader.read(); 258 switch (current) { 259 default: 260 break l; 261 case '0': case '1': case '2': case '3': case '4': 262 case '5': case '6': case '7': case '8': case '9': 263 } 264 } 265 } 266 default: 267 } 268 269 if (!expPos) { 270 exp = -exp; 271 } 272 exp += expAdj; 273 if (!mantPos) { 274 mant = -mant; 275 } 276 277 lengthHandler.lengthValue(NumberParser.buildFloat(mant, exp)); 278 279 switch (unitState) { 280 case 1: 281 lengthHandler.em(); 282 current = reader.read(); 283 return; 284 case 2: 285 lengthHandler.ex(); 286 current = reader.read(); 287 return; 288 } 289 290 switch (current) { 291 case 'e': 292 current = reader.read(); 293 switch (current) { 294 case 'm': 295 lengthHandler.em(); 296 current = reader.read(); 297 break; 298 case 'x': 299 lengthHandler.ex(); 300 current = reader.read(); 301 break; 302 default: 303 reportError("character.unexpected", 304 new Object [] { new Integer (current) }); 305 } 306 break; 307 308 case 'p': 309 current = reader.read(); 310 switch (current) { 311 case 'c': 312 lengthHandler.pc(); 313 current = reader.read(); 314 break; 315 case 't': 316 lengthHandler.pt(); 317 current = reader.read(); 318 break; 319 case 'x': 320 lengthHandler.px(); 321 current = reader.read(); 322 break; 323 default: 324 reportError("character.unexpected", 325 new Object [] { new Integer (current) }); 326 } 327 break; 328 329 case 'i': 330 current = reader.read(); 331 if (current != 'n') { 332 reportError("character.expected", 333 new Object [] { new Character ('n'), 334 new Integer (current) }); 335 break; 336 } 337 lengthHandler.in(); 338 current = reader.read(); 339 break; 340 case 'c': 341 current = reader.read(); 342 if (current != 'm') { 343 reportError("character.expected", 344 new Object [] { new Character ('m'), 345 new Integer (current) }); 346 break; 347 } 348 lengthHandler.cm(); 349 current = reader.read(); 350 break; 351 case 'm': 352 current = reader.read(); 353 if (current != 'm') { 354 reportError("character.expected", 355 new Object [] { new Character ('m'), 356 new Integer (current) }); 357 break; 358 } 359 lengthHandler.mm(); 360 current = reader.read(); 361 break; 362 case '%': 363 lengthHandler.percentage(); 364 current = reader.read(); 365 break; 366 } 367 } 368 } 369 | Popular Tags |