You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExternalNameRecord.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.hssf.record.constant.ConstantValueParser;
  17. import org.apache.poi.ss.formula.Formula;
  18. import org.apache.poi.util.LittleEndianOutput;
  19. import org.apache.poi.util.StringUtil;
  20. /**
  21. * EXTERNALNAME (0x0023)<p/>
  22. *
  23. * @author Josh Micich
  24. */
  25. public final class ExternalNameRecord extends StandardRecord {
  26. public final static short sid = 0x0023; // as per BIFF8. (some old versions used 0x223)
  27. private static final int OPT_BUILTIN_NAME = 0x0001;
  28. private static final int OPT_AUTOMATIC_LINK = 0x0002; // m$ doc calls this fWantAdvise
  29. private static final int OPT_PICTURE_LINK = 0x0004;
  30. private static final int OPT_STD_DOCUMENT_NAME = 0x0008;
  31. private static final int OPT_OLE_LINK = 0x0010;
  32. // private static final int OPT_CLIP_FORMAT_MASK = 0x7FE0;
  33. private static final int OPT_ICONIFIED_PICTURE_LINK= 0x8000;
  34. private short field_1_option_flag;
  35. private short field_2_index;
  36. private short field_3_not_used;
  37. private String field_4_name;
  38. private Formula field_5_name_definition;
  39. /**
  40. * 'rgoper' / 'Last received results of the DDE link'
  41. * (seems to be only applicable to DDE links)<br/>
  42. * Logically this is a 2-D array, which has been flattened into 1-D array here.
  43. */
  44. private Object[] _ddeValues;
  45. /**
  46. * (logical) number of columns in the {@link #_ddeValues} array
  47. */
  48. private int _nColumns;
  49. /**
  50. * (logical) number of rows in the {@link #_ddeValues} array
  51. */
  52. private int _nRows;
  53. /**
  54. * Convenience Function to determine if the name is a built-in name
  55. */
  56. public boolean isBuiltInName() {
  57. return (field_1_option_flag & OPT_BUILTIN_NAME) != 0;
  58. }
  59. /**
  60. * For OLE and DDE, links can be either 'automatic' or 'manual'
  61. */
  62. public boolean isAutomaticLink() {
  63. return (field_1_option_flag & OPT_AUTOMATIC_LINK) != 0;
  64. }
  65. /**
  66. * only for OLE and DDE
  67. */
  68. public boolean isPicureLink() {
  69. return (field_1_option_flag & OPT_PICTURE_LINK) != 0;
  70. }
  71. /**
  72. * DDE links only. If <code>true</code>, this denotes the 'StdDocumentName'
  73. */
  74. public boolean isStdDocumentNameIdentifier() {
  75. return (field_1_option_flag & OPT_STD_DOCUMENT_NAME) != 0;
  76. }
  77. public boolean isOLELink() {
  78. return (field_1_option_flag & OPT_OLE_LINK) != 0;
  79. }
  80. public boolean isIconifiedPictureLink() {
  81. return (field_1_option_flag & OPT_ICONIFIED_PICTURE_LINK) != 0;
  82. }
  83. /**
  84. * @return the standard String representation of this name
  85. */
  86. public String getText() {
  87. return field_4_name;
  88. }
  89. protected int getDataSize(){
  90. int result = 3 * 2 // 3 short fields
  91. + 2 + field_4_name.length(); // nameLen and name
  92. if(hasFormula()) {
  93. result += field_5_name_definition.getEncodedSize();
  94. } else {
  95. if (_ddeValues != null) {
  96. result += 3; // byte, short
  97. result += ConstantValueParser.getEncodedSize(_ddeValues);
  98. }
  99. }
  100. return result;
  101. }
  102. public void serialize(LittleEndianOutput out) {
  103. out.writeShort(field_1_option_flag);
  104. out.writeShort(field_2_index);
  105. out.writeShort(field_3_not_used);
  106. int nameLen = field_4_name.length();
  107. out.writeShort(nameLen);
  108. StringUtil.putCompressedUnicode(field_4_name, out);
  109. if (hasFormula()) {
  110. field_5_name_definition.serialize(out);
  111. } else {
  112. if (_ddeValues != null) {
  113. out.writeByte(_nColumns-1);
  114. out.writeShort(_nRows-1);
  115. ConstantValueParser.encode(out, _ddeValues);
  116. }
  117. }
  118. }
  119. public ExternalNameRecord(RecordInputStream in) {
  120. field_1_option_flag = in.readShort();
  121. field_2_index = in.readShort();
  122. field_3_not_used = in.readShort();
  123. int nameLength = in.readUByte();
  124. int multibyteFlag = in.readUByte();
  125. if (multibyteFlag == 0) {
  126. field_4_name = in.readCompressedUnicode(nameLength);
  127. } else {
  128. field_4_name = in.readUnicodeLEString(nameLength);
  129. }
  130. if(!hasFormula()) {
  131. if (!isStdDocumentNameIdentifier() && !isOLELink() && isAutomaticLink()) {
  132. // both need to be incremented
  133. int nColumns = in.readUByte() + 1;
  134. int nRows = in.readShort() + 1;
  135. int totalCount = nRows * nColumns;
  136. _ddeValues = ConstantValueParser.parse(in, totalCount);
  137. _nColumns = nColumns;
  138. _nRows = nRows;
  139. }
  140. if(in.remaining() > 0) {
  141. throw readFail("Some unread data (is formula present?)");
  142. }
  143. field_5_name_definition = null;
  144. return;
  145. }
  146. int nBytesRemaining = in.available();
  147. if(nBytesRemaining <= 0) {
  148. throw readFail("Ran out of record data trying to read formula.");
  149. }
  150. int formulaLen = in.readUShort();
  151. nBytesRemaining -=2;
  152. field_5_name_definition = Formula.read(formulaLen, in, nBytesRemaining);
  153. }
  154. /*
  155. * Makes better error messages (while hasFormula() is not reliable)
  156. * Remove this when hasFormula() is stable.
  157. */
  158. private RuntimeException readFail(String msg) {
  159. String fullMsg = msg + " fields: (option=" + field_1_option_flag + " index=" + field_2_index
  160. + " not_used=" + field_3_not_used + " name='" + field_4_name + "')";
  161. return new RecordFormatException(fullMsg);
  162. }
  163. private boolean hasFormula() {
  164. // TODO - determine exact conditions when formula is present
  165. if (false) {
  166. // "Microsoft Office Excel 97-2007 Binary File Format (.xls) Specification"
  167. // m$'s document suggests logic like this, but bugzilla 44774 att 21790 seems to disagree
  168. if (isStdDocumentNameIdentifier()) {
  169. if (isOLELink()) {
  170. // seems to be not possible according to m$ document
  171. throw new IllegalStateException(
  172. "flags (std-doc-name and ole-link) cannot be true at the same time");
  173. }
  174. return false;
  175. }
  176. if (isOLELink()) {
  177. return false;
  178. }
  179. return true;
  180. }
  181. // This was derived by trial and error, but doesn't seem quite right
  182. if (isAutomaticLink()) {
  183. return false;
  184. }
  185. return true;
  186. }
  187. public short getSid() {
  188. return sid;
  189. }
  190. public String toString() {
  191. StringBuffer sb = new StringBuffer();
  192. sb.append(getClass().getName()).append(" [EXTERNALNAME ");
  193. sb.append(" ").append(field_4_name);
  194. sb.append(" ix=").append(field_2_index);
  195. sb.append("]");
  196. return sb.toString();
  197. }
  198. }