1 2 17 18 package org.apache.poi.ddf; 19 20 import org.apache.poi.hssf.record.RecordFormatException; 21 22 import java.lang.reflect.Constructor ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 32 public class DefaultEscherRecordFactory 33 implements EscherRecordFactory 34 { 35 private static Class [] escherRecordClasses = { 36 EscherBSERecord.class, EscherOptRecord.class, EscherClientAnchorRecord.class, EscherDgRecord.class, 37 EscherSpgrRecord.class, EscherSpRecord.class, EscherClientDataRecord.class, EscherDggRecord.class, 38 EscherSplitMenuColorsRecord.class, EscherChildAnchorRecord.class, EscherTextboxRecord.class 39 }; 40 private static Map recordsMap = recordsToMap( escherRecordClasses ); 41 42 45 public DefaultEscherRecordFactory() 46 { 47 } 48 49 57 public EscherRecord createRecord( byte[] data, int offset ) 58 { 59 EscherRecord.EscherRecordHeader header = EscherRecord.EscherRecordHeader.readHeader( data, offset ); 60 if ( ( header.getOptions() & (short) 0x000F ) == (short) 0x000F ) 61 { 62 EscherContainerRecord r = new EscherContainerRecord(); 63 r.setRecordId( header.getRecordId() ); 64 r.setOptions( header.getOptions() ); 65 return r; 66 } 67 else if ( header.getRecordId() >= EscherBlipRecord.RECORD_ID_START && header.getRecordId() <= EscherBlipRecord.RECORD_ID_END ) 68 { 69 EscherBlipRecord r = new EscherBlipRecord(); 70 r.setRecordId( header.getRecordId() ); 71 r.setOptions( header.getOptions() ); 72 return r; 73 } 74 else 75 { 76 Constructor recordConstructor = (Constructor ) recordsMap.get( new Short ( header.getRecordId() ) ); 77 EscherRecord escherRecord = null; 78 if ( recordConstructor != null ) 79 { 80 try 81 { 82 escherRecord = (EscherRecord) recordConstructor.newInstance( new Object []{} ); 83 escherRecord.setRecordId( header.getRecordId() ); 84 escherRecord.setOptions( header.getOptions() ); 85 } 86 catch ( Exception e ) 87 { 88 escherRecord = null; 89 } 90 } 91 return escherRecord == null ? new UnknownEscherRecord() : escherRecord; 92 } 93 } 94 95 103 private static Map recordsToMap( Class [] records ) 104 { 105 Map result = new HashMap (); 106 Constructor constructor; 107 108 for ( int i = 0; i < records.length; i++ ) 109 { 110 Class record = null; 111 short sid = 0; 112 113 record = records[i]; 114 try 115 { 116 sid = record.getField( "RECORD_ID" ).getShort( null ); 117 constructor = record.getConstructor( new Class [] 118 { 119 } ); 120 } 121 catch ( Exception illegalArgumentException ) 122 { 123 throw new RecordFormatException( 124 "Unable to determine record types" ); 125 } 126 result.put( new Short ( sid ), constructor ); 127 } 128 return result; 129 } 130 131 } 132 | Popular Tags |