1 19 20 package jxl.read.biff; 21 22 import java.util.ArrayList ; 23 24 import common.Assert; 25 import common.Logger; 26 27 import jxl.WorkbookSettings; 28 import jxl.biff.RecordData; 29 import jxl.biff.StringHelper; 30 import jxl.biff.IntegerHelper; 31 32 36 public class NameRecord extends RecordData 37 { 38 41 private static Logger logger = Logger.getLogger(NameRecord.class); 42 43 46 private String name; 47 48 51 private int index; 52 53 57 private int sheetRef = 0; 58 59 62 private boolean isbiff8; 63 64 67 private static class Biff7 {}; 68 public static Biff7 biff7 = new Biff7(); 69 70 private static final int commandMacro = 0xc; 72 private static final int builtIn = 0x20; 73 74 private static final int cellReference = 0x3a; 76 private static final int areaReference = 0x3b; 77 private static final int subExpression = 0x29; 78 private static final int union = 0x10; 79 80 83 public class NameRange 84 { 85 88 private int columnFirst; 89 90 93 private int rowFirst; 94 95 98 private int columnLast; 99 100 103 private int rowLast; 104 105 108 private int externalSheet; 109 110 119 NameRange(int s1, int c1, int r1, int c2, int r2) 120 { 121 columnFirst = c1; 122 rowFirst = r1; 123 columnLast = c2; 124 rowLast = r2; 125 externalSheet = s1; 126 } 127 128 133 public int getFirstColumn() 134 { 135 return columnFirst; 136 } 137 138 143 public int getFirstRow() 144 { 145 return rowFirst; 146 } 147 148 153 public int getLastColumn() 154 { 155 return columnLast; 156 } 157 158 163 public int getLastRow() 164 { 165 return rowLast; 166 } 167 168 173 public int getExternalSheet() 174 { 175 return externalSheet; 176 } 177 } 178 179 182 private ArrayList ranges; 183 184 187 private static final String [] builtInNames = 188 { "Consolidate_Area", 189 "Auto_Open", 190 "Auto_Close", 191 "Extract", 192 "Database", 193 "Criteria", 194 "Print_Area", 195 "Print_Titles", 196 "Recorder", 197 "Data_Form", 198 "Auto_Activate", 199 "Auto_Deactivate", 200 "Sheet_Title", 201 "_FilterDatabase" }; 202 203 210 NameRecord(Record t, WorkbookSettings ws, int ind) 211 { 212 super(t); 213 index = ind; 214 isbiff8 = true; 215 216 try 217 { 218 ranges = new ArrayList (); 219 220 byte[] data = getRecord().getData(); 221 int option = IntegerHelper.getInt(data[0], data[1]); 222 int length = data[3]; 223 sheetRef = IntegerHelper.getInt(data[8],data[9]); 224 225 if ((option & builtIn) != 0) 226 { 227 name = (data[15] < 0x0d) ? builtInNames[data[15]] : 229 "Builtin_" + Integer.toString(data[15], 16); 230 return; 231 } 232 233 name = StringHelper.getString(data, length, 15, ws); 234 235 if ((option & commandMacro) != 0) 236 { 237 return; 239 } 240 241 int pos = length + 15; 242 243 if (data[pos] == cellReference) 244 { 245 int sheet = IntegerHelper.getInt(data[pos + 1], data[pos + 2]); 246 int row = IntegerHelper.getInt(data[pos + 3], data[pos + 4]); 247 int columnMask = IntegerHelper.getInt(data[pos + 5], data[pos + 6]); 248 int column = columnMask & 0xff; 249 250 Assert.verify((columnMask & 0xc0000) == 0); 252 253 NameRange r = new NameRange(sheet, column, row, column, row); 254 ranges.add(r); 255 } 256 else if (data[pos] == areaReference) 257 { 258 int sheet1 = 0; 259 int sheet2 = 0; 260 int r1 = 0; 261 int columnMask = 0; 262 int c1 = 0; 263 int r2 = 0; 264 int c2 = 0; 265 NameRange range = null; 266 267 while (pos < data.length) 268 { 269 sheet1 = IntegerHelper.getInt(data[pos + 1], data[pos + 2]); 270 r1 = IntegerHelper.getInt(data[pos + 3], data[pos + 4]); 271 r2 = IntegerHelper.getInt(data[pos + 5], data[pos + 6]); 272 273 columnMask = IntegerHelper.getInt(data[pos + 7], data[pos + 8]); 274 c1 = columnMask & 0xff; 275 276 Assert.verify((columnMask & 0xc0000) == 0); 278 279 columnMask = IntegerHelper.getInt(data[pos + 9], data[pos + 10]); 280 c2 = columnMask & 0xff; 281 282 Assert.verify((columnMask & 0xc0000) == 0); 284 285 range = new NameRange(sheet1, c1, r1, c2, r2); 286 ranges.add(range); 287 288 pos += 11; 289 } 290 } 291 else if (data[pos] == subExpression) 292 { 293 int sheet1 = 0; 294 int sheet2 = 0; 295 int r1 = 0; 296 int columnMask = 0; 297 int c1 = 0; 298 int r2 = 0; 299 int c2 = 0; 300 NameRange range = null; 301 302 if (pos < data.length && 304 data[pos] != cellReference && 305 data[pos] != areaReference) 306 { 307 if (data[pos] == subExpression) 308 { 309 pos += 3; 310 } 311 else if (data[pos] == union) 312 { 313 pos += 1; 314 } 315 } 316 317 while (pos < data.length) 318 { 319 sheet1 = IntegerHelper.getInt(data[pos + 1], data[pos + 2]); 320 r1 = IntegerHelper.getInt(data[pos + 3], data[pos + 4]); 321 r2 = IntegerHelper.getInt(data[pos + 5], data[pos + 6]); 322 323 columnMask = IntegerHelper.getInt(data[pos + 7], data[pos + 8]); 324 c1 = columnMask & 0xff; 325 326 Assert.verify((columnMask & 0xc0000) == 0); 328 329 columnMask = IntegerHelper.getInt(data[pos + 9], data[pos + 10]); 330 c2 = columnMask & 0xff; 331 332 Assert.verify((columnMask & 0xc0000) == 0); 334 335 range = new NameRange(sheet1, c1, r1, c2, r2); 336 ranges.add(range); 337 338 pos += 11; 339 340 if (pos < data.length && 342 data[pos] != cellReference && 343 data[pos] != areaReference) 344 { 345 if (data[pos] == subExpression) 346 { 347 pos += 3; 348 } 349 else if (data[pos] == union) 350 { 351 pos += 1; 352 } 353 } 354 } 355 } 356 } 357 catch (Throwable t1) 358 { 359 logger.warn("Cannot read name"); 363 name = "ERROR"; 364 } 365 } 366 367 375 NameRecord(Record t, WorkbookSettings ws, int ind, Biff7 dummy) 376 { 377 super(t); 378 index = ind; 379 isbiff8 = false; 380 381 try 382 { 383 ranges = new ArrayList (); 384 byte[] data = getRecord().getData(); 385 int length = data[3]; 386 sheetRef = IntegerHelper.getInt(data[8], data[9]); 387 name = StringHelper.getString(data, length, 14, ws); 388 389 int pos = length + 14; 390 391 if (pos >= data.length) 392 { 393 return; 395 } 396 397 if (data[pos] == cellReference) 398 { 399 int sheet = IntegerHelper.getInt(data[pos + 11], data[pos + 12]); 400 int row = IntegerHelper.getInt(data[pos + 15], data[pos + 16]); 401 int column = data[pos + 17]; 402 403 NameRange r = new NameRange(sheet, column, row, column, row); 404 ranges.add(r); 405 } 406 else if (data[pos] == areaReference) 407 { 408 int sheet1 = 0; 409 int sheet2 = 0; 410 int r1 = 0; 411 int c1 = 0; 412 int r2 = 0; 413 int c2 = 0; 414 NameRange range = null; 415 416 while (pos < data.length) 417 { 418 sheet1 = IntegerHelper.getInt(data[pos + 11], data[pos + 12]); 419 sheet2 = IntegerHelper.getInt(data[pos + 13], data[pos + 14]); 420 r1 = IntegerHelper.getInt(data[pos + 15], data[pos + 16]); 421 r2 = IntegerHelper.getInt(data[pos + 17], data[pos + 18]); 422 423 c1 = data[pos + 19]; 424 c2 = data[pos + 20]; 425 426 range = new NameRange(sheet1, c1, r1, c2, r2); 427 ranges.add(range); 428 429 pos += 21; 430 } 431 } 432 else if (data[pos] == subExpression) 433 { 434 int sheet1 = 0; 435 int sheet2 = 0; 436 int r1 = 0; 437 int c1 = 0; 438 int r2 = 0; 439 int c2 = 0; 440 NameRange range = null; 441 442 if (pos < data.length && 444 data[pos] != cellReference && 445 data[pos] != areaReference) 446 { 447 if (data[pos] == subExpression) 448 { 449 pos += 3; 450 } 451 else if (data[pos] == union) 452 { 453 pos += 1; 454 } 455 } 456 457 while (pos < data.length) 458 { 459 sheet1 = IntegerHelper.getInt(data[pos + 11], data[pos + 12]); 460 sheet2 = IntegerHelper.getInt(data[pos + 13], data[pos + 14]); 461 r1 = IntegerHelper.getInt(data[pos + 15], data[pos + 16]); 462 r2 = IntegerHelper.getInt(data[pos + 17], data[pos + 18]); 463 464 c1 = data[pos + 19]; 465 c2 = data[pos + 20]; 466 467 range = new NameRange(sheet1, c1, r1, c2, r2); 468 ranges.add(range); 469 470 pos += 21; 471 472 if (pos < data.length && 474 data[pos] != cellReference && 475 data[pos] != areaReference) 476 { 477 if (data[pos] == subExpression) 478 { 479 pos += 3; 480 } 481 else if (data[pos] == union) 482 { 483 pos += 1; 484 } 485 } 486 } 487 } 488 } 489 catch (Throwable t1) 490 { 491 logger.warn("Cannot read name."); 495 name = "ERROR"; 496 } 497 } 498 499 504 public String getName() 505 { 506 return name; 507 } 508 509 515 public NameRange[] getRanges() 516 { 517 NameRange[] nr = new NameRange[ranges.size()]; 518 return (NameRange[]) ranges.toArray(nr); 519 } 520 521 526 int getIndex() 527 { 528 return index; 529 } 530 531 537 public int getSheetRef() 538 { 539 return sheetRef; 540 } 541 542 547 public void setSheetRef(int i) 548 { 549 sheetRef = i; 550 } 551 552 557 public byte[] getData() 558 { 559 return getRecord().getData(); 560 } 561 562 567 public boolean isBiff8() 568 { 569 return isbiff8; 570 } 571 } 572 573 | Popular Tags |