fix for bug 9908, gracefully warn and write back for unknown ptg in namerecord

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352704 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Avik Sengupta 2002-06-17 17:20:16 +00:00
parent c5285fb57e
commit 70c6ad8bce

View File

@ -89,22 +89,23 @@ public class NameRecord extends Record {
private byte field_11_compressed_unicode_flag; // not documented private byte field_11_compressed_unicode_flag; // not documented
private String field_12_name_text; private String field_12_name_text;
private Stack field_13_name_definition; private Stack field_13_name_definition;
private byte[] field_13_raw_name_definition = null; // raw data
private String field_14_custom_menu_text; private String field_14_custom_menu_text;
private String field_15_description_text; private String field_15_description_text;
private String field_16_help_topic_text; private String field_16_help_topic_text;
private String field_17_status_bar_text; private String field_17_status_bar_text;
/** Creates new NameRecord */ /** Creates new NameRecord */
public NameRecord() { public NameRecord() {
field_13_name_definition = new Stack(); field_13_name_definition = new Stack();
field_12_name_text = new String(); field_12_name_text = new String();
field_14_custom_menu_text = new String(); field_14_custom_menu_text = new String();
field_15_description_text = new String(); field_15_description_text = new String();
field_16_help_topic_text = new String(); field_16_help_topic_text = new String();
field_17_status_bar_text = new String(); field_17_status_bar_text = new String();
} }
/** /**
* Constructs a Name record and sets its fields appropriately. * Constructs a Name record and sets its fields appropriately.
* *
@ -115,7 +116,7 @@ public class NameRecord extends Record {
public NameRecord(short id, short size, byte [] data) { public NameRecord(short id, short size, byte [] data) {
super(id, size, data); super(id, size, data);
} }
/** /**
* Constructs a Name record and sets its fields appropriately. * Constructs a Name record and sets its fields appropriately.
* *
@ -127,232 +128,232 @@ public class NameRecord extends Record {
public NameRecord(short id, short size, byte [] data, int offset) { public NameRecord(short id, short size, byte [] data, int offset) {
super(id, size, data, offset); super(id, size, data, offset);
} }
/** sets the option flag for the named range /** sets the option flag for the named range
* @param flag option flag * @param flag option flag
*/ */
public void setOptionFlag(short flag){ public void setOptionFlag(short flag){
field_1_option_flag = flag; field_1_option_flag = flag;
} }
/** sets the keyboard shortcut /** sets the keyboard shortcut
* @param shortcut keyboard shortcut * @param shortcut keyboard shortcut
*/ */
public void setKeyboardShortcut(byte shortcut){ public void setKeyboardShortcut(byte shortcut){
field_2_keyboard_shortcut = shortcut; field_2_keyboard_shortcut = shortcut;
} }
/** sets the name of the named range length /** sets the name of the named range length
* @param length name length * @param length name length
*/ */
public void setNameTextLength(byte length){ public void setNameTextLength(byte length){
field_3_length_name_text = length; field_3_length_name_text = length;
} }
/** sets the definition (reference - formula) length /** sets the definition (reference - formula) length
* @param length defenition length * @param length defenition length
*/ */
public void setDefinitionTextLength(short length){ public void setDefinitionTextLength(short length){
field_4_length_name_definition = length; field_4_length_name_definition = length;
} }
/** sets the index number to the extern sheet (thats is what writen in ducomentetion /** sets the index number to the extern sheet (thats is what writen in ducomentetion
* but as i saw , its work direrent) * but as i saw , its work direrent)
* @param index extern sheet index * @param index extern sheet index
*/ */
public void setIndexToSheet(short index){ public void setIndexToSheet(short index){
field_5_index_to_sheet = index; field_5_index_to_sheet = index;
// field_6_equals_to_index_to_sheet is equal to field_5_index_to_sheet // field_6_equals_to_index_to_sheet is equal to field_5_index_to_sheet
field_6_equals_to_index_to_sheet = index; field_6_equals_to_index_to_sheet = index;
} }
/** sets the custom menu length /** sets the custom menu length
* @param length custom menu length * @param length custom menu length
*/ */
public void setCustomMenuLength(byte length){ public void setCustomMenuLength(byte length){
field_7_length_custom_menu = length; field_7_length_custom_menu = length;
} }
/** sets the length of named range description /** sets the length of named range description
* @param length description length * @param length description length
*/ */
public void setDescriptionTextLength(byte length){ public void setDescriptionTextLength(byte length){
field_8_length_description_text = length; field_8_length_description_text = length;
} }
/** sets the help topic length /** sets the help topic length
* @param length help topic length * @param length help topic length
*/ */
public void setHelpTopicLength(byte length){ public void setHelpTopicLength(byte length){
field_9_length_help_topic_text = length; field_9_length_help_topic_text = length;
} }
/** sets the length of the status bar text /** sets the length of the status bar text
* @param length status bar text length * @param length status bar text length
*/ */
public void setStatusBarLength(byte length){ public void setStatusBarLength(byte length){
field_10_length_status_bar_text = length; field_10_length_status_bar_text = length;
} }
/** sets the compressed unicode flag /** sets the compressed unicode flag
* @param flag unicode flag * @param flag unicode flag
*/ */
public void setCompressedUnicodeFlag(byte flag) { public void setCompressedUnicodeFlag(byte flag) {
field_11_compressed_unicode_flag = flag; field_11_compressed_unicode_flag = flag;
} }
/** sets the name of the named range /** sets the name of the named range
* @param name named range name * @param name named range name
*/ */
public void setNameText(String name){ public void setNameText(String name){
field_12_name_text = name; field_12_name_text = name;
} }
// public void setNameDefintion(String definition){ // public void setNameDefintion(String definition){
// test = definition; // test = definition;
// } // }
/** sets the custom menu text /** sets the custom menu text
* @param text custom menu text * @param text custom menu text
*/ */
public void setCustomMenuText(String text){ public void setCustomMenuText(String text){
field_14_custom_menu_text = text; field_14_custom_menu_text = text;
} }
/** sets the description text /** sets the description text
* @param text the description text * @param text the description text
*/ */
public void setDescriptionText(String text){ public void setDescriptionText(String text){
field_15_description_text = text; field_15_description_text = text;
} }
/** sets the help topic text /** sets the help topic text
* @param text help topix text * @param text help topix text
*/ */
public void setHelpTopicText(String text){ public void setHelpTopicText(String text){
field_16_help_topic_text = text; field_16_help_topic_text = text;
} }
/** sets the status bar text /** sets the status bar text
* @param text status bar text * @param text status bar text
*/ */
public void setStatusBarText(String text){ public void setStatusBarText(String text){
field_17_status_bar_text = text; field_17_status_bar_text = text;
} }
/** gets the option flag /** gets the option flag
* @return option flag * @return option flag
*/ */
public short getOptionFlag(){ public short getOptionFlag(){
return field_1_option_flag; return field_1_option_flag;
} }
/** returns the keyboard shortcut /** returns the keyboard shortcut
* @return keyboard shortcut * @return keyboard shortcut
*/ */
public byte getKeyboardShortcut(){ public byte getKeyboardShortcut(){
return field_2_keyboard_shortcut ; return field_2_keyboard_shortcut ;
} }
/** gets the name length /** gets the name length
* @return name length * @return name length
*/ */
public byte getNameTextLength(){ public byte getNameTextLength(){
return field_3_length_name_text; return field_3_length_name_text;
} }
/** get the definition length /** get the definition length
* @return definition length * @return definition length
*/ */
public short getDefinitionTextLength(){ public short getDefinitionTextLength(){
return field_4_length_name_definition; return field_4_length_name_definition;
} }
/** gets the index to extern sheet /** gets the index to extern sheet
* @return index to extern sheet * @return index to extern sheet
*/ */
public short getIndexToSheet(){ public short getIndexToSheet(){
return field_5_index_to_sheet; return field_5_index_to_sheet;
} }
/** gets the custom menu length /** gets the custom menu length
* @return custom menu length * @return custom menu length
*/ */
public byte getCustomMenuLength(){ public byte getCustomMenuLength(){
return field_7_length_custom_menu; return field_7_length_custom_menu;
} }
/** gets the description text length /** gets the description text length
* @return description text length * @return description text length
*/ */
public byte getDescriptionTextLength(){ public byte getDescriptionTextLength(){
return field_8_length_description_text; return field_8_length_description_text;
} }
/** gets the help topic length /** gets the help topic length
* @return help topic length * @return help topic length
*/ */
public byte getHelpTopicLength(){ public byte getHelpTopicLength(){
return field_9_length_help_topic_text; return field_9_length_help_topic_text;
} }
/** get the status bar text length /** get the status bar text length
* @return satus bar length * @return satus bar length
*/ */
public byte getStatusBarLength(){ public byte getStatusBarLength(){
return field_10_length_status_bar_text; return field_10_length_status_bar_text;
} }
/** gets the name compressed Unicode flag /** gets the name compressed Unicode flag
* @return compressed unicode flag * @return compressed unicode flag
*/ */
public byte getCompressedUnicodeFlag() { public byte getCompressedUnicodeFlag() {
return field_11_compressed_unicode_flag; return field_11_compressed_unicode_flag;
} }
/** gets the name /** gets the name
* @return name * @return name
*/ */
public String getNameText(){ public String getNameText(){
return field_12_name_text; return field_12_name_text;
} }
/** gets the definition, reference (Formula) /** gets the definition, reference (Formula)
* @return definition * @return definition -- can be null if we cant parse ptgs
*/ */
public List getNameDefinition() { protected List getNameDefinition() {
return ( List ) field_13_name_definition; return ( List ) field_13_name_definition;
} }
/** get the custom menu text /** get the custom menu text
* @return custom menu text * @return custom menu text
*/ */
public String getCustomMenuText(){ public String getCustomMenuText(){
return field_14_custom_menu_text; return field_14_custom_menu_text;
} }
/** gets the description text /** gets the description text
* @return description text * @return description text
*/ */
public String getDescriptionText(){ public String getDescriptionText(){
return field_15_description_text; return field_15_description_text;
} }
/** get the help topic text /** get the help topic text
* @return gelp topic text * @return gelp topic text
*/ */
public String getHelpTopicText(){ public String getHelpTopicText(){
return field_16_help_topic_text; return field_16_help_topic_text;
} }
/** gets the status bar text /** gets the status bar text
* @return status bar text * @return status bar text
*/ */
public String getStatusBarText(){ public String getStatusBarText(){
return field_17_status_bar_text; return field_17_status_bar_text;
} }
/** /**
* called by constructor, should throw runtime exception in the event of a * called by constructor, should throw runtime exception in the event of a
* record passed with a differing ID. * record passed with a differing ID.
@ -364,7 +365,7 @@ public class NameRecord extends Record {
throw new RecordFormatException("NOT A valid Name RECORD"); throw new RecordFormatException("NOT A valid Name RECORD");
} }
} }
/** /**
* called by the class that is responsible for writing this sucker. * called by the class that is responsible for writing this sucker.
* Subclasses should implement this so that their data is passed back in a * Subclasses should implement this so that their data is passed back in a
@ -388,125 +389,133 @@ public class NameRecord extends Record {
data [16 + offset] = getHelpTopicLength(); data [16 + offset] = getHelpTopicLength();
data [17 + offset] = getStatusBarLength(); data [17 + offset] = getStatusBarLength();
data [18 + offset] = getCompressedUnicodeFlag(); data [18 + offset] = getCompressedUnicodeFlag();
StringUtil.putCompressedUnicode(getNameText(), data , 19 + offset); StringUtil.putCompressedUnicode(getNameText(), data , 19 + offset);
int start_of_name_definition = 19 + field_3_length_name_text; int start_of_name_definition = 19 + field_3_length_name_text;
serializePtgs(data, start_of_name_definition + offset); if (this.field_13_name_definition != null) {
serializePtgs(data, start_of_name_definition + offset);
} else {
System.arraycopy(field_13_raw_name_definition,0,data
,start_of_name_definition + offset,field_13_raw_name_definition.length);
}
int start_of_custom_menu_text = start_of_name_definition + field_4_length_name_definition; int start_of_custom_menu_text = start_of_name_definition + field_4_length_name_definition;
StringUtil.putCompressedUnicode(getCustomMenuText(), data , start_of_custom_menu_text + offset); StringUtil.putCompressedUnicode(getCustomMenuText(), data , start_of_custom_menu_text + offset);
int start_of_description_text = start_of_custom_menu_text + field_8_length_description_text; int start_of_description_text = start_of_custom_menu_text + field_8_length_description_text;
StringUtil.putCompressedUnicode(getDescriptionText(), data , start_of_description_text + offset); StringUtil.putCompressedUnicode(getDescriptionText(), data , start_of_description_text + offset);
int start_of_help_topic_text = start_of_description_text + field_9_length_help_topic_text; int start_of_help_topic_text = start_of_description_text + field_9_length_help_topic_text;
StringUtil.putCompressedUnicode(getHelpTopicText(), data , start_of_help_topic_text + offset); StringUtil.putCompressedUnicode(getHelpTopicText(), data , start_of_help_topic_text + offset);
int start_of_status_bar_text = start_of_help_topic_text + field_10_length_status_bar_text; int start_of_status_bar_text = start_of_help_topic_text + field_10_length_status_bar_text;
StringUtil.putCompressedUnicode(getStatusBarText(), data , start_of_status_bar_text + offset); StringUtil.putCompressedUnicode(getStatusBarText(), data , start_of_status_bar_text + offset);
return getRecordSize(); return getRecordSize();
} }
private void serializePtgs(byte [] data, int offset) { private void serializePtgs(byte [] data, int offset) {
int pos = offset; int pos = offset;
for (int k = 0; k < field_13_name_definition.size(); k++) { for (int k = 0; k < field_13_name_definition.size(); k++) {
Ptg ptg = ( Ptg ) field_13_name_definition.get(k); Ptg ptg = ( Ptg ) field_13_name_definition.get(k);
ptg.writeBytes(data, pos); ptg.writeBytes(data, pos);
pos += ptg.getSize(); pos += ptg.getSize();
} }
} }
/** gets the length of all texts /** gets the length of all texts
* @return total length * @return total length
*/ */
public int getTextsLength(){ public int getTextsLength(){
int result; int result;
result = getNameTextLength() + getDefinitionTextLength() + getDescriptionTextLength() + result = getNameTextLength() + getDefinitionTextLength() + getDescriptionTextLength() +
getHelpTopicLength() + getStatusBarLength(); getHelpTopicLength() + getStatusBarLength();
return result; return result;
} }
/** returns the record size /** returns the record size
*/ */
public int getRecordSize(){ public int getRecordSize(){
int result; int result;
result = 19 + getTextsLength(); result = 19 + getTextsLength();
return result; return result;
} }
/** gets the extern sheet number /** gets the extern sheet number
* @return extern sheet index * @return extern sheet index
*/ */
public short getExternSheetNumber(){ public short getExternSheetNumber(){
if (field_13_name_definition == null) return 0;
Ptg ptg = (Ptg) field_13_name_definition.peek(); Ptg ptg = (Ptg) field_13_name_definition.peek();
short result = 0; short result = 0;
if (ptg.getClass() == Area3DPtg.class){ if (ptg.getClass() == Area3DPtg.class){
result = ((Area3DPtg) ptg).getExternSheetIndex(); result = ((Area3DPtg) ptg).getExternSheetIndex();
} else if (ptg.getClass() == Ref3DPtg.class){ } else if (ptg.getClass() == Ref3DPtg.class){
result = ((Ref3DPtg) ptg).getExternSheetIndex(); result = ((Ref3DPtg) ptg).getExternSheetIndex();
} }
return result; return result;
} }
/** sets the extern sheet number /** sets the extern sheet number
* @param externSheetNumber extern sheet number * @param externSheetNumber extern sheet number
*/ */
public void setExternSheetNumber(short externSheetNumber){ public void setExternSheetNumber(short externSheetNumber){
Ptg ptg; Ptg ptg;
if (field_13_name_definition.isEmpty()){ if (field_13_name_definition == null || field_13_name_definition.isEmpty()){
field_13_name_definition = new Stack();
ptg = createNewPtg(); ptg = createNewPtg();
} else { } else {
ptg = (Ptg) field_13_name_definition.peek(); ptg = (Ptg) field_13_name_definition.peek();
} }
if (ptg.getClass() == Area3DPtg.class){ if (ptg.getClass() == Area3DPtg.class){
((Area3DPtg) ptg).setExternSheetIndex(externSheetNumber); ((Area3DPtg) ptg).setExternSheetIndex(externSheetNumber);
} else if (ptg.getClass() == Ref3DPtg.class){ } else if (ptg.getClass() == Ref3DPtg.class){
((Ref3DPtg) ptg).setExternSheetIndex(externSheetNumber); ((Ref3DPtg) ptg).setExternSheetIndex(externSheetNumber);
} }
} }
private Ptg createNewPtg(){ private Ptg createNewPtg(){
Ptg ptg = new Area3DPtg(); Ptg ptg = new Area3DPtg();
field_13_name_definition.push(ptg); field_13_name_definition.push(ptg);
return ptg; return ptg;
} }
/** gets the reference , the area only (range) /** gets the reference , the area only (range)
* @return area reference * @return area reference
*/ */
public String getAreaReference(){ public String getAreaReference(){
if (field_13_name_definition == null) return "#REF!";
Ptg ptg = (Ptg) field_13_name_definition.peek(); Ptg ptg = (Ptg) field_13_name_definition.peek();
String result = ""; String result = "";
if (ptg.getClass() == Area3DPtg.class){ if (ptg.getClass() == Area3DPtg.class){
result = ((Area3DPtg) ptg).getArea(); result = ((Area3DPtg) ptg).getArea();
} else if (ptg.getClass() == Ref3DPtg.class){ } else if (ptg.getClass() == Ref3DPtg.class){
result = ((Ref3DPtg) ptg).getArea(); result = ((Ref3DPtg) ptg).getArea();
} }
return result; return result;
} }
/** sets the reference , the area only (range) /** sets the reference , the area only (range)
* @param ref area reference * @param ref area reference
*/ */
@ -515,23 +524,24 @@ public class NameRecord extends Record {
RangeAddress ra = new RangeAddress(ref); RangeAddress ra = new RangeAddress(ref);
Ptg oldPtg; Ptg oldPtg;
Ptg ptg; Ptg ptg;
if (field_13_name_definition.isEmpty()){ if (field_13_name_definition==null ||field_13_name_definition.isEmpty()){
field_13_name_definition = new Stack();
oldPtg = createNewPtg(); oldPtg = createNewPtg();
} else { } else {
//Trying to find extern sheet index //Trying to find extern sheet index
oldPtg = (Ptg) field_13_name_definition.pop(); oldPtg = (Ptg) field_13_name_definition.pop();
} }
short externSheetIndex = 0; short externSheetIndex = 0;
if (oldPtg.getClass() == Area3DPtg.class){ if (oldPtg.getClass() == Area3DPtg.class){
externSheetIndex = ((Area3DPtg) oldPtg).getExternSheetIndex(); externSheetIndex = ((Area3DPtg) oldPtg).getExternSheetIndex();
} else if (oldPtg.getClass() == Ref3DPtg.class){ } else if (oldPtg.getClass() == Ref3DPtg.class){
externSheetIndex = ((Ref3DPtg) oldPtg).getExternSheetIndex(); externSheetIndex = ((Ref3DPtg) oldPtg).getExternSheetIndex();
} }
if (ra.hasRange()) { if (ra.hasRange()) {
ptg = new Area3DPtg(); ptg = new Area3DPtg();
((Area3DPtg) ptg).setExternSheetIndex(externSheetIndex); ((Area3DPtg) ptg).setExternSheetIndex(externSheetIndex);
@ -543,11 +553,11 @@ public class NameRecord extends Record {
((Ref3DPtg) ptg).setArea(ref); ((Ref3DPtg) ptg).setArea(ref);
this.setDefinitionTextLength((short)((Ref3DPtg) ptg).getSize()); this.setDefinitionTextLength((short)((Ref3DPtg) ptg).getSize());
} }
field_13_name_definition.push(ptg); field_13_name_definition.push(ptg);
} }
/** /**
* called by the constructor, should set class level fields. Should throw * called by the constructor, should set class level fields. Should throw
* runtime exception for bad/icomplete data. * runtime exception for bad/icomplete data.
@ -567,55 +577,62 @@ public class NameRecord extends Record {
field_8_length_description_text = data [11 + offset]; field_8_length_description_text = data [11 + offset];
field_9_length_help_topic_text = data [12 + offset]; field_9_length_help_topic_text = data [12 + offset];
field_10_length_status_bar_text = data [13 + offset]; field_10_length_status_bar_text = data [13 + offset];
field_11_compressed_unicode_flag= data [14 + offset]; field_11_compressed_unicode_flag= data [14 + offset];
field_12_name_text = new String(data, 15 + offset, field_12_name_text = new String(data, 15 + offset,
LittleEndian.ubyteToInt(field_3_length_name_text)); LittleEndian.ubyteToInt(field_3_length_name_text));
int start_of_name_definition = 15 + field_3_length_name_text; int start_of_name_definition = 15 + field_3_length_name_text;
field_13_name_definition = getParsedExpressionTokens(data, field_4_length_name_definition, field_13_name_definition = getParsedExpressionTokens(data, field_4_length_name_definition,
offset, start_of_name_definition); offset, start_of_name_definition);
int start_of_custom_menu_text = start_of_name_definition + field_4_length_name_definition; int start_of_custom_menu_text = start_of_name_definition + field_4_length_name_definition;
field_14_custom_menu_text = new String(data, start_of_custom_menu_text + offset, field_14_custom_menu_text = new String(data, start_of_custom_menu_text + offset,
LittleEndian.ubyteToInt(field_7_length_custom_menu)); LittleEndian.ubyteToInt(field_7_length_custom_menu));
int start_of_description_text = start_of_custom_menu_text + field_8_length_description_text; int start_of_description_text = start_of_custom_menu_text + field_8_length_description_text;
field_15_description_text = new String(data, start_of_description_text + offset, field_15_description_text = new String(data, start_of_description_text + offset,
LittleEndian.ubyteToInt(field_8_length_description_text)); LittleEndian.ubyteToInt(field_8_length_description_text));
int start_of_help_topic_text = start_of_description_text + field_9_length_help_topic_text; int start_of_help_topic_text = start_of_description_text + field_9_length_help_topic_text;
field_16_help_topic_text = new String(data, start_of_help_topic_text + offset, field_16_help_topic_text = new String(data, start_of_help_topic_text + offset,
LittleEndian.ubyteToInt(field_9_length_help_topic_text)); LittleEndian.ubyteToInt(field_9_length_help_topic_text));
int start_of_status_bar_text = start_of_help_topic_text + field_10_length_status_bar_text; int start_of_status_bar_text = start_of_help_topic_text + field_10_length_status_bar_text;
field_17_status_bar_text = new String(data, start_of_status_bar_text + offset, field_17_status_bar_text = new String(data, start_of_status_bar_text + offset,
LittleEndian.ubyteToInt(field_10_length_status_bar_text)); LittleEndian.ubyteToInt(field_10_length_status_bar_text));
} }
private Stack getParsedExpressionTokens(byte [] data, short size, private Stack getParsedExpressionTokens(byte [] data, short size,
int offset, int start_of_expression) { int offset, int start_of_expression) {
Stack stack = new Stack(); Stack stack = new Stack();
int pos = start_of_expression + offset; int pos = start_of_expression + offset;
int sizeCounter = 0; int sizeCounter = 0;
try {
while (sizeCounter < size) { while (sizeCounter < size) {
Ptg ptg = Ptg.createPtg(data, pos); Ptg ptg = Ptg.createPtg(data, pos);
pos += ptg.getSize(); pos += ptg.getSize();
sizeCounter += ptg.getSize(); sizeCounter += ptg.getSize();
stack.push(ptg); stack.push(ptg);
}
} catch (java.lang.UnsupportedOperationException uoe) {
System.err.println("[WARNING] Unknown Ptg "
+ uoe.getMessage() );
field_13_raw_name_definition=new byte[size];
System.arraycopy(data,offset,field_13_raw_name_definition,0,size);
return null;
} }
return stack; return stack;
} }
/** /**
* return the non static version of the id for this record. * return the non static version of the id for this record.
*/ */
public short getSid() { public short getSid() {
return this.sid; return this.sid;
} }
} }