PageRenderTime 68ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/semantalytics/RightField
Java | 139 lines | 99 code | 15 blank | 25 comment | 19 complexity | 9b32ac4d62bd845be04702f4e7103178 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.util.ArrayList;
  9. import java.util.List;
  10. import org.apache.poi.hssf.record.DVRecord;
  11. import org.apache.poi.hssf.record.Record;
  12. import org.apache.poi.hssf.record.aggregates.DataValidityTable;
  13. import org.apache.poi.hssf.record.aggregates.RecordAggregate;
  14. import org.apache.poi.hssf.usermodel.DVConstraint;
  15. import org.apache.poi.hssf.usermodel.HSSFDataValidation;
  16. import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
  17. import org.apache.poi.hssf.usermodel.HSSFSheet;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.ss.formula.Formula;
  20. import org.apache.poi.ss.formula.ptg.IntPtg;
  21. import org.apache.poi.ss.formula.ptg.NamePtg;
  22. import org.apache.poi.ss.formula.ptg.Ptg;
  23. import org.apache.poi.ss.formula.ptg.StringPtg;
  24. import org.apache.poi.ss.util.CellRangeAddressList;
  25. /**
  26. * This class isolates the interaction with Apache POI that relies on following accessor methods that were added to POI in the
  27. * patch. Hopefully one day this patching won't be necessary
  28. *
  29. * {@link HSSFSheet#getDataValidityTable()}
  30. * {@link DataValidityTable#clear()}
  31. * {@link DVRecord#getFormula1()}
  32. * {@link DVRecord#getFormula2()}
  33. *
  34. * @author Stuart Owen
  35. */
  36. public class PatchedPoi {
  37. private static PatchedPoi instance = new PatchedPoi();
  38. private PatchedPoi() {
  39. }
  40. public static PatchedPoi getInstance() {
  41. return instance;
  42. }
  43. public List<HSSFDataValidation> getValidationData(final HSSFSheet sheet,
  44. final HSSFWorkbook workbook) {
  45. final List<HSSFDataValidation> dataValidation = new ArrayList<HSSFDataValidation>();
  46. DataValidityTable dvt = sheet.getDataValidityTable();
  47. dvt.visitContainedRecords(new RecordAggregate.RecordVisitor() {
  48. /**
  49. * /** Implementors may call non-mutating methods on Record r.
  50. *
  51. * @param r must not be <code>null</code>
  52. *
  53. */
  54. public void visitRecord(Record r) {
  55. if (r instanceof DVRecord) {
  56. DVRecord dvRecord = (DVRecord) r;
  57. CellRangeAddressList cellRangeAddressList = dvRecord
  58. .getCellRangeAddress();
  59. int validationType = dvRecord.getDataType();
  60. if (validationType == DVConstraint.ValidationType.LIST) {
  61. Formula f1 = dvRecord.getFormula1();
  62. String formula1 = getStringFromPtgTokens(
  63. f1.getTokens(), workbook);
  64. DVConstraint dvConstraint = DVConstraint
  65. .createFormulaListConstraint(formula1);
  66. HSSFDataValidation validation = new HSSFDataValidation(
  67. cellRangeAddressList, dvConstraint);
  68. dataValidation.add(validation);
  69. } else if (validationType == DVConstraint.ValidationType.INTEGER
  70. || validationType == DVConstraint.ValidationType.DECIMAL
  71. || validationType == DVConstraint.ValidationType.TEXT_LENGTH
  72. ) {
  73. Formula f1 = dvRecord.getFormula1();
  74. Formula f2 = dvRecord.getFormula2();
  75. String formula1 = getStringFromPtgTokens(
  76. f1.getTokens(), workbook);
  77. String formula2 = getStringFromPtgTokens(
  78. f2.getTokens(), workbook);
  79. int comparison = dvRecord.getConditionOperator();
  80. DVConstraint dvConstraint = DVConstraint
  81. .createNumericConstraint(validationType,
  82. comparison, formula1, formula2);
  83. HSSFDataValidation validation = new HSSFDataValidation(
  84. cellRangeAddressList, dvConstraint);
  85. dataValidation.add(validation);
  86. } else if (validationType == DVConstraint.ValidationType.FORMULA) {
  87. Formula f1 = dvRecord.getFormula1();
  88. String formula1 = getStringFromPtgTokens(
  89. f1.getTokens(), workbook);
  90. //doesn't contain the full formula, including cell and comparison - but should contain all the information needed. If not, getStringFromPtgTokens
  91. //needs updating to handle the extra token types
  92. DVConstraint dvConstraint = DVConstraint.createCustomFormulaConstraint(formula1);
  93. HSSFDataValidation validation = new HSSFDataValidation(
  94. cellRangeAddressList, dvConstraint);
  95. dataValidation.add(validation);
  96. }
  97. }
  98. }
  99. });
  100. return dataValidation;
  101. }
  102. public void clearValidationData(HSSFSheet sheet) {
  103. sheet.getDataValidityTable().clear();
  104. }
  105. protected String getStringFromPtgTokens(Ptg[] tokens,
  106. HSSFWorkbook hssfWorkbook) {
  107. StringBuilder sb = new StringBuilder();
  108. for (Ptg token : tokens) {
  109. if (token instanceof NamePtg) {
  110. NamePtg namePtg = (NamePtg) token;
  111. HSSFEvaluationWorkbook wb = HSSFEvaluationWorkbook
  112. .create(hssfWorkbook);
  113. sb.append(wb.getNameText(namePtg));
  114. } else if (token instanceof StringPtg) {
  115. StringPtg stringPtg = (StringPtg) token;
  116. sb.append(stringPtg.getValue());
  117. } else if (token instanceof IntPtg) {
  118. IntPtg intPtg = (IntPtg) token;
  119. sb.append(intPtg.getValue());
  120. }
  121. }
  122. return sb.toString();
  123. }
  124. }