1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/
19
20 package jxl;
21
22 import java.io.File;
23 import java.util.HashMap;
24 import java.util.Locale;
25
26 import common.Logger;
27
28 import jxl.biff.CountryCode;
29 import jxl.biff.formula.FunctionNames;
30
31 /**
32 * This is a bean which client applications may use to set various advanced
33 * workbook properties. Use of this bean is not mandatory, and its absence
34 * will merely result in workbooks being read/written using the default
35 * settings
36 */
37 public final class WorkbookSettings
38 {
39 /**
40 * The logger
41 */
42 private static Logger logger = Logger.getLogger(WorkbookSettings.class);
43
44 /**
45 * The amount of memory allocated to store the workbook data when
46 * reading a worksheet. For processeses reading many small workbooks inside
47 * a WAS it might be necessary to reduce the default size
48 */
49 private int initialFileSize;
50
51 /**
52 * The amount of memory allocated to the array containing the workbook
53 * data when its current amount is exhausted.
54 */
55 private int arrayGrowSize;
56
57 /**
58 * Flag to indicate whether the drawing feature is enabled or not
59 * Drawings deactivated using -Djxl.nodrawings=true on the JVM command line
60 * Activated by default or by using -Djxl.nodrawings=false on the JVM command
61 * line
62 */
63 private boolean drawingsDisabled;
64
65 /**
66 * Flag to indicate whether the name feature is enabled or not
67 * Names deactivated using -Djxl.nonames=true on the JVM command line
68 * Activated by default or by using -Djxl.nonames=false on the JVM command
69 * line
70 */
71 private boolean namesDisabled;
72
73 /**
74 * Flag to indicate whether formula cell references should be adjusted
75 * following row/column insertion/deletion
76 */
77 private boolean formulaReferenceAdjustDisabled;
78
79 /**
80 * Flag to indicate whether the system hint garbage collection
81 * is enabled or not.
82 * As a rule of thumb, it is desirable to enable garbage collection
83 * when reading large spreadsheets from a batch process or from the
84 * command line, but better to deactivate the feature when reading
85 * large spreadsheets within a WAS, as the calls to System.gc() not
86 * only garbage collect the junk in JExcelApi, but also in the
87 * webservers JVM and can cause significant slowdown
88 * GC deactivated using -Djxl.nogc=true on the JVM command line
89 * Activated by default or by using -Djxl.nogc=false on the JVM command line
90 */
91 private boolean gcDisabled;
92
93 /**
94 * Flag to indicate whether the rationalization of cell formats is
95 * disabled or not.
96 * Rationalization is enabled by default, but may be disabled for
97 * performance reasons. It can be deactivated using -Djxl.norat=true on
98 * the JVM command line
99 */
100 private boolean rationalizationDisabled;
101
102 /**
103 * Flag to indicate whether or not the merged cell checking has been
104 * disabled
105 */
106 private boolean mergedCellCheckingDisabled;
107
108 /**
109 * Flag to indicate whether the copying of additional property sets
110 * are disabled
111 */
112 private boolean propertySetsDisabled;
113
114 /**
115 * Flag to indicate that cell validation criteria are ignored
116 */
117 private boolean cellValidationDisabled;
118
119 /**
120 * Flag to indicate whether or not to ignore blank cells when processing
121 * sheets. Cells which are identified as blank can still have associated
122 * cell formats which the processing program may still need to read
123 */
124 private boolean ignoreBlankCells;
125
126 /**
127 * Flag to indicate whether auto filtering should be read/copied
128 */
129 private boolean autoFilterDisabled;
130
131 /**
132 * Flag to indicate whether a temporary file should be used when
133 * writing out the workbook
134 */
135 private boolean useTemporaryFileDuringWrite;
136
137 /**
138 * The directory for used for the temporary file during write. If this
139 * is NULL, the default system directory is used
140 */
141 private File temporaryFileDuringWriteDirectory;
142
143 /**
144 * The locale. Normally this is the same as the system locale, but there
145 * may be cases (eg. where you are uploading many spreadsheets from foreign
146 * sources) where you may want to specify the locale on an individual
147 * worksheet basis
148 * The locale may also be specified on the command line using the lang and
149 * country System properties eg. -Djxl.lang=en -Djxl.country=UK for UK
150 * English
151 */
152 private Locale locale;
153
154 /**
155 * The locale specific function names for this workbook
156 */
157 private FunctionNames functionNames;
158
159 /**
160 * The character encoding used for reading non-unicode strings. This can
161 * be different from the default platform encoding if processing spreadsheets
162 * from abroad. This may also be set using the system property jxl.encoding
163 */
164 private String encoding;
165
166 /**
167 * The character set used by the readable spreadsheeet
168 */
169 private int characterSet;
170
171 /**
172 * The display language used by Excel (ISO 3166 mnemonic)
173 */
174 private String excelDisplayLanguage;
175
176 /**
177 * The regional settings used by Excel (ISO 3166 mnemonic)
178 */
179 private String excelRegionalSettings;
180
181 /**
182 * A hash map of function names keyed on locale
183 */
184 private HashMap localeFunctionNames;
185
186 // **
187 // The default values
188 // **
189 private static final int DEFAULT_INITIAL_FILE_SIZE = 5 * 1024 * 1024;
190 // 5 megabytes
191 private static final int DEFAULT_ARRAY_GROW_SIZE = 1024 * 1024; // 1 megabyte
192
193 /**
194 * Default constructor
195 */
196 public WorkbookSettings()
197 {
198 initialFileSize = DEFAULT_INITIAL_FILE_SIZE;
199 arrayGrowSize = DEFAULT_ARRAY_GROW_SIZE;
200 localeFunctionNames = new HashMap();
201 excelDisplayLanguage = CountryCode.USA.getCode();
202 excelRegionalSettings = CountryCode.UK.getCode();
203
204 // Initialize other properties from the system properties
205 try
206 {
207 boolean suppressWarnings = Boolean.getBoolean("jxl.nowarnings");
208 setSuppressWarnings(suppressWarnings);
209 drawingsDisabled = Boolean.getBoolean("jxl.nodrawings");
210 namesDisabled = Boolean.getBoolean("jxl.nonames");
211 gcDisabled = Boolean.getBoolean("jxl.nogc");
212 rationalizationDisabled = Boolean.getBoolean("jxl.norat");
213 mergedCellCheckingDisabled =
214 Boolean.getBoolean("jxl.nomergedcellchecks");
215 formulaReferenceAdjustDisabled =
216 Boolean.getBoolean("jxl.noformulaadjust");
217 propertySetsDisabled = Boolean.getBoolean("jxl.nopropertysets");
218 ignoreBlankCells = Boolean.getBoolean("jxl.ignoreblanks");
219 cellValidationDisabled = Boolean.getBoolean("jxl.nocellvalidation");
220 autoFilterDisabled = !Boolean.getBoolean("jxl.autofilter");
221 // autofilter currently disabled by default
222 useTemporaryFileDuringWrite =
223 Boolean.getBoolean("jxl.usetemporaryfileduringwrite");
224 String tempdir =
225 System.getProperty("jxl.temporaryfileduringwritedirectory");
226
227 if (tempdir != null)
228 {
229 temporaryFileDuringWriteDirectory = new File(tempdir);
230 }
231
232 encoding = System.getProperty("file.encoding");
233 }
234 catch (SecurityException e)
235 {
236 logger.warn("Error accessing system properties.", e);
237 }
238
239 // Initialize the locale to the system locale
240 try
241 {
242 if (System.getProperty("jxl.lang") == null ||
243 System.getProperty("jxl.country") == null)
244 {
245 locale = Locale.getDefault();
246 }
247 else
248 {
249 locale = new Locale(System.getProperty("jxl.lang"),
250 System.getProperty("jxl.country"));
251 }
252
253 if (System.getProperty("jxl.encoding") != null)
254 {
255 encoding = System.getProperty("jxl.encoding");
256 }
257 }
258 catch (SecurityException e)
259 {
260 logger.warn("Error accessing system properties.", e);
261 locale = Locale.getDefault();
262 }
263 }
264
265 /**
266 * Sets the amount of memory by which to increase the amount of
267 * memory allocated to storing the workbook data.
268 * For processeses reading many small workbooks
269 * inside a WAS it might be necessary to reduce the default size
270 * Default value is 1 megabyte
271 *
272 * @param sz the file size in bytes
273 */
274 public void setArrayGrowSize(int sz)
275 {
276 arrayGrowSize = sz;
277 }
278
279 /**
280 * Accessor for the array grow size property
281 *
282 * @return the array grow size
283 */
284 public int getArrayGrowSize()
285 {
286 return arrayGrowSize;
287 }
288
289 /**
290 * Sets the initial amount of memory allocated to store the workbook data
291 * when reading a worksheet. For processeses reading many small workbooks
292 * inside a WAS it might be necessary to reduce the default size
293 * Default value is 5 megabytes
294 *
295 * @param sz the file size in bytes
296 */
297 public void setInitialFileSize(int sz)
298 {
299 initialFileSize = sz;
300 }
301
302 /**
303 * Accessor for the initial file size property
304 *
305 * @return the initial file size
306 */
307 public int getInitialFileSize()
308 {
309 return initialFileSize;
310 }
311
312 /**
313 * Gets the drawings disabled flag
314 *
315 * @return TRUE if drawings are disabled, FALSE otherwise
316 */
317 public boolean getDrawingsDisabled()
318 {
319 return drawingsDisabled;
320 }
321
322 /**
323 * Accessor for the disabling of garbage collection
324 *
325 * @return FALSE if JExcelApi hints for garbage collection, TRUE otherwise
326 */
327 public boolean getGCDisabled()
328 {
329 return gcDisabled;
330 }
331
332 /**
333 * Accessor for the disabling of interpretation of named ranges
334 *
335 * @return FALSE if named cells are interpreted, TRUE otherwise
336 */
337 public boolean getNamesDisabled()
338 {
339 return namesDisabled;
340 }
341
342 /**
343 * Disables the handling of names
344 *
345 * @param b TRUE to disable the names feature, FALSE otherwise
346 */
347 public void setNamesDisabled(boolean b)
348 {
349 namesDisabled = b;
350 }
351
352 /**
353 * Disables the handling of drawings
354 *
355 * @param b TRUE to disable the names feature, FALSE otherwise
356 */
357 public void setDrawingsDisabled(boolean b)
358 {
359 drawingsDisabled = b;
360 }
361
362 /**
363 * Sets whether or not to rationalize the cell formats before
364 * writing out the sheet. The default value is true
365 *
366 * @param r the rationalization flag
367 */
368 public void setRationalization(boolean r)
369 {
370 rationalizationDisabled = !r;
371 }
372
373 /**
374 * Accessor to retrieve the rationalization flag
375 *
376 * @return TRUE if rationalization is off, FALSE if rationalization is on
377 */
378 public boolean getRationalizationDisabled()
379 {
380 return rationalizationDisabled;
381 }
382
383 /**
384 * Accessor to retrieve the merged cell checking flag
385 *
386 * @return TRUE if merged cell checking is off, FALSE if it is on
387 */
388 public boolean getMergedCellCheckingDisabled()
389 {
390 return mergedCellCheckingDisabled;
391 }
392
393 /**
394 * Accessor to set the merged cell checking
395 *
396 * @param b - TRUE to enable merged cell checking, FALSE otherwise
397 */
398 public void setMergedCellChecking(boolean b)
399 {
400 mergedCellCheckingDisabled = !b;
401 }
402
403 /**
404 * Sets whether or not to enable any property sets (such as macros)
405 * to be copied along with the workbook
406 * Leaving this feature enabled will result in the JXL process using
407 * more memory
408 *
409 * @param r the property sets flag
410 */
411 public void setPropertySets(boolean r)
412 {
413 propertySetsDisabled = !r;
414 }
415
416 /**
417 * Accessor to retrieve the property sets disabled flag
418 *
419 * @return TRUE if property sets are disabled, FALSE otherwise
420 */
421 public boolean getPropertySetsDisabled()
422 {
423 return propertySetsDisabled;
424 }
425
426 /**
427 * Accessor to set the suppress warnings flag. Due to the change
428 * in logging in version 2.4, this will now set the warning
429 * behaviour across the JVM (depending on the type of logger used)
430 *
431 * @param w the flag
432 */
433 public void setSuppressWarnings(boolean w)
434 {
435 logger.setSuppressWarnings(w);
436 }
437
438 /**
439 * Accessor for the formula adjust disabled
440 *
441 * @return TRUE if formulas are adjusted following row/column inserts/deletes
442 * FALSE otherwise
443 */
444 public boolean getFormulaAdjust()
445 {
446 return !formulaReferenceAdjustDisabled;
447 }
448
449 /**
450 * Setter for the formula adjust disabled property
451 *
452 * @param b TRUE to adjust formulas, FALSE otherwise
453 */
454 public void setFormulaAdjust(boolean b)
455 {
456 formulaReferenceAdjustDisabled = !b;
457 }
458
459 /**
460 * Sets the locale used by JExcelApi to generate the spreadsheet.
461 * Setting this value has no effect on the language or region of
462 * the generated excel file
463 *
464 * @param l the locale
465 */
466 public void setLocale(Locale l)
467 {
468 locale = l;
469 }
470
471 /**
472 * Returns the locale used by JExcelAPI to read the spreadsheet
473 *
474 * @return the locale
475 */
476 public Locale getLocale()
477 {
478 return locale;
479 }
480
481 /**
482 * Accessor for the character encoding
483 *
484 * @return the character encoding for this workbook
485 */
486 public String getEncoding()
487 {
488 return encoding;
489 }
490
491 /**
492 * Sets the encoding for this workbook
493 *
494 * @param enc the encoding
495 */
496 public void setEncoding(String enc)
497 {
498 encoding = enc;
499 }
500
501 /**
502 * Gets the function names. This is used by the formula parsing package
503 * in order to get the locale specific function names for this particular
504 * workbook
505 *
506 * @return the list of function names
507 */
508 public FunctionNames getFunctionNames()
509 {
510 if (functionNames == null)
511 {
512 functionNames = (FunctionNames) localeFunctionNames.get(locale);
513
514 // have not previously accessed function names for this locale,
515 // so create a brand new one and add it to the list
516 if (functionNames == null)
517 {
518 functionNames = new FunctionNames(locale);
519 localeFunctionNames.put(locale, functionNames);
520 }
521 }
522
523 return functionNames;
524 }
525
526 /**
527 * Accessor for the character set. This value is only used for reading
528 * and has no effect when writing out the spreadsheet
529 *
530 * @return the character set used by this spreadsheet
531 */
532 public int getCharacterSet()
533 {
534 return characterSet;
535 }
536
537 /**
538 * Sets the character set. This is only used when the spreadsheet is
539 * read, and has no effect when the spreadsheet is written
540 *
541 * @param cs the character set encoding value
542 */
543 public void setCharacterSet(int cs)
544 {
545 characterSet = cs;
546 }
547
548 /**
549 * Sets the garbage collection disabled
550 *
551 * @param disabled TRUE to disable garbage collection, FALSE to enable it
552 */
553 public void setGCDisabled(boolean disabled)
554 {
555 gcDisabled = disabled;
556 }
557
558 /**
559 * Sets the ignore blanks flag
560 *
561 * @param ignoreBlanks TRUE to ignore blanks, FALSE to take them into account
562 */
563 public void setIgnoreBlanks(boolean ignoreBlanks)
564 {
565 ignoreBlankCells = ignoreBlanks;
566 }
567
568 /**
569 * Accessor for the ignore blanks flag
570 *
571 * @return TRUE if blank cells are being ignored, FALSE otherwise
572 */
573 public boolean getIgnoreBlanks()
574 {
575 return ignoreBlankCells;
576 }
577
578 /**
579 * Sets the ignore cell validation flag
580 *
581 * @param cv TRUE to disable cell validation, FALSE to enable it
582 */
583 public void setCellValidationDisabled(boolean cv)
584 {
585 cellValidationDisabled = cv;
586 }
587
588 /**
589 * Accessor for the ignore cell validation
590 *
591 * @return TRUE if cell validation is disabled
592 */
593 public boolean getCellValidationDisabled()
594 {
595 return cellValidationDisabled;
596 }
597
598 /**
599 * Returns the two character ISO 3166 mnemonic used by excel for user
600 * language displayto display
601 * @return the display language
602 */
603 public String getExcelDisplayLanguage()
604 {
605 return excelDisplayLanguage;
606 }
607
608 /**
609 * Returns the two character ISO 3166 mnemonic used by excel for
610 * its regional settings
611 * @return the regional settings
612 */
613 public String getExcelRegionalSettings()
614 {
615 return excelRegionalSettings;
616 }
617
618 /**
619 * Sets the language in which the generated file will display
620 *
621 * @param code the two character ISO 3166 country code
622 */
623 public void setExcelDisplayLanguage(String code)
624 {
625 excelDisplayLanguage = code;
626 }
627
628 /**
629 * Sets the regional settings for the generated excel file
630 *
631 * @param code the two character ISO 3166 country code
632 */
633 public void setExcelRegionalSettings(String code)
634 {
635 excelRegionalSettings = code;
636 }
637
638 /**
639 * Accessor for the autofilter disabled feature
640 *
641 * @return TRUE if autofilter is disabled, FALSE otherwise
642 */
643 public boolean getAutoFilterDisabled()
644 {
645 return autoFilterDisabled;
646 }
647
648 /**
649 * Sets the autofilter disabled
650 *
651 * @param disabled
652 */
653 public void setAutoFilterDisabled(boolean disabled)
654 {
655 autoFilterDisabled = disabled;
656 }
657
658 /**
659 * Accessor for the temporary file during write. If this is set, then
660 * when the workbook is written a temporary file will be used to store
661 * the interim binary data, otherwise it will take place in memory. Setting
662 * this flag involves an assessment of the trade-offs between memory usage
663 * and performance
664 *
665 * @return TRUE if a temporary is file is used during writing,
666 * FALSE otherwise
667 */
668 public boolean getUseTemporaryFileDuringWrite()
669 {
670 return useTemporaryFileDuringWrite;
671 }
672
673 /**
674 * Sets whether a temporary file is used during the generation of
675 * the workbook. If not set, the workbook will take place entirely in
676 * memory. Setting
677 * this flag involves an assessment of the trade-offs between memory usage
678 * and performance
679 *
680 * @return TRUE if a temporary is file is used during writing,
681 * FALSE otherwise
682 */
683 public void setUseTemporaryFileDuringWrite(boolean temp)
684 {
685 useTemporaryFileDuringWrite = temp;
686 }
687
688 /**
689 * Used in conjunction with the UseTemporaryFileDuringWrite setting to
690 * set the target directory for the temporary files. If this is not set,
691 * the system default temporary directory is used.
692 * This has no effect unless the useTemporaryFileDuringWrite setting
693 * is TRUE
694 *
695 * @param dir the directory to which temporary files should be written
696 */
697 public void setTemporaryFileDuringWriteDirectory(File dir)
698 {
699 temporaryFileDuringWriteDirectory = dir;
700 }
701
702 /**
703 * Used in conjunction with the UseTemporaryFileDuringWrite setting to
704 * set the target directory for the temporary files. This value can
705 * be NULL, in which case the normal system default temporary directory
706 * is used instead
707 *
708 * @return the temporary directory used during write, or NULL if it is
709 * not set
710 */
711 public File getTemporaryFileDuringWriteDirectory()
712 {
713 return temporaryFileDuringWriteDirectory;
714 }
715 }