KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > WorkbookSettings


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.util.Locale JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import common.Logger;
26
27 import jxl.biff.formula.FunctionNames;
28 import jxl.biff.CountryCode;
29
30 /**
31  * This is a bean which client applications may use to set various advanced
32  * workbook properties. Use of this bean is not mandatory, and its absence
33  * will merely result in workbooks being read/written using the default
34  * settings
35  */

36 public final class WorkbookSettings
37 {
38   /**
39    * The logger
40    */

41   private static Logger logger = Logger.getLogger(WorkbookSettings.class);
42
43   /**
44    * The amount of memory allocated to store the workbook data when
45    * reading a worksheet. For processeses reading many small workbooks inside
46    * a WAS it might be necessary to reduce the default size
47    */

48   private int initialFileSize;
49
50   /**
51    * The amount of memory allocated to the array containing the workbook
52    * data when its current amount is exhausted.
53    */

54   private int arrayGrowSize;
55
56   /**
57    * Flag to indicate whether the drawing feature is enabled or not
58    * Drawings deactivated using -Djxl.nodrawings=true on the JVM command line
59    * Activated by default or by using -Djxl.nodrawings=false on the JVM command
60    * line
61    */

62   private boolean drawingsDisabled;
63
64   /**
65    * Flag to indicate whether the name feature is enabled or not
66    * Names deactivated using -Djxl.nonames=true on the JVM command line
67    * Activated by default or by using -Djxl.nonames=false on the JVM command
68    * line
69    */

70   private boolean namesDisabled;
71
72   /**
73    * Flag to indicate whether formula cell references should be adjusted
74    * following row/column insertion/deletion
75    */

76   private boolean formulaReferenceAdjustDisabled;
77
78   /**
79    * Flag to indicate whether the system hint garbage collection
80    * is enabled or not.
81    * As a rule of thumb, it is desirable to enable garbage collection
82    * when reading large spreadsheets from a batch process or from the
83    * command line, but better to deactivate the feature when reading
84    * large spreadsheets within a WAS, as the calls to System.gc() not
85    * only garbage collect the junk in JExcelApi, but also in the
86    * webservers JVM and can cause significant slowdown
87    * GC deactivated using -Djxl.nogc=true on the JVM command line
88    * Activated by default or by using -Djxl.nogc=false on the JVM command line
89    */

90   private boolean gcDisabled;
91
92   /**
93    * Flag to indicate whether the rationalization of cell formats is
94    * disabled or not.
95    * Rationalization is enabled by default, but may be disabled for
96    * performance reasons. It can be deactivated using -Djxl.norat=true on
97    * the JVM command line
98    */

99   private boolean rationalizationDisabled;
100
101   /**
102    * Flag to indicate whether the copying of additional property sets
103    * are disabled
104    */

105   private boolean propertySetsDisabled;
106
107   /**
108    * Flag to indicate whether or not to ignore blank cells when processing
109    * sheets. Cells which are identified as blank can still have associated
110    * cell formats which the processing program may still need to read
111    */

112   private boolean ignoreBlankCells;
113
114   /**
115    * The locale. Normally this is the same as the system locale, but there
116    * may be cases (eg. where you are uploading many spreadsheets from foreign
117    * sources) where you may want to specify the locale on an individual
118    * worksheet basis
119    * The locale may also be specified on the command line using the lang and
120    * country System properties eg. -Djxl.lang=en -Djxl.country=UK for UK
121    * English
122    */

123   private Locale JavaDoc locale;
124
125   /**
126    * The locale specific function names for this workbook
127    */

128   private FunctionNames functionNames;
129
130   /**
131    * The character encoding used for reading non-unicode strings. This can
132    * be different from the default platform encoding if processing spreadsheets
133    * from abroad. This may also be set using the system property jxl.encoding
134    */

135   private String JavaDoc encoding;
136
137   /**
138    * The character set used by the readable spreadsheeet
139    */

140   private int characterSet;
141
142   /**
143    * The display language used by Excel (ISO 3166 mnemonic)
144    */

145   private String JavaDoc excelDisplayLanguage;
146
147   /**
148    * The regional settings used by Excel (ISO 3166 mnemonic)
149    */

150   private String JavaDoc excelRegionalSettings;
151
152   /**
153    * A hash map of function names keyed on locale
154    */

155   private HashMap JavaDoc localeFunctionNames;
156
157   // **
158
// The default values
159
// **
160
private static final int defaultInitialFileSize = 5 * 1024 * 1024;
161     // 5 megabytes
162
private static final int defaultArrayGrowSize = 1024 * 1024; // 1 megabyte
163

164   /**
165    * Default constructor
166    */

167   public WorkbookSettings()
168   {
169     initialFileSize = defaultInitialFileSize;
170     arrayGrowSize = defaultArrayGrowSize;
171     localeFunctionNames = new HashMap JavaDoc();
172     excelDisplayLanguage = CountryCode.USA.getCode();
173     excelRegionalSettings = CountryCode.UK.getCode();
174
175     // Initialize other properties from the system properties
176
try
177     {
178       boolean suppressWarnings = Boolean.getBoolean("jxl.nowarnings");
179       setSuppressWarnings(suppressWarnings);
180       drawingsDisabled = Boolean.getBoolean("jxl.nodrawings");
181       namesDisabled = Boolean.getBoolean("jxl.nonames");
182       gcDisabled = Boolean.getBoolean("jxl.nogc");
183       rationalizationDisabled = Boolean.getBoolean("jxl.norat");
184       formulaReferenceAdjustDisabled =
185                                 Boolean.getBoolean("jxl.noformulaadjust");
186       propertySetsDisabled = Boolean.getBoolean("jxl.nopropertysets");
187       ignoreBlankCells = Boolean.getBoolean("jxl.ignoreblanks");
188
189       encoding = System.getProperty("file.encoding");
190     }
191     catch (SecurityException JavaDoc e)
192     {
193       logger.warn("Error accessing system properties.", e);
194     }
195
196     // Initialize the locale to the system locale
197
try
198     {
199       if (System.getProperty("jxl.lang") == null ||
200           System.getProperty("jxl.country") == null)
201       {
202         locale = Locale.getDefault();
203       }
204       else
205       {
206         locale = new Locale JavaDoc(System.getProperty("jxl.lang"),
207                             System.getProperty("jxl.country"));
208       }
209
210       if (System.getProperty("jxl.encoding") != null)
211       {
212         encoding = System.getProperty("jxl.encoding");
213       }
214     }
215     catch (SecurityException JavaDoc e)
216     {
217       logger.warn("Error accessing system properties.", e);
218       locale = Locale.getDefault();
219     }
220   }
221
222   /**
223    * Sets the amount of memory by which to increase the amount of
224    * memory allocated to storing the workbook data.
225    * For processeses reading many small workbooks
226    * inside a WAS it might be necessary to reduce the default size
227    * Default value is 1 megabyte
228    *
229    * @param sz the file size in bytes
230    */

231   public void setArrayGrowSize(int sz)
232   {
233     arrayGrowSize = sz;
234   }
235
236   /**
237    * Accessor for the array grow size property
238    *
239    * @return the array grow size
240    */

241   public int getArrayGrowSize()
242   {
243     return arrayGrowSize;
244   }
245
246   /**
247    * Sets the initial amount of memory allocated to store the workbook data
248    * when reading a worksheet. For processeses reading many small workbooks
249    * inside a WAS it might be necessary to reduce the default size
250    * Default value is 5 megabytes
251    *
252    * @param sz the file size in bytes
253    */

254   public void setInitialFileSize(int sz)
255   {
256     initialFileSize = sz;
257   }
258
259   /**
260    * Accessor for the initial file size property
261    *
262    * @return the initial file size
263    */

264   public int getInitialFileSize()
265   {
266     return initialFileSize;
267   }
268
269   /**
270    * Gets the drawings disabled flag
271    *
272    * @return TRUE if drawings are disabled, FALSE otherwise
273    */

274   public boolean getDrawingsDisabled()
275   {
276     return drawingsDisabled;
277   }
278
279   /**
280    * Accessor for the disabling of garbage collection
281    *
282    * @return FALSE if JExcelApi hints for garbage collection, TRUE otherwise
283    */

284   public boolean getGCDisabled()
285   {
286     return gcDisabled;
287   }
288
289   /**
290    * Accessor for the disabling of interpretation of named ranges
291    *
292    * @return FALSE if named cells are interpreted, TRUE otherwise
293    */

294   public boolean getNamesDisabled()
295   {
296     return namesDisabled;
297   }
298
299   /**
300    * Disables the handling of names
301    *
302    * @param b TRUE to disable the names feature, FALSE otherwise
303    */

304   public void setNamesDisabled(boolean b)
305   {
306     namesDisabled = b;
307   }
308
309   /**
310    * Disables the handling of drawings
311    *
312    * @param b TRUE to disable the names feature, FALSE otherwise
313    */

314   public void setDrawingsDisabled(boolean b)
315   {
316     drawingsDisabled = b;
317   }
318
319   /**
320    * Sets whether or not to rationalize the cell formats before
321    * writing out the sheet. The default value is true
322    *
323    * @param r the rationalization flag
324    */

325   public void setRationalization(boolean r)
326   {
327     rationalizationDisabled = !r;
328   }
329
330   /**
331    * Accessor to retrieve the rationalization flag
332    *
333    * @return TRUE if rationalization is off, FALSE if rationalization is on
334    */

335   public boolean getRationalizationDisabled()
336   {
337     return rationalizationDisabled;
338   }
339
340   /**
341    * Sets whether or not to enable any property sets (such as macros)
342    * to be copied along with the workbook
343    * Leaving this feature enabled will result in the JXL process using
344    * more memory
345    *
346    * @param r the property sets flag
347    */

348   public void setPropertySets(boolean r)
349   {
350     propertySetsDisabled = !r;
351   }
352
353   /**
354    * Accessor to retrieve the property sets disabled flag
355    *
356    * @return TRUE if property sets are disabled, FALSE otherwise
357    */

358   public boolean getPropertySetsDisabled()
359   {
360     return propertySetsDisabled;
361   }
362
363   /**
364    * Accessor to set the suppress warnings flag. Due to the change
365    * in logging in version 2.4, this will now set the warning
366    * behaviour across the JVM (depending on the type of logger used)
367    *
368    * @param w the flag
369    */

370   public void setSuppressWarnings(boolean w)
371   {
372     logger.setSuppressWarnings(w);
373   }
374
375   /**
376    * Accessor for the formula adjust disabled
377    *
378    * @return TRUE if formulas are adjusted following row/column inserts/deletes
379    * FALSE otherwise
380    */

381   public boolean getFormulaAdjust()
382   {
383     return !formulaReferenceAdjustDisabled;
384   }
385
386   /**
387    * Setter for the formula adjust disabled property
388    *
389    * @param b TRUE to adjust formulas, FALSE otherwise
390    */

391   public void setFormulaAdjust(boolean b)
392   {
393     formulaReferenceAdjustDisabled = !b;
394   }
395
396   /**
397    * Sets the locale used by JExcelApi to generate the spreadsheet.
398    * Setting this value has no effect on the language or region of
399    * the generated excel file
400    *
401    * @param l the locale
402    */

403   public void setLocale(Locale JavaDoc l)
404   {
405     locale = l;
406   }
407
408   /**
409    * Returns the locale used by JExcelAPI to read the spreadsheet
410    *
411    * @return the locale
412    */

413   public Locale JavaDoc getLocale()
414   {
415     return locale;
416   }
417
418   /**
419    * Accessor for the character encoding
420    *
421    * @return the character encoding for this workbook
422    */

423   public String JavaDoc getEncoding()
424   {
425     return encoding;
426   }
427
428   /**
429    * Sets the encoding for this workbook
430    *
431    * @param enc the encoding
432    */

433   public void setEncoding(String JavaDoc enc)
434   {
435     encoding = enc;
436   }
437
438   /**
439    * Gets the function names. This is used by the formula parsing package
440    * in order to get the locale specific function names for this particular
441    * workbook
442    *
443    * @return the list of function names
444    */

445   public FunctionNames getFunctionNames()
446   {
447     if (functionNames == null)
448     {
449       functionNames = (FunctionNames) localeFunctionNames.get(locale);
450
451       // have not previously accessed function names for this locale,
452
// so create a brand new one and add it to the list
453
if (functionNames == null)
454       {
455         functionNames = new FunctionNames(locale);
456         localeFunctionNames.put(locale, functionNames);
457       }
458     }
459
460     return functionNames;
461   }
462
463   /**
464    * Accessor for the character set. This value is only used for reading
465    * and has no effect when writing out the spreadsheet
466    *
467    * @return the character set used by this spreadsheet
468    */

469   public int getCharacterSet()
470   {
471     return characterSet;
472   }
473
474   /**
475    * Sets the character set. This is only used when the spreadsheet is
476    * read, and has no effect when the spreadsheet is written
477    *
478    * @param cs the character set encoding value
479    */

480   public void setCharacterSet(int cs)
481   {
482     characterSet = cs;
483   }
484
485   /**
486    * Sets the garbage collection disabled
487    *
488    * @param disabled
489    */

490   public void setGCDisabled(boolean disabled)
491   {
492     gcDisabled = disabled;
493   }
494
495   /**
496    * Sets the ignore blanks flag
497    *
498    * @param ignoreBlanks
499    */

500   public void setIgnoreBlanks(boolean ignoreBlanks)
501   {
502     ignoreBlankCells = ignoreBlanks;
503   }
504
505   /**
506    * Accessor for the ignore blanks flag
507    *
508    * @return TRUE if blank cells are being ignored, FALSE otherwise
509    */

510   public boolean getIgnoreBlanks()
511   {
512     return ignoreBlankCells;
513   }
514
515   /**
516    * Returns the two character ISO 3166 mnemonic used by excel for user
517    * language displayto display
518    */

519   public String JavaDoc getExcelDisplayLanguage()
520   {
521     return excelDisplayLanguage;
522   }
523
524   /**
525    * Returns the two character ISO 3166 mnemonic used by excel for
526    * its regional settings
527    */

528   public String JavaDoc getExcelRegionalSettings()
529   {
530     return excelRegionalSettings;
531   }
532
533   /**
534    * Sets the language in which the generated file will display
535    *
536    * @param code the two character ISO 3166 country code
537    */

538   public void setExcelDisplayLanguage(String JavaDoc code)
539   {
540     excelDisplayLanguage = code;
541   }
542
543   /**
544    * Sets the regional settings for the generated excel file
545    *
546    * @param code the two character ISO 3166 country code
547    */

548   public void setExcelRegionalSettings(String JavaDoc code)
549   {
550     excelRegionalSettings = code;
551   }
552 }
553
Popular Tags