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.eventusermodel;
019:
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.ArrayList;
023:
024: import org.apache.poi.hssf.record.Record;
025: import org.apache.poi.hssf.record.RecordFactory;
026:
027: /**
028: * An HSSFRequest object should be constructed registering an instance or multiple
029: * instances of HSSFListener with each Record.sid you wish to listen for.
030: *
031: * @see org.apache.poi.hssf.eventusermodel.HSSFEventFactory
032: * @see org.apache.poi.hssf.eventusermodel.HSSFListener
033: * @see org.apache.poi.hssf.dev.EFHSSF
034: * @see org.apache.poi.hssf.eventusermodel.HSSFUserException
035: * @author Andrew C. Oliver (acoliver at apache dot org)
036: * @author Carey Sublette (careysub@earthling.net)
037: */
038:
039: public class HSSFRequest {
040: private HashMap records;
041:
042: /** Creates a new instance of HSSFRequest */
043:
044: public HSSFRequest() {
045: records = new HashMap(50); // most folks won't listen for too many of these
046: }
047:
048: /**
049: * add an event listener for a particular record type. The trick is you have to know
050: * what the records are for or just start with our examples and build on them. Alternatively,
051: * you CAN call addListenerForAllRecords and you'll recieve ALL record events in one listener,
052: * but if you like to squeeze every last byte of efficiency out of life you my not like this.
053: * (its sure as heck what I plan to do)
054: *
055: * @see #addListenerForAllRecords(HSSFListener)
056: *
057: * @param lsnr for the event
058: * @param sid identifier for the record type this is the .sid static member on the individual records
059: * for example req.addListener(myListener, BOFRecord.sid)
060: */
061:
062: public void addListener(HSSFListener lsnr, short sid) {
063: List list = null;
064: Object obj = records.get(new Short(sid));
065:
066: if (obj != null) {
067: list = (List) obj;
068: } else {
069: list = new ArrayList(1); // probably most people will use one listener
070: list.add(lsnr);
071: records.put(new Short(sid), list);
072: }
073: }
074:
075: /**
076: * This is the equivilent of calling addListener(myListener, sid) for EVERY
077: * record in the org.apache.poi.hssf.record package. This is for lazy
078: * people like me. You can call this more than once with more than one listener, but
079: * that seems like a bad thing to do from a practice-perspective unless you have a
080: * compelling reason to do so (like maybe you send the event two places or log it or
081: * something?).
082: *
083: * @param lsnr a single listener to associate with ALL records
084: */
085:
086: public void addListenerForAllRecords(HSSFListener lsnr) {
087: short[] rectypes = RecordFactory.getAllKnownRecordSIDs();
088:
089: for (int k = 0; k < rectypes.length; k++) {
090: addListener(lsnr, rectypes[k]);
091: }
092: }
093:
094: /**
095: * Called by HSSFEventFactory, passes the Record to each listener associated with
096: * a record.sid.
097: *
098: * Exception and return value added 2002-04-19 by Carey Sublette
099: *
100: * @return numeric user-specified result code. If zero continue processing.
101: * @throws HSSFUserException User exception condition
102: */
103:
104: protected short processRecord(Record rec) throws HSSFUserException {
105: Object obj = records.get(new Short(rec.getSid()));
106: short userCode = 0;
107:
108: if (obj != null) {
109: List listeners = (List) obj;
110:
111: for (int k = 0; k < listeners.size(); k++) {
112: Object listenObj = listeners.get(k);
113: if (listenObj instanceof AbortableHSSFListener) {
114: AbortableHSSFListener listener = (AbortableHSSFListener) listenObj;
115: userCode = listener.abortableProcessRecord(rec);
116: if (userCode != 0)
117: break;
118: } else {
119: HSSFListener listener = (HSSFListener) listenObj;
120: listener.processRecord(rec);
121: }
122: }
123: }
124: return userCode;
125: }
126: }
|