/* ====================================================================
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.
==================================================================== */
namespace HH.WMS.Utils.NPOI.HSSF.EventUserModel
{
using System;
using System.Collections;
using HH.WMS.Utils.NPOI.HSSF.Model;
using HH.WMS.Utils.NPOI.HSSF.Record;
using HH.WMS.Utils.NPOI.HSSF.UserModel;
using System.Collections.Generic;
///
/// When working with the EventUserModel, if you want to
/// Process formulas, you need an instance of
/// Workbook to pass to a HSSFWorkbook,
/// to finally give to HSSFFormulaParser,
/// and this will build you stub ones.
/// Since you're working with the EventUserModel, you
/// wouldn't want to Get a full Workbook and
/// HSSFWorkbook, as they would eat too much memory.
/// Instead, you should collect a few key records as they
/// go past, then call this once you have them to build a
/// stub Workbook, and from that a stub
/// HSSFWorkbook, to use with the HSSFFormulaParser.
/// The records you should collect are:
/// ExternSheetRecord
/// BoundSheetRecord
/// You should probably also collect SSTRecord,
/// but it's not required to pass this in.
/// To help, this class includes a HSSFListener wrapper
/// that will do the collecting for you.
///
public class EventWorkbookBuilder
{
///
/// Wraps up your stub Workbook as a stub
/// HSSFWorkbook, ready for passing to
/// HSSFFormulaParser
///
/// The stub workbook.
///
public static HSSFWorkbook CreateStubHSSFWorkbook(InternalWorkbook workbook)
{
return new StubHSSFWorkbook(workbook);
}
///
/// Creates a stub Workbook from the supplied records,
/// suitable for use with the {@link HSSFFormulaParser}
///
/// The ExternSheetRecords in your file
/// The BoundSheetRecords in your file
/// TThe SSTRecord in your file.
/// A stub Workbook suitable for use with HSSFFormulaParser
public static InternalWorkbook CreateStubWorkbook(ExternSheetRecord[] externs,
BoundSheetRecord[] bounds, SSTRecord sst)
{
List wbRecords = new List();
// Core Workbook records go first
if (bounds != null)
{
for (int i = 0; i < bounds.Length; i++)
{
wbRecords.Add(bounds[i]);
}
}
if (sst != null)
{
wbRecords.Add(sst);
}
// Now we can have the ExternSheetRecords,
// preceded by a SupBookRecord
if (externs != null)
{
wbRecords.Add(SupBookRecord.CreateInternalReferences(
(short)externs.Length));
for (int i = 0; i < externs.Length; i++)
{
wbRecords.Add(externs[i]);
}
}
// Finally we need an EoF record
wbRecords.Add(EOFRecord.instance);
return InternalWorkbook.CreateWorkbook(wbRecords);
}
///
/// Creates a stub workbook from the supplied records,
/// suitable for use with the HSSFFormulaParser
///
/// The ExternSheetRecords in your file
/// A stub Workbook suitable for use with HSSFFormulaParser
/// A stub Workbook suitable for use with {@link HSSFFormulaParser}
public static InternalWorkbook CreateStubWorkbook(ExternSheetRecord[] externs,
BoundSheetRecord[] bounds)
{
return CreateStubWorkbook(externs, bounds, null);
}
///
/// A wrapping HSSFListener which will collect
/// BoundSheetRecords and {@link ExternSheetRecord}s as
/// they go past, so you can Create a Stub {@link Workbook} from
/// them once required.
///
public class SheetRecordCollectingListener : HSSFListener
{
private HSSFListener childListener;
private ArrayList boundSheetRecords = new ArrayList();
private ArrayList externSheetRecords = new ArrayList();
private SSTRecord sstRecord = null;
///
/// Initializes a new instance of the class.
///
/// The child listener.
public SheetRecordCollectingListener(HSSFListener childListener)
{
this.childListener = childListener;
}
///
/// Gets the bound sheet records.
///
///
public BoundSheetRecord[] GetBoundSheetRecords()
{
return (BoundSheetRecord[])boundSheetRecords.ToArray(
typeof(BoundSheetRecord)
);
}
///
/// Gets the extern sheet records.
///
///
public ExternSheetRecord[] GetExternSheetRecords()
{
return (ExternSheetRecord[])externSheetRecords.ToArray(
typeof(ExternSheetRecord)
);
}
///
/// Gets the SST record.
///
///
public SSTRecord GetSSTRecord()
{
return sstRecord;
}
///
/// Gets the stub HSSF workbook.
///
///
public HSSFWorkbook GetStubHSSFWorkbook()
{
return CreateStubHSSFWorkbook(
GetStubWorkbook()
);
}
///
/// Gets the stub workbook.
///
///
public InternalWorkbook GetStubWorkbook()
{
return CreateStubWorkbook(
GetExternSheetRecords(), GetBoundSheetRecords(),
GetSSTRecord()
);
}
///
/// Process this record ourselves, and then
/// pass it on to our child listener
///
/// The record.
public void ProcessRecord(Record record)
{
// Handle it ourselves
ProcessRecordInternally(record);
// Now pass on to our child
childListener.ProcessRecord(record);
}
///
/// Process the record ourselves, but do not
/// pass it on to the child Listener.
///
/// The record.
public void ProcessRecordInternally(Record record)
{
if (record is BoundSheetRecord)
{
boundSheetRecords.Add(record);
}
else if (record is ExternSheetRecord)
{
externSheetRecords.Add(record);
}
else if (record is SSTRecord)
{
sstRecord = (SSTRecord)record;
}
}
}
/**
* Let us at the {@link Workbook} constructor on
* {@link HSSFWorkbook}
*/
private class StubHSSFWorkbook : HSSFWorkbook
{
public StubHSSFWorkbook(InternalWorkbook wb)
: base(wb)
{
}
}
}
}