您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ExternalNameRecord.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.record;
  16. import org.apache.poi.ss.formula.constant.ConstantValueParser;
  17. import org.apache.poi.ss.formula.Formula;
  18. import org.apache.poi.ss.formula.ptg.Ptg;
  19. import org.apache.poi.util.LittleEndianOutput;
  20. import org.apache.poi.util.StringUtil;
  21. /**
  22. * EXTERNALNAME (0x0023)<p/>
  23. *
  24. * @author Josh Micich
  25. */
  26. public final class ExternalNameRecord extends StandardRecord {
  27. public final static short sid = 0x0023; // as per BIFF8. (some old versions used 0x223)
  28. private static final int OPT_BUILTIN_NAME = 0x0001;
  29. private static final int OPT_AUTOMATIC_LINK = 0x0002; // m$ doc calls this fWantAdvise
  30. private static final int OPT_PICTURE_LINK = 0x0004;
  31. private static final int OPT_STD_DOCUMENT_NAME = 0x0008; //fOle
  32. private static final int OPT_OLE_LINK = 0x0010; //fOleLink
  33. // private static final int OPT_CLIP_FORMAT_MASK = 0x7FE0;
  34. private static final int OPT_ICONIFIED_PICTURE_LINK= 0x8000;
  35. private short field_1_option_flag;
  36. private short field_2_ixals;
  37. private short field_3_not_used;
  38. private String field_4_name;
  39. private Formula field_5_name_definition;
  40. /**
  41. * 'rgoper' / 'Last received results of the DDE link'
  42. * (seems to be only applicable to DDE links)<br/>
  43. * Logically this is a 2-D array, which has been flattened into 1-D array here.
  44. */
  45. private Object[] _ddeValues;
  46. /**
  47. * (logical) number of columns in the {@link #_ddeValues} array
  48. */
  49. private int _nColumns;
  50. /**
  51. * (logical) number of rows in the {@link #_ddeValues} array
  52. */
  53. private int _nRows;
  54. /**
  55. * Convenience Function to determine if the name is a built-in name
  56. */
  57. public boolean isBuiltInName() {
  58. return (field_1_option_flag & OPT_BUILTIN_NAME) != 0;
  59. }
  60. /**
  61. * For OLE and DDE, links can be either 'automatic' or 'manual'
  62. */
  63. public boolean isAutomaticLink() {
  64. return (field_1_option_flag & OPT_AUTOMATIC_LINK) != 0;
  65. }
  66. /**
  67. * only for OLE and DDE
  68. */
  69. public boolean isPicureLink() {
  70. return (field_1_option_flag & OPT_PICTURE_LINK) != 0;
  71. }
  72. /**
  73. * DDE links only. If <code>true</code>, this denotes the 'StdDocumentName'
  74. */
  75. public boolean isStdDocumentNameIdentifier() {
  76. return (field_1_option_flag & OPT_STD_DOCUMENT_NAME) != 0;
  77. }
  78. public boolean isOLELink() {
  79. return (field_1_option_flag & OPT_OLE_LINK) != 0;
  80. }
  81. public boolean isIconifiedPictureLink() {
  82. return (field_1_option_flag & OPT_ICONIFIED_PICTURE_LINK) != 0;
  83. }
  84. /**
  85. * @return the standard String representation of this name
  86. */
  87. public String getText() {
  88. return field_4_name;
  89. }
  90. public void setText(String str) {
  91. field_4_name = str;
  92. }
  93. /**
  94. * If this is a local name, then this is the (1 based)
  95. * index of the name of the Sheet this refers to, as
  96. * defined in the preceeding {@link SupBookRecord}.
  97. * If it isn't a local name, then it must be zero.
  98. */
  99. public short getIx() {
  100. return field_2_ixals;
  101. }
  102. public void setIx(short ix) {
  103. field_2_ixals = ix;
  104. }
  105. public Ptg[] getParsedExpression() {
  106. return Formula.getTokens(field_5_name_definition);
  107. }
  108. public void setParsedExpression(Ptg[] ptgs) {
  109. field_5_name_definition = Formula.create(ptgs);
  110. }
  111. protected int getDataSize(){
  112. int result = 2 + 4; // short and int
  113. result += StringUtil.getEncodedSize(field_4_name) - 1; //size is byte, not short
  114. if(!isOLELink() && !isStdDocumentNameIdentifier()){
  115. if(isAutomaticLink()){
  116. result += 3; // byte, short
  117. result += ConstantValueParser.getEncodedSize(_ddeValues);
  118. } else {
  119. result += field_5_name_definition.getEncodedSize();
  120. }
  121. }
  122. return result;
  123. }
  124. public void serialize(LittleEndianOutput out) {
  125. out.writeShort(field_1_option_flag);
  126. out.writeShort(field_2_ixals);
  127. out.writeShort(field_3_not_used);
  128. out.writeByte(field_4_name.length());
  129. StringUtil.writeUnicodeStringFlagAndData(out, field_4_name);
  130. if(!isOLELink() && !isStdDocumentNameIdentifier()){
  131. if(isAutomaticLink()){
  132. out.writeByte(_nColumns-1);
  133. out.writeShort(_nRows-1);
  134. ConstantValueParser.encode(out, _ddeValues);
  135. } else {
  136. field_5_name_definition.serialize(out);
  137. }
  138. }
  139. }
  140. public ExternalNameRecord() {
  141. field_2_ixals = 0;
  142. }
  143. public ExternalNameRecord(RecordInputStream in) {
  144. field_1_option_flag = in.readShort();
  145. field_2_ixals = in.readShort();
  146. field_3_not_used = in.readShort();
  147. int numChars = in.readUByte();
  148. field_4_name = StringUtil.readUnicodeString(in, numChars);
  149. // the record body can take different forms.
  150. // The form is dictated by the values of 3-th and 4-th bits in field_1_option_flag
  151. if(!isOLELink() && !isStdDocumentNameIdentifier()){
  152. // another switch: the fWantAdvise bit specifies whether the body describes
  153. // an external defined name or a DDE data item
  154. if(isAutomaticLink()){
  155. if(in.available() > 0) {
  156. //body specifies DDE data item
  157. int nColumns = in.readUByte() + 1;
  158. int nRows = in.readShort() + 1;
  159. int totalCount = nRows * nColumns;
  160. _ddeValues = ConstantValueParser.parse(in, totalCount);
  161. _nColumns = nColumns;
  162. _nRows = nRows;
  163. }
  164. } else {
  165. //body specifies an external defined name
  166. int formulaLen = in.readUShort();
  167. field_5_name_definition = Formula.read(formulaLen, in);
  168. }
  169. }
  170. }
  171. public short getSid() {
  172. return sid;
  173. }
  174. public String toString() {
  175. StringBuffer sb = new StringBuffer();
  176. sb.append("[EXTERNALNAME]\n");
  177. sb.append(" .options = ").append(field_1_option_flag).append("\n");
  178. sb.append(" .ix = ").append(field_2_ixals).append("\n");
  179. sb.append(" .name = ").append(field_4_name).append("\n");
  180. if(field_5_name_definition != null) {
  181. Ptg[] ptgs = field_5_name_definition.getTokens();
  182. for (int i = 0; i < ptgs.length; i++) {
  183. Ptg ptg = ptgs[i];
  184. sb.append(ptg.toString()).append(ptg.getRVAType()).append("\n");
  185. }
  186. }
  187. sb.append("[/EXTERNALNAME]\n");
  188. return sb.toString();
  189. }
  190. }