Lines 1-4
Link Here
|
1 |
|
|
|
2 |
/* ==================================================================== |
1 |
/* ==================================================================== |
3 |
* The Apache Software License, Version 1.1 |
2 |
* The Apache Software License, Version 1.1 |
4 |
* |
3 |
* |
Lines 60-70
Link Here
|
60 |
*/ |
59 |
*/ |
61 |
package org.apache.poi.hssf.usermodel; |
60 |
package org.apache.poi.hssf.usermodel; |
62 |
|
61 |
|
63 |
import java.util.ArrayList; |
62 |
import java.util.Vector; |
64 |
import java.util.List; |
63 |
import java.util.List; |
|
|
64 |
import java.util.ListIterator; |
65 |
|
66 |
import org.apache.poi.hssf.model.Workbook; |
67 |
import org.apache.poi.hssf.record.Record; |
68 |
import org.apache.poi.hssf.record.FormatRecord; |
65 |
|
69 |
|
66 |
/** |
70 |
/** |
67 |
* Utility to identify builin formats. The following is a list of the formats as |
71 |
* Utility to identify builin formats. Now can handle user defined data formats also. The following is a list of the formats as |
68 |
* returned by this class.<P> |
72 |
* returned by this class.<P> |
69 |
*<P> |
73 |
*<P> |
70 |
* 0, "General"<P> |
74 |
* 0, "General"<P> |
Lines 112-186
Link Here
|
112 |
|
116 |
|
113 |
public class HSSFDataFormat |
117 |
public class HSSFDataFormat |
114 |
{ |
118 |
{ |
115 |
private static ArrayList formats; |
119 |
private static Vector builtinFormats; |
|
|
120 |
|
121 |
private Vector formats = new Vector(); |
122 |
private Workbook workbook; |
123 |
private boolean movedBuiltins = false; // Flag to see if need to |
124 |
// check the built in list |
125 |
// or if the regular list |
126 |
// has all entries. |
127 |
|
128 |
/** |
129 |
* Construncts a new data formatter. It takes a workbook to have |
130 |
* access to the workbooks format records. |
131 |
* @param workbook the workbook the formats are tied to. |
132 |
*/ |
133 |
public HSSFDataFormat(Workbook workbook) { |
134 |
this.workbook = workbook; |
135 |
if (builtinFormats == null) populateBuiltinFormats(); |
136 |
} |
116 |
|
137 |
|
117 |
private static synchronized void populateFormats() |
138 |
private static synchronized void populateBuiltinFormats() |
118 |
{ |
139 |
{ |
119 |
formats = new ArrayList(); |
140 |
builtinFormats = new Vector(); |
120 |
formats.add(0, "General"); |
141 |
builtinFormats.add(0, "General"); |
121 |
formats.add(1, "0"); |
142 |
builtinFormats.add(1, "0"); |
122 |
formats.add(2, "0.00"); |
143 |
builtinFormats.add(2, "0.00"); |
123 |
formats.add(3, "#,##0"); |
144 |
builtinFormats.add(3, "#,##0"); |
124 |
formats.add(4, "#,##0.00"); |
145 |
builtinFormats.add(4, "#,##0.00"); |
125 |
formats.add(5, "($#,##0_);($#,##0)"); |
146 |
builtinFormats.add(5, "($#,##0_);($#,##0)"); |
126 |
formats.add(6, "($#,##0_);[Red]($#,##0)"); |
147 |
builtinFormats.add(6, "($#,##0_);[Red]($#,##0)"); |
127 |
formats.add(7, "($#,##0.00);($#,##0.00)"); |
148 |
builtinFormats.add(7, "($#,##0.00);($#,##0.00)"); |
128 |
formats.add(8, "($#,##0.00_);[Red]($#,##0.00)"); |
149 |
builtinFormats.add(8, "($#,##0.00_);[Red]($#,##0.00)"); |
129 |
formats.add(9, "0%"); |
150 |
builtinFormats.add(9, "0%"); |
130 |
formats.add(0xa, "0.00%"); |
151 |
builtinFormats.add(0xa, "0.00%"); |
131 |
formats.add(0xb, "0.00E+00"); |
152 |
builtinFormats.add(0xb, "0.00E+00"); |
132 |
formats.add(0xc, "# ?/?"); |
153 |
builtinFormats.add(0xc, "# ?/?"); |
133 |
formats.add(0xd, "# ??/??"); |
154 |
builtinFormats.add(0xd, "# ??/??"); |
134 |
formats.add(0xe, "m/d/yy"); |
155 |
builtinFormats.add(0xe, "m/d/yy"); |
135 |
formats.add(0xf, "d-mmm-yy"); |
156 |
builtinFormats.add(0xf, "d-mmm-yy"); |
136 |
formats.add(0x10, "d-mmm"); |
157 |
builtinFormats.add(0x10, "d-mmm"); |
137 |
formats.add(0x11, "mmm-yy"); |
158 |
builtinFormats.add(0x11, "mmm-yy"); |
138 |
formats.add(0x12, "h:mm AM/PM"); |
159 |
builtinFormats.add(0x12, "h:mm AM/PM"); |
139 |
formats.add(0x13, "h:mm:ss AM/PM"); |
160 |
builtinFormats.add(0x13, "h:mm:ss AM/PM"); |
140 |
formats.add(0x14, "h:mm"); |
161 |
builtinFormats.add(0x14, "h:mm"); |
141 |
formats.add(0x15, "h:mm:ss"); |
162 |
builtinFormats.add(0x15, "h:mm:ss"); |
142 |
formats.add(0x16, "m/d/yy h:mm"); |
163 |
builtinFormats.add(0x16, "m/d/yy h:mm"); |
143 |
|
164 |
|
144 |
// 0x17 - 0x24 reserved for international and undocumented |
165 |
// 0x17 - 0x24 reserved for international and undocumented |
145 |
formats.add(0x17, "0x17"); |
166 |
builtinFormats.add(0x17, "0x17"); |
146 |
formats.add(0x18, "0x18"); |
167 |
builtinFormats.add(0x18, "0x18"); |
147 |
formats.add(0x19, "0x19"); |
168 |
builtinFormats.add(0x19, "0x19"); |
148 |
formats.add(0x1a, "0x1a"); |
169 |
builtinFormats.add(0x1a, "0x1a"); |
149 |
formats.add(0x1b, "0x1b"); |
170 |
builtinFormats.add(0x1b, "0x1b"); |
150 |
formats.add(0x1c, "0x1c"); |
171 |
builtinFormats.add(0x1c, "0x1c"); |
151 |
formats.add(0x1d, "0x1d"); |
172 |
builtinFormats.add(0x1d, "0x1d"); |
152 |
formats.add(0x1e, "0x1e"); |
173 |
builtinFormats.add(0x1e, "0x1e"); |
153 |
formats.add(0x1f, "0x1f"); |
174 |
builtinFormats.add(0x1f, "0x1f"); |
154 |
formats.add(0x20, "0x20"); |
175 |
builtinFormats.add(0x20, "0x20"); |
155 |
formats.add(0x21, "0x21"); |
176 |
builtinFormats.add(0x21, "0x21"); |
156 |
formats.add(0x22, "0x22"); |
177 |
builtinFormats.add(0x22, "0x22"); |
157 |
formats.add(0x23, "0x23"); |
178 |
builtinFormats.add(0x23, "0x23"); |
158 |
formats.add(0x24, "0x24"); |
179 |
builtinFormats.add(0x24, "0x24"); |
159 |
|
180 |
|
160 |
// 0x17 - 0x24 reserved for international and undocumented |
181 |
// 0x17 - 0x24 reserved for international and undocumented |
161 |
formats.add(0x25, "(#,##0_);(#,##0)"); |
182 |
builtinFormats.add(0x25, "(#,##0_);(#,##0)"); |
162 |
formats.add(0x26, "(#,##0_);[Red](#,##0)"); |
183 |
builtinFormats.add(0x26, "(#,##0_);[Red](#,##0)"); |
163 |
formats.add(0x27, "(#,##0.00_);(#,##0.00)"); |
184 |
builtinFormats.add(0x27, "(#,##0.00_);(#,##0.00)"); |
164 |
formats.add(0x28, "(#,##0.00_);[Red](#,##0.00)"); |
185 |
builtinFormats.add(0x28, "(#,##0.00_);[Red](#,##0.00)"); |
165 |
formats.add(0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"); |
186 |
builtinFormats.add(0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"); |
166 |
formats.add(0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"); |
187 |
builtinFormats.add(0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"); |
167 |
formats.add(0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"); |
188 |
builtinFormats.add(0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"); |
168 |
formats.add(0x2c, |
189 |
builtinFormats.add(0x2c, |
169 |
"_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"); |
190 |
"_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"); |
170 |
formats.add(0x2d, "mm:ss"); |
191 |
builtinFormats.add(0x2d, "mm:ss"); |
171 |
formats.add(0x2e, "[h]:mm:ss"); |
192 |
builtinFormats.add(0x2e, "[h]:mm:ss"); |
172 |
formats.add(0x2f, "mm:ss.0"); |
193 |
builtinFormats.add(0x2f, "mm:ss.0"); |
173 |
formats.add(0x30, "##0.0E+0"); |
194 |
builtinFormats.add(0x30, "##0.0E+0"); |
174 |
formats.add(0x31, "@"); |
195 |
builtinFormats.add(0x31, "@"); |
175 |
} |
196 |
} |
176 |
|
197 |
|
177 |
public static List getFormats() |
198 |
public static List getBuiltinFormats() |
178 |
{ |
199 |
{ |
179 |
if (formats == null) |
200 |
if (builtinFormats == null) |
180 |
{ |
201 |
{ |
181 |
populateFormats(); |
202 |
populateBuiltinFormats(); |
182 |
} |
203 |
} |
183 |
return formats; |
204 |
return builtinFormats; |
184 |
} |
205 |
} |
185 |
|
206 |
|
186 |
/** |
207 |
/** |
Lines 189-205
Link Here
|
189 |
* @return index of format or -1 if undefined. |
210 |
* @return index of format or -1 if undefined. |
190 |
*/ |
211 |
*/ |
191 |
|
212 |
|
192 |
public static short getFormat(String format) |
213 |
public static short getBuiltinFormat(String format) |
193 |
{ |
214 |
{ |
194 |
if (formats == null) |
215 |
if (builtinFormats == null) |
195 |
{ |
216 |
{ |
196 |
populateFormats(); |
217 |
populateBuiltinFormats(); |
197 |
} |
218 |
} |
198 |
short retval = -1; |
219 |
short retval = -1; |
199 |
|
220 |
|
200 |
for (short k = 0; k < 0x31; k++) |
221 |
for (short k = 0; k < 0x31; k++) |
201 |
{ |
222 |
{ |
202 |
String nformat = ( String ) formats.get(k); |
223 |
String nformat = ( String ) builtinFormats.get(k); |
203 |
|
224 |
|
204 |
if ((nformat != null) && nformat.equals(format)) |
225 |
if ((nformat != null) && nformat.equals(format)) |
205 |
{ |
226 |
{ |
Lines 211-241
Link Here
|
211 |
} |
232 |
} |
212 |
|
233 |
|
213 |
/** |
234 |
/** |
|
|
235 |
* get the format index that matches the given format string. |
236 |
* Creates a new format if one is not found. |
237 |
* @param format string matching a built in format |
238 |
* @return index of format. |
239 |
*/ |
240 |
|
241 |
public short getFormat(String format) |
242 |
{ |
243 |
ListIterator i; |
244 |
int ind; |
245 |
if (!movedBuiltins) { |
246 |
i = builtinFormats.listIterator(); |
247 |
while (i.hasNext()) { |
248 |
ind = i.nextIndex(); |
249 |
formats.add(ind, i.next()); |
250 |
} |
251 |
movedBuiltins = true; |
252 |
} |
253 |
i = formats.listIterator(); |
254 |
while (i.hasNext()) { |
255 |
ind = i.nextIndex(); |
256 |
if (format.equals(i.next())) |
257 |
return (short)ind; |
258 |
} |
259 |
|
260 |
ind = workbook.getFormat(format, true); |
261 |
if (formats.size() <= ind) |
262 |
formats.setSize(ind + 1); |
263 |
formats.add(ind, format); |
264 |
|
265 |
return (short)ind; |
266 |
} |
267 |
|
268 |
/** |
269 |
* get the format string that matches the given format index |
270 |
* @param index of a format |
271 |
* @return string represented at index of format or null if there is not a format at that index |
272 |
*/ |
273 |
|
274 |
public String getFormat(short index) |
275 |
{ |
276 |
if (movedBuiltins) |
277 |
return ( String ) formats.get(index); |
278 |
else |
279 |
return (String) (builtinFormats.get(index) != null ? builtinFormats.get(index) : formats.get(index)); |
280 |
} |
281 |
|
282 |
/** |
214 |
* get the format string that matches the given format index |
283 |
* get the format string that matches the given format index |
215 |
* @param index of a built in format |
284 |
* @param index of a built in format |
216 |
* @return string represented at index of format or null if there is not a builtin format at that index |
285 |
* @return string represented at index of format or null if there is not a builtin format at that index |
217 |
*/ |
286 |
*/ |
218 |
|
287 |
|
219 |
public static String getFormat(short index) |
288 |
public static String getBuiltinFormat(short index) |
220 |
{ |
289 |
{ |
221 |
if (formats == null) |
290 |
if (builtinFormats == null) |
222 |
{ |
291 |
{ |
223 |
populateFormats(); |
292 |
populateBuiltinFormats(); |
224 |
} |
293 |
} |
225 |
return ( String ) formats.get(index); |
294 |
return ( String ) builtinFormats.get(index); |
226 |
} |
295 |
} |
227 |
|
296 |
|
228 |
/** |
297 |
/** |
229 |
* get the number of builtin and reserved formats |
298 |
* get the number of builtin and reserved builtinFormats |
230 |
* @return number of builtin and reserved formats |
299 |
* @return number of builtin and reserved builtinFormats |
231 |
*/ |
300 |
*/ |
232 |
|
301 |
|
233 |
public static int getNumberOfBuiltinFormats() |
302 |
public static int getNumberOfBuiltinBuiltinFormats() |
234 |
{ |
303 |
{ |
235 |
if (formats == null) |
304 |
if (builtinFormats == null) |
236 |
{ |
305 |
{ |
237 |
populateFormats(); |
306 |
populateBuiltinFormats(); |
238 |
} |
307 |
} |
239 |
return formats.size(); |
308 |
return builtinFormats.size(); |
240 |
} |
309 |
} |
241 |
} |
310 |
} |