1 16 17 18 24 package org.apache.poi.hssf.dev; 25 26 import java.io.InputStream ; 27 import java.io.IOException ; 28 import java.io.ByteArrayInputStream ; 29 import java.io.FileInputStream ; 30 import java.io.FileOutputStream ; 31 32 import java.util.List ; 34 35 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 36 import org.apache.poi.util.LittleEndian; 37 import org.apache.poi.util.HexDump; 38 import org.apache.poi.hssf.record.*; 39 import org.apache.poi.hssf.record.formula.*; 40 import org.apache.poi.hssf.model.*; 41 import org.apache.poi.hssf.usermodel.*; 42 43 49 50 public class FormulaViewer 51 { 52 private String file; 53 private boolean list=false; 54 55 56 57 public FormulaViewer() 58 { 59 } 60 61 68 69 public void run() 70 throws Exception 71 { 72 POIFSFileSystem fs = 73 new POIFSFileSystem(new FileInputStream (file)); 74 List records = 75 RecordFactory 76 .createRecords(fs.createDocumentInputStream("Workbook")); 77 78 for (int k = 0; k < records.size(); k++) 79 { 80 Record record = ( Record ) records.get(k); 81 82 if (record.getSid() == FormulaRecord.sid) 83 { 84 if (list) { 85 listFormula((FormulaRecord) record); 86 }else { 87 parseFormulaRecord(( FormulaRecord ) record); 88 } 89 } 90 } 91 } 92 93 private void listFormula(FormulaRecord record) { 94 String sep="~"; 95 List tokens= record.getParsedExpression(); 96 int numptgs = record.getNumberOfExpressionTokens(); 97 Ptg token = null; 98 String name,numArg; 99 if (tokens != null) { 100 token = (Ptg) tokens.get(numptgs-1); 101 if (token instanceof FuncPtg) { 102 numArg = String.valueOf(numptgs-1); 103 } else { numArg = String.valueOf(-1);} 104 105 StringBuffer buf = new StringBuffer (); 106 107 if (token instanceof ExpPtg) return; 108 buf.append(name=((OperationPtg) token).toFormulaString((Workbook)null)); 109 buf.append(sep); 110 switch (token.getPtgClass()) { 111 case Ptg.CLASS_REF : 112 buf.append("REF"); 113 break; 114 case Ptg.CLASS_VALUE : 115 buf.append("VALUE"); 116 break; 117 case Ptg.CLASS_ARRAY : 118 buf.append("ARRAY"); 119 break; 120 } 121 122 buf.append(sep); 123 if (numptgs>1) { 124 token = (Ptg) tokens.get(numptgs-2); 125 switch (token.getPtgClass()) { 126 case Ptg.CLASS_REF : 127 buf.append("REF"); 128 break; 129 case Ptg.CLASS_VALUE : 130 buf.append("VALUE"); 131 break; 132 case Ptg.CLASS_ARRAY : 133 buf.append("ARRAY"); 134 break; 135 } 136 }else { 137 buf.append("VALUE"); 138 } 139 buf.append(sep); 140 buf.append(numArg); 141 System.out.println(buf.toString()); 142 } else { 143 System.out.println("#NAME"); 144 } 145 } 146 147 154 155 public void parseFormulaRecord(FormulaRecord record) 156 { 157 System.out.println("=============================="); 158 System.out.print("row = " + record.getRow()); 159 System.out.println(", col = " + record.getColumn()); 160 System.out.println("value = " + record.getValue()); 161 System.out.print("xf = " + record.getXFIndex()); 162 System.out.print(", number of ptgs = " 163 + record.getNumberOfExpressionTokens()); 164 System.out.println(", options = " + record.getOptions()); 165 System.out.println("RPN List = "+formulaString(record)); 166 System.out.println("Formula text = "+ composeFormula(record)); 167 } 168 169 private String formulaString(FormulaRecord record) { 170 StringBuffer formula = new StringBuffer ("="); 171 int numptgs = record.getNumberOfExpressionTokens(); 172 List tokens = record.getParsedExpression(); 173 Ptg token; 174 StringBuffer buf = new StringBuffer (); 175 for (int i=0;i<numptgs;i++) { 176 token = (Ptg) tokens.get(i); 177 buf.append( token.toFormulaString((Workbook)null)); 178 switch (token.getPtgClass()) { 179 case Ptg.CLASS_REF : 180 buf.append("(R)"); 181 break; 182 case Ptg.CLASS_VALUE : 183 buf.append("(V)"); 184 break; 185 case Ptg.CLASS_ARRAY : 186 buf.append("(A)"); 187 break; 188 } 189 buf.append(' '); 190 } 191 return buf.toString(); 192 } 193 194 195 private String composeFormula(FormulaRecord record) 196 { 197 return org.apache.poi.hssf.model.FormulaParser.toFormulaString((Workbook)null,record.getParsedExpression()); 198 } 199 200 207 208 public void setFile(String file) 209 { 210 this.file = file; 211 } 212 213 public void setList(boolean list) { 214 this.list=list; 215 } 216 217 225 226 public static void main(String args[]) 227 { 228 if ((args == null) || (args.length >2 ) 229 || args[ 0 ].equals("--help")) 230 { 231 System.out.println( 232 "FormulaViewer .8 proof that the devil lies in the details (or just in BIFF8 files in general)"); 233 System.out.println("usage: Give me a big fat file name"); 234 } else if (args[0].equals("--listFunctions")) { try { 236 FormulaViewer viewer = new FormulaViewer(); 237 viewer.setFile(args[1]); 238 viewer.setList(true); 239 viewer.run(); 240 } 241 catch (Exception e) { 242 System.out.println("Whoops!"); 243 e.printStackTrace(); 244 } 245 } 246 else 247 { 248 try 249 { 250 FormulaViewer viewer = new FormulaViewer(); 251 252 viewer.setFile(args[ 0 ]); 253 viewer.run(); 254 } 255 catch (Exception e) 256 { 257 System.out.println("Whoops!"); 258 e.printStackTrace(); 259 } 260 } 261 } 262 } 263 | Popular Tags |