You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FormulaViewer.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.dev;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.List;
  20. import org.apache.poi.hssf.model.HSSFFormulaParser;
  21. import org.apache.poi.hssf.record.FormulaRecord;
  22. import org.apache.poi.hssf.record.Record;
  23. import org.apache.poi.hssf.record.RecordFactory;
  24. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  25. import org.apache.poi.ss.formula.ptg.ExpPtg;
  26. import org.apache.poi.ss.formula.ptg.FuncPtg;
  27. import org.apache.poi.ss.formula.ptg.Ptg;
  28. /**
  29. * FormulaViewer - finds formulas in a BIFF8 file and attempts to read them/display
  30. * data from them. Only works if Formulas are enabled in "RecordFactory"
  31. * @author andy
  32. * @author Avik
  33. */
  34. public class FormulaViewer
  35. {
  36. private String file;
  37. private boolean list;
  38. /** Creates new FormulaViewer */
  39. public FormulaViewer()
  40. {
  41. }
  42. /**
  43. * Method run
  44. *
  45. * @throws IOException if the file contained errors
  46. */
  47. public void run() throws IOException {
  48. POIFSFileSystem fs = new POIFSFileSystem(new File(file), true);
  49. try {
  50. InputStream is = BiffViewer.getPOIFSInputStream(fs);
  51. try {
  52. List<Record> records = RecordFactory.createRecords(is);
  53. for (Record record : records) {
  54. if (record.getSid() == FormulaRecord.sid) {
  55. if (list) {
  56. listFormula((FormulaRecord) record);
  57. } else {
  58. parseFormulaRecord((FormulaRecord) record);
  59. }
  60. }
  61. }
  62. } finally {
  63. is.close();
  64. }
  65. } finally {
  66. fs.close();
  67. }
  68. }
  69. private void listFormula(FormulaRecord record) {
  70. String sep="~";
  71. Ptg[] tokens= record.getParsedExpression();
  72. Ptg token;
  73. int numptgs = tokens.length;
  74. String numArg;
  75. token = tokens[numptgs-1];
  76. if (token instanceof FuncPtg) {
  77. numArg = String.valueOf(numptgs-1);
  78. } else {
  79. numArg = String.valueOf(-1);
  80. }
  81. StringBuilder buf = new StringBuilder();
  82. if (token instanceof ExpPtg) return;
  83. buf.append(token.toFormulaString());
  84. buf.append(sep);
  85. switch (token.getPtgClass()) {
  86. case Ptg.CLASS_REF :
  87. buf.append("REF");
  88. break;
  89. case Ptg.CLASS_VALUE :
  90. buf.append("VALUE");
  91. break;
  92. case Ptg.CLASS_ARRAY :
  93. buf.append("ARRAY");
  94. break;
  95. default:
  96. throwInvalidRVAToken(token);
  97. }
  98. buf.append(sep);
  99. if (numptgs>1) {
  100. token = tokens[numptgs-2];
  101. switch (token.getPtgClass()) {
  102. case Ptg.CLASS_REF :
  103. buf.append("REF");
  104. break;
  105. case Ptg.CLASS_VALUE :
  106. buf.append("VALUE");
  107. break;
  108. case Ptg.CLASS_ARRAY :
  109. buf.append("ARRAY");
  110. break;
  111. default:
  112. throwInvalidRVAToken(token);
  113. }
  114. }else {
  115. buf.append("VALUE");
  116. }
  117. buf.append(sep);
  118. buf.append(numArg);
  119. System.out.println(buf);
  120. }
  121. /**
  122. * Method parseFormulaRecord
  123. *
  124. * @param record the record to be parsed
  125. */
  126. public void parseFormulaRecord(FormulaRecord record)
  127. {
  128. System.out.println("==============================");
  129. System.out.print("row = " + record.getRow());
  130. System.out.println(", col = " + record.getColumn());
  131. System.out.println("value = " + record.getValue());
  132. System.out.print("xf = " + record.getXFIndex());
  133. System.out.print(", number of ptgs = "
  134. + record.getParsedExpression().length);
  135. System.out.println(", options = " + record.getOptions());
  136. System.out.println("RPN List = "+formulaString(record));
  137. System.out.println("Formula text = "+ composeFormula(record));
  138. }
  139. private String formulaString(FormulaRecord record) {
  140. StringBuilder buf = new StringBuilder();
  141. Ptg[] tokens = record.getParsedExpression();
  142. for (Ptg token : tokens) {
  143. buf.append( token.toFormulaString());
  144. switch (token.getPtgClass()) {
  145. case Ptg.CLASS_REF :
  146. buf.append("(R)");
  147. break;
  148. case Ptg.CLASS_VALUE :
  149. buf.append("(V)");
  150. break;
  151. case Ptg.CLASS_ARRAY :
  152. buf.append("(A)");
  153. break;
  154. default:
  155. throwInvalidRVAToken(token);
  156. }
  157. buf.append(' ');
  158. }
  159. return buf.toString();
  160. }
  161. private static void throwInvalidRVAToken(Ptg token) {
  162. throw new IllegalStateException("Invalid RVA type (" + token.getPtgClass() + "). This should never happen.");
  163. }
  164. private static String composeFormula(FormulaRecord record)
  165. {
  166. return HSSFFormulaParser.toFormulaString(null, record.getParsedExpression());
  167. }
  168. /**
  169. * Method setFile
  170. *
  171. * @param file the file to process
  172. */
  173. public void setFile(String file)
  174. {
  175. this.file = file;
  176. }
  177. public void setList(boolean list) {
  178. this.list=list;
  179. }
  180. /**
  181. * Method main
  182. *
  183. * pass me a filename and I'll try and parse the formulas from it
  184. *
  185. * @param args pass one argument with the filename or --help
  186. * @throws IOException if the file can't be read or contained errors
  187. */
  188. public static void main(String args[]) throws IOException
  189. {
  190. if ((args == null) || (args.length >2 )
  191. || args[ 0 ].equals("--help"))
  192. {
  193. System.out.println(
  194. "FormulaViewer .8 proof that the devil lies in the details (or just in BIFF8 files in general)");
  195. System.out.println("usage: Give me a big fat file name");
  196. } else if (args[0].equals("--listFunctions")) { // undocumented attribute to research functions!~
  197. FormulaViewer viewer = new FormulaViewer();
  198. viewer.setFile(args[1]);
  199. viewer.setList(true);
  200. viewer.run();
  201. }
  202. else
  203. {
  204. FormulaViewer viewer = new FormulaViewer();
  205. viewer.setFile(args[ 0 ]);
  206. viewer.run();
  207. }
  208. }
  209. }