Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 21 additions & 66 deletions main/DDF/DefaultEscherRecordFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ limitations under the License.
namespace NPOI.DDF
{
using System;
using System.Reflection;
using NPOI.Util;
using System.Collections.Generic;

Expand All @@ -30,16 +29,6 @@ namespace NPOI.DDF
/// </summary>
public class DefaultEscherRecordFactory : IEscherRecordFactory
{
private static Type[] escherRecordClasses = {

typeof(EscherBSERecord), typeof(EscherOptRecord), typeof(EscherTertiaryOptRecord),
typeof(EscherClientAnchorRecord),
typeof(EscherDgRecord), typeof(EscherSpgrRecord), typeof(EscherSpRecord),
typeof(EscherClientDataRecord), typeof(EscherDggRecord),
typeof(EscherSplitMenuColorsRecord), typeof(EscherChildAnchorRecord), typeof(EscherTextboxRecord)
};
private static Dictionary<short,ConstructorInfo> recordsMap = RecordsToMap(escherRecordClasses);

/// <summary>
/// Initializes a new instance of the <see cref="DefaultEscherRecordFactory"/> class.
/// </summary>
Expand Down Expand Up @@ -96,72 +85,38 @@ public virtual EscherRecord CreateRecord(byte[] data, int offset)
return r;
}

//ConstructorInfo recordConstructor = (ConstructorInfo) recordsMap[header.RecordId];
ConstructorInfo recordConstructor = null;
if (recordsMap.ContainsKey(recordId))
recordConstructor = recordsMap[recordId];

EscherRecord escherRecord = null;
if (recordConstructor == null)
{
var escherRecord = CreateRecordById(recordId);
if (escherRecord is null)
return new UnknownEscherRecord();
}

try
{
escherRecord = (EscherRecord)recordConstructor.Invoke(new object[] { });
//escherRecord = (EscherRecord)Activator.CreateInstance(recordConstructor);
}
catch
{
return new UnknownEscherRecord();
}
escherRecord.RecordId = recordId;
escherRecord.Options = options;
return escherRecord;

}

/// <summary>
/// Converts from a list of classes into a map that Contains the record id as the key and
/// the Constructor in the value part of the map. It does this by using reflection to look up
/// the RECORD_ID field then using reflection again to find a reference to the constructor.
/// REMOVE-REFLECTION: Change reflection-based to a lookup.
/// </summary>
/// <param name="records">The records to convert</param>
/// <returns>The map containing the id/constructor pairs.</returns>
private static Dictionary<short, ConstructorInfo> RecordsToMap(Type[] records)
{
Dictionary<short, ConstructorInfo> result = new Dictionary<short, ConstructorInfo>();
//ConstructorInfo constructor;
Type[] EMPTY_CLASS_ARRAY = new Type[0];
for (int i = 0; i < records.Length; i++)
/// <param name="id"></param>
/// <returns></returns>
private static EscherRecord CreateRecordById(short id)
=> id switch
{
Type recordType = records[i];
short sid = 0;

try
{
sid = (short)recordType.GetField("RECORD_ID").GetValue(null);
}
catch
{
throw new RecordFormatException(
"Unable to determine record types");
}
ConstructorInfo ci;
try
{
ci = recordType.GetConstructor(EMPTY_CLASS_ARRAY);
}
catch(Exception e)
{
throw new RuntimeException(e);
}
//result[sid] = recordType; //constructor;
result.Add(sid, ci);
}
return result;
}
EscherBSERecord.RECORD_ID => new EscherBSERecord(),
EscherOptRecord.RECORD_ID => new EscherOptRecord(),
EscherTertiaryOptRecord.RECORD_ID => new EscherTertiaryOptRecord(),
EscherClientAnchorRecord.RECORD_ID => new EscherClientAnchorRecord(),
EscherDgRecord.RECORD_ID => new EscherDgRecord(),
EscherSpgrRecord.RECORD_ID => new EscherSpgrRecord(),
EscherSpRecord.RECORD_ID => new EscherSpRecord(),
EscherClientDataRecord.RECORD_ID => new EscherClientDataRecord(),
EscherDggRecord.RECORD_ID => new EscherDggRecord(),
EscherSplitMenuColorsRecord.RECORD_ID => new EscherSplitMenuColorsRecord(),
EscherChildAnchorRecord.RECORD_ID => new EscherChildAnchorRecord(),
EscherTextboxRecord.RECORD_ID => new EscherTextboxRecord(),
_ => null
};

public static bool IsContainer(short options, short recordId)
{
Expand Down
2 changes: 1 addition & 1 deletion main/HSSF/Record/CFHeader12Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace NPOI.HSSF.Record
*/
public class CFHeader12Record : CFHeaderBase, IFutureRecord, ICloneable
{
public static short sid = 0x0879;
public const short sid = 0x0879;

private FtrHeader futureHeader;

Expand Down
2 changes: 1 addition & 1 deletion main/HSSF/Record/CFHeaderRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace NPOI.HSSF.Record
*/
public class CFHeaderRecord : CFHeaderBase, ICloneable
{
public static short sid = 0x01B0;
public const short sid = 0x01B0;

/** Creates new CFHeaderRecord */
public CFHeaderRecord()
Expand Down
2 changes: 1 addition & 1 deletion main/HSSF/Record/CFRule12Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace NPOI.HSSF.Record
*/
public class CFRule12Record : CFRuleBase, IFutureRecord, ICloneable
{
public static short sid = 0x087A;
public const short sid = 0x087A;

private FtrHeader futureHeader;
private int ext_formatting_length;
Expand Down
2 changes: 1 addition & 1 deletion main/HSSF/Record/CFRuleRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace NPOI.HSSF.Record
*/
public class CFRuleRecord : CFRuleBase, ICloneable
{
public static short sid = 0x01B1;
public const short sid = 0x01B1;

/** Creates new CFRuleRecord */
private CFRuleRecord(byte conditionType, byte comparisonOperation)
Expand Down
2 changes: 1 addition & 1 deletion main/HSSF/Record/Chart/LinkedDataRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace NPOI.HSSF.Record.Chart
*/
public class LinkedDataRecord : StandardRecord, ICloneable
{
public static short sid = 0x1051;
public const short sid = 0x1051;

private static BitField customNumberFormat = BitFieldFactory.GetInstance(0x1);

Expand Down
2 changes: 1 addition & 1 deletion main/HSSF/Record/Chart/StartBlockRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace NPOI.HSSF.Record.Chart
*/
public class StartBlockRecord : StandardRecord
{
public static short sid = 0x0852;
public const short sid = 0x0852;

private short rt;
private short grbitFrt;
Expand Down
Loading