PageRenderTime 317ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/uk/ac/manchester/cs/owl/semspreadsheets/model/hssf/impl/WorkbookHSSFImpl.java

https://github.com/semantalytics/RightField
Java | 273 lines | 228 code | 35 blank | 10 comment | 38 complexity | 4a84f463416de04c18fd17d4ba4d20d6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*******************************************************************************
  2. * Copyright (c) 2009-2012, University of Manchester
  3. *
  4. * Licensed under the New BSD License.
  5. * Please see LICENSE file that is distributed with the source code
  6. ******************************************************************************/
  7. package uk.ac.manchester.cs.owl.semspreadsheets.model.hssf.impl;
  8. import java.io.BufferedInputStream;
  9. import java.io.BufferedOutputStream;
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.net.URI;
  15. import java.util.ArrayList;
  16. import java.util.Collection;
  17. import java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Set;
  20. import org.apache.log4j.Logger;
  21. import org.apache.poi.hssf.usermodel.HSSFCell;
  22. import org.apache.poi.hssf.usermodel.HSSFName;
  23. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  24. import org.apache.poi.hssf.usermodel.HSSFRow;
  25. import org.apache.poi.hssf.usermodel.HSSFSheet;
  26. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  27. import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
  28. import uk.ac.manchester.cs.owl.semspreadsheets.listeners.WorkbookChangeListener;
  29. import uk.ac.manchester.cs.owl.semspreadsheets.model.InvalidWorkbookFormatException;
  30. import uk.ac.manchester.cs.owl.semspreadsheets.model.MutableWorkbook;
  31. import uk.ac.manchester.cs.owl.semspreadsheets.model.NamedRange;
  32. import uk.ac.manchester.cs.owl.semspreadsheets.model.Range;
  33. import uk.ac.manchester.cs.owl.semspreadsheets.model.Sheet;
  34. import uk.ac.manchester.cs.owl.semspreadsheets.model.change.SetCellValue;
  35. import uk.ac.manchester.cs.owl.semspreadsheets.model.change.WorkbookChange;
  36. import uk.ac.manchester.cs.owl.semspreadsheets.model.change.WorkbookChangeEvent;
  37. import uk.ac.manchester.cs.owl.semspreadsheets.model.change.WorkbookChangeVisitor;
  38. /**
  39. * @author Stuart Owen
  40. * @author Matthew Horridge
  41. */
  42. public class WorkbookHSSFImpl implements MutableWorkbook, WorkbookChangeVisitor {
  43. private HSSFWorkbook workbook;
  44. private static final Logger logger = Logger.getLogger(WorkbookHSSFImpl.class);
  45. private Set<WorkbookChangeListener> changeListeners = new HashSet<WorkbookChangeListener>();
  46. public WorkbookHSSFImpl() {
  47. workbook = new HSSFWorkbook();
  48. workbook.createSheet();
  49. }
  50. public WorkbookHSSFImpl(HSSFWorkbook workbook) {
  51. this.workbook=workbook;
  52. }
  53. public WorkbookHSSFImpl(URI uri) throws IOException,InvalidWorkbookFormatException {
  54. InputStream inputStream = uri.toURL().openStream();
  55. try {
  56. workbook = new HSSFWorkbook(new BufferedInputStream(inputStream));
  57. }
  58. catch (OfficeXmlFileException e) {
  59. throw new InvalidWorkbookFormatException(e,uri);
  60. }
  61. catch (IOException e) {
  62. if (e.getMessage().toLowerCase().contains("invalid header signature")) {
  63. throw new InvalidWorkbookFormatException(e,uri);
  64. }
  65. else {
  66. throw e;
  67. }
  68. }
  69. }
  70. public HSSFWorkbook getHSSFWorkbook() {
  71. return workbook;
  72. }
  73. public void applyChange(WorkbookChange change) {
  74. logger.debug("APPLY CHANGE: " + change);
  75. change.accept(this);
  76. for (WorkbookChangeListener listener : new ArrayList<WorkbookChangeListener>(changeListeners)) {
  77. try {
  78. listener.workbookChanged(new WorkbookChangeEvent(change));
  79. }
  80. catch (Throwable e) {
  81. logger.error("Error notifying listener",e);
  82. }
  83. }
  84. }
  85. public Collection<NamedRange> getNamedRanges() {
  86. Collection<NamedRange> result = new ArrayList<NamedRange>();
  87. for(int i = 0; i < workbook.getNumberOfNames(); i++) {
  88. HSSFName name = workbook.getNameAt(i);
  89. if(!name.isDeleted() && !name.isFunctionName()) {
  90. NamedRange range = new NamedRangeHSSFImpl(this, name);
  91. result.add(range);
  92. }
  93. }
  94. return result;
  95. }
  96. public void addChangeListener(WorkbookChangeListener changeListener) {
  97. changeListeners.add(changeListener);
  98. }
  99. public void removeChangeListener(WorkbookChangeListener changeListener) {
  100. changeListeners.remove(changeListener);
  101. }
  102. public boolean containsSheet(String name) {
  103. return workbook.getSheet(name) != null;
  104. }
  105. public void deleteSheet(String name) {
  106. int index = workbook.getSheetIndex(name);
  107. if (index != -1) {
  108. workbook.removeSheetAt(index);
  109. }
  110. for(WorkbookChangeListener listener : new ArrayList<WorkbookChangeListener>(changeListeners)) {
  111. try {
  112. listener.sheetRemoved();
  113. }
  114. catch (Exception e) {
  115. logger.error("Error removing a sheet",e);
  116. }
  117. }
  118. }
  119. public void addName(String name, Range rng) {
  120. if(workbook.getName(name) != null) {
  121. workbook.removeName(name);
  122. }
  123. HSSFName hssfName = workbook.createName();
  124. hssfName.setNameName(name);
  125. hssfName.setRefersToFormula(rng.toFixedAddress());
  126. }
  127. public void removeName(String name) {
  128. workbook.removeName(name);
  129. }
  130. public Sheet addSheet() {
  131. Sheet sheet = createSheet();
  132. for(WorkbookChangeListener listener : new ArrayList<WorkbookChangeListener>(changeListeners)) {
  133. try {
  134. listener.sheetAdded();
  135. }
  136. catch (Exception e) {
  137. logger.error("Error adding a new sheet",e);
  138. }
  139. }
  140. return sheet;
  141. }
  142. protected Sheet createSheet() {
  143. int x=0;
  144. String name = "Sheet" + Integer.toString(x);
  145. while (containsSheet(name)) {
  146. x++;
  147. name = "Sheet" + Integer.toString(x);
  148. }
  149. HSSFSheet hssfSheet = workbook.createSheet(name);
  150. return new SheetHSSFImpl(this, hssfSheet);
  151. }
  152. public Sheet addHiddenSheet() {
  153. Sheet sheet = createSheet();
  154. sheet.setHidden(true);
  155. return sheet;
  156. }
  157. public Sheet addVeryHiddenSheet() {
  158. Sheet sheet = createSheet();
  159. sheet.setVeryHidden(true);
  160. return sheet;
  161. }
  162. protected void fireSheetRenamed(String oldName, String newName) {
  163. for(WorkbookChangeListener listener : new ArrayList<WorkbookChangeListener>(changeListeners)) {
  164. try {
  165. listener.sheetRenamed(oldName, newName);
  166. }
  167. catch (Exception e) {
  168. e.printStackTrace();
  169. }
  170. }
  171. }
  172. public List<Sheet> getSheets() {
  173. List<Sheet> sheets = new ArrayList<Sheet>();
  174. for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
  175. sheets.add(new SheetHSSFImpl(this, workbook.getSheetAt(i)));
  176. }
  177. return sheets;
  178. }
  179. public Sheet getSheet(String name) {
  180. HSSFSheet hssfSheet = workbook.getSheet(name);
  181. if (hssfSheet == null) {
  182. return null;
  183. }
  184. else {
  185. return new SheetHSSFImpl(this, hssfSheet);
  186. }
  187. }
  188. public Sheet getSheet(int index) {
  189. HSSFSheet hssfSheet = workbook.getSheetAt(index);
  190. if(hssfSheet == null) {
  191. return null;
  192. }
  193. else {
  194. return new SheetHSSFImpl(this, hssfSheet);
  195. }
  196. }
  197. public void saveAs(URI uri) throws IOException {
  198. File file = new File(uri);
  199. BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file));
  200. workbook.write(stream);
  201. stream.close();
  202. }
  203. public void visit(SetCellValue setCellValue) {
  204. HSSFSheet hssfSheet = workbook.getSheet(setCellValue.getSheet().getName());
  205. HSSFRow hssfRow = hssfSheet.getRow(setCellValue.getRow());
  206. if(hssfRow == null && setCellValue.getNewValue() != null) {
  207. hssfRow = hssfSheet.createRow(setCellValue.getRow());
  208. }
  209. HSSFCell hssfCell = hssfRow.getCell(setCellValue.getCol());
  210. if (hssfCell == null && setCellValue.getNewValue() != null) {
  211. hssfCell = hssfRow.createCell(setCellValue.getCol());
  212. }
  213. if (hssfCell != null) {
  214. if (setCellValue.getNewValue() != null) {
  215. hssfCell.setCellValue(new HSSFRichTextString(setCellValue.getNewValue().toString()));
  216. }
  217. else {
  218. hssfRow.removeCell(hssfCell);
  219. }
  220. }
  221. }
  222. @Override
  223. public List<Sheet> getVisibleSheets() {
  224. List<Sheet> result = new ArrayList<Sheet>();
  225. for (Sheet sheet : getSheets()) {
  226. if (!sheet.isHidden() && !sheet.isVeryHidden()) result.add(sheet);
  227. }
  228. return result;
  229. }
  230. @Override
  231. public List<WorkbookChangeListener> getAllChangeListeners() {
  232. return new ArrayList<WorkbookChangeListener>(changeListeners);
  233. }
  234. @Override
  235. public void clearChangeListeners() {
  236. changeListeners.clear();
  237. }
  238. }