1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   /*
57    * HSSFDataFormat.java
58    *
59    * Created on December 18, 2001, 12:42 PM
60    */
61   package org.apache.poi.hssf.usermodel;
62   
63   import java.util.ArrayList;
64   import java.util.List;
65   
66   /**
67    * Utility to identify builin formats.  The following is a list of the formats as
68    * returned by this class.<P>
69    *<P>
70    *       0, "General"<P>
71    *       1, "0"<P>
72    *       2, "0.00"<P>
73    *       3, "#,##0"<P>
74    *       4, "#,##0.00"<P>
75    *       5, "($#,##0_);($#,##0)"<P>
76    *       6, "($#,##0_);[Red]($#,##0)"<P>
77    *       7, "($#,##0.00);($#,##0.00)"<P>
78    *       8, "($#,##0.00_);[Red]($#,##0.00)"<P>
79    *       9, "0%"<P>
80    *       0xa, "0.00%"<P>
81    *       0xb, "0.00E+00"<P>
82    *       0xc, "# ?/?"<P>
83    *       0xd, "# ??/??"<P>
84    *       0xe, "m/d/yy"<P>
85    *       0xf, "d-mmm-yy"<P>
86    *       0x10, "d-mmm"<P>
87    *       0x11, "mmm-yy"<P>
88    *       0x12, "h:mm AM/PM"<P>
89    *       0x13, "h:mm:ss AM/PM"<P>
90    *       0x14, "h:mm"<P>
91    *       0x15, "h:mm:ss"<P>
92    *       0x16, "m/d/yy h:mm"<P>
93    *<P>
94    *       // 0x17 - 0x24 reserved for international and undocumented
95    *       0x25, "(#,##0_);(#,##0)"<P>
96    *       0x26, "(#,##0_);[Red](#,##0)"<P>
97    *       0x27, "(#,##0.00_);(#,##0.00)"<P>
98    *       0x28, "(#,##0.00_);[Red](#,##0.00)"<P>
99    *       0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"<P>
100   *       0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"<P>
101   *       0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"<P>
102   *       0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"<P>
103   *       0x2d, "mm:ss"<P>
104   *       0x2e, "[h]:mm:ss"<P>
105   *       0x2f, "mm:ss.0"<P>
106   *       0x30, "##0.0E+0"<P>
107   *       0x31, "@"<P>
108   *
109   *
110   * @author  Andrew C. Oliver (acoliver at apache dot org)
111   */
112  
113  public class HSSFDataFormat
114  {
115      private static ArrayList formats;
116  
117      private static synchronized void populateFormats()
118      {
119          formats = new ArrayList();
120          formats.add(0, "General");
121          formats.add(1, "0");
122          formats.add(2, "0.00");
123          formats.add(3, "#,##0");
124          formats.add(4, "#,##0.00");
125          formats.add(5, "($#,##0_);($#,##0)");
126          formats.add(6, "($#,##0_);[Red]($#,##0)");
127          formats.add(7, "($#,##0.00);($#,##0.00)");
128          formats.add(8, "($#,##0.00_);[Red]($#,##0.00)");
129          formats.add(9, "0%");
130          formats.add(0xa, "0.00%");
131          formats.add(0xb, "0.00E+00");
132          formats.add(0xc, "# ?/?");
133          formats.add(0xd, "# ??/??");
134          formats.add(0xe, "m/d/yy");
135          formats.add(0xf, "d-mmm-yy");
136          formats.add(0x10, "d-mmm");
137          formats.add(0x11, "mmm-yy");
138          formats.add(0x12, "h:mm AM/PM");
139          formats.add(0x13, "h:mm:ss AM/PM");
140          formats.add(0x14, "h:mm");
141          formats.add(0x15, "h:mm:ss");
142          formats.add(0x16, "m/d/yy h:mm");
143  
144          // 0x17 - 0x24 reserved for international and undocumented
145          formats.add(0x17, "0x17");
146          formats.add(0x18, "0x18");
147          formats.add(0x19, "0x19");
148          formats.add(0x1a, "0x1a");
149          formats.add(0x1b, "0x1b");
150          formats.add(0x1c, "0x1c");
151          formats.add(0x1d, "0x1d");
152          formats.add(0x1e, "0x1e");
153          formats.add(0x1f, "0x1f");
154          formats.add(0x20, "0x20");
155          formats.add(0x21, "0x21");
156          formats.add(0x22, "0x22");
157          formats.add(0x23, "0x23");
158          formats.add(0x24, "0x24");
159  
160          // 0x17 - 0x24 reserved for international and undocumented
161          formats.add(0x25, "(#,##0_);(#,##0)");
162          formats.add(0x26, "(#,##0_);[Red](#,##0)");
163          formats.add(0x27, "(#,##0.00_);(#,##0.00)");
164          formats.add(0x28, "(#,##0.00_);[Red](#,##0.00)");
165          formats.add(0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)");
166          formats.add(0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)");
167          formats.add(0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)");
168          formats.add(0x2c,
169                      "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)");
170          formats.add(0x2d, "mm:ss");
171          formats.add(0x2e, "[h]:mm:ss");
172          formats.add(0x2f, "mm:ss.0");
173          formats.add(0x30, "##0.0E+0");
174          formats.add(0x31, "@");
175      }
176  
177      public static List getFormats()
178      {
179          if (formats == null)
180          {
181              populateFormats();
182          }
183          return formats;
184      }
185  
186      /**
187       * get the format index that matches the given format string
188       * @param format string matching a built in format
189       * @return index of format or -1 if undefined.
190       */
191  
192      public static short getFormat(String format)
193      {
194          if (formats == null)
195          {
196              populateFormats();
197          }
198          short retval = -1;
199  
200          for (short k = 0; k < 0x31; k++)
201          {
202              String nformat = ( String ) formats.get(k);
203  
204              if ((nformat != null) && nformat.equals(format))
205              {
206                  retval = k;
207                  break;
208              }
209          }
210          return retval;
211      }
212  
213      /**
214       * get the format string that matches the given format index
215       * @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
217       */
218  
219      public static String getFormat(short index)
220      {
221          if (formats == null)
222          {
223              populateFormats();
224          }
225          return ( String ) formats.get(index);
226      }
227  
228      /**
229       * get the number of builtin and reserved formats
230       * @return number of builtin and reserved formats
231       */
232  
233      public static int getNumberOfBuiltinFormats()
234      {
235          if (formats == null)
236          {
237              populateFormats();
238          }
239          return formats.size();
240      }
241  }
242