Logo Search packages:      
Sourcecode: jexcelapi version File versions  Download package

void jxl::read::biff::WorkbookParser::parse (  )  throws BiffException, PasswordException [inline, protected, virtual]

Does the hard work of building up the object graph from the excel bytes

Exceptions:
BiffException 
PasswordException if the workbook is password protected

Implements jxl::Workbook.

Definition at line 544 of file WorkbookParser.java.

References jxl::biff::drawing::DrawingGroup::add(), jxl::read::biff::Record::addContinueRecord(), jxl::biff::Fonts::addFont(), jxl::biff::FormattingRecords::addFormat(), addInFunctions, jxl::read::biff::SheetImpl::addLocalName(), addSheet(), jxl::biff::FormattingRecords::addStyle(), jxl::biff::Type::BOF, bofs, jxl::biff::Type::BOUNDSHEET, boundsheets, buttonPropertySet, jxl::biff::Type::BUTTONPROPERTYSET, jxl::biff::Type::CODEPAGE, containsMacros(), jxl::biff::Type::CONTINUE, jxl::biff::Type::COUNTRY, countryRecord, drawingGroup, jxl::biff::Type::EOF, jxl::biff::Type::EXCEL9FILE, excelFile, jxl::biff::Type::EXTERNNAME, externSheet, jxl::biff::Type::EXTERNSHEET, jxl::biff::Type::FILEPASS, jxl::biff::Type::FONT, fonts, jxl::biff::Type::FORMAT, formattingRecords, jxl::read::biff::NameRecord::getBuiltInName(), jxl::read::biff::CodepageRecord::getCharacterSet(), jxl::WorkbookSettings::getDrawingsDisabled(), jxl::read::biff::Excel9FileRecord::getExcel9File(), jxl::read::biff::HideobjRecord::getHideMode(), jxl::read::biff::BoundsheetRecord::getName(), jxl::read::biff::ExternalNameRecord::getName(), jxl::read::biff::NameRecord::getName(), getNumberOfSheets(), jxl::read::biff::RefreshAllRecord::getRefreshAll(), jxl::read::biff::NameRecord::getSheetRef(), jxl::read::biff::TemplateRecord::getTemplate(), jxl::read::biff::Record::getType(), jxl::read::biff::WindowProtectedRecord::getWindowProtected(), jxl::read::biff::WriteAccessRecord::getWriteAccess(), jxl::read::biff::File::hasNext(), jxl::biff::Type::HIDEOBJ, jxl::read::biff::NineteenFourRecord::is1904(), jxl::read::biff::ExternalNameRecord::isAddInFunction(), jxl::read::biff::BOFRecord::isBiff7(), jxl::read::biff::BOFRecord::isBiff8(), jxl::read::biff::BOFRecord::isChart(), jxl::read::biff::BoundsheetRecord::isChart(), jxl::read::biff::NameRecord::isGlobal(), jxl::read::biff::BoundsheetRecord::isHidden(), jxl::read::biff::ProtectRecord::isProtected(), jxl::read::biff::BoundsheetRecord::isSheet(), jxl::read::biff::BOFRecord::isWorkbookGlobals(), jxl::read::biff::BOFRecord::isWorksheet(), logger, msoDrawingGroup, jxl::biff::Type::MSODRAWINGGROUP, jxl::biff::Type::NAME, namedRecords, nameTable, jxl::read::biff::File::next(), nineteenFour, jxl::biff::Type::NINETEENFOUR, jxl::biff::Type::OBJPROJ, jxl::biff::Type::PALETTE, jxl::read::biff::File::peek(), jxl::biff::BuiltInName::PRINT_AREA, jxl::biff::BuiltInName::PRINT_TITLES, jxl::biff::Type::PROTECT, jxl::biff::Type::REFRESHALL, jxl::WorkbookSettings::setCharacterSet(), jxl::WorkbookSettings::setExcel9File(), jxl::read::biff::SheetImpl::setHidden(), jxl::WorkbookSettings::setHideobj(), jxl::read::biff::SheetImpl::setName(), jxl::biff::FormattingRecords::setPalette(), jxl::WorkbookSettings::setRefreshAll(), jxl::WorkbookSettings::setTemplate(), settings, jxl::WorkbookSettings::setWindowProtected(), jxl::WorkbookSettings::setWriteAccess(), sharedStrings, sheets, jxl::biff::Type::SST, jxl::biff::Type::SUPBOOK, supbooks, jxl::biff::Type::TEMPLATE, jxl::common::Logger::warn(), wbProtected, jxl::biff::Type::WINDOWPROTECT, workbookBof, jxl::biff::Type::WRITEACCESS, jxl::biff::Type::XCT, and jxl::biff::Type::XF.

  {
    Record r = null;

    BOFRecord bof = new BOFRecord(excelFile.next());
    workbookBof = bof;
    bofs++;

    if (!bof.isBiff8() && !bof.isBiff7())
    {
      throw new BiffException(BiffException.unrecognizedBiffVersion);
    }

    if (!bof.isWorkbookGlobals())
    {
      throw new BiffException(BiffException.expectedGlobals);
    }
    ArrayList continueRecords = new ArrayList();
    ArrayList localNames = new ArrayList();
    nameTable = new ArrayList();
    addInFunctions = new ArrayList();

    // Skip to the first worksheet
    while (bofs == 1)
    {
      r = excelFile.next();

      if (r.getType() == Type.SST)
      {
        continueRecords.clear();
        Record nextrec = excelFile.peek();
        while (nextrec.getType() == Type.CONTINUE)
        {
          continueRecords.add(excelFile.next());
          nextrec = excelFile.peek();
        }

        // cast the array
        Record[] records = new Record[continueRecords.size()];
        records = (Record[]) continueRecords.toArray(records);

        sharedStrings = new SSTRecord(r, records, settings);
      }
      else if (r.getType() == Type.FILEPASS)
      {
        throw new PasswordException();
      }
      else if (r.getType() == Type.NAME)
      {
        NameRecord nr = null;

        if (bof.isBiff8())
        {
          nr = new NameRecord(r, settings, nameTable.size());
        
        }
        else
        {
          nr = new NameRecord(r, settings, nameTable.size(),
                              NameRecord.biff7);
        }

        // Add all local and global names to the name table in order to
        // preserve the indexing
        nameTable.add(nr);

        if (nr.isGlobal())
        {
          namedRecords.put(nr.getName(), nr);
        }
        else
        {
          localNames.add(nr);
        }
      }
      else if (r.getType() == Type.FONT)
      {
        FontRecord fr = null;

        if (bof.isBiff8())
        {
          fr = new FontRecord(r, settings);
        }
        else
        {
          fr = new FontRecord(r, settings, FontRecord.biff7);
        }
        fonts.addFont(fr);
      }
      else if (r.getType() == Type.PALETTE)
      {
        PaletteRecord palette = new PaletteRecord(r);
        formattingRecords.setPalette(palette);
      }
      else if (r.getType() == Type.NINETEENFOUR)
      {
        NineteenFourRecord nr = new NineteenFourRecord(r);
        nineteenFour = nr.is1904();
      }
      else if (r.getType() == Type.FORMAT)
      {
        FormatRecord fr = null;
        if (bof.isBiff8())
        {
          fr = new FormatRecord(r, settings, FormatRecord.biff8);
        }
        else
        {
          fr = new FormatRecord(r, settings, FormatRecord.biff7);
        }
        try
        {
          formattingRecords.addFormat(fr);
        }
        catch (NumFormatRecordsException e)
        {
          e.printStackTrace();
          // This should not happen.  Bomb out
          Assert.verify(false, e.getMessage());
        }
      }
      else if (r.getType() == Type.XF)
      {
        XFRecord xfr = null;
        if (bof.isBiff8())
        {
          xfr = new XFRecord(r, settings, XFRecord.biff8);
        }
        else
        {
          xfr = new XFRecord(r, settings, XFRecord.biff7);
        }

        try
        {
          formattingRecords.addStyle(xfr);
        }
        catch (NumFormatRecordsException e)
        {
          // This should not happen.  Bomb out
          Assert.verify(false, e.getMessage());
        }
      }
      else if (r.getType() == Type.BOUNDSHEET)
      {
        BoundsheetRecord br = null;

        if (bof.isBiff8())
        {
          br = new BoundsheetRecord(r, settings);
        }
        else
        {
          br = new BoundsheetRecord(r, BoundsheetRecord.biff7);
        }

        if (br.isSheet())
        {
          boundsheets.add(br);
        }
        else if (br.isChart() && !settings.getDrawingsDisabled())
        {
          boundsheets.add(br);
        }
      }
      else if (r.getType() == Type.EXTERNSHEET)
      {
        if (bof.isBiff8())
        {
          externSheet = new ExternalSheetRecord(r, settings);
        }
        else
        {
          externSheet = new ExternalSheetRecord(r, settings,
                                                ExternalSheetRecord.biff7);
        }
      }
      else if (r.getType() == Type.XCT)
      {
        XCTRecord xctr = new XCTRecord(r);
        xctRecords.add(xctr);
      }
      else if (r.getType() == Type.CODEPAGE)
      {
        CodepageRecord cr = new CodepageRecord(r);
        settings.setCharacterSet(cr.getCharacterSet());
      }
      else if (r.getType() == Type.SUPBOOK)
      {
        Record nextrec = excelFile.peek();
        while (nextrec.getType() == Type.CONTINUE)
        {
          r.addContinueRecord(excelFile.next());
          nextrec = excelFile.peek();
        }

        SupbookRecord sr = new SupbookRecord(r, settings);
        supbooks.add(sr);
      }
      else if (r.getType() == Type.EXTERNNAME)
      {
        ExternalNameRecord enr = new ExternalNameRecord(r, settings);

        if (enr.isAddInFunction())
        {
          addInFunctions.add(enr.getName());
        }
      }
      else if (r.getType() == Type.PROTECT)
      {
        ProtectRecord pr = new ProtectRecord(r);
        wbProtected = pr.isProtected();
      }
      else if (r.getType() == Type.OBJPROJ)
      {
        containsMacros = true;
      }
      else if (r.getType() == Type.COUNTRY)
      {
        countryRecord = new CountryRecord(r);
      }
      else if (r.getType() == Type.MSODRAWINGGROUP)
      {
        if (!settings.getDrawingsDisabled())
        {
          msoDrawingGroup = new MsoDrawingGroupRecord(r);

          if (drawingGroup == null)
          {
            drawingGroup = new DrawingGroup(Origin.READ);
          }

          drawingGroup.add(msoDrawingGroup);

          Record nextrec = excelFile.peek();
          while (nextrec.getType() == Type.CONTINUE)
          {
            drawingGroup.add(excelFile.next());
            nextrec = excelFile.peek();
          }
        }
      }
      else if (r.getType() == Type.BUTTONPROPERTYSET)
      {
        buttonPropertySet = new ButtonPropertySetRecord(r);
      }
      else if (r.getType() == Type.EOF)
      {
        bofs--;
      }
      else if (r.getType() == Type.REFRESHALL)
      {
        RefreshAllRecord rfm = new RefreshAllRecord(r);
        settings.setRefreshAll(rfm.getRefreshAll());
      }
      else if (r.getType() == Type.TEMPLATE)
      {
        TemplateRecord rfm = new TemplateRecord(r);
        settings.setTemplate(rfm.getTemplate());
      }
      else if (r.getType() == Type.EXCEL9FILE)
      {
        Excel9FileRecord e9f = new Excel9FileRecord(r);
        settings.setExcel9File(e9f.getExcel9File());
      }
      else if (r.getType() == Type.WINDOWPROTECT)
      {
        WindowProtectedRecord winp = new WindowProtectedRecord(r);
        settings.setWindowProtected(winp.getWindowProtected());
      }
      else if (r.getType() == Type.HIDEOBJ)
      {
        HideobjRecord hobj = new HideobjRecord(r);
        settings.setHideobj(hobj.getHideMode());
      }
      else if (r.getType() == Type.WRITEACCESS)
      {
        WriteAccessRecord war = new WriteAccessRecord(r, bof.isBiff8(),
                                                      settings);
        settings.setWriteAccess(war.getWriteAccess());
      }
      else
      {
        // logger.info("Unsupported record type: " +
        //            Integer.toHexString(r.getCode())+"h");
      }
    }

    bof = null;
    if (excelFile.hasNext())
    {
      r = excelFile.next();

      if (r.getType() == Type.BOF)
      {
        bof = new BOFRecord(r);
      }
    }

    // Only get sheets for which there is a corresponding Boundsheet record
    while (bof != null && getNumberOfSheets() < boundsheets.size())
    {
      if (!bof.isBiff8() && !bof.isBiff7())
      {
        throw new BiffException(BiffException.unrecognizedBiffVersion);
      }

      if (bof.isWorksheet())
      {
        // Read the sheet in
        SheetImpl s = new SheetImpl(excelFile,
                                    sharedStrings,
                                    formattingRecords,
                                    bof,
                                    workbookBof,
                                    nineteenFour,
                                    this);

        BoundsheetRecord br = (BoundsheetRecord) boundsheets.get
          (getNumberOfSheets());
        s.setName(br.getName());
        s.setHidden(br.isHidden());
        addSheet(s);
      }
      else if (bof.isChart())
      {
        // Read the sheet in
        SheetImpl s = new SheetImpl(excelFile,
                                    sharedStrings,
                                    formattingRecords,
                                    bof,
                                    workbookBof,
                                    nineteenFour,
                                    this);

        BoundsheetRecord br = (BoundsheetRecord) boundsheets.get
          (getNumberOfSheets());
        s.setName(br.getName());
        s.setHidden(br.isHidden());
        addSheet(s);
      }
      else
      {
        logger.warn("BOF is unrecognized");


        while (excelFile.hasNext() && r.getType() != Type.EOF)
        {
          r = excelFile.next();
        }
      }

      // The next record will normally be a BOF or empty padding until
      // the end of the block is reached.  In exceptionally unlucky cases,
      // the last EOF  will coincide with a block division, so we have to
      // check there is more data to retrieve.
      // Thanks to liamg for spotting this
      bof = null;
      if (excelFile.hasNext())
      {
        r = excelFile.next();

        if (r.getType() == Type.BOF)
        {
          bof = new BOFRecord(r);
        }
      }
    }

    // Add all the local names to the specific sheets
    for (Iterator it = localNames.iterator() ; it.hasNext() ;)
    {
      NameRecord nr  = (NameRecord) it.next();

      if (nr.getBuiltInName() == null)
      {
        logger.warn("Usage of a local non-builtin name");
      } 
      else if (nr.getBuiltInName() == BuiltInName.PRINT_AREA || 
               nr.getBuiltInName() == BuiltInName.PRINT_TITLES)
      {
        // appears to use the internal tab number rather than the
        // external sheet index
        SheetImpl s = (SheetImpl) sheets.get(nr.getSheetRef() - 1);
        s.addLocalName(nr);
      }
    }
  }


Generated by  Doxygen 1.6.0   Back to index