KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > HSSFDataFormat


1 /* ====================================================================
2    Copyright 2003-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17
18 /*
19  * HSSFDataFormat.java
20  *
21  * Created on December 18, 2001, 12:42 PM
22  */

23 package org.apache.poi.hssf.usermodel;
24
25 import org.apache.poi.hssf.model.Workbook;
26 import org.apache.poi.hssf.record.FormatRecord;
27
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ListIterator JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 /**
34  * Utility to identify builin formats. Now can handle user defined data formats also. The following is a list of the formats as
35  * returned by this class.<P>
36  *<P>
37  * 0, "General"<br>
38  * 1, "0"<br>
39  * 2, "0.00"<br>
40  * 3, "#,##0"<br>
41  * 4, "#,##0.00"<br>
42  * 5, "($#,##0_);($#,##0)"<br>
43  * 6, "($#,##0_);[Red]($#,##0)"<br>
44  * 7, "($#,##0.00);($#,##0.00)"<br>
45  * 8, "($#,##0.00_);[Red]($#,##0.00)"<br>
46  * 9, "0%"<br>
47  * 0xa, "0.00%"<br>
48  * 0xb, "0.00E+00"<br>
49  * 0xc, "# ?/?"<br>
50  * 0xd, "# ??/??"<br>
51  * 0xe, "m/d/yy"<br>
52  * 0xf, "d-mmm-yy"<br>
53  * 0x10, "d-mmm"<br>
54  * 0x11, "mmm-yy"<br>
55  * 0x12, "h:mm AM/PM"<br>
56  * 0x13, "h:mm:ss AM/PM"<br>
57  * 0x14, "h:mm"<br>
58  * 0x15, "h:mm:ss"<br>
59  * 0x16, "m/d/yy h:mm"<br>
60  *<P>
61  * // 0x17 - 0x24 reserved for international and undocumented
62  * 0x25, "(#,##0_);(#,##0)"<P>
63  * 0x26, "(#,##0_);[Red](#,##0)"<P>
64  * 0x27, "(#,##0.00_);(#,##0.00)"<P>
65  * 0x28, "(#,##0.00_);[Red](#,##0.00)"<P>
66  * 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"<P>
67  * 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"<P>
68  * 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"<P>
69  * 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"<P>
70  * 0x2d, "mm:ss"<P>
71  * 0x2e, "[h]:mm:ss"<P>
72  * 0x2f, "mm:ss.0"<P>
73  * 0x30, "##0.0E+0"<P>
74  * 0x31, "@" - This is text format.<P>
75  * 0x31 "text" - Alias for "@"<P>
76  *
77  * @author Andrew C. Oliver (acoliver at apache dot org)
78  * @author Shawn M. Laubach (slaubach at apache dot org)
79  */

