/* ZssWorkbookParser.java
{{IS_NOTE
Purpose:
Description:
History:
Wed Jun 13 18:41:12 2007, Created by tomyeh
}}IS_NOTE
Copyright (C) 2007 Potix Corporation. All Rights Reserved.
{{IS_RIGHT
This program is distributed under GPL Version 2.0 in the hope that
it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
*/
package jxl.read.biff;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.zkoss.lang.Classes;
import org.zkoss.lang.SystemException;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.biff.StringHelper;
import jxl.biff.ZssBuiltInFormat;
import jxl.read.biff.BiffException;
import jxl.read.biff.File;
import jxl.read.biff.NameRecord;
import jxl.read.biff.PasswordException;
import jxl.read.biff.WorkbookParser;
import jxl.biff.formula.ZssVariableArgFunction;
public class ZssWorkbookParser extends WorkbookParser {
private Field _nmField;
public ZssWorkbookParser(File f, WorkbookSettings s) {
super(f, s);
ZssVariableArgFunction.initFunctions(); //force loading undefined Functions
ZssBuiltInFormat.initBuiltInFormat(); //force fixing incorrect built in format
}
// post processing to convert unknown variables from "ERROR" to its real variable name
protected void parse() throws BiffException, PasswordException {
super.parse();
//convert ERROR NameRecord to unresolved NameRecord
final List nameTable = (List) getField(this, "nameTable");
final Map nameRecord = (Map) getField(this, "namedRecords");
nameRecord.remove("ERROR");
int j = 0;
for (final Iterator it = nameTable.iterator(); it.hasNext(); ++j) {
final NameRecord nr = (NameRecord) it.next();
if ("ERROR".equals(nr.getName())) {
updateNameRecordName(nr);
nameRecord.put(nr.getName(), nr);
}
}
}
private void updateNameRecordName(NameRecord nr) {
final byte[] data = nr.getData();
final int length = data[3];
final String name = StringHelper.getString(data, length, 15, getSettings());
setNameField(nr, name);
}
public static Object getField(Object obj, String nm) {
try {
final Field fld = Classes.getAnyField(obj.getClass(), nm);
fld.setAccessible(true);
return fld.get(obj);
} catch (Exception ex) {
throw SystemException.Aide.wrap(ex);
}
}
private void setNameField(Object obj, Object val) {
if (_nmField == null) {
try {
_nmField = Classes.getAnyField(obj.getClass(), "name");
_nmField.setAccessible(true);
} catch (java.lang.NoSuchFieldException ex) {
throw SystemException.Aide.wrap(ex);
}
}
try {
_nmField.set(obj, val);
} catch (java.lang.IllegalAccessException ex) {
throw SystemException.Aide.wrap(ex);
}
}
public static Workbook getWorkbook(InputStream is)
throws IOException, BiffException {
final WorkbookSettings ws = new WorkbookSettings();
File dataFile = new File(is, ws);
ZssWorkbookParser workbook = new ZssWorkbookParser(dataFile, ws);
workbook.parse();
return workbook;
}
//delegate to make the function public
public static boolean isDefaultHeight(RowRecord rinfo) {
return rinfo.isDefaultHeight();
}
}