KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > codec > CodecFormat


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.util.codec;
37
38 import java.util.*;
39 import java.text.*;
40
41 import com.micronova.util.*;
42
43 public class CodecFormat extends Codec
44 {
45     public static Object JavaDoc date(Object JavaDoc object, Object JavaDoc patternSpec, Object JavaDoc localeSpec, Object JavaDoc timeZoneSpec) throws Exception JavaDoc
46     {
47         if (object != null)
48         {
49             String JavaDoc pattern = TypeUtil.isString(patternSpec);
50
51             Locale locale = TypeUtil.isLocale(localeSpec);
52             TimeZone timeZone = TypeUtil.isTimeZone(timeZoneSpec);
53
54             Date date = TypeUtil.isDate(object, pattern, locale, timeZone);
55
56             if (date != null)
57             {
58                 SimpleDateFormat simpleDateFormat;
59                     
60                 if (pattern == null)
61                 {
62                     simpleDateFormat = new SimpleDateFormat();
63                 }
64                 else
65                 {
66                     simpleDateFormat = new SimpleDateFormat(pattern, locale);
67                 }
68
69                 if (timeZone != null)
70                 {
71                     simpleDateFormat.setTimeZone(timeZone);
72                 }
73                 
74                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
75                 FieldPosition fieldPosition = new FieldPosition(0);
76
77                 buffer = simpleDateFormat.format(date, buffer, fieldPosition);
78
79                 object = buffer.toString();
80             }
81             else
82             {
83                 object = null;
84             }
85         }
86
87         return object;
88     }
89
90     public static Object JavaDoc date(Object JavaDoc object, Object JavaDoc patternSpec, Object JavaDoc localeSpec) throws Exception JavaDoc
91     {
92         return date(object, patternSpec, localeSpec, null);
93     }
94
95     public static Object JavaDoc date(Object JavaDoc object, Object JavaDoc patternSpec) throws Exception JavaDoc
96     {
97         return date(object, patternSpec, null);
98     }
99
100     public static Object JavaDoc date(Object JavaDoc object) throws Exception JavaDoc
101     {
102         return date(object, null, null);
103     }
104
105     public static Object JavaDoc number(Object JavaDoc object, Object JavaDoc patternSpec, Object JavaDoc localeSpec) throws Exception JavaDoc
106     {
107         if (object != null)
108         {
109             String JavaDoc pattern = TypeUtil.isString(patternSpec);
110
111             Locale locale = TypeUtil.isLocale(localeSpec);
112
113             Number JavaDoc number = TypeUtil.isNumber(object, pattern, locale);
114
115             if (number != null)
116             {
117                 DecimalFormat decimalFormat;
118
119                 if (pattern == null)
120                 {
121                     decimalFormat = new DecimalFormat();
122                 }
123                 else
124                 {
125                     decimalFormat = new DecimalFormat(pattern, new DecimalFormatSymbols(locale));
126                 }
127                 
128                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
129                 FieldPosition fieldPosition = new FieldPosition(0);
130
131                 buffer = decimalFormat.format(number, buffer, fieldPosition);
132
133                 object = buffer.toString();
134             }
135             else
136             {
137                 object = null;
138             }
139         }
140
141         return object;
142     }
143
144
145     public static Object JavaDoc number(Object JavaDoc object, Object JavaDoc patternSpec) throws Exception JavaDoc
146     {
147         return number(object, patternSpec, null);
148     }
149
150     public static Object JavaDoc number(Object JavaDoc object) throws Exception JavaDoc
151     {
152         return number(object, null, null);
153     }
154 }
155
Popular Tags