80
81 public class HSSFDataFormat
82 {
83     private static Vector JavaDoc builtinFormats;
84
85     private Vector JavaDoc formats = new Vector JavaDoc();
86     private Workbook workbook;
87     private boolean movedBuiltins = false; // Flag to see if need to
88
// check the built in list
89
// or if the regular list
90
// has all entries.
91

92     /**
93      * Construncts a new data formatter. It takes a workbook to have
94      * access to the workbooks format records.
95      * @param workbook the workbook the formats are tied to.
96      */

97     public HSSFDataFormat( Workbook workbook )
98     {
99         this.workbook = workbook;
100         if ( builtinFormats == null ) populateBuiltinFormats();
101         Iterator JavaDoc i = workbook.getFormats().iterator();
102         while ( i.hasNext() )
103         {
104             FormatRecord r = (FormatRecord) i.next();
105             if ( formats.size() < r.getIndexCode() + 1 )
106             {
107                 formats.setSize( r.getIndexCode() + 1 );
108             }
109             formats.set( r.getIndexCode(), r.getFormatString() );
110         }
111
112     }
113
114     private static synchronized void populateBuiltinFormats()
115     {
116         builtinFormats = new Vector JavaDoc();
117         builtinFormats.add( 0, "General" );
118         builtinFormats.add( 1, "0" );
119         builtinFormats.add( 2, "0.00" );
120         builtinFormats.add( 3, "#,##0" );
121         builtinFormats.add( 4, "#,##0.00" );
122         builtinFormats.add( 5, "($#,##0_);($#,##0)" );
123         builtinFormats.add( 6, "($#,##0_);[Red]($#,##0)" );
124         builtinFormats.add( 7, "($#,##0.00);($#,##0.00)" );
125         builtinFormats.add( 8, "($#,##0.00_);[Red]($#,##0.00)" );
126         builtinFormats.add( 9, "0%" );
127         builtinFormats.add( 0xa, "0.00%" );
128         builtinFormats.add( 0xb, "0.00E+00" );
129         builtinFormats.add( 0xc, "# ?/?" );
130         builtinFormats.add( 0xd, "# ??/??" );
131         builtinFormats.add( 0xe, "m/d/yy" );
132         builtinFormats.add( 0xf, "d-mmm-yy" );
133         builtinFormats.add( 0x10, "d-mmm" );
134         builtinFormats.add( 0x11, "mmm-yy" );
135         builtinFormats.add( 0x12, "h:mm AM/PM" );
136         builtinFormats.add( 0x13, "h:mm:ss AM/PM" );
137         builtinFormats.add( 0x14, "h:mm" );
138         builtinFormats.add( 0x15, "h:mm:ss" );
139         builtinFormats.add( 0x16, "m/d/yy h:mm" );
140
141         // 0x17 - 0x24 reserved for international and undocumented
142
builtinFormats.add( 0x17, "0x17" );
143         builtinFormats.add( 0x18, "0x18" );
144         builtinFormats.add( 0x19, "0x19" );
145         builtinFormats.add( 0x1a, "0x1a" );
146         builtinFormats.add( 0x1b, "0x1b" );
147         builtinFormats.add( 0x1c, "0x1c" );
148         builtinFormats.add( 0x1d, "0x1d" );
149         builtinFormats.add( 0x1e, "0x1e" );
150         builtinFormats.add( 0x1f, "0x1f" );
151         builtinFormats.add( 0x20, "0x20" );
152         builtinFormats.add( 0x21, "0x21" );
153         builtinFormats.add( 0x22, "0x22" );
154         builtinFormats.add( 0x23, "0x23" );
155         builtinFormats.add( 0x24, "0x24" );
156
157         // 0x17 - 0x24 reserved for international and undocumented
158
builtinFormats.add( 0x25, "(#,##0_);(#,##0)" );
159         builtinFormats.add( 0x26, "(#,##0_);[Red](#,##0)" );
160         builtinFormats.add( 0x27, "(#,##0.00_);(#,##0.00)" );
161         builtinFormats.add( 0x28, "(#,##0.00_);[Red](#,##0.00)" );
162         builtinFormats.add( 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)" );
163         builtinFormats.add( 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)" );
164         builtinFormats.add( 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)" );
165         builtinFormats.add( 0x2c,
166                 "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)" );
167         builtinFormats.add( 0x2d, "mm:ss" );
168         builtinFormats.add( 0x2e, "[h]:mm:ss" );
169         builtinFormats.add( 0x2f, "mm:ss.0" );
170         builtinFormats.add( 0x30, "##0.0E+0" );
171         builtinFormats.add( 0x31, "@" );
172     }
173
174     public static List JavaDoc getBuiltinFormats()
175     {
176         if ( builtinFormats == null )
177         {
178             populateBuiltinFormats();
179         }
180         return builtinFormats;
181     }
182
183     /**
184      * get the format index that matches the given format string<p>
185      * Automatically converts "text" to excel's format string to represent text.
186      * @param format string matching a built in format
187      * @return index of format or -1 if undefined.
188      */

189
190     public static short getBuiltinFormat( String JavaDoc format )
191     {
192     if (format.toUpperCase().equals("TEXT"))
193         format = "@";
194
195         if ( builtinFormats == null )
196         {
197             populateBuiltinFormats();
198         }
199         short retval = -1;
200
201         for (short k = 0; k <= 0x31; k++)
202         {
203             String JavaDoc nformat = (String JavaDoc) builtinFormats.get( k );
204
205             if ( ( nformat != null ) && nformat.equals( format ) )
206             {
207                 retval = k;
208                 break;
209             }
210         }
211         return retval;
212     }
213
214     /**
215      * get the format index that matches the given format string.
216      * Creates a new format if one is not found. Aliases text to the proper format.
217      * @param format string matching a built in format
218      * @return index of format.
219      */

220
221     public short getFormat( String JavaDoc format )
222     {
223         ListIterator JavaDoc i;
224         int ind;
225
226     if (format.toUpperCase().equals("TEXT"))
227         format = "@";
228
229         if ( !movedBuiltins )
230         {
231             i = builtinFormats.listIterator();
232             while ( i.hasNext() )
233             {
234                 ind = i.nextIndex();
235                 formats.add( ind, i.next() );
236             }
237             movedBuiltins = true;
238         }
239         i = formats.listIterator();
240         while ( i.hasNext() )
241         {
242             ind = i.nextIndex();
243             if ( format.equals( i.next() ) )
244                 return (short) ind;
245         }
246
247         ind = workbook.getFormat( format, true );
248         if ( formats.size() <= ind )
249             formats.setSize( ind + 1 );
250         formats.add( ind, format );
251
252         return (short) ind;
253     }
254
255     /**
256      * get the format string that matches the given format index
257      * @param index of a format
258      * @return string represented at index of format or null if there is not a format at that index
259      */

260
261     public String JavaDoc getFormat( short index )
262     {
263         if ( movedBuiltins )
264             return (String JavaDoc) formats.get( index );
265         else
266             return (String JavaDoc) ( builtinFormats.size() > index
267                     && builtinFormats.get( index ) != null
268                     ? builtinFormats.get( index ) : formats.get( index ) );
269     }
270
271     /**
272      * get the format string that matches the given format index
273      * @param index of a built in format
274      * @return string represented at index of format or null if there is not a builtin format at that index
275      */

276
277     public static String JavaDoc getBuiltinFormat( short index )
278     {
279         if ( builtinFormats == null )
280         {
281             populateBuiltinFormats();
282         }
283         return (String JavaDoc) builtinFormats.get( index );
284     }
285
286     /**
287      * get the number of builtin and reserved builtinFormats
288      * @return number of builtin and reserved builtinFormats
289      */

290
291     public static int getNumberOfBuiltinBuiltinFormats()
292     {
293         if ( builtinFormats == null )
294         {
295             populateBuiltinFormats();
296         }
297         return builtinFormats.size();
298     }
299 }
300
Popular Tags