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 |
|
---|
9 | http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 |
|
---|
11 | Unless required by applicable law or agreed to in writing, software
|
---|
12 | distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | See the License for the specific language governing permissions and
|
---|
15 | limitations under the License.
|
---|
16 | ==================================================================== */
|
---|
17 |
|
---|
18 | package org.apache.poi.hssf.record;
|
---|
19 |
|
---|
20 | import org.apache.poi.hssf.record.formula.Ptg;
|
---|
21 | import org.apache.poi.ss.formula.Formula;
|
---|
22 | import org.apache.poi.util.HexDump;
|
---|
23 | import org.apache.poi.util.LittleEndianInput;
|
---|
24 | import org.apache.poi.util.LittleEndianOutput;
|
---|
25 | import org.apache.poi.util.StringUtil;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Title: DEFINEDNAME Record (0x0018) <p/>
|
---|
29 | * Description: Defines a named range within a workbook. <P>
|
---|
30 | * REFERENCE: <P>
|
---|
31 | * @author Libin Roman (Vista Portal LDT. Developer)
|
---|
32 | * @author Sergei Kozello (sergeikozello at mail.ru)
|
---|
33 | * @author Glen Stampoultzis (glens at apache.org)
|
---|
34 | * @author Petr Udalau - added method setFunction(boolean)
|
---|
35 | */
|
---|
36 | public final class NameRecord extends StandardRecord {
|
---|
37 | public final static short sid = 0x0018;
|
---|
38 | /**Included for completeness sake, not implemented */
|
---|
39 | public final static byte BUILTIN_CONSOLIDATE_AREA = 1;
|
---|
40 | /**Included for completeness sake, not implemented */
|
---|
41 | public final static byte BUILTIN_AUTO_OPEN = 2;
|
---|
42 | /**Included for completeness sake, not implemented */
|
---|
43 | public final static byte BUILTIN_AUTO_CLOSE = 3;
|
---|
44 | /**Included for completeness sake, not implemented */
|
---|
45 | public final static byte BUILTIN_DATABASE = 4;
|
---|
46 | /**Included for completeness sake, not implemented */
|
---|
47 | public final static byte BUILTIN_CRITERIA = 5;
|
---|
48 |
|
---|
49 | public final static byte BUILTIN_PRINT_AREA = 6;
|
---|
50 | public final static byte BUILTIN_PRINT_TITLE = 7;
|
---|
51 |
|
---|
52 | /**Included for completeness sake, not implemented */
|
---|
53 | public final static byte BUILTIN_RECORDER = 8;
|
---|
54 | /**Included for completeness sake, not implemented */
|
---|
55 | public final static byte BUILTIN_DATA_FORM = 9;
|
---|
56 | /**Included for completeness sake, not implemented */
|
---|
57 | public final static byte BUILTIN_AUTO_ACTIVATE = 10;
|
---|
58 | /**Included for completeness sake, not implemented */
|
---|
59 | public final static byte BUILTIN_AUTO_DEACTIVATE = 11;
|
---|
60 | /**Included for completeness sake, not implemented */
|
---|
61 | public final static byte BUILTIN_SHEET_TITLE = 12;
|
---|
62 |
|
---|
63 | public final static byte BUILTIN_FILTER_DB = 13;
|
---|
64 |
|
---|
65 | private static final class Option {
|
---|
66 | public static final int OPT_BUILTIN = 0x0020;
|
---|
67 | }
|
---|
68 |
|
---|
69 | private short field_1_option_flag;
|
---|
70 | private byte field_2_keyboard_shortcut;
|
---|
71 | /** One-based extern index of sheet (resolved via LinkTable). Zero if this is a global name */
|
---|
72 | private short field_5_externSheetIndex_plus1;
|
---|
73 | /** the one based sheet number. */
|
---|
74 | private int field_6_sheetNumber;
|
---|
75 | private boolean field_11_nameIsMultibyte;
|
---|
76 | private byte field_12_built_in_code;
|
---|
77 | private String field_12_name_text;
|
---|
78 | private Formula field_13_name_definition;
|
---|
79 | private String field_14_custom_menu_text;
|
---|
80 | private String field_15_description_text;
|
---|
81 | private String field_16_help_topic_text;
|
---|
82 | private String field_17_status_bar_text;
|
---|
83 |
|
---|
84 |
|
---|
85 | /** Creates new NameRecord */
|
---|
86 | public NameRecord() {
|
---|
87 | field_13_name_definition = Formula.create(Ptg.EMPTY_PTG_ARRAY);
|
---|
88 |
|
---|
89 | field_12_name_text = "";
|
---|
90 | field_14_custom_menu_text = "";
|
---|
91 | field_15_description_text = "";
|
---|
92 | field_16_help_topic_text = "";
|
---|
93 | field_17_status_bar_text = "";
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Constructor to create a built-in named region
|
---|
98 | * @param builtin Built-in byte representation for the name record, use the public constants
|
---|
99 | */
|
---|
100 | public NameRecord(byte builtin, int sheetNumber) // NO_UCD
|
---|
101 | {
|
---|
102 | this();
|
---|
103 | field_12_built_in_code = builtin;
|
---|
104 | setOptionFlag((short)(field_1_option_flag | Option.OPT_BUILTIN));
|
---|
105 | field_6_sheetNumber = sheetNumber; //the extern sheets are set through references
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** sets the option flag for the named range
|
---|
109 | * @param flag option flag
|
---|
110 | */
|
---|
111 | public void setOptionFlag(short flag){
|
---|
112 | field_1_option_flag = flag;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** gets the option flag
|
---|
116 | * @return option flag
|
---|
117 | */
|
---|
118 | public short getOptionFlag(){
|
---|
119 | return field_1_option_flag;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** returns the keyboard shortcut
|
---|
123 | * @return keyboard shortcut
|
---|
124 | */
|
---|
125 | public byte getKeyboardShortcut(){
|
---|
126 | return field_2_keyboard_shortcut ;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * gets the name length, in characters
|
---|
131 | * @return name length
|
---|
132 | */
|
---|
133 | private int getNameTextLength(){
|
---|
134 | if (isBuiltInName()) {
|
---|
135 | return 1;
|
---|
136 | }
|
---|
137 | return field_12_name_text.length();
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**Convenience Function to determine if the name is a built-in name
|
---|
141 | */
|
---|
142 | public boolean isBuiltInName()
|
---|
143 | {
|
---|
144 | return ((field_1_option_flag & Option.OPT_BUILTIN) != 0);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /** gets the name
|
---|
149 | * @return name
|
---|
150 | */
|
---|
151 | public String getNameText(){
|
---|
152 |
|
---|
153 | return isBuiltInName() ? translateBuiltInName(getBuiltInName()) : field_12_name_text;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /** Gets the Built In Name
|
---|
157 | * @return the built in Name
|
---|
158 | */
|
---|
159 | public byte getBuiltInName()
|
---|
160 | {
|
---|
161 | return field_12_built_in_code;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** get the custom menu text
|
---|
165 | * @return custom menu text
|
---|
166 | */
|
---|
167 | public String getCustomMenuText(){
|
---|
168 | return field_14_custom_menu_text;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** gets the description text
|
---|
172 | * @return description text
|
---|
173 | */
|
---|
174 | public String getDescriptionText(){
|
---|
175 | return field_15_description_text;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** get the help topic text
|
---|
179 | * @return gelp topic text
|
---|
180 | */
|
---|
181 | public String getHelpTopicText(){
|
---|
182 | return field_16_help_topic_text;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /** gets the status bar text
|
---|
186 | * @return status bar text
|
---|
187 | */
|
---|
188 | public String getStatusBarText(){
|
---|
189 | return field_17_status_bar_text;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | public void serialize(LittleEndianOutput out) {
|
---|
194 |
|
---|
195 | int field_7_length_custom_menu = field_14_custom_menu_text.length();
|
---|
196 | int field_8_length_description_text = field_15_description_text.length();
|
---|
197 | int field_9_length_help_topic_text = field_16_help_topic_text.length();
|
---|
198 | int field_10_length_status_bar_text = field_17_status_bar_text.length();
|
---|
199 |
|
---|
200 | // size defined below
|
---|
201 | out.writeShort(getOptionFlag());
|
---|
202 | out.writeByte(getKeyboardShortcut());
|
---|
203 | out.writeByte(getNameTextLength());
|
---|
204 | // Note - formula size is not immediately before encoded formula, and does not include any array constant data
|
---|
205 | out.writeShort(field_13_name_definition.getEncodedTokenSize());
|
---|
206 | out.writeShort(field_5_externSheetIndex_plus1);
|
---|
207 | out.writeShort(field_6_sheetNumber);
|
---|
208 | out.writeByte(field_7_length_custom_menu);
|
---|
209 | out.writeByte(field_8_length_description_text);
|
---|
210 | out.writeByte(field_9_length_help_topic_text);
|
---|
211 | out.writeByte(field_10_length_status_bar_text);
|
---|
212 | out.writeByte(field_11_nameIsMultibyte ? 1 : 0);
|
---|
213 |
|
---|
214 | if (isBuiltInName()) {
|
---|
215 | //can send the builtin name directly in
|
---|
216 | out.writeByte(field_12_built_in_code);
|
---|
217 | } else {
|
---|
218 | String nameText = field_12_name_text;
|
---|
219 | if (field_11_nameIsMultibyte) {
|
---|
220 | StringUtil.putUnicodeLE(nameText, out);
|
---|
221 | } else {
|
---|
222 | StringUtil.putCompressedUnicode(nameText, out);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | field_13_name_definition.serializeTokens(out);
|
---|
226 | field_13_name_definition.serializeArrayConstantData(out);
|
---|
227 |
|
---|
228 | StringUtil.putCompressedUnicode( getCustomMenuText(), out);
|
---|
229 | StringUtil.putCompressedUnicode( getDescriptionText(), out);
|
---|
230 | StringUtil.putCompressedUnicode( getHelpTopicText(), out);
|
---|
231 | StringUtil.putCompressedUnicode( getStatusBarText(), out);
|
---|
232 | }
|
---|
233 | private int getNameRawSize() {
|
---|
234 | if (isBuiltInName()) {
|
---|
235 | return 1;
|
---|
236 | }
|
---|
237 | int nChars = field_12_name_text.length();
|
---|
238 | if(field_11_nameIsMultibyte) {
|
---|
239 | return 2 * nChars;
|
---|
240 | }
|
---|
241 | return nChars;
|
---|
242 | }
|
---|
243 |
|
---|
244 | protected int getDataSize() {
|
---|
245 | return 13 // 3 shorts + 7 bytes
|
---|
246 | + getNameRawSize()
|
---|
247 | + field_14_custom_menu_text.length()
|
---|
248 | + field_15_description_text.length()
|
---|
249 | + field_16_help_topic_text.length()
|
---|
250 | + field_17_status_bar_text.length()
|
---|
251 | + field_13_name_definition.getEncodedSize();
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * called by the constructor, should set class level fields. Should throw
|
---|
257 | * runtime exception for bad/icomplete data.
|
---|
258 | *
|
---|
259 | * @param ris the RecordInputstream to read the record from
|
---|
260 | */
|
---|
261 | public NameRecord(RecordInputStream ris) { // NO_UCD
|
---|
262 | LittleEndianInput in = ris;
|
---|
263 | field_1_option_flag = in.readShort();
|
---|
264 | field_2_keyboard_shortcut = in.readByte();
|
---|
265 | int field_3_length_name_text = in.readUByte();
|
---|
266 | int field_4_length_name_definition = in.readShort();
|
---|
267 | field_5_externSheetIndex_plus1 = in.readShort();
|
---|
268 | field_6_sheetNumber = in.readUShort();
|
---|
269 | int f7_customMenuLen = in.readUByte();
|
---|
270 | int f8_descriptionTextLen = in.readUByte();
|
---|
271 | int f9_helpTopicTextLen = in.readUByte();
|
---|
272 | int f10_statusBarTextLen = in.readUByte();
|
---|
273 |
|
---|
274 | //store the name in byte form if it's a built-in name
|
---|
275 | field_11_nameIsMultibyte = (in.readByte() != 0);
|
---|
276 | if (isBuiltInName()) {
|
---|
277 | field_12_built_in_code = in.readByte();
|
---|
278 | } else {
|
---|
279 | if (field_11_nameIsMultibyte) {
|
---|
280 | field_12_name_text = StringUtil.readUnicodeLE(in, field_3_length_name_text);
|
---|
281 | } else {
|
---|
282 | field_12_name_text = StringUtil.readCompressedUnicode(in, field_3_length_name_text);
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | int nBytesAvailable = in.available() - (f7_customMenuLen
|
---|
287 | + f8_descriptionTextLen + f9_helpTopicTextLen + f10_statusBarTextLen);
|
---|
288 | field_13_name_definition = Formula.read(field_4_length_name_definition, in, nBytesAvailable);
|
---|
289 |
|
---|
290 | //Who says that this can only ever be compressed unicode???
|
---|
291 | field_14_custom_menu_text = StringUtil.readCompressedUnicode(in, f7_customMenuLen);
|
---|
292 | field_15_description_text = StringUtil.readCompressedUnicode(in, f8_descriptionTextLen);
|
---|
293 | field_16_help_topic_text = StringUtil.readCompressedUnicode(in, f9_helpTopicTextLen);
|
---|
294 | field_17_status_bar_text = StringUtil.readCompressedUnicode(in, f10_statusBarTextLen);
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * return the non static version of the id for this record.
|
---|
299 | */
|
---|
300 | public short getSid() {
|
---|
301 | return sid;
|
---|
302 | }
|
---|
303 | /*
|
---|
304 | 20 00
|
---|
305 | 00
|
---|
306 | 01
|
---|
307 | 1A 00 // sz = 0x1A = 26
|
---|
308 | 00 00
|
---|
309 | 01 00
|
---|
310 | 00
|
---|
311 | 00
|
---|
312 | 00
|
---|
313 | 00
|
---|
314 | 00 // unicode flag
|
---|
315 | 07 // name
|
---|
316 |
|
---|
317 | 29 17 00 3B 00 00 00 00 FF FF 00 00 02 00 3B 00 //{ 26
|
---|
318 | 00 07 00 07 00 00 00 FF 00 10 // }
|
---|
319 |
|
---|
320 |
|
---|
321 |
|
---|
322 | 20 00
|
---|
323 | 00
|
---|
324 | 01
|
---|
325 | 0B 00 // sz = 0xB = 11
|
---|
326 | 00 00
|
---|
327 | 01 00
|
---|
328 | 00
|
---|
329 | 00
|
---|
330 | 00
|
---|
331 | 00
|
---|
332 | 00 // unicode flag
|
---|
333 | 07 // name
|
---|
334 |
|
---|
335 | 3B 00 00 07 00 07 00 00 00 FF 00 // { 11 }
|
---|
336 | */
|
---|
337 | /*
|
---|
338 | 18, 00,
|
---|
339 | 1B, 00,
|
---|
340 |
|
---|
341 | 20, 00,
|
---|
342 | 00,
|
---|
343 | 01,
|
---|
344 | 0B, 00,
|
---|
345 | 00,
|
---|
346 | 00,
|
---|
347 | 00,
|
---|
348 | 00,
|
---|
349 | 00,
|
---|
350 | 07,
|
---|
351 | 3B 00 00 07 00 07 00 00 00 FF 00 ]
|
---|
352 | */
|
---|
353 |
|
---|
354 | public String toString() {
|
---|
355 | StringBuffer sb = new StringBuffer();
|
---|
356 |
|
---|
357 | sb.append("[NAME]\n");
|
---|
358 | sb.append(" .option flags = ").append(HexDump.shortToHex(field_1_option_flag)).append("\n");
|
---|
359 | sb.append(" .keyboard shortcut = ").append(HexDump.byteToHex(field_2_keyboard_shortcut)).append("\n");
|
---|
360 | sb.append(" .length of the name = ").append(getNameTextLength()).append("\n");
|
---|
361 | sb.append(" .extSheetIx(1-based, 0=Global)= ").append( field_5_externSheetIndex_plus1 ).append("\n");
|
---|
362 | sb.append(" .sheetTabIx = ").append(field_6_sheetNumber ).append("\n");
|
---|
363 | sb.append(" .Menu text length = ").append(field_14_custom_menu_text.length()).append("\n");
|
---|
364 | sb.append(" .Description text length= ").append(field_15_description_text.length()).append("\n");
|
---|
365 | sb.append(" .Help topic text length = ").append(field_16_help_topic_text.length()).append("\n");
|
---|
366 | sb.append(" .Status bar text length = ").append(field_17_status_bar_text.length()).append("\n");
|
---|
367 | sb.append(" .NameIsMultibyte = ").append(field_11_nameIsMultibyte).append("\n");
|
---|
368 | sb.append(" .Name (Unicode text) = ").append( getNameText() ).append("\n");
|
---|
369 | Ptg[] ptgs = field_13_name_definition.getTokens();
|
---|
370 | sb.append(" .Formula (nTokens=").append(ptgs.length).append("):") .append("\n");
|
---|
371 | for (int i = 0; i < ptgs.length; i++) {
|
---|
372 | Ptg ptg = ptgs[i];
|
---|
373 | sb.append(" " + ptg.toString()).append(ptg.getRVAType()).append("\n");
|
---|
374 | }
|
---|
375 |
|
---|
376 | sb.append(" .Menu text = ").append(field_14_custom_menu_text).append("\n");
|
---|
377 | sb.append(" .Description text= ").append(field_15_description_text).append("\n");
|
---|
378 | sb.append(" .Help topic text = ").append(field_16_help_topic_text).append("\n");
|
---|
379 | sb.append(" .Status bar text = ").append(field_17_status_bar_text).append("\n");
|
---|
380 | sb.append("[/NAME]\n");
|
---|
381 |
|
---|
382 | return sb.toString();
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**Creates a human readable name for built in types
|
---|
386 | * @return Unknown if the built-in name cannot be translated
|
---|
387 | */
|
---|
388 | private static String translateBuiltInName(byte name)
|
---|
389 | {
|
---|
390 | switch (name)
|
---|
391 | {
|
---|
392 | case NameRecord.BUILTIN_AUTO_ACTIVATE : return "Auto_Activate";
|
---|
393 | case NameRecord.BUILTIN_AUTO_CLOSE : return "Auto_Close";
|
---|
394 | case NameRecord.BUILTIN_AUTO_DEACTIVATE : return "Auto_Deactivate";
|
---|
395 | case NameRecord.BUILTIN_AUTO_OPEN : return "Auto_Open";
|
---|
396 | case NameRecord.BUILTIN_CONSOLIDATE_AREA : return "Consolidate_Area";
|
---|
397 | case NameRecord.BUILTIN_CRITERIA : return "Criteria";
|
---|
398 | case NameRecord.BUILTIN_DATABASE : return "Database";
|
---|
399 | case NameRecord.BUILTIN_DATA_FORM : return "Data_Form";
|
---|
400 | case NameRecord.BUILTIN_PRINT_AREA : return "Print_Area";
|
---|
401 | case NameRecord.BUILTIN_PRINT_TITLE : return "Print_Titles";
|
---|
402 | case NameRecord.BUILTIN_RECORDER : return "Recorder";
|
---|
403 | case NameRecord.BUILTIN_SHEET_TITLE : return "Sheet_Title";
|
---|
404 | case NameRecord.BUILTIN_FILTER_DB : return "_FilterDatabase";
|
---|
405 |
|
---|
406 | }
|
---|
407 |
|
---|
408 | return "Unknown";
|
---|
409 | }
|
---|
410 | }
|
---|