KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > write > biff > NumberFormatRecord


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.write.biff;
21
22 import common.Logger;
23 import jxl.biff.FormatRecord;
24
25 /**
26  * A class which contains a number format
27  */

28 public class NumberFormatRecord extends FormatRecord
29 {
30   private static Logger logger = Logger.getLogger(NumberFormatRecord.class);
31
32   /**
33    * Constructor. Replaces some of the characters in the java number
34    * format string with the appropriate excel format characters
35    *
36    * @param fmt the number format
37    */

38   protected NumberFormatRecord(String JavaDoc fmt)
39   {
40     super();
41
42     // Do the replacements in the format string
43
String JavaDoc fs = fmt;
44
45     fs = replace(fs, "E0", "E+0");
46
47     fs = trimInvalidChars(fs);
48
49     setFormatString(fs);
50   }
51
52   /**
53    * Remove all but the first characters preceding the # or the 0.
54    * Remove all characters after the # or the 0, unless it is a )
55    *
56    * @param fs the candidate number format
57    * @return the string with spurious characters removed
58    */

59   private String JavaDoc trimInvalidChars(String JavaDoc fs)
60   {
61     int firstHash = fs.indexOf('#');
62     int firstZero = fs.indexOf('0');
63     int firstValidChar = 0;
64
65     if (firstHash == -1 && firstZero == -1)
66     {
67       // The string is complete nonsense. Return a default string
68
return "#.###";
69     }
70
71     if (firstHash != 0 && firstZero != 0 &&
72         firstHash != 1 && firstZero != 1)
73     {
74       // The string is dodgy. Find the first valid char
75
firstHash = firstHash == -1?firstHash = Integer.MAX_VALUE:firstHash;
76       firstZero = firstZero == -1?firstZero = Integer.MAX_VALUE:firstZero;
77       firstValidChar = Math.min(firstHash, firstZero);
78
79       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
80       tmp.append(fs.charAt(0));
81       tmp.append(fs.substring(firstValidChar));
82       fs = tmp.toString();
83     }
84
85     // Now strip of everything at the end that isn't a # or 0
86
int lastHash = fs.lastIndexOf('#');
87     int lastZero = fs.lastIndexOf('0');
88
89     if (lastHash == fs.length() ||
90         lastZero == fs.length())
91     {
92       return fs;
93     }
94
95     // Find the last valid character
96
int lastValidChar = Math.max(lastHash, lastZero);
97
98     // Check for the existence of a ) or %
99
while ((fs.length() > lastValidChar + 1) &&
100            (fs.charAt(lastValidChar+1) == ')' ||
101             (fs.charAt(lastValidChar+1) == '%')))
102     {
103       lastValidChar++;
104     }
105
106     return fs.substring(0, lastValidChar+1);
107   }
108 }
109
110
111
112
Popular Tags