1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml.sxc.pexcel.records;
25 
26 import java.io.OutputStream;
27 import java.io.InputStream;
28 import java.io.IOException;
29 import java.util.Vector;
30 import java.util.Enumeration;
31 import java.awt.Point;
32 
33 import org.openoffice.xmerge.util.Debug;
34 import org.openoffice.xmerge.converter.xml.sxc.SheetSettings;
35 import org.openoffice.xmerge.converter.xml.sxc.pexcel.PocketExcelConstants;
36 
37 
38 /**
39  *  This class is used by <code>PxlDocument</code> to maintain pexcel
40  *  worksheets.
41  *
42  *  @author  Martin Maher
43  */
44 public class Worksheet {
45 
46 	private String name;
47 	private Workbook wb;
48 	private Vector rows			= new Vector();
49 	private Vector colInfo		= new Vector();
50 	private Vector cells		= new Vector();
51 	private DefColWidth dcw		= new DefColWidth();
52 	private DefRowHeight drh	= new DefRowHeight();
53 	private Window2 win2		= new Window2();
54 	private Selection sel		= new Selection();
55 	private Pane p				= new Pane();
56 	private BeginningOfFile bof;
57 	private Eof eof;
58 
Worksheet(Workbook wb)59 	public Worksheet(Workbook wb) {
60 		this.wb = wb;
61 	}
62 
63 	/**
64  	 * Default Contructor
65  	 */
Worksheet()66 	public Worksheet() {
67 	}
68 
69 	/**
70  	 *	Writes the current workbook to the <code>OutputStream</code>
71  	 *
72  	 * @param	os The destination outputstream
73  	 */
write(OutputStream os)74 	public void write(OutputStream os) throws IOException {
75 
76 		bof		= new BeginningOfFile(false);
77 		bof.write(os);
78 		dcw.write(os);
79 		for(Enumeration e = colInfo.elements();e.hasMoreElements();) {
80 			ColInfo ci = (ColInfo) e.nextElement();
81 			ci.write(os);
82 		}
83 		drh.write(os);
84 		for(Enumeration e = rows.elements();e.hasMoreElements();) {
85 			Row rw = (Row) e.nextElement();
86 			rw.write(os);
87 		}
88 		for(Enumeration e = cells.elements();e.hasMoreElements();) {
89 			BIFFRecord cv = (BIFFRecord) e.nextElement();
90 			cv.write(os);
91 		}
92 		win2.write(os);
93 		p.write(os);
94 		sel.write(os);
95 		eof		= new Eof();
96 		eof.write(os);
97 	}
98 
99 	/**
100  	 *	Reads a worksheet from the <code>InputStream</code> and contructs a
101 	 *	workbook object from it
102 	 *
103  	 * @param	is InputStream containing a Pocket Excel Data file.
104  	 */
read(InputStream is)105 	public boolean read(InputStream is) throws IOException {
106 
107 		int b = is.read();
108 
109 		if (b==-1)
110 			return false;
111 
112 		while(b!=-1) {
113 			switch (b)
114 			{
115                 case PocketExcelConstants.BLANK_CELL:
116                     Debug.log(Debug.TRACE,"Blank Cell (01h)");
117                     BlankCell bc = new BlankCell(is);
118 					cells.add(bc);
119                     break;
120 
121                 case PocketExcelConstants.NUMBER_CELL:
122                     Debug.log(Debug.TRACE,"NUMBER: Cell Value, Floating-Point Number (03h)");
123                     FloatNumber fn = new FloatNumber(is);
124 					cells.add(fn);
125                     break;
126 
127                 case PocketExcelConstants.LABEL_CELL:
128                     Debug.log(Debug.TRACE,"LABEL: Cell Value, String Constant (04h)");
129                     LabelCell lc = new LabelCell(is);
130 					cells.add(lc);
131                     break;
132 
133                 case PocketExcelConstants.BOOLERR_CELL:
134                     Debug.log(Debug.TRACE,"BOOLERR: Cell Value, Boolean or Error (05h)");
135                     BoolErrCell bec = new BoolErrCell(is);
136                     break;
137 
138                 case PocketExcelConstants.FORMULA_CELL:
139                     Debug.log(Debug.TRACE,"FORMULA: Cell Formula (06h)");
140                     Formula f = new Formula(is, wb);
141 					cells.add(f);
142                     break;
143 
144                  case PocketExcelConstants.FORMULA_STRING:
145                     Debug.log(Debug.TRACE,"String Value of a Formula (07h)");
146                     StringValue sv = new StringValue(is);
147                     break;
148 
149                 case PocketExcelConstants.ROW_DESCRIPTION:
150                     Debug.log(Debug.TRACE,"ROW: Describes a Row (08h)");
151                     Row rw = new Row(is);
152 					rows.add(rw);
153                     break;
154 
155                 case PocketExcelConstants.BOF_RECORD:
156                     Debug.log(Debug.TRACE,"BOF Record");
157 					bof = new BeginningOfFile(is);
158                     break;
159 
160                 case PocketExcelConstants.EOF_MARKER:
161                     Debug.log(Debug.TRACE,"EOF Marker");
162                     eof = new Eof();
163 					return true;
164 
165                 case PocketExcelConstants.CURRENT_SELECTION:
166                     Debug.log(Debug.TRACE,"SELECTION: Current Selection (1Dh)");
167                     sel = new Selection(is);
168                     break;
169 
170                 case PocketExcelConstants.NUMBER_FORMAT:
171                     Debug.log(Debug.TRACE,"FORMAT: Number Format (1Eh)");
172                     NumberFormat nf = new NumberFormat(is);
173                     break;
174 
175                 case PocketExcelConstants.DEFAULT_ROW_HEIGHT:
176                     Debug.log(Debug.TRACE,"DEFAULTROWHEIGHT: Default Row Height (25h)");
177 					drh = new DefRowHeight(is);
178                     break;
179 
180                 case PocketExcelConstants.SHEET_WINDOW_INFO:
181                     Debug.log(Debug.TRACE,"WINDOW2: Sheet Window Information (3Eh) [PXL 2.0]");
182 					win2 = new Window2(is);
183                     break;
184 
185                 case PocketExcelConstants.PANE_INFO:
186                     Debug.log(Debug.TRACE,"PANE: Number of Panes and their Position (41h) [PXL 2.0]");
187                     p = new Pane(is);
188                     break;
189 
190                 case PocketExcelConstants.DEF_COL_WIDTH:
191                     Debug.log(Debug.TRACE,"DEFCOLWIDTH: Default Column Width (55h) [PXL 2.0]");
192 					dcw = new DefColWidth(is);
193                     break;
194 
195                 case PocketExcelConstants.COLINFO:
196                     Debug.log(Debug.TRACE,"COLINFO: Column Formatting Information (7Dh) [PXL 2.0]");
197                     ColInfo ci = new ColInfo(is);
198 					colInfo.add(ci);
199                     break;
200 
201                 default:
202                     break;
203             }
204 			b = is.read();
205 
206 		}
207 		Debug.log(Debug.TRACE,"Leaving Worksheet:");
208 
209 		return true;
210 	}
211 
212 	/**
213  	 *	Returns an enumerator which will be used to access individual cells
214 	 *
215  	 * @return an enumerator to the worksheet cells
216  	 */
getCellEnumerator()217 	public Enumeration getCellEnumerator() throws IOException {
218 		return (cells.elements());
219 	}
220 
221 	/**
222  	 * Adds a cell to this worksheet. Current valdid celltypes are
223 	 * <code>FloatNumber</code>, <code>LabelCell</code> or <code>Formula</code>
224 	 *
225  	 * @param	br
226  	 */
addCell(BIFFRecord br)227 	public void addCell(BIFFRecord br) {
228 		cells.add(br);
229 	}
230 
231 	/**
232  	 * Adds a number of ColInfo Records to the worksheet base on a list of
233 	 * clumnwidths passed in
234 	 *
235  	 * @param r	list of column widths
236  	 */
addRow(Row r)237 	public void addRow(Row r) {
238 		rows.add(r);
239 	}
240 
241 	/**
242  	 * Adds a number of ColInfo Records to the worksheet base on a list of
243 	 * clumnwidths passed in
244 	 *
245  	 * @param c	list of column widths
246  	 */
addCol(ColInfo c)247 	public void addCol(ColInfo c) {
248 		colInfo.add(c);
249 	}
250 	/**
251 	 * Returns an <code>Enumeration</code> to the ColInfo's for this worksheet
252 	 *
253 	 * @return an <code>Enumeration</code> to the ColInfo's
254 	 */
addSettings(SheetSettings s)255 	public void addSettings(SheetSettings s) {
256 
257 		sel.setActiveCell(s.getCursor());
258 		p.setLeft(s.getLeft());
259 		p.setTop(s.getTop());
260 		p.setPaneNumber(s.getPaneNumber());
261 		Point split = s.getSplit();
262 		if(split.getX()!=0 || split.getY()!=0) {
263 			p.setSplitPoint(s.getSplitType(), split);
264 			win2.setSplitType(s.getSplitType());
265 		}
266 	}
267 
268 	/**
269 	 * Returns an <code>Enumeration</code> to the ColInfo's for this worksheet
270 	 *
271 	 * @return an <code>Enumeration</code> to the ColInfo's
272 	 */
getColInfos()273 	 public Enumeration getColInfos() {
274 
275 	 	return (colInfo.elements());
276 	 }
277 
278 	/**
279 	 * Returns a <code>SheetSettings</code> object containing a collection of data
280 	 * contained in <code>Pane</code>, <code>Window2</code> and
281 	 * <code>Selection</code>
282 	 *
283 	 * @return an <code>SheetSettings</code>
284 	 */
getSettings()285 	 public SheetSettings getSettings() {
286 
287 		SheetSettings s = new SheetSettings();
288 		s.setCursor(sel.getActiveCell());
289 		if(win2.isFrozen()) {
290 			s.setFreeze(p.getFreezePoint());
291 		} else if(win2.isSplit()) {
292 			s.setSplit(p.getSplitPoint());
293 		}
294 		s.setPaneNumber(p.getPaneNumber());
295 		s.setTopLeft(p.getTop(), p.getLeft());
296 	 	return s;
297 	 }
298 	/**
299 	 * Returns an <code>Enumeration</code> to the Rows for this worksheet
300 	 *
301 	 * @return an <code>Enumeration</code> to the Rows
302 	 */
getRows()303 	 public Enumeration getRows() {
304 
305 	 	return (rows.elements());
306 	 }
307 
308 }
309