PageRenderTime 87ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/components/forks/poi/src/loci/poi/hssf/record/RecordFactory.java

http://github.com/openmicroscopy/bioformats
Java | 312 lines | 216 code | 29 blank | 67 comment | 39 complexity | 3a96eda8fddbdf5ee7f2d333771bc3be MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.record;
  38. import java.io.InputStream;
  39. import java.lang.reflect.Constructor;
  40. import java.util.*;
  41. /**
  42. * Title: Record Factory<P>
  43. * Description: Takes a stream and outputs an array of Record objects.<P>
  44. *
  45. * @deprecated use {@link loci.poi.hssf.eventmodel.EventRecordFactory} instead
  46. * @see loci.poi.hssf.eventmodel.EventRecordFactory
  47. * @author Andrew C. Oliver (acoliver at apache dot org)
  48. * @author Marc Johnson (mjohnson at apache dot org)
  49. * @author Glen Stampoultzis (glens at apache.org)
  50. * @author Csaba Nagy (ncsaba at yahoo dot com)
  51. */
  52. public class RecordFactory
  53. {
  54. private static int NUM_RECORDS = 10000;
  55. private static final Class[] records;
  56. static {
  57. records = new Class[]
  58. {
  59. BOFRecord.class, InterfaceHdrRecord.class, MMSRecord.class,
  60. InterfaceEndRecord.class, WriteAccessRecord.class,
  61. CodepageRecord.class, DSFRecord.class, TabIdRecord.class,
  62. FnGroupCountRecord.class, WindowProtectRecord.class,
  63. ProtectRecord.class, PasswordRecord.class, ProtectionRev4Record.class,
  64. PasswordRev4Record.class, WindowOneRecord.class, BackupRecord.class,
  65. HideObjRecord.class, DateWindow1904Record.class,
  66. PrecisionRecord.class, RefreshAllRecord.class, BookBoolRecord.class,
  67. FontRecord.class, FormatRecord.class, ExtendedFormatRecord.class,
  68. StyleRecord.class, UseSelFSRecord.class, BoundSheetRecord.class,
  69. CountryRecord.class, SSTRecord.class, ExtSSTRecord.class,
  70. EOFRecord.class, IndexRecord.class, CalcModeRecord.class,
  71. CalcCountRecord.class, RefModeRecord.class, IterationRecord.class,
  72. DeltaRecord.class, SaveRecalcRecord.class, PrintHeadersRecord.class,
  73. PrintGridlinesRecord.class, GridsetRecord.class, GutsRecord.class,
  74. DefaultRowHeightRecord.class, WSBoolRecord.class, HeaderRecord.class,
  75. FooterRecord.class, HCenterRecord.class, VCenterRecord.class,
  76. PrintSetupRecord.class, DefaultColWidthRecord.class,
  77. DimensionsRecord.class, RowRecord.class, LabelSSTRecord.class,
  78. RKRecord.class, NumberRecord.class, DBCellRecord.class,
  79. WindowTwoRecord.class, SelectionRecord.class, ContinueRecord.class,
  80. LabelRecord.class, BlankRecord.class, ColumnInfoRecord.class,
  81. MulRKRecord.class, MulBlankRecord.class, MergeCellsRecord.class,
  82. FormulaRecord.class, BoolErrRecord.class, ExternSheetRecord.class,
  83. NameRecord.class, LeftMarginRecord.class, RightMarginRecord.class,
  84. TopMarginRecord.class, BottomMarginRecord.class,
  85. DrawingRecord.class, DrawingGroupRecord.class, DrawingSelectionRecord.class,
  86. ObjRecord.class, TextObjectRecord.class,
  87. PaletteRecord.class, StringRecord.class, RecalcIdRecord.class, SharedFormulaRecord.class,
  88. HorizontalPageBreakRecord.class, VerticalPageBreakRecord.class,
  89. WriteProtectRecord.class, FilePassRecord.class, PaneRecord.class,
  90. NoteRecord.class, ObjectProtectRecord.class, ScenarioProtectRecord.class, FileSharingRecord.class
  91. };
  92. }
  93. private static Map recordsMap = recordsToMap(records);
  94. /**
  95. * changes the default capacity (10000) to handle larger files
  96. */
  97. public static void setCapacity(int capacity)
  98. {
  99. NUM_RECORDS = capacity;
  100. }
  101. /**
  102. * Create an array of records from an input stream
  103. *
  104. * @param in the InputStream from which the records will be
  105. * obtained
  106. *
  107. * @return an array of Records created from the InputStream
  108. *
  109. * @exception RecordFormatException on error processing the
  110. * InputStream
  111. */
  112. public static List createRecords(InputStream in)
  113. throws RecordFormatException
  114. {
  115. ArrayList records = new ArrayList(NUM_RECORDS);
  116. RecordInputStream recStream = new RecordInputStream(in);
  117. DrawingRecord lastDrawingRecord = new DrawingRecord( );
  118. Record lastRecord = null;
  119. while (recStream.hasNextRecord()) {
  120. recStream.nextRecord();
  121. if (recStream.getSid() != 0)
  122. {
  123. Record[] recs = createRecord(recStream); // handle MulRK records
  124. if (recs.length > 1)
  125. {
  126. for (int k = 0; k < recs.length; k++)
  127. {
  128. records.add(
  129. recs[ k ]); // these will be number records
  130. }
  131. }
  132. else
  133. {
  134. Record record = recs[ 0 ];
  135. if (record != null)
  136. {
  137. if (record.getSid() == DrawingGroupRecord.sid
  138. && lastRecord instanceof DrawingGroupRecord)
  139. {
  140. DrawingGroupRecord lastDGRecord = (DrawingGroupRecord) lastRecord;
  141. lastDGRecord.join((AbstractEscherHolderRecord) record);
  142. }
  143. else if (record.getSid() == ContinueRecord.sid &&
  144. ((lastRecord instanceof ObjRecord) || (lastRecord instanceof TextObjectRecord))) {
  145. // Drawing records have a very strange continue behaviour.
  146. //There can actually be OBJ records mixed between the continues.
  147. lastDrawingRecord.processContinueRecord( ((ContinueRecord)record).getData() );
  148. } else if (record.getSid() == ContinueRecord.sid &&
  149. (lastRecord instanceof DrawingGroupRecord)) {
  150. ((DrawingGroupRecord)lastRecord).processContinueRecord(((ContinueRecord)record).getData());
  151. } else if (record.getSid() == ContinueRecord.sid) {
  152. if (lastRecord instanceof UnknownRecord) {
  153. //Gracefully handle records that we dont know about,
  154. //that happen to be continued
  155. records.add(record);
  156. } else
  157. throw new RecordFormatException("Unhandled Continue Record");
  158. }
  159. else {
  160. lastRecord = record;
  161. if (record instanceof DrawingRecord)
  162. lastDrawingRecord = (DrawingRecord) record;
  163. records.add(record);
  164. }
  165. }
  166. }
  167. }
  168. }
  169. return records;
  170. }
  171. public static Record [] createRecord(RecordInputStream in)
  172. {
  173. Record retval;
  174. Record[] realretval = null;
  175. try
  176. {
  177. Constructor constructor =
  178. ( Constructor ) recordsMap.get(new Short(in.getSid()));
  179. if (constructor != null)
  180. {
  181. retval = ( Record ) constructor.newInstance(new Object[]
  182. {
  183. in
  184. });
  185. }
  186. else
  187. {
  188. retval = new UnknownRecord(in);
  189. }
  190. }
  191. catch (Exception introspectionException)
  192. {
  193. throw new RecordFormatException("Unable to construct record instance",introspectionException);
  194. }
  195. if (retval instanceof RKRecord)
  196. {
  197. RKRecord rk = ( RKRecord ) retval;
  198. NumberRecord num = new NumberRecord();
  199. num.setColumn(rk.getColumn());
  200. num.setRow(rk.getRow());
  201. num.setXFIndex(rk.getXFIndex());
  202. num.setValue(rk.getRKNumber());
  203. retval = num;
  204. }
  205. else if (retval instanceof DBCellRecord)
  206. {
  207. retval = null;
  208. }
  209. else if (retval instanceof MulRKRecord)
  210. {
  211. MulRKRecord mrk = ( MulRKRecord ) retval;
  212. realretval = new Record[ mrk.getNumColumns() ];
  213. for (int k = 0; k < mrk.getNumColumns(); k++)
  214. {
  215. NumberRecord nr = new NumberRecord();
  216. nr.setColumn(( short ) (k + mrk.getFirstColumn()));
  217. nr.setRow(mrk.getRow());
  218. nr.setXFIndex(mrk.getXFAt(k));
  219. nr.setValue(mrk.getRKNumberAt(k));
  220. realretval[ k ] = nr;
  221. }
  222. }
  223. else if (retval instanceof MulBlankRecord)
  224. {
  225. MulBlankRecord mb = ( MulBlankRecord ) retval;
  226. realretval = new Record[ mb.getNumColumns() ];
  227. for (int k = 0; k < mb.getNumColumns(); k++)
  228. {
  229. BlankRecord br = new BlankRecord();
  230. br.setColumn(( short ) (k + mb.getFirstColumn()));
  231. br.setRow(mb.getRow());
  232. br.setXFIndex(mb.getXFAt(k));
  233. realretval[ k ] = br;
  234. }
  235. }
  236. if (realretval == null)
  237. {
  238. realretval = new Record[ 1 ];
  239. realretval[ 0 ] = retval;
  240. }
  241. return realretval;
  242. }
  243. public static short [] getAllKnownRecordSIDs()
  244. {
  245. short[] results = new short[ recordsMap.size() ];
  246. int i = 0;
  247. for (Iterator iterator = recordsMap.keySet().iterator();
  248. iterator.hasNext(); )
  249. {
  250. Short sid = ( Short ) iterator.next();
  251. results[ i++ ] = sid.shortValue();
  252. }
  253. return results;
  254. }
  255. private static Map recordsToMap(Class [] records)
  256. {
  257. Map result = new HashMap();
  258. Constructor constructor;
  259. for (int i = 0; i < records.length; i++)
  260. {
  261. Class record;
  262. short sid;
  263. record = records[ i ];
  264. try
  265. {
  266. sid = record.getField("sid").getShort(null);
  267. constructor = record.getConstructor(new Class[]
  268. {
  269. RecordInputStream.class
  270. });
  271. }
  272. catch (Exception illegalArgumentException)
  273. {
  274. throw new RecordFormatException(
  275. "Unable to determine record types", illegalArgumentException);
  276. }
  277. result.put(new Short(sid), constructor);
  278. }
  279. return result;
  280. }
  281. }