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.

SupBookRecord.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 java.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.logging.log4j.LogManager;
  19. import org.apache.logging.log4j.Logger;
  20. import org.apache.poi.util.GenericRecordUtil;
  21. import org.apache.poi.util.LittleEndianOutput;
  22. import org.apache.poi.util.StringUtil;
  23. /**
  24. * A External Workbook Description (Supplemental Book).
  25. * Its only a dummy record for making new ExternSheet Record
  26. */
  27. public final class SupBookRecord extends StandardRecord {
  28. private static final Logger LOG = LogManager.getLogger(SupBookRecord.class);
  29. public static final short sid = 0x01AE;
  30. private static final short SMALL_RECORD_SIZE = 4;
  31. private static final short TAG_INTERNAL_REFERENCES = 0x0401;
  32. private static final short TAG_ADD_IN_FUNCTIONS = 0x3A01;
  33. static final char CH_VOLUME = 1;
  34. static final char CH_SAME_VOLUME = 2;
  35. static final char CH_DOWN_DIR = 3;
  36. static final char CH_UP_DIR = 4;
  37. static final char CH_LONG_VOLUME = 5;
  38. static final char CH_STARTUP_DIR = 6;
  39. static final char CH_ALT_STARTUP_DIR = 7;
  40. static final char CH_LIB_DIR = 8;
  41. static final String PATH_SEPERATOR = System.getProperty("file.separator");
  42. private short field_1_number_of_sheets;
  43. private String field_2_encoded_url;
  44. private final String[] field_3_sheet_names;
  45. private final boolean _isAddInFunctions;
  46. public SupBookRecord(SupBookRecord other) {
  47. super(other);
  48. field_1_number_of_sheets = other.field_1_number_of_sheets;
  49. field_2_encoded_url = other.field_2_encoded_url;
  50. field_3_sheet_names = other.field_3_sheet_names;
  51. _isAddInFunctions = other._isAddInFunctions;
  52. }
  53. private SupBookRecord(boolean isAddInFuncs, short numberOfSheets) {
  54. // else not 'External References'
  55. field_1_number_of_sheets = numberOfSheets;
  56. field_2_encoded_url = null;
  57. field_3_sheet_names = null;
  58. _isAddInFunctions = isAddInFuncs;
  59. }
  60. public SupBookRecord(String url, String[] sheetNames) {
  61. field_1_number_of_sheets = (short) sheetNames.length;
  62. field_2_encoded_url = url;
  63. field_3_sheet_names = sheetNames;
  64. _isAddInFunctions = false;
  65. }
  66. public static SupBookRecord createInternalReferences(short numberOfSheets) {
  67. return new SupBookRecord(false, numberOfSheets);
  68. }
  69. public static SupBookRecord createAddInFunctions() {
  70. return new SupBookRecord(true, (short)1 /* this field MUST be 0x0001 for add-in referencing */);
  71. }
  72. public static SupBookRecord createExternalReferences(String url, String[] sheetNames) {
  73. return new SupBookRecord(url, sheetNames);
  74. }
  75. public boolean isExternalReferences() {
  76. return field_3_sheet_names != null;
  77. }
  78. public boolean isInternalReferences() {
  79. return field_3_sheet_names == null && !_isAddInFunctions;
  80. }
  81. public boolean isAddInFunctions() {
  82. return field_3_sheet_names == null && _isAddInFunctions;
  83. }
  84. /**
  85. * called by the constructor, should set class level fields. Should throw
  86. * runtime exception for bad/incomplete data.
  87. *
  88. * @param in the stream to read from
  89. */
  90. public SupBookRecord(RecordInputStream in) {
  91. int recLen = in.remaining();
  92. field_1_number_of_sheets = in.readShort();
  93. if(recLen > SMALL_RECORD_SIZE) {
  94. // 5.38.1 External References
  95. _isAddInFunctions = false;
  96. field_2_encoded_url = in.readString();
  97. String[] sheetNames = new String[field_1_number_of_sheets];
  98. for (int i = 0; i < sheetNames.length; i++) {
  99. sheetNames[i] = in.readString();
  100. }
  101. field_3_sheet_names = sheetNames;
  102. return;
  103. }
  104. // else not 'External References'
  105. field_2_encoded_url = null;
  106. field_3_sheet_names = null;
  107. short nextShort = in.readShort();
  108. if(nextShort == TAG_INTERNAL_REFERENCES) {
  109. // 5.38.2 'Internal References'
  110. _isAddInFunctions = false;
  111. } else if(nextShort == TAG_ADD_IN_FUNCTIONS) {
  112. // 5.38.3 'Add-In Functions'
  113. _isAddInFunctions = true;
  114. if(field_1_number_of_sheets != 1) {
  115. throw new RuntimeException("Expected 0x0001 for number of sheets field in 'Add-In Functions' but got ("
  116. + field_1_number_of_sheets + ")");
  117. }
  118. } else {
  119. throw new RuntimeException("invalid EXTERNALBOOK code ("
  120. + Integer.toHexString(nextShort) + ")");
  121. }
  122. }
  123. protected int getDataSize() {
  124. if(!isExternalReferences()) {
  125. return SMALL_RECORD_SIZE;
  126. }
  127. int sum = 2; // u16 number of sheets
  128. sum += StringUtil.getEncodedSize(field_2_encoded_url);
  129. for (String field_3_sheet_name : field_3_sheet_names) {
  130. sum += StringUtil.getEncodedSize(field_3_sheet_name);
  131. }
  132. return sum;
  133. }
  134. public void serialize(LittleEndianOutput out) {
  135. out.writeShort(field_1_number_of_sheets);
  136. if(isExternalReferences()) {
  137. StringUtil.writeUnicodeString(out, field_2_encoded_url);
  138. for (String field_3_sheet_name : field_3_sheet_names) {
  139. StringUtil.writeUnicodeString(out, field_3_sheet_name);
  140. }
  141. } else {
  142. int field2val = _isAddInFunctions ? TAG_ADD_IN_FUNCTIONS : TAG_INTERNAL_REFERENCES;
  143. out.writeShort(field2val);
  144. }
  145. }
  146. public void setNumberOfSheets(short number){
  147. field_1_number_of_sheets = number;
  148. }
  149. public short getNumberOfSheets(){
  150. return field_1_number_of_sheets;
  151. }
  152. public short getSid()
  153. {
  154. return sid;
  155. }
  156. public String getURL() {
  157. String encodedUrl = field_2_encoded_url;
  158. if (encodedUrl != null && encodedUrl.length() >= 2) {
  159. switch(encodedUrl.charAt(0)) {
  160. case 0: // Reference to an empty workbook name
  161. case 2: // Self-referential external reference
  162. // will this just be empty string?
  163. return encodedUrl.substring(1);
  164. case 1: // encoded file name
  165. return decodeFileName(encodedUrl);
  166. }
  167. }
  168. return encodedUrl;
  169. }
  170. private static String decodeFileName(String encodedUrl) {
  171. /* see "MICROSOFT OFFICE EXCEL 97-2007 BINARY FILE FORMAT SPECIFICATION" */
  172. StringBuilder sb = new StringBuilder();
  173. for(int i=1; i<encodedUrl.length(); i++) {
  174. char c = encodedUrl.charAt(i);
  175. switch (c) {
  176. case CH_VOLUME:
  177. char driveLetter = encodedUrl.charAt(++i);
  178. if (driveLetter == '@') {
  179. sb.append("\\\\");
  180. } else {
  181. //Windows notation for drive letters
  182. sb.append(driveLetter).append(":");
  183. }
  184. break;
  185. case CH_SAME_VOLUME:
  186. case CH_DOWN_DIR:
  187. sb.append(PATH_SEPERATOR);
  188. break;
  189. case CH_UP_DIR:
  190. sb.append("..").append(PATH_SEPERATOR);
  191. break;
  192. case CH_LONG_VOLUME:
  193. //Don't known to handle...
  194. LOG.atWarn().log("Found unexpected key: ChLongVolume - IGNORING");
  195. break;
  196. case CH_STARTUP_DIR:
  197. case CH_ALT_STARTUP_DIR:
  198. case CH_LIB_DIR:
  199. LOG.atWarn().log("EXCEL.EXE path unknown - using this directory instead: .");
  200. sb.append(".").append(PATH_SEPERATOR);
  201. break;
  202. default:
  203. sb.append(c);
  204. }
  205. }
  206. return sb.toString();
  207. }
  208. public String[] getSheetNames() {
  209. return field_3_sheet_names == null ? null : field_3_sheet_names.clone();
  210. }
  211. public void setURL(String pUrl) {
  212. //Keep the first marker character!
  213. field_2_encoded_url = field_2_encoded_url.substring(0, 1) + pUrl;
  214. }
  215. @Override
  216. public SupBookRecord copy() {
  217. return new SupBookRecord(this);
  218. }
  219. @Override
  220. public HSSFRecordTypes getGenericRecordType() {
  221. return HSSFRecordTypes.SUP_BOOK;
  222. }
  223. @Override
  224. public Map<String, Supplier<?>> getGenericProperties() {
  225. return GenericRecordUtil.getGenericProperties(
  226. "externalReferences", this::isExternalReferences,
  227. "internalReferences", this::isInternalReferences,
  228. "url", this::getURL,
  229. "numberOfSheets", this::getNumberOfSheets,
  230. "sheetNames", this::getSheetNames,
  231. "addInFunctions", this::isAddInFunctions
  232. );
  233. }
  234. }