PageRenderTime 101ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/eventmodel/ModelFactory.java

http://github.com/openmicroscopy/bioformats
Java | 144 lines | 66 code | 18 blank | 60 comment | 10 complexity | 6650dc23e857d74ec265cc2fa653c40f 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.eventmodel;
  38. import java.io.InputStream;
  39. import java.util.ArrayList;
  40. import java.util.Iterator;
  41. import java.util.List;
  42. import loci.poi.hssf.model.Model;
  43. import loci.poi.hssf.model.Sheet;
  44. import loci.poi.hssf.model.Workbook;
  45. import loci.poi.hssf.record.BOFRecord;
  46. import loci.poi.hssf.record.EOFRecord;
  47. import loci.poi.hssf.record.Record;
  48. /**
  49. * ModelFactory creates workbook and sheet models based upon
  50. * events thrown by them there events from the EventRecordFactory.
  51. *
  52. * @see loci.poi.hssf.eventmodel.EventRecordFactory
  53. * @author Andrew C. Oliver acoliver@apache.org
  54. */
  55. public class ModelFactory implements ERFListener
  56. {
  57. List listeners;
  58. Model currentmodel;
  59. boolean lastEOF;
  60. /**
  61. * Constructor for ModelFactory. Does practically nothing.
  62. */
  63. public ModelFactory()
  64. {
  65. super();
  66. listeners = new ArrayList(1);
  67. }
  68. /**
  69. * register a ModelFactoryListener so that it can receive
  70. * Models as they are created.
  71. */
  72. public void registerListener(ModelFactoryListener listener) {
  73. listeners.add(listener);
  74. }
  75. /**
  76. * Start processing the Workbook stream into Model events.
  77. */
  78. public void run(InputStream stream) {
  79. EventRecordFactory factory = new EventRecordFactory(true);
  80. factory.registerListener(this,null);
  81. lastEOF = true;
  82. factory.processRecords(stream);
  83. }
  84. //ERFListener
  85. public boolean processRecord(Record rec)
  86. {
  87. if (rec.getSid() == BOFRecord.sid) {
  88. if (lastEOF != true) {
  89. throw new RuntimeException("Not yet handled embedded models");
  90. } else {
  91. BOFRecord bof = (BOFRecord)rec;
  92. switch (bof.getType()) {
  93. case BOFRecord.TYPE_WORKBOOK:
  94. currentmodel = new Workbook();
  95. break;
  96. case BOFRecord.TYPE_WORKSHEET:
  97. currentmodel = new Sheet();
  98. break;
  99. default:
  100. throw new RuntimeException("Unsupported model type "+bof.getType());
  101. }
  102. }
  103. }
  104. if (rec.getSid() == EOFRecord.sid) {
  105. lastEOF = true;
  106. throwEvent(currentmodel);
  107. } else {
  108. lastEOF = false;
  109. }
  110. return true;
  111. }
  112. /**
  113. * Throws the model as an event to the listeners
  114. * @param model to be thrown
  115. */
  116. private void throwEvent(Model model)
  117. {
  118. Iterator i = listeners.iterator();
  119. while (i.hasNext()) {
  120. ModelFactoryListener mfl = (ModelFactoryListener) i.next();
  121. mfl.process(model);
  122. }
  123. }
  124. }