/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.hssf.extractor; import static org.apache.poi.hssf.model.InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME; import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES; import java.io.BufferedInputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.hssf.OldExcelFormatException; import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.CodepageRecord; import org.apache.poi.hssf.record.FormulaRecord; import org.apache.poi.hssf.record.NumberRecord; import org.apache.poi.hssf.record.OldFormulaRecord; import org.apache.poi.hssf.record.OldLabelRecord; import org.apache.poi.hssf.record.OldSheetRecord; import org.apache.poi.hssf.record.OldStringRecord; import org.apache.poi.hssf.record.RKRecord; import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DocumentNode; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NotOLE2FileException; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.util.IOUtils; /** * A text extractor for old Excel files, which are too old for * HSSFWorkbook to handle. This includes Excel 95, and very old * (pre-OLE2) Excel files, such as Excel 4 files. *
* Returns much (but not all) of the textual content of the file, * suitable for indexing by something like Apache Lucene, or used * by Apache Tika, but not really intended for display to the user. *
*/ public class OldExcelExtractor implements Closeable { private final static int FILE_PASS_RECORD_SID = 0x2f; private RecordInputStream ris; // sometimes we hold the stream here and thus need to ensure it is closed at some point private Closeable toClose; private int biffVersion; private int fileType; public OldExcelExtractor(InputStream input) throws IOException { open(input); } public OldExcelExtractor(File f) throws IOException { NPOIFSFileSystem poifs = null; try { poifs = new NPOIFSFileSystem(f); open(poifs); toClose = poifs; return; } catch (OldExcelFormatException e) { // will be handled by workaround below } catch (NotOLE2FileException e) { // will be handled by workaround below } catch (IOException e) { // ensure streams are closed correctly throw e; } catch (RuntimeException e) { // ensure streams are closed correctly throw e; } finally { if (toClose == null) { IOUtils.closeQuietly(poifs); } } @SuppressWarnings("resource") FileInputStream biffStream = new FileInputStream(f); // NOSONAR try { open(biffStream); } catch (IOException e) { // ensure that the stream is properly closed here if an Exception // is thrown while opening biffStream.close(); throw e; } catch (RuntimeException e) { // ensure that the stream is properly closed here if an Exception // is thrown while opening biffStream.close(); throw e; } } public OldExcelExtractor(NPOIFSFileSystem fs) throws IOException { open(fs); } public OldExcelExtractor(DirectoryNode directory) throws IOException { open(directory); } private void open(InputStream biffStream) throws IOException { BufferedInputStream bis = (biffStream instanceof BufferedInputStream) ? (BufferedInputStream)biffStream : new BufferedInputStream(biffStream, 8); if (NPOIFSFileSystem.hasPOIFSHeader(bis)) { NPOIFSFileSystem poifs = new NPOIFSFileSystem(bis); try { open(poifs); } finally { poifs.close(); } } else { ris = new RecordInputStream(bis); toClose = bis; prepare(); } } private void open(NPOIFSFileSystem fs) throws IOException { open(fs.getRoot()); } private void open(DirectoryNode directory) throws IOException { DocumentNode book; try { book = (DocumentNode)directory.getEntry(OLD_WORKBOOK_DIR_ENTRY_NAME); } catch (FileNotFoundException e) { // some files have "Workbook" instead book = (DocumentNode)directory.getEntry(WORKBOOK_DIR_ENTRY_NAMES[0]); } if (book == null) { throw new IOException("No Excel 5/95 Book stream found"); } ris = new RecordInputStream(directory.createDocumentInputStream(book)); prepare(); } public static void main(String[] args) throws IOException { if (args.length < 1) { System.err.println("Use:"); System.err.println(" OldExcelExtractor