PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/dev/FormulaViewer.java

http://github.com/openmicroscopy/bioformats
Java | 281 lines | 170 code | 30 blank | 81 comment | 21 complexity | 8c0f3f19688417e8138fee787add6462 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. /*
  38. * FormulaViewer.java - finds formulas in a BIFF8 file and attempts to parse them and
  39. * display info about them.
  40. *
  41. * Created on November 18, 2001, 7:58 AM
  42. */
  43. package loci.poi.hssf.dev;
  44. import java.io.FileInputStream;
  45. //import java.io.*;
  46. import java.util.List;
  47. import loci.common.*;
  48. import loci.poi.poifs.filesystem.POIFSFileSystem;
  49. import loci.poi.hssf.record.*;
  50. import loci.poi.hssf.record.formula.*;
  51. import loci.poi.hssf.model.*;
  52. /**
  53. * FormulaViewer - finds formulas in a BIFF8 file and attempts to read them/display
  54. * data from them. Only works if Formulas are enabled in "RecordFactory"
  55. * @author andy
  56. * @author Avik
  57. */
  58. public class FormulaViewer
  59. {
  60. private String file;
  61. private boolean list=false;
  62. /** Creates new FormulaViewer */
  63. public FormulaViewer()
  64. {
  65. }
  66. /**
  67. * Method run
  68. *
  69. *
  70. * @exception Exception
  71. *
  72. */
  73. public void run()
  74. throws Exception
  75. {
  76. POIFSFileSystem fs =
  77. new POIFSFileSystem(new RandomAccessInputStream(file), 512);
  78. List records =
  79. RecordFactory
  80. .createRecords(fs.createDocumentInputStream("Workbook"));
  81. for (int k = 0; k < records.size(); k++)
  82. {
  83. Record record = ( Record ) records.get(k);
  84. if (record.getSid() == FormulaRecord.sid)
  85. {
  86. if (list) {
  87. listFormula((FormulaRecord) record);
  88. }else {
  89. parseFormulaRecord(( FormulaRecord ) record);
  90. }
  91. }
  92. }
  93. }
  94. private void listFormula(FormulaRecord record) {
  95. String sep="~";
  96. List tokens= record.getParsedExpression();
  97. int numptgs = record.getNumberOfExpressionTokens();
  98. Ptg token = null;
  99. String name,numArg;
  100. if (tokens != null) {
  101. token = (Ptg) tokens.get(numptgs-1);
  102. if (token instanceof FuncPtg) {
  103. numArg = String.valueOf(numptgs-1);
  104. } else { numArg = String.valueOf(-1);}
  105. StringBuffer buf = new StringBuffer();
  106. if (token instanceof ExpPtg) return;
  107. buf.append(name=((OperationPtg) token).toFormulaString((Workbook)null));
  108. buf.append(sep);
  109. switch (token.getPtgClass()) {
  110. case Ptg.CLASS_REF :
  111. buf.append("REF");
  112. break;
  113. case Ptg.CLASS_VALUE :
  114. buf.append("VALUE");
  115. break;
  116. case Ptg.CLASS_ARRAY :
  117. buf.append("ARRAY");
  118. break;
  119. }
  120. buf.append(sep);
  121. if (numptgs>1) {
  122. token = (Ptg) tokens.get(numptgs-2);
  123. switch (token.getPtgClass()) {
  124. case Ptg.CLASS_REF :
  125. buf.append("REF");
  126. break;
  127. case Ptg.CLASS_VALUE :
  128. buf.append("VALUE");
  129. break;
  130. case Ptg.CLASS_ARRAY :
  131. buf.append("ARRAY");
  132. break;
  133. }
  134. }else {
  135. buf.append("VALUE");
  136. }
  137. buf.append(sep);
  138. buf.append(numArg);
  139. System.out.println(buf.toString());
  140. } else {
  141. System.out.println("#NAME");
  142. }
  143. }
  144. /**
  145. * Method parseFormulaRecord
  146. *
  147. *
  148. * @param record
  149. *
  150. */
  151. public void parseFormulaRecord(FormulaRecord record)
  152. {
  153. System.out.println("==============================");
  154. System.out.print("row = " + record.getRow());
  155. System.out.println(", col = " + record.getColumn());
  156. System.out.println("value = " + record.getValue());
  157. System.out.print("xf = " + record.getXFIndex());
  158. System.out.print(", number of ptgs = "
  159. + record.getNumberOfExpressionTokens());
  160. System.out.println(", options = " + record.getOptions());
  161. System.out.println("RPN List = "+formulaString(record));
  162. System.out.println("Formula text = "+ composeFormula(record));
  163. }
  164. private String formulaString(FormulaRecord record) {
  165. StringBuffer formula = new StringBuffer("=");
  166. int numptgs = record.getNumberOfExpressionTokens();
  167. List tokens = record.getParsedExpression();
  168. Ptg token;
  169. StringBuffer buf = new StringBuffer();
  170. for (int i=0;i<numptgs;i++) {
  171. token = (Ptg) tokens.get(i);
  172. buf.append( token.toFormulaString((Workbook)null));
  173. switch (token.getPtgClass()) {
  174. case Ptg.CLASS_REF :
  175. buf.append("(R)");
  176. break;
  177. case Ptg.CLASS_VALUE :
  178. buf.append("(V)");
  179. break;
  180. case Ptg.CLASS_ARRAY :
  181. buf.append("(A)");
  182. break;
  183. }
  184. buf.append(' ');
  185. }
  186. return buf.toString();
  187. }
  188. private String composeFormula(FormulaRecord record)
  189. {
  190. return loci.poi.hssf.model.FormulaParser.toFormulaString((Workbook)null,record.getParsedExpression());
  191. }
  192. /**
  193. * Method setFile
  194. *
  195. *
  196. * @param file
  197. *
  198. */
  199. public void setFile(String file)
  200. {
  201. this.file = file;
  202. }
  203. public void setList(boolean list) {
  204. this.list=list;
  205. }
  206. /**
  207. * Method main
  208. *
  209. * pass me a filename and I'll try and parse the formulas from it
  210. *
  211. * @param args pass one argument with the filename or --help
  212. *
  213. */
  214. public static void main(String args[])
  215. {
  216. if ((args == null) || (args.length >2 )
  217. || args[ 0 ].equals("--help"))
  218. {
  219. System.out.println(
  220. "FormulaViewer .8 proof that the devil lies in the details (or just in BIFF8 files in general)");
  221. System.out.println("usage: Give me a big fat file name");
  222. } else if (args[0].equals("--listFunctions")) { // undocumented attribute to research functions!~
  223. try {
  224. FormulaViewer viewer = new FormulaViewer();
  225. viewer.setFile(args[1]);
  226. viewer.setList(true);
  227. viewer.run();
  228. }
  229. catch (Exception e) {
  230. System.out.println("Whoops!");
  231. e.printStackTrace();
  232. }
  233. }
  234. else
  235. {
  236. try
  237. {
  238. FormulaViewer viewer = new FormulaViewer();
  239. viewer.setFile(args[ 0 ]);
  240. viewer.run();
  241. }
  242. catch (Exception e)
  243. {
  244. System.out.println("Whoops!");
  245. e.printStackTrace();
  246. }
  247. }
  248. }
  249. }