001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.eventmodel;
019:
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.poi.hssf.model.Model;
026: import org.apache.poi.hssf.model.Sheet;
027: import org.apache.poi.hssf.model.Workbook;
028: import org.apache.poi.hssf.record.BOFRecord;
029: import org.apache.poi.hssf.record.EOFRecord;
030: import org.apache.poi.hssf.record.Record;
031:
032: /**
033: * ModelFactory creates workbook and sheet models based upon
034: * events thrown by them there events from the EventRecordFactory.
035: *
036: * @see org.apache.poi.hssf.eventmodel.EventRecordFactory
037: * @author Andrew C. Oliver acoliver@apache.org
038: */
039: public class ModelFactory implements ERFListener {
040:
041: List listeners;
042: Model currentmodel;
043: boolean lastEOF;
044:
045: /**
046: * Constructor for ModelFactory. Does practically nothing.
047: */
048: public ModelFactory() {
049: super ();
050: listeners = new ArrayList(1);
051: }
052:
053: /**
054: * register a ModelFactoryListener so that it can receive
055: * Models as they are created.
056: */
057: public void registerListener(ModelFactoryListener listener) {
058: listeners.add(listener);
059: }
060:
061: /**
062: * Start processing the Workbook stream into Model events.
063: */
064: public void run(InputStream stream) {
065: EventRecordFactory factory = new EventRecordFactory(true);
066: factory.registerListener(this , null);
067: lastEOF = true;
068: factory.processRecords(stream);
069: }
070:
071: //ERFListener
072: public boolean processRecord(Record rec) {
073: if (rec.getSid() == BOFRecord.sid) {
074: if (lastEOF != true) {
075: throw new RuntimeException(
076: "Not yet handled embedded models");
077: } else {
078: BOFRecord bof = (BOFRecord) rec;
079: switch (bof.getType()) {
080: case BOFRecord.TYPE_WORKBOOK:
081: currentmodel = new Workbook();
082: break;
083: case BOFRecord.TYPE_WORKSHEET:
084: currentmodel = new Sheet();
085: break;
086: default:
087: throw new RuntimeException(
088: "Unsupported model type " + bof.getType());
089: }
090:
091: }
092: }
093:
094: if (rec.getSid() == EOFRecord.sid) {
095: lastEOF = true;
096: throwEvent(currentmodel);
097: } else {
098: lastEOF = false;
099: }
100:
101: return true;
102: }
103:
104: /**
105: * Throws the model as an event to the listeners
106: * @param model to be thrown
107: */
108: private void throwEvent(Model model) {
109: Iterator i = listeners.iterator();
110: while (i.hasNext()) {
111: ModelFactoryListener mfl = (ModelFactoryListener) i.next();
112: mfl.process(model);
113: }
114: }
115:
116: }
|