]> source.dussan.org Git - poi.git/commitdiff
Move from having the list of classes to use for records from being in here, to being...
authorNick Burch <nick@apache.org>
Fri, 8 Jul 2005 17:38:55 +0000 (17:38 +0000)
committerNick Burch <nick@apache.org>
Fri, 8 Jul 2005 17:38:55 +0000 (17:38 +0000)
Should make adding new record handling classes cleaner and more extensible
(Change inspired by discussions with Yegor Kozlov)

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353745 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hslf/record/Record.java

index b1bbf77cf7537158b6577879ec161ed698ba2cd8..b9dddf32b4e7b55e2889d6c6afdcad2a1a57ff61 100644 (file)
@@ -125,105 +125,45 @@ public abstract class Record
        public static Record createRecordForType(long type, byte[] b, int start, int len) {
                Record toReturn = null;
 
-               // Default is to use UnknownRecordPlaceholder
-               // When you create classes for new Records, add them here
-               switch((int)type) {
-                       // Document
-                       case 1000:
-                               toReturn = new DummyPositionSensitiveRecordWithChildren(b,start,len);
-                               break;
-                               
-                       // DocumentAtom
-                       case 1001:
-                               toReturn = new DocumentAtom(b,start,len);
-                               break;
-                               
-                       // "Slide"
-                       case 1006:
-                               toReturn = new Slide(b,start,len);
-                               break;
-
-                       // "SlideAtom"
-                       case 1007:
-                               toReturn = new SlideAtom(b,start,len);
-                               break;
-
-                       // "Notes"
-                       case 1008:
-                               toReturn = new Notes(b,start,len);
-                               break;
-                               
-                       // "NotesAtom" (Details on Notes sheets)
-                       case 1009:
-                               toReturn = new NotesAtom(b,start,len);
-                               break;
-                               
-                       // "SlidePersistAtom" (Details on text for a sheet)
-                       case 1011:
-                               toReturn = new SlidePersistAtom(b,start,len);
-                               break;
-                               
-                       // MainMaster (MetaSheet lives inside the PPDrawing inside this)
-                       case 1016:
-                               toReturn = new DummyPositionSensitiveRecordWithChildren(b,start,len);
-                               break;
-
-                       // PPDrawing (MetaSheet lives inside this)
-                       case 1036:
-                               toReturn = new PPDrawing(b,start,len);
-                               break;
-
-                       // ColorSchemeAtom (Holds the colours that make up a colour scheme)
-                       case 2032:
-                               toReturn = new ColorSchemeAtom(b,start,len);
-                               break;
-
-                       // TextHeaderAtom (Holds details on following text)
-                       case 3999:
-                               toReturn = new TextHeaderAtom(b,start,len);
-                               break;
-                               
-                       // TextCharsAtom (Text in Unicode format)
-                       case 4000:
-                               toReturn = new TextCharsAtom(b,start,len);
-                               break;
-                               
-                       // TextByteAtom (Text in ascii format)
-                       case 4008:
-                               toReturn = new TextBytesAtom(b,start,len);
-                               break;
-                               
-                       // SlideListWithText (Many Sheets live inside here)
-                       case 4080:
-                               toReturn = new SlideListWithText(b,start,len);
-                               break;
-
-                       // UserEditAtom (Holds pointers, last viewed etc)
-                       case 4085:
-                               toReturn = new UserEditAtom(b,start,len);
-                               break;
-
-                       // PersistPtrFullBlock (Don't know what it holds, but do care about where it lives)
-                       case 6001:
-                               toReturn = new PersistPtrHolder(b,start,len);
-                               break;
-                       // PersistPtrIncrementalBlock (Don't know what it holds, but do care about where it lives)
-                       case 6002:
-                               toReturn = new PersistPtrHolder(b,start,len);
-                               break;
-                               
-                       default:
-                               toReturn = new UnknownRecordPlaceholder(b,start,len);
-                               break;
+               // We use the RecordTypes class to provide us with the right
+               //  class to use for a given type
+               // A spot of reflection gets us the (byte[],int,int) constructor
+               // From there, we instanciate the class
+               // Any special record handling occurs once we have the class
+               Class c = null;
+               try {
+                       c = RecordTypes.recordHandlingClass((int)type);
+                       if(c == null) { 
+                               // How odd. RecordTypes normally subsitutes in
+                               //  a default handler class if it has heard of the record
+                               //  type but there's no support for it. Explicitly request
+                               //  that now
+                               c = RecordTypes.recordHandlingClass( RecordTypes.Unknown.typeID );
+                       }
+
+                       // Grab the right constructor
+                       java.lang.reflect.Constructor con = c.getDeclaredConstructor(new Class[] { byte[].class, Integer.TYPE, Integer.TYPE });
+                       // Instantiate
+                       toReturn = (Record)(con.newInstance(new Object[] { b, new Integer(start), new Integer(len) }));
+               } catch(InstantiationException ie) {
+                       throw new RuntimeException("Couldn't instantiate the class for type with id " + type + " on class " + c + " : " + ie);
+               } catch(java.lang.reflect.InvocationTargetException ite) {
+                       throw new RuntimeException("Couldn't instantiate the class for type with id " + type + " on class " + c + " : " + ite);
+               } catch(IllegalAccessException iae) {
+                       throw new RuntimeException("Couldn't access the constructor for type with id " + type + " on class " + c + " : " + iae);
+               } catch(NoSuchMethodException nsme) {
+                       throw new RuntimeException("Couldn't access the constructor for type with id " + type + " on class " + c + " : " + nsme);
                }
 
+               // Handling for special kinds of records follow
+
                // If it's a position aware record, tell it where it is
                if(toReturn instanceof PositionDependentRecord) {
                        PositionDependentRecord pdr = (PositionDependentRecord)toReturn;
                        pdr.setLastOnDiskOffset(start);
                }
 
-               // Return the record
+               // Return the created record
                return toReturn;
        }
 }