KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > read > biff > WorkbookParser


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.read.biff;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import common.Assert;
27 import common.Logger;
28
29 import jxl.Workbook;
30 import jxl.Sheet;
31 import jxl.Cell;
32 import jxl.Range;
33 import jxl.WorkbookSettings;
34 import jxl.biff.RangeImpl;
35 import jxl.biff.Type;
36 import jxl.biff.FormatRecord;
37 import jxl.biff.Fonts;
38 import jxl.biff.FontRecord;
39 import jxl.biff.XFRecord;
40 import jxl.biff.FormattingRecords;
41 import jxl.biff.NumFormatRecordsException;
42 import jxl.biff.PaletteRecord;
43 import jxl.biff.WorkbookMethods;
44 import jxl.biff.formula.ExternalSheet;
45 import jxl.biff.drawing.DrawingGroup;
46 import jxl.biff.drawing.Origin;
47 import jxl.biff.drawing.MsoDrawingGroupRecord;
48
49 /**
50  * Parses the biff file passed in, and builds up an internal representation of
51  * the spreadsheet
52  */

53 public class WorkbookParser extends Workbook
54   implements ExternalSheet, WorkbookMethods
55 {
56   /**
57    * The logger
58    */

59   private static Logger logger = Logger.getLogger(WorkbookParser.class);
60
61   /**
62    * The excel file
63    */

64   private File excelFile;
65   /**
66    * The number of open bofs
67    */

68   private int bofs;
69   /**
70    * Indicates whether or not the dates are based around the 1904 date system
71    */

72   private boolean nineteenFour;
73   /**
74    * The shared string table
75    */

76   private SSTRecord sharedStrings;
77   /**
78    * The names of all the worksheets
79    */

80   private ArrayList JavaDoc boundsheets;
81   /**
82    * The xf records
83    */

84   private FormattingRecords formattingRecords;
85   /**
86    * The fonts used by this workbook
87    */

88   private Fonts fonts;
89
90   /**
91    * The sheets contained in this workbook
92    */

93   private ArrayList JavaDoc sheets;
94
95   /**
96    * The last sheet accessed
97    */

98   private SheetImpl lastSheet;
99
100   /**
101    * The index of the last sheet retrieved
102    */

103   private int lastSheetIndex;
104
105   /**
106    * The named records found in this workbook
107    */

108   private HashMap JavaDoc namedRecords;
109
110   /**
111    * The list of named records
112    */

113   private ArrayList JavaDoc nameTable;
114
115   /**
116    * The external sheet record. Used by formulas, and names
117    */

118   private ExternalSheetRecord externSheet;
119
120   /**
121    * The list of supporting workbooks - used by formulas
122    */

123   private ArrayList JavaDoc supbooks;
124
125   /**
126    * The bof record for this workbook
127    */

128   private BOFRecord workbookBof;
129
130   /**
131    * The Mso Drawing Group record for this workbook
132    */

133   private MsoDrawingGroupRecord msoDrawingGroup;
134
135   /**
136    * The property set record associated with this workbook
137    */

138   private ButtonPropertySetRecord buttonPropertySet;
139
140   /**
141    * Workbook protected flag
142    */

143   private boolean wbProtected;
144
145   /**
146    * Contains macros flag
147    */

148   private boolean containsMacros;
149
150   /**
151    * The workbook settings
152    */

153   private WorkbookSettings settings;
154
155   /**
156    * The drawings contained in this workbook
157    */

158   private DrawingGroup drawingGroup;
159
160   /**
161    * The country record (containing the language and regional settings)
162    * for this workbook
163    */

164   private CountryRecord countryRecord;
165
166   /**
167    * Constructs this object from the raw excel data
168    *
169    * @param f the excel 97 biff file
170    * @param s the workbook settings
171    */

172   public WorkbookParser(File f, WorkbookSettings s)
173   {
174     super();
175     excelFile = f;
176     boundsheets = new ArrayList JavaDoc(10);
177     fonts = new Fonts();
178     formattingRecords = new FormattingRecords(fonts);
179     sheets = new ArrayList JavaDoc(10);
180     supbooks = new ArrayList JavaDoc(10);
181     namedRecords = new HashMap JavaDoc();
182     lastSheetIndex = -1;
183     wbProtected = false;
184     containsMacros = false;
185     settings = s;
186   }
187
188  /**
189    * Gets the sheets within this workbook.
190    * NOTE: Use of this method for
191    * very large worksheets can cause performance and out of memory problems.
192    * Use the alternative method getSheet() to retrieve each sheet individually
193    *
194    * @return an array of the individual sheets
195    */

196   public Sheet[] getSheets()
197   {
198     Sheet[] sheetArray = new Sheet[getNumberOfSheets()];
199     return (Sheet[]) sheets.toArray(sheetArray);
200   }
201
202   /**
203    * Interface method from WorkbookMethods - gets the specified
204    * sheet within this workbook
205    *
206    * @param index the zero based index of the required sheet
207    * @return The sheet specified by the index
208    */

209   public Sheet getReadSheet(int index)
210   {
211     return getSheet(index);
212   }
213
214   /**
215    * Gets the specified sheet within this workbook
216    *
217    * @param index the zero based index of the required sheet
218    * @return The sheet specified by the index
219    */

220   public Sheet getSheet(int index)
221   {
222     // First see if the last sheet index is the same as this sheet index.
223
// If so, then the same sheet is being re-requested, so simply
224
// return it instead of rereading it
225
if ((lastSheet != null) && lastSheetIndex == index)
226     {
227       return lastSheet;
228     }
229
230     // Flush out all of the cached data in the last sheet
231
if (lastSheet != null)
232     {
233       lastSheet.clear();
234
235       if (!settings.getGCDisabled())
236       {
237         System.gc();
238       }
239     }
240
241     lastSheet = (SheetImpl) sheets.get(index);
242     lastSheetIndex = index;
243     lastSheet.readSheet();
244
245     return lastSheet;
246   }
247
248   /**
249    * Gets the sheet with the specified name from within this workbook
250    *
251    * @param name the sheet name
252    * @return The sheet with the specified name, or null if it is not found
253    */

254   public Sheet getSheet(String JavaDoc name)
255   {
256     // Iterate through the boundsheet records
257
int pos = 0;
258     boolean found = false;
259     Iterator JavaDoc i = boundsheets.iterator();
260     BoundsheetRecord br = null;
261
262     while (i.hasNext() && !found)
263     {
264       br = (BoundsheetRecord) i.next();
265
266       if (br.getName().equals(name))
267       {
268         found = true;
269       }
270       else
271       {
272         pos++;
273       }
274     }
275
276     return found ? getSheet(pos) : null;
277   }
278
279   /**
280    * Gets the sheet names
281    *
282    * @return an array of strings containing the sheet names
283    */

284   public String JavaDoc[] getSheetNames()
285   {
286     String JavaDoc[] names = new String JavaDoc[boundsheets.size()];
287
288     BoundsheetRecord br = null;
289     for (int i = 0; i < names.length; i++)
290     {
291       br = (BoundsheetRecord) boundsheets.get(i);
292       names[i] = br.getName();
293     }
294
295     return names;
296   }
297
298
299   /**
300    * Package protected function which gets the real internal sheet index
301    * based upon the external sheet reference. This is used for extern sheet
302    * references which are specified in formulas
303    *
304    * @param index the external sheet reference
305    * @return the actual sheet index
306    */

307   public int getExternalSheetIndex(int index)
308   {
309     // For biff7, the whole external reference thing works differently
310
// Hopefully for our purposes sheet references will all be local
311
if (workbookBof.isBiff7())
312     {
313       return index;
314     }
315
316     Assert.verify(externSheet != null);
317
318     int firstTab = externSheet.getFirstTabIndex(index);
319
320     return firstTab;
321   }
322
323   /**
324    * Package protected function which gets the real internal sheet index
325    * based upon the external sheet reference. This is used for extern sheet
326    * references which are specified in formulas
327    *
328    * @param index the external sheet reference
329    * @return the actual sheet index
330    */

331   public int getLastExternalSheetIndex(int index)
332   {
333     // For biff7, the whole external reference thing works differently
334
// Hopefully for our purposes sheet references will all be local
335
if (workbookBof.isBiff7())
336     {
337       return index;
338     }
339
340     Assert.verify(externSheet != null);
341
342     int lastTab = externSheet.getLastTabIndex(index);
343
344     return lastTab;
345   }
346
347   /**
348    * Gets the name of the external sheet specified by the index
349    *
350    * @param index the external sheet index
351    * @return the name of the external sheet
352    */

353   public String JavaDoc getExternalSheetName(int index)
354   {
355     // For biff7, the whole external reference thing works differently
356
// Hopefully for our purposes sheet references will all be local
357
if (workbookBof.isBiff7())
358     {
359       BoundsheetRecord br = (BoundsheetRecord) boundsheets.get(index);
360
361       return br.getName();
362     }
363
364     int supbookIndex = externSheet.getSupbookIndex(index);
365     SupbookRecord sr = (SupbookRecord) supbooks.get(supbookIndex);
366
367     int firstTab = externSheet.getFirstTabIndex(index);
368
369     if (sr.getType() == SupbookRecord.INTERNAL)
370     {
371       // It's an internal reference - get the name from the boundsheets list
372
BoundsheetRecord br = (BoundsheetRecord) boundsheets.get(firstTab);
373
374       return br.getName();
375     }
376     else if (sr.getType() == SupbookRecord.EXTERNAL)
377     {
378       // External reference - get the sheet name from the supbook record
379
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
380       sb.append('[');
381       sb.append(sr.getFileName());
382       sb.append(']');
383       sb.append(sr.getSheetName(firstTab));
384       return sb.toString();
385     }
386
387     // An unknown supbook - return unkown
388
return "[UNKNOWN]";
389   }
390
391   /**
392    * Gets the name of the external sheet specified by the index
393    *
394    * @param index the external sheet index
395    * @return the name of the external sheet
396    */

397   public String JavaDoc getLastExternalSheetName(int index)
398   {
399     // For biff7, the whole external reference thing works differently
400
// Hopefully for our purposes sheet references will all be local
401
if (workbookBof.isBiff7())
402     {
403       BoundsheetRecord br = (BoundsheetRecord) boundsheets.get(index);
404
405       return br.getName();
406     }
407
408     int supbookIndex = externSheet.getSupbookIndex(index);
409     SupbookRecord sr = (SupbookRecord) supbooks.get(supbookIndex);
410
411     int lastTab = externSheet.getLastTabIndex(index);
412
413     if (sr.getType() == SupbookRecord.INTERNAL)
414     {
415       // It's an internal reference - get the name from the boundsheets list
416
BoundsheetRecord br = (BoundsheetRecord) boundsheets.get(lastTab);
417
418       return br.getName();
419     }
420     else if (sr.getType() == SupbookRecord.EXTERNAL)
421     {
422       // External reference - get the sheet name from the supbook record
423
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
424       sb.append('[');
425       sb.append(sr.getFileName());
426       sb.append(']');
427       sb.append(sr.getSheetName(lastTab));
428       return sb.toString();
429     }
430
431     // An unknown supbook - return unkown
432
return "[UNKNOWN]";
433   }
434
435   /**
436    * Returns the number of sheets in this workbook
437    *
438    * @return the number of sheets in this workbook
439    */

440   public int getNumberOfSheets()
441   {
442     return sheets.size();
443   }
444
445   /**
446    * Closes this workbook, and frees makes any memory allocated available
447    * for garbage collection
448    */

449   public void close()
450   {
451     if (lastSheet != null)
452     {
453       lastSheet.clear();
454     }
455     excelFile.clear();
456
457     if (!settings.getGCDisabled())
458     {
459       System.gc();
460     }
461   }
462
463   /**
464    * Adds the sheet to the end of the array
465    *
466    * @param s the sheet to add
467    */

468   final void addSheet(Sheet s)
469   {
470     sheets.add(s);
471   }
472
473   /**
474    * Does the hard work of building up the object graph from the excel bytes
475    *
476    * @exception BiffException
477    * @exception PasswordException if the workbook is password protected
478    */

479   protected void parse() throws BiffException, PasswordException
480   {
481     Record r = null;
482
483     BOFRecord bof = new BOFRecord(excelFile.next());
484     workbookBof = bof;
485     bofs++;
486
487     if (!bof.isBiff8() && !bof.isBiff7())
488     {
489       throw new BiffException(BiffException.unrecognizedBiffVersion);
490     }
491
492     if (!bof.isWorkbookGlobals())
493     {
494       throw new BiffException(BiffException.expectedGlobals);
495     }
496     ArrayList JavaDoc continueRecords = new ArrayList JavaDoc();
497     nameTable = new ArrayList JavaDoc();
498
499     // Skip to the first worksheet
500
while (bofs == 1)
501     {
502       r = excelFile.next();
503
504       if (r.getType() == Type.SST)
505       {
506         continueRecords.clear();
507         Record nextrec = excelFile.peek();
508         while (nextrec.getType() == Type.CONTINUE)
509         {
510           continueRecords.add(excelFile.next());
511           nextrec = excelFile.peek();
512         }
513
514         // cast the array
515
Record[] records = new Record[continueRecords.size()];
516         records = (Record[]) continueRecords.toArray(records);
517
518         sharedStrings = new SSTRecord(r, records, settings);
519       }
520       else if (r.getType() == Type.FILEPASS)
521       {
522         throw new PasswordException();
523       }
524       else if (r.getType() == Type.NAME)
525       {
526         NameRecord nr = null;
527
528         if (bof.isBiff8())
529         {
530           nr = new NameRecord(r, settings, namedRecords.size());
531         
532         }
533         else
534         {
535           nr = new NameRecord(r, settings, namedRecords.size(),
536                               NameRecord.biff7);
537         }
538         namedRecords.put(nr.getName(), nr);
539         nameTable.add(nr);
540       }
541       else if (r.getType() == Type.FONT)
542       {
543         FontRecord fr = null;
544
545         if (bof.isBiff8())
546         {
547           fr = new FontRecord(r, settings);
548         }
549         else
550         {
551           fr = new FontRecord(r, settings, FontRecord.biff7);
552         }
553         fonts.addFont(fr);
554       }
555       else if (r.getType() == Type.PALETTE)
556       {
557         PaletteRecord palette = new PaletteRecord(r);
558         formattingRecords.setPalette(palette);
559       }
560       else if (r.getType() == Type.NINETEENFOUR)
561       {
562         NineteenFourRecord nr = new NineteenFourRecord(r);
563         nineteenFour = nr.is1904();
564       }
565       else if (r.getType() == Type.FORMAT)
566       {
567         FormatRecord fr = null;
568         if (bof.isBiff8())
569         {
570           fr = new FormatRecord(r, settings, FormatRecord.biff8);
571         }
572         else
573         {
574           fr = new FormatRecord(r, settings, FormatRecord.biff7);
575         }
576         try
577         {
578           formattingRecords.addFormat(fr);
579         }
580         catch (NumFormatRecordsException e)
581         {
582           e.printStackTrace();
583           // This should not happen. Bomb out
584
Assert.verify(false, e.getMessage());
585         }
586       }
587       else if (r.getType() == Type.XF)
588       {
589         XFRecord xfr = null;
590         if (bof.isBiff8())
591         {
592           xfr = new XFRecord(r, settings, XFRecord.biff8);
593         }
594         else
595         {
596           xfr = new XFRecord(r, settings, XFRecord.biff7);
597         }
598
599         try
600         {
601           formattingRecords.addStyle(xfr);
602         }
603         catch (NumFormatRecordsException e)
604         {
605           // This should not happen. Bomb out
606
Assert.verify(false, e.getMessage());
607         }
608       }
609       else if (r.getType() == Type.BOUNDSHEET)
610       {
611         BoundsheetRecord br = null;
612
613         if (bof.isBiff8())
614         {
615           br = new BoundsheetRecord(r);
616         }
617         else
618         {
619           br = new BoundsheetRecord(r, BoundsheetRecord.biff7);
620         }
621
622         if (br.isSheet())
623         {
624           boundsheets.add(br);
625         }
626         else if (br.isChart() && !settings.getDrawingsDisabled())
627         {
628           boundsheets.add(br);
629         }
630       }
631       else if (r.getType() == Type.EXTERNSHEET)
632       {
633         if (bof.isBiff8())
634         {
635           externSheet = new ExternalSheetRecord(r, settings);
636         }
637         else
638         {
639           externSheet = new ExternalSheetRecord(r, settings,
640                                                 ExternalSheetRecord.biff7);
641         }
642       }
643       else if (r.getType() == Type.CODEPAGE)
644       {
645         CodepageRecord cr = new CodepageRecord(r);
646         settings.setCharacterSet(cr.getCharacterSet());
647       }
648       else if (r.getType() == Type.SUPBOOK)
649       {
650         Record nextrec = excelFile.peek();
651         while (nextrec.getType() == Type.CONTINUE)
652         {
653           r.addContinueRecord(excelFile.next());
654           nextrec = excelFile.peek();
655         }
656
657         SupbookRecord sr = new SupbookRecord(r, settings);
658         supbooks.add(sr);
659       }
660       else if (r.getType() == Type.PROTECT)
661       {
662         ProtectRecord pr = new ProtectRecord(r);
663         wbProtected = pr.isProtected();
664       }
665       else if (r.getType() == Type.OBJPROJ)
666       {
667         containsMacros = true;
668       }
669       else if (r.getType() == Type.COUNTRY)
670       {
671         countryRecord = new CountryRecord(r);
672       }
673       else if (r.getType() == Type.MSODRAWINGGROUP)
674       {
675         if (!settings.getDrawingsDisabled())
676         {
677           msoDrawingGroup = new MsoDrawingGroupRecord(r);
678
679           if (drawingGroup == null)
680           {
681             drawingGroup = new DrawingGroup(Origin.READ);
682           }
683
684           drawingGroup.add(msoDrawingGroup);
685
686           Record nextrec = excelFile.peek();
687           while (nextrec.getType() == Type.CONTINUE)
688           {
689             drawingGroup.add(excelFile.next());
690             nextrec = excelFile.peek();
691           }
692         }
693       }
694       else if (r.getType() == Type.BUTTONPROPERTYSET)
695       {
696         buttonPropertySet = new ButtonPropertySetRecord(r);
697       }
698       else if (r.getType() == Type.EOF)
699       {
700         bofs--;
701       }
702     }
703
704     bof = null;
705     if (excelFile.hasNext())
706     {
707       r = excelFile.next();
708
709       if (r.getType() == Type.BOF)
710       {
711         bof = new BOFRecord(r);
712       }
713     }
714
715     // Only get sheets for which there is a corresponding Boundsheet record
716
while (bof != null && getNumberOfSheets() < boundsheets.size())
717     {
718       if (!bof.isBiff8() && !bof.isBiff7())
719       {
720         throw new BiffException(BiffException.unrecognizedBiffVersion);
721       }
722
723       if (bof.isWorksheet())
724       {
725         // Read the sheet in
726
SheetImpl s = new SheetImpl(excelFile,
727                                     sharedStrings,
728                                     formattingRecords,
729                                     bof,
730                                     workbookBof,
731                                     nineteenFour,
732                                     this);
733
734         BoundsheetRecord br = (BoundsheetRecord) boundsheets.get
735           (getNumberOfSheets());
736         s.setName(br.getName());
737         s.setHidden(br.isHidden());
738         addSheet(s);
739       }
740       else if (bof.isChart())
741       {
742         // Read the sheet in
743
SheetImpl s = new SheetImpl(excelFile,
744                                     sharedStrings,
745                                     formattingRecords,
746                                     bof,
747                                     workbookBof,
748                                     nineteenFour,
749                                     this);
750
751         BoundsheetRecord br = (BoundsheetRecord) boundsheets.get
752           (getNumberOfSheets());
753         s.setName(br.getName());
754         s.setHidden(br.isHidden());
755         addSheet(s);
756       }
757       else
758       {
759         logger.warn("BOF is unrecognized");
760
761
762         while (excelFile.hasNext() && r.getType() != Type.EOF)
763         {
764           r = excelFile.next();
765         }
766       }
767
768       // The next record will normally be a BOF or empty padding until
769
// the end of the block is reached. In exceptionally unlucky cases,
770
// the last EOF will coincide with a block division, so we have to
771
// check there is more data to retrieve.
772
// Thanks to liamg for spotting this
773
bof = null;
774       if (excelFile.hasNext())
775       {
776         r = excelFile.next();
777
778         if (r.getType() == Type.BOF)
779         {
780           bof = new BOFRecord(r);
781         }
782       }
783     }
784   }
785
786   /**
787    * Accessor for the formattingRecords, used by the WritableWorkbook
788    * when creating a copy of this
789    *
790    * @return the formatting records
791    */

792   public FormattingRecords getFormattingRecords()
793   {
794     return formattingRecords;
795   }
796
797   /**
798    * Accessor for the externSheet, used by the WritableWorkbook
799    * when creating a copy of this
800    *
801    * @return the external sheet record
802    */

803   public ExternalSheetRecord getExternalSheetRecord()
804   {
805     return externSheet;
806   }
807
808   /**
809    * Accessor for the MsoDrawingGroup, used by the WritableWorkbook
810    * when creating a copy of this
811    *
812    * @return the Mso Drawing Group record
813    */

814   public MsoDrawingGroupRecord getMsoDrawingGroupRecord()
815   {
816     return msoDrawingGroup;
817   }
818
819   /**
820    * Accessor for the supbook records, used by the WritableWorkbook
821    * when creating a copy of this
822    *
823    * @return the supbook records
824    */

825   public SupbookRecord[] getSupbookRecords()
826   {
827     SupbookRecord[] sr = new SupbookRecord[supbooks.size()];
828     return (SupbookRecord[]) supbooks.toArray(sr);
829   }
830
831   /**
832    * Accessor for the name records. Used by the WritableWorkbook when
833    * creating a copy of this
834    *
835    * @return the array of names
836    */

837   public NameRecord[] getNameRecords()
838   {
839     NameRecord[] na = new NameRecord[nameTable.size()];
840     return (NameRecord[]) nameTable.toArray(na);
841   }
842
843   /**
844    * Accessor for the fonts, used by the WritableWorkbook
845    * when creating a copy of this
846    * @return the fonts used in this workbook
847    */

848   public Fonts getFonts()
849   {
850     return fonts;
851   }
852
853   /**
854    * Gets the named cell from this workbook. If the name refers to a
855    * range of cells, then the cell on the top left is returned. If
856    * the name cannot be found, null is returned
857    *
858    * @param name the name of the cell/range to search for
859    * @return the cell in the top left of the range if found, NULL
860    * otherwise
861    */

862   public Cell findCellByName(String JavaDoc name)
863   {
864     NameRecord nr = (NameRecord) namedRecords.get(name);
865
866     if (nr == null)
867     {
868       return null;
869     }
870
871     NameRecord.NameRange[] ranges = nr.getRanges();
872
873     // Go and retrieve the first cell in the first range
874
Sheet s = getSheet(ranges[0].getExternalSheet());
875     Cell cell = s.getCell(ranges[0].getFirstColumn(), ranges[0].getFirstRow());
876
877     return cell;
878   }
879
880   /**
881    * Gets the named range from this workbook. The Range object returns
882    * contains all the cells from the top left to the bottom right
883    * of the range.
884    * If the named range comprises an adjacent range,
885    * the Range[] will contain one object; for non-adjacent
886    * ranges, it is necessary to return an array of length greater than
887    * one.
888    * If the named range contains a single cell, the top left and
889    * bottom right cell will be the same cell
890    *
891    * @param name the name to find
892    * @return the range of cells
893    */

894   public Range[] findByName(String JavaDoc name)
895   {
896     NameRecord nr = (NameRecord) namedRecords.get(name);
897
898     if (nr == null)
899     {
900       return null;
901     }
902
903     NameRecord.NameRange[] ranges = nr.getRanges();
904
905     Range[] cellRanges = new Range[ranges.length];
906
907     for (int i = 0; i < ranges.length; i++)
908     {
909       cellRanges[i] = new RangeImpl
910         (this,
911          getExternalSheetIndex(ranges[i].getExternalSheet()),
912          ranges[i].getFirstColumn(),
913          ranges[i].getFirstRow(),
914          getLastExternalSheetIndex(ranges[i].getExternalSheet()),
915          ranges[i].getLastColumn(),
916          ranges[i].getLastRow());
917     }
918
919     return cellRanges;
920   }
921
922   /**
923    * Gets the named ranges
924    *
925    * @return the list of named cells within the workbook
926    */

927   public String JavaDoc[] getRangeNames()
928   {
929     Object JavaDoc[] keys = namedRecords.keySet().toArray();
930     String JavaDoc[] names = new String JavaDoc[keys.length];
931     System.arraycopy(keys, 0, names, 0, keys.length);
932
933     return names;
934   }
935
936   /**
937    * Method used when parsing formulas to make sure we are trying
938    * to parse a supported biff version
939    *
940    * @return the BOF record
941    */

942   public BOFRecord getWorkbookBof()
943   {
944     return workbookBof;
945   }
946
947   /**
948    * Determines whether the sheet is protected
949    *
950    * @return whether or not the sheet is protected
951    */

952   public boolean isProtected()
953   {
954     return wbProtected;
955   }
956
957   /**
958    * Accessor for the settings
959    *
960    * @return the workbook settings
961    */

962   public WorkbookSettings getSettings()
963   {
964     return settings;
965   }
966
967   /**
968    * Accessor/implementation method for the external sheet reference
969    *
970    * @param sheetName the sheet name to look for
971    * @return the external sheet index
972    */

973   public int getExternalSheetIndex(String JavaDoc sheetName)
974   {
975     return 0;
976   }
977
978   /**
979    * Accessor/implementation method for the external sheet reference
980    *
981    * @param sheetName the sheet name to look for
982    * @return the external sheet index
983    */

984   public int getLastExternalSheetIndex(String JavaDoc sheetName)
985   {
986     return 0;
987   }
988
989   /**
990    * Gets the name at the specified index
991    *
992    * @param index the index into the name table
993    * @return the name of the cell
994    */

995   public String JavaDoc getName(int index)
996   {
997     Assert.verify(index >= 0 && index < nameTable.size());
998     return ((NameRecord) nameTable.get(index)).getName();
999   }
1000
1001  /**
1002   * Gets the index of the name record for the name
1003   *
1004   * @param name the name to search for
1005   * @return the index in the name table
1006   */

1007  public int getNameIndex(String JavaDoc name)
1008  {
1009    NameRecord nr = (NameRecord) namedRecords.get(name);
1010
1011    return nr != null ? nr.getIndex() : 0;
1012  }
1013
1014  /**
1015   * Accessor for the drawing group
1016   *
1017   * @return the drawing group
1018   */

1019  public DrawingGroup getDrawingGroup()
1020  {
1021    return drawingGroup;
1022  }
1023
1024  /**
1025   * Accessor for the CompoundFile. For this feature to return non-null
1026   * value, the propertySets feature in WorkbookSettings must be enabled
1027   * and the workbook must contain additional property sets. This
1028   * method is used during the workbook copy
1029   *
1030   * @return the base compound file if it contains additional data items
1031   * and property sets are enabled
1032   */

1033  public CompoundFile getCompoundFile()
1034  {
1035    return excelFile.getCompoundFile();
1036  }
1037
1038  /**
1039   * Accessor for the containsMacros
1040   *
1041   * @return TRUE if this workbook contains macros, FALSE otherwise
1042   */

1043  public boolean containsMacros()
1044  {
1045    return containsMacros;
1046  }
1047
1048  /**
1049   * Accessor for the button property set, used during copying
1050   *
1051   * @return the button property set
1052   */

1053  public ButtonPropertySetRecord getButtonPropertySet()
1054  {
1055    return buttonPropertySet;
1056  }
1057
1058  /**
1059   * Accessor for the country record, using during copying
1060   *
1061   * @return the country record read in
1062   */

1063  public CountryRecord getCountryRecord()
1064  {
1065    return countryRecord;
1066  }
1067}
1068
1069
1070
1071
1072
1073
1074
1075
Popular Tags