001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: /*
019: * HSSFDataFormat.java
020: *
021: * Created on December 18, 2001, 12:42 PM
022: */
023: package org.apache.poi.hssf.usermodel;
024:
025: import org.apache.poi.hssf.model.Workbook;
026: import org.apache.poi.hssf.record.FormatRecord;
027:
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.ListIterator;
031: import java.util.Vector;
032:
033: /**
034: * Utility to identify builtin formats. Now can handle user defined data formats also. The following is a list of the formats as
035: * returned by this class.<P>
036: *<P>
037: * 0, "General"<br>
038: * 1, "0"<br>
039: * 2, "0.00"<br>
040: * 3, "#,##0"<br>
041: * 4, "#,##0.00"<br>
042: * 5, "($#,##0_);($#,##0)"<br>
043: * 6, "($#,##0_);[Red]($#,##0)"<br>
044: * 7, "($#,##0.00);($#,##0.00)"<br>
045: * 8, "($#,##0.00_);[Red]($#,##0.00)"<br>
046: * 9, "0%"<br>
047: * 0xa, "0.00%"<br>
048: * 0xb, "0.00E+00"<br>
049: * 0xc, "# ?/?"<br>
050: * 0xd, "# ??/??"<br>
051: * 0xe, "m/d/yy"<br>
052: * 0xf, "d-mmm-yy"<br>
053: * 0x10, "d-mmm"<br>
054: * 0x11, "mmm-yy"<br>
055: * 0x12, "h:mm AM/PM"<br>
056: * 0x13, "h:mm:ss AM/PM"<br>
057: * 0x14, "h:mm"<br>
058: * 0x15, "h:mm:ss"<br>
059: * 0x16, "m/d/yy h:mm"<br>
060: *<P>
061: * // 0x17 - 0x24 reserved for international and undocumented
062: * 0x25, "(#,##0_);(#,##0)"<P>
063: * 0x26, "(#,##0_);[Red](#,##0)"<P>
064: * 0x27, "(#,##0.00_);(#,##0.00)"<P>
065: * 0x28, "(#,##0.00_);[Red](#,##0.00)"<P>
066: * 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"<P>
067: * 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"<P>
068: * 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"<P>
069: * 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"<P>
070: * 0x2d, "mm:ss"<P>
071: * 0x2e, "[h]:mm:ss"<P>
072: * 0x2f, "mm:ss.0"<P>
073: * 0x30, "##0.0E+0"<P>
074: * 0x31, "@" - This is text format.<P>
075: * 0x31 "text" - Alias for "@"<P>
076: *
077: * @author Andrew C. Oliver (acoliver at apache dot org)
078: * @author Shawn M. Laubach (slaubach at apache dot org)
079: */
080:
081: public class HSSFDataFormat {
082: private static List builtinFormats = createBuiltinFormats();
083:
084: private Vector formats = new Vector();
085: private Workbook workbook;
086: private boolean movedBuiltins = false; // Flag to see if need to
087:
088: // check the built in list
089: // or if the regular list
090: // has all entries.
091:
092: /**
093: * Construncts a new data formatter. It takes a workbook to have
094: * access to the workbooks format records.
095: * @param workbook the workbook the formats are tied to.
096: */
097: public HSSFDataFormat(Workbook workbook) {
098: this .workbook = workbook;
099: Iterator i = workbook.getFormats().iterator();
100: while (i.hasNext()) {
101: FormatRecord r = (FormatRecord) i.next();
102: if (formats.size() < r.getIndexCode() + 1) {
103: formats.setSize(r.getIndexCode() + 1);
104: }
105: formats.set(r.getIndexCode(), r.getFormatString());
106: }
107:
108: }
109:
110: private static synchronized List createBuiltinFormats() {
111: List builtinFormats = new Vector();
112: builtinFormats.add(0, "General");
113: builtinFormats.add(1, "0");
114: builtinFormats.add(2, "0.00");
115: builtinFormats.add(3, "#,##0");
116: builtinFormats.add(4, "#,##0.00");
117: builtinFormats.add(5, "($#,##0_);($#,##0)");
118: builtinFormats.add(6, "($#,##0_);[Red]($#,##0)");
119: builtinFormats.add(7, "($#,##0.00);($#,##0.00)");
120: builtinFormats.add(8, "($#,##0.00_);[Red]($#,##0.00)");
121: builtinFormats.add(9, "0%");
122: builtinFormats.add(0xa, "0.00%");
123: builtinFormats.add(0xb, "0.00E+00");
124: builtinFormats.add(0xc, "# ?/?");
125: builtinFormats.add(0xd, "# ??/??");
126: builtinFormats.add(0xe, "m/d/yy");
127: builtinFormats.add(0xf, "d-mmm-yy");
128: builtinFormats.add(0x10, "d-mmm");
129: builtinFormats.add(0x11, "mmm-yy");
130: builtinFormats.add(0x12, "h:mm AM/PM");
131: builtinFormats.add(0x13, "h:mm:ss AM/PM");
132: builtinFormats.add(0x14, "h:mm");
133: builtinFormats.add(0x15, "h:mm:ss");
134: builtinFormats.add(0x16, "m/d/yy h:mm");
135:
136: // 0x17 - 0x24 reserved for international and undocumented
137: builtinFormats.add(0x17, "0x17");
138: builtinFormats.add(0x18, "0x18");
139: builtinFormats.add(0x19, "0x19");
140: builtinFormats.add(0x1a, "0x1a");
141: builtinFormats.add(0x1b, "0x1b");
142: builtinFormats.add(0x1c, "0x1c");
143: builtinFormats.add(0x1d, "0x1d");
144: builtinFormats.add(0x1e, "0x1e");
145: builtinFormats.add(0x1f, "0x1f");
146: builtinFormats.add(0x20, "0x20");
147: builtinFormats.add(0x21, "0x21");
148: builtinFormats.add(0x22, "0x22");
149: builtinFormats.add(0x23, "0x23");
150: builtinFormats.add(0x24, "0x24");
151:
152: // 0x17 - 0x24 reserved for international and undocumented
153: builtinFormats.add(0x25, "(#,##0_);(#,##0)");
154: builtinFormats.add(0x26, "(#,##0_);[Red](#,##0)");
155: builtinFormats.add(0x27, "(#,##0.00_);(#,##0.00)");
156: builtinFormats.add(0x28, "(#,##0.00_);[Red](#,##0.00)");
157: builtinFormats.add(0x29,
158: "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)");
159: builtinFormats.add(0x2a,
160: "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)");
161: builtinFormats.add(0x2b,
162: "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)");
163: builtinFormats.add(0x2c,
164: "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)");
165: builtinFormats.add(0x2d, "mm:ss");
166: builtinFormats.add(0x2e, "[h]:mm:ss");
167: builtinFormats.add(0x2f, "mm:ss.0");
168: builtinFormats.add(0x30, "##0.0E+0");
169: builtinFormats.add(0x31, "@");
170: return builtinFormats;
171: }
172:
173: public static List getBuiltinFormats() {
174: return builtinFormats;
175: }
176:
177: /**
178: * get the format index that matches the given format string<p>
179: * Automatically converts "text" to excel's format string to represent text.
180: * @param format string matching a built in format
181: * @return index of format or -1 if undefined.
182: */
183:
184: public static short getBuiltinFormat(String format) {
185: if (format.toUpperCase().equals("TEXT"))
186: format = "@";
187:
188: short retval = -1;
189:
190: for (short k = 0; k <= 0x31; k++) {
191: String nformat = (String) builtinFormats.get(k);
192:
193: if ((nformat != null) && nformat.equals(format)) {
194: retval = k;
195: break;
196: }
197: }
198: return retval;
199: }
200:
201: /**
202: * get the format index that matches the given format string.
203: * Creates a new format if one is not found. Aliases text to the proper format.
204: * @param format string matching a built in format
205: * @return index of format.
206: */
207:
208: public short getFormat(String format) {
209: ListIterator i;
210: int ind;
211:
212: if (format.toUpperCase().equals("TEXT"))
213: format = "@";
214:
215: if (!movedBuiltins) {
216: i = builtinFormats.listIterator();
217: while (i.hasNext()) {
218: ind = i.nextIndex();
219: if (formats.size() < ind + 1) {
220: formats.setSize(ind + 1);
221: }
222:
223: formats.set(ind, i.next());
224: }
225: movedBuiltins = true;
226: }
227: i = formats.listIterator();
228: while (i.hasNext()) {
229: ind = i.nextIndex();
230: if (format.equals(i.next()))
231: return (short) ind;
232: }
233:
234: ind = workbook.getFormat(format, true);
235: if (formats.size() <= ind)
236: formats.setSize(ind + 1);
237: formats.set(ind, format);
238:
239: return (short) ind;
240: }
241:
242: /**
243: * get the format string that matches the given format index
244: * @param index of a format
245: * @return string represented at index of format or null if there is not a format at that index
246: */
247:
248: public String getFormat(short index) {
249: if (movedBuiltins)
250: return (String) formats.get(index);
251: else
252: return (String) (builtinFormats.size() > index
253: && builtinFormats.get(index) != null ? builtinFormats
254: .get(index)
255: : formats.get(index));
256: }
257:
258: /**
259: * get the format string that matches the given format index
260: * @param index of a built in format
261: * @return string represented at index of format or null if there is not a builtin format at that index
262: * @throws ArrayOutOfBoundsException when the index exceeds the number of builtin formats.
263: */
264:
265: public static String getBuiltinFormat(short index) {
266: return (String) builtinFormats.get(index);
267: }
268:
269: /**
270: * get the number of builtin and reserved builtinFormats
271: * @return number of builtin and reserved builtinFormats
272: */
273:
274: public static int getNumberOfBuiltinBuiltinFormats() {
275: return builtinFormats.size();
276: }
277: }
|