KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > eventmodel > ModelFactory


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

16
17 package org.apache.poi.hssf.eventmodel;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.poi.hssf.model.Model;
25 import org.apache.poi.hssf.model.Sheet;
26 import org.apache.poi.hssf.model.Workbook;
27 import org.apache.poi.hssf.record.BOFRecord;
28 import org.apache.poi.hssf.record.EOFRecord;
29 import org.apache.poi.hssf.record.Record;
30
31
32 /**
33  * ModelFactory creates workbook and sheet models based upon
34  * events thrown by them there events from the EventRecordFactory.
35  *
36  * @see org.apache.poi.hssf.eventmodel.EventRecordFactory
37  * @author Andrew C. Oliver acoliver@apache.org
38  */

39 public class ModelFactory implements ERFListener
40 {
41
42     List JavaDoc listeners;
43     Model currentmodel;
44     boolean lastEOF;
45  
46     /**
47      * Constructor for ModelFactory. Does practically nothing.
48      */

49     public ModelFactory()
50     {
51         super();
52         listeners = new ArrayList JavaDoc(1);
53     }
54     
55     /**
56      * register a ModelFactoryListener so that it can receive
57      * Models as they are created.
58      */

59     public void registerListener(ModelFactoryListener listener) {
60         listeners.add(listener);
61     }
62     
63     /**
64      * Start processing the Workbook stream into Model events.
65      */

66     public void run(InputStream JavaDoc stream) {
67         EventRecordFactory factory = new EventRecordFactory(true);
68         factory.registerListener(this,null);
69         lastEOF = true;
70         factory.processRecords(stream);
71     }
72
73     //ERFListener
74
public boolean processRecord(Record rec)
75     {
76        if (rec.getSid() == BOFRecord.sid) {
77              if (lastEOF != true) {
78               throw new RuntimeException JavaDoc("Not yet handled embedded models");
79              } else {
80               BOFRecord bof = (BOFRecord)rec;
81               switch (bof.getType()) {
82                case BOFRecord.TYPE_WORKBOOK:
83                  currentmodel = new Workbook();
84                break;
85                case BOFRecord.TYPE_WORKSHEET:
86                  currentmodel = new Sheet();
87                break;
88               default:
89                    throw new RuntimeException JavaDoc("Unsupported model type "+bof.getType());
90               }
91                
92              }
93         }
94         
95         if (rec.getSid() == EOFRecord.sid) {
96             lastEOF = true;
97             throwEvent(currentmodel);
98         } else {
99             lastEOF = false;
100         }
101         
102  
103         return true;
104     }
105
106     /**
107      * Throws the model as an event to the listeners
108      * @param model to be thrown
109      */

110     private void throwEvent(Model model)
111     {
112         Iterator JavaDoc i = listeners.iterator();
113         while (i.hasNext()) {
114           ModelFactoryListener mfl = (ModelFactoryListener) i.next();
115           mfl.process(model);
116         }
117     }
118
119
120 }
121
Popular Tags