PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/uk/ac/manchester/cs/owl/semspreadsheets/model/xssf/impl/WorkbookXSSFImpl.java

https://github.com/semantalytics/RightField
Java | 290 lines | 223 code | 39 blank | 28 comment | 38 complexity | 6097966462091e76b85ae67e547abd0b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package uk.ac.manchester.cs.owl.semspreadsheets.model.xssf.impl;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.URI;
  9. import java.util.ArrayList;
  10. import java.util.Collection;
  11. import java.util.List;
  12. import org.apache.log4j.Logger;
  13. import org.apache.poi.ss.usermodel.Name;
  14. import org.apache.poi.xssf.usermodel.XSSFCell;
  15. import org.apache.poi.xssf.usermodel.XSSFName;
  16. import org.apache.poi.xssf.usermodel.XSSFRichTextString;
  17. import org.apache.poi.xssf.usermodel.XSSFRow;
  18. import org.apache.poi.xssf.usermodel.XSSFSheet;
  19. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  20. import uk.ac.manchester.cs.owl.semspreadsheets.listeners.WorkbookChangeListener;
  21. import uk.ac.manchester.cs.owl.semspreadsheets.model.MutableWorkbook;
  22. import uk.ac.manchester.cs.owl.semspreadsheets.model.NamedRange;
  23. import uk.ac.manchester.cs.owl.semspreadsheets.model.Range;
  24. import uk.ac.manchester.cs.owl.semspreadsheets.model.Sheet;
  25. import uk.ac.manchester.cs.owl.semspreadsheets.model.change.SetCellValue;
  26. import uk.ac.manchester.cs.owl.semspreadsheets.model.change.WorkbookChange;
  27. import uk.ac.manchester.cs.owl.semspreadsheets.model.change.WorkbookChangeEvent;
  28. import uk.ac.manchester.cs.owl.semspreadsheets.model.change.WorkbookChangeVisitor;
  29. /*
  30. * Copyright (C) 2009, University of Manchester
  31. *
  32. * Modifications to the initial code base are copyright of their
  33. * respective authors, or their employers as appropriate. Authorship
  34. * of the modifications may be determined from the ChangeLog placed at
  35. * the end of this file.
  36. *
  37. * This library is free software; you can redistribute it and/or
  38. * modify it under the terms of the GNU Lesser General Public
  39. * License as published by the Free Software Foundation; either
  40. * version 2.1 of the License, or (at your option) any later version.
  41. * This library is distributed in the hope that it will be useful,
  42. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  44. * Lesser General Public License for more details.
  45. * You should have received a copy of the GNU Lesser General Public
  46. * License along with this library; if not, write to the Free Software
  47. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  48. */
  49. /**
  50. * @author Stuart Owen
  51. * @author Matthew Horridge
  52. */
  53. public class WorkbookXSSFImpl implements MutableWorkbook, WorkbookChangeVisitor {
  54. private XSSFWorkbook workbook;
  55. private static final Logger logger = Logger.getLogger(WorkbookXSSFImpl.class);
  56. private List<WorkbookChangeListener> changeListeners = new ArrayList<WorkbookChangeListener>();
  57. public WorkbookXSSFImpl() {
  58. workbook = new XSSFWorkbook();
  59. workbook.createSheet();
  60. }
  61. public WorkbookXSSFImpl(XSSFWorkbook workbook) {
  62. this.workbook=workbook;
  63. }
  64. public WorkbookXSSFImpl(URI uri) throws IOException {
  65. readWorkbook(uri);
  66. }
  67. public void saveAs(URI uri) throws IOException {
  68. File file = new File(uri);
  69. BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file));
  70. workbook.write(stream);
  71. stream.close();
  72. //this is a work-around to avoid https://issues.apache.org/bugzilla/show_bug.cgi?id=52233
  73. for (int i=0; i<workbook.getNumberOfSheets();i++) {
  74. workbook.getSheetAt(i).getColumnHelper().cleanColumns();
  75. }
  76. }
  77. private void readWorkbook(URI uri) throws IOException {
  78. InputStream inputStream = uri.toURL().openStream();
  79. workbook = new XSSFWorkbook(new BufferedInputStream(inputStream));
  80. }
  81. public XSSFWorkbook getXSSFWorkbook() {
  82. return workbook;
  83. }
  84. public void applyChange(WorkbookChange change) {
  85. logger.debug("APPLY CHANGE: " + change);
  86. change.accept(this);
  87. for (WorkbookChangeListener listener : new ArrayList<WorkbookChangeListener>(changeListeners)) {
  88. try {
  89. listener.workbookChanged(new WorkbookChangeEvent(change));
  90. }
  91. catch (Throwable e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. }
  96. public Collection<NamedRange> getNamedRanges() {
  97. Collection<NamedRange> result = new ArrayList<NamedRange>();
  98. for(int i = 0; i < workbook.getNumberOfNames(); i++) {
  99. XSSFName name = workbook.getNameAt(i);
  100. if(!name.isDeleted() && !name.isFunctionName()) {
  101. NamedRange range = new NamedRangeXSSFImpl(this, name);
  102. result.add(range);
  103. }
  104. }
  105. return result;
  106. }
  107. public void addChangeListener(WorkbookChangeListener changeListener) {
  108. changeListeners.add(changeListener);
  109. }
  110. public void removeChangeListener(WorkbookChangeListener changeListener) {
  111. changeListeners.remove(changeListener);
  112. }
  113. public boolean containsSheet(String name) {
  114. return workbook.getSheet(name) != null;
  115. }
  116. public void deleteSheet(String name) {
  117. logger.debug("Deleting sheet "+name);
  118. int index = workbook.getSheetIndex(name);
  119. if (index != -1) {
  120. workbook.removeSheetAt(index);
  121. logger.debug("Sheet removed with index "+index);
  122. }
  123. else {
  124. logger.debug("Sheet not found");
  125. }
  126. for(WorkbookChangeListener listener : new ArrayList<WorkbookChangeListener>(changeListeners)) {
  127. try {
  128. listener.sheetRemoved();
  129. }
  130. catch (Exception e) {
  131. logger.error("Error firing listener for removing a sheet",e);
  132. }
  133. }
  134. }
  135. public void addName(String name, Range rng) {
  136. if(workbook.getName(name) != null) {
  137. workbook.removeName(name);
  138. }
  139. Name xssfName = workbook.createName();
  140. xssfName.setNameName(name);
  141. xssfName.setRefersToFormula(rng.toFixedAddress());
  142. }
  143. public void removeName(String name) {
  144. workbook.removeName(name);
  145. }
  146. public Sheet addSheet() {
  147. Sheet sheet = createSheet();
  148. for(WorkbookChangeListener listener : new ArrayList<WorkbookChangeListener>(changeListeners)) {
  149. try {
  150. listener.sheetAdded();
  151. }
  152. catch (Exception e) {
  153. logger.error("Error adding a new sheet",e);
  154. }
  155. }
  156. return sheet;
  157. }
  158. protected Sheet createSheet() {
  159. int x=0;
  160. String name = "Sheet" + Integer.toString(x);
  161. while (containsSheet(name)) {
  162. x++;
  163. name = "Sheet" + Integer.toString(x);
  164. }
  165. XSSFSheet xssfSheet = workbook.createSheet(name);
  166. return new SheetXSSFImpl(this, xssfSheet);
  167. }
  168. public Sheet addHiddenSheet() {
  169. Sheet sheet = createSheet();
  170. sheet.setHidden(true);
  171. return sheet;
  172. }
  173. public Sheet addVeryHiddenSheet() {
  174. Sheet sheet = createSheet();
  175. sheet.setVeryHidden(true);
  176. return sheet;
  177. }
  178. protected void fireSheetRenamed(String oldName, String newName) {
  179. for(WorkbookChangeListener listener : new ArrayList<WorkbookChangeListener>(changeListeners)) {
  180. try {
  181. listener.sheetRenamed(oldName, newName);
  182. }
  183. catch (Exception e) {
  184. e.printStackTrace();
  185. }
  186. }
  187. }
  188. public List<Sheet> getSheets() {
  189. List<Sheet> sheets = new ArrayList<Sheet>();
  190. for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
  191. sheets.add(new SheetXSSFImpl(this, workbook.getSheetAt(i)));
  192. }
  193. return sheets;
  194. }
  195. public Sheet getSheet(String name) {
  196. XSSFSheet xssfSheet = workbook.getSheet(name);
  197. if (xssfSheet == null) {
  198. return null;
  199. }
  200. else {
  201. return new SheetXSSFImpl(this, xssfSheet);
  202. }
  203. }
  204. public Sheet getSheet(int index) {
  205. XSSFSheet xssfSheet = workbook.getSheetAt(index);
  206. if(xssfSheet == null) {
  207. return null;
  208. }
  209. else {
  210. return new SheetXSSFImpl(this, xssfSheet);
  211. }
  212. }
  213. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  214. public void visit(SetCellValue setCellValue) {
  215. XSSFSheet xssfSheet = workbook.getSheet(setCellValue.getSheet().getName());
  216. XSSFRow xssfRow = xssfSheet.getRow(setCellValue.getRow());
  217. if(xssfRow == null && setCellValue.getNewValue() != null) {
  218. xssfRow = xssfSheet.createRow(setCellValue.getRow());
  219. }
  220. XSSFCell xssfCell = xssfRow.getCell(setCellValue.getCol());
  221. if (xssfCell == null && setCellValue.getNewValue() != null) {
  222. xssfCell = xssfRow.createCell(setCellValue.getCol());
  223. }
  224. if (xssfCell != null) {
  225. if (setCellValue.getNewValue() != null) {
  226. xssfCell.setCellValue(new XSSFRichTextString(setCellValue.getNewValue().toString()));
  227. }
  228. else {
  229. xssfRow.removeCell(xssfCell);
  230. }
  231. }
  232. }
  233. @Override
  234. public List<Sheet> getVisibleSheets() {
  235. List<Sheet> result = new ArrayList<Sheet>();
  236. for (Sheet sheet : getSheets()) {
  237. if (!sheet.isHidden() && !sheet.isVeryHidden()) result.add(sheet);
  238. }
  239. return result;
  240. }
  241. @Override
  242. public List<WorkbookChangeListener> getAllChangeListeners() {
  243. return new ArrayList<WorkbookChangeListener>(changeListeners);
  244. }
  245. @Override
  246. public void clearChangeListeners() {
  247. changeListeners.clear();
  248. }
  249. }