KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > ddf > DefaultEscherRecordFactory


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.ddf;
19
20 import org.apache.poi.hssf.record.RecordFormatException;
21
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * Generates escher records when provided the byte array containing those records.
28  *
29  * @author Glen Stampoultzis
30  * @see EscherRecordFactory
31  */

32 public class DefaultEscherRecordFactory
33         implements EscherRecordFactory
34 {
35     private static Class JavaDoc[] 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 JavaDoc recordsMap = recordsToMap( escherRecordClasses );
41
42     /**
43      * Creates an instance of the escher record factory
44      */

45     public DefaultEscherRecordFactory()
46     {
47     }
48
49     /**
50      * Generates an escher record including the any children contained under that record.
51      * An exception is thrown if the record could not be generated.
52      *
53      * @param data The byte array containing the records
54      * @param offset The starting offset into the byte array
55      * @return The generated escher record
56      */

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 JavaDoc recordConstructor = (Constructor JavaDoc) recordsMap.get( new Short JavaDoc( header.getRecordId() ) );
77             EscherRecord escherRecord = null;
78             if ( recordConstructor != null )
79             {
80                 try
81                 {
82                     escherRecord = (EscherRecord) recordConstructor.newInstance( new Object JavaDoc[]{} );
83                     escherRecord.setRecordId( header.getRecordId() );
84                     escherRecord.setOptions( header.getOptions() );
85                 }
86                 catch ( Exception JavaDoc e )
87                 {
88                     escherRecord = null;
89                 }
90             }
91             return escherRecord == null ? new UnknownEscherRecord() : escherRecord;
92         }
93     }
94
95     /**
96      * Converts from a list of classes into a map that contains the record id as the key and
97      * the Constructor in the value part of the map. It does this by using reflection to look up
98      * the RECORD_ID field then using reflection again to find a reference to the constructor.
99      *
100      * @param records The records to convert
101      * @return The map containing the id/constructor pairs.
102      */

103     private static Map JavaDoc recordsToMap( Class JavaDoc[] records )
104     {
105         Map JavaDoc result = new HashMap JavaDoc();
106         Constructor JavaDoc constructor;
107
108         for ( int i = 0; i < records.length; i++ )
109         {
110             Class JavaDoc 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 JavaDoc[]
118                 {
119                 } );
120             }
121             catch ( Exception JavaDoc illegalArgumentException )
122             {
123                 throw new RecordFormatException(
124                         "Unable to determine record types" );
125             }
126             result.put( new Short JavaDoc( sid ), constructor );
127         }
128         return result;
129     }
130
131 }
132
Popular Tags