001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.datatypes;
051:
052: import org.jaffa.metadata.*;
053: import java.text.*;
054: import org.jaffa.util.LocaleHelper;
055: import org.jaffa.presentation.portlet.session.LocaleContext;
056:
057: /** This class has convenience methods for formatting objects based on the datatypes and layouts.
058: */
059: public class Formatter {
060:
061: /** Formats the input.
062: * This used the default layout specified in the corresponding meta class.
063: * @param input the input.
064: * @return a String representation of the input.
065: */
066: public static String format(Boolean input) {
067: return format(input, BooleanFieldMetaData.DEFAULT_LAYOUT);
068: }
069:
070: /** Formats the input based on the input layout.
071: * @param input the input.
072: * @param layout the layout to be used for formatting the input.
073: * @return a String representation of the input.
074: */
075: public static String format(Boolean input, String layout) {
076: String out = null;
077: if (input != null) {
078: if (layout == null) {
079: out = input.toString();
080: } else {
081: layout = LocaleHelper.determineLayout(layout);
082: // Assume that the true/false values are separated by a comma
083: int i = layout.indexOf(',');
084: if (input.booleanValue()) {
085: out = layout.substring(0, i);
086: } else {
087: if (++i < layout.length())
088: out = layout.substring(i);
089: }
090: }
091: }
092: return out;
093: }
094:
095: /** Formats the input.
096: * This used the default layout specified in the corresponding meta class.
097: * @param input the input.
098: * @return a String representation of the input.
099: */
100: public static String format(Long input) {
101: return format(input, null);
102: }
103:
104: /** Formats the input based on the input layout.
105: * @param input the input.
106: * @param layout the layout to be used for formatting the input.
107: * @return a String representation of the input.
108: */
109: public static String format(Long input, String layout) {
110: String out = null;
111: if (input != null) {
112: if (layout == null)
113: layout = IntegerFieldMetaData.getIntegerFormat();
114: layout = LocaleHelper.determineLayout(layout);
115:
116: NumberFormat fmt;
117: if (LocaleContext.getLocale() != null)
118: fmt = NumberFormat.getIntegerInstance(LocaleContext
119: .getLocale());
120: else
121: fmt = NumberFormat.getIntegerInstance();
122: if (layout != null && layout.length() > 0
123: && fmt instanceof DecimalFormat)
124: ((DecimalFormat) fmt).applyPattern(layout);
125: out = fmt.format(input);
126: }
127: return out;
128: }
129:
130: /** Formats the input.
131: * This used the default layout specified in the corresponding meta class.
132: * @param input the input.
133: * @return a String representation of the input.
134: */
135: public static String format(Double input) {
136: return format(input, null);
137: }
138:
139: /** Formats the input based on the input layout.
140: * @param input the input.
141: * @param layout the layout to be used for formatting the input.
142: * @return a String representation of the input.
143: */
144: public static String format(Double input, String layout) {
145: String out = null;
146: if (input != null) {
147: if (layout == null)
148: layout = DecimalFieldMetaData.getDecimalFormat();
149: layout = LocaleHelper.determineLayout(layout);
150:
151: NumberFormat fmt;
152: if (LocaleContext.getLocale() != null)
153: fmt = NumberFormat.getNumberInstance(LocaleContext
154: .getLocale());
155: else
156: fmt = NumberFormat.getNumberInstance();
157: if (layout != null && layout.length() > 0
158: && fmt instanceof DecimalFormat)
159: ((DecimalFormat) fmt).applyPattern(layout);
160: out = fmt.format(input);
161: }
162: return out;
163: }
164:
165: /** Formats the input.
166: * This used the default layout specified in the corresponding meta class.
167: * @param input the input.
168: * @return a String representation of the input.
169: */
170: public static String format(DateOnly input) {
171: return format(input, null);
172: }
173:
174: /** Formats the input based on the input layout.
175: * @param input the input.
176: * @param layout the layout to be used for formatting the input.
177: * @return a String representation of the input.
178: */
179: public static String format(DateOnly input, String layout) {
180: String out = null;
181: if (input != null) {
182: if (layout == null)
183: layout = DateOnlyFieldMetaData.getDateOnlyFormat();
184: layout = LocaleHelper.determineLayout(layout);
185:
186: SimpleDateFormat fmt;
187: if (LocaleContext.getLocale() != null)
188: fmt = new SimpleDateFormat(layout, LocaleContext
189: .getLocale());
190: else
191: fmt = new SimpleDateFormat(layout);
192: out = fmt.format(input.getUtilDate());
193: }
194: return out;
195: }
196:
197: /** Formats the input.
198: * This used the default layout specified in the corresponding meta class.
199: * @param input the input.
200: * @return a String representation of the input.
201: */
202: public static String format(DateTime input) {
203: return format(input, null);
204: }
205:
206: /** Formats the input based on the input layout.
207: * @param input the input.
208: * @param layout the layout to be used for formatting the input.
209: * @return a String representation of the input.
210: */
211: public static String format(DateTime input, String layout) {
212: String out = null;
213: if (input != null) {
214: if (layout == null)
215: layout = DateTimeFieldMetaData.getDateTimeFormat();
216: layout = LocaleHelper.determineLayout(layout);
217:
218: SimpleDateFormat fmt;
219: if (LocaleContext.getLocale() != null)
220: fmt = new SimpleDateFormat(layout, LocaleContext
221: .getLocale());
222: else
223: fmt = new SimpleDateFormat(layout);
224: out = fmt.format(input.getUtilDate());
225: }
226: return out;
227: }
228:
229: /** Formats the input.
230: * This used the default layout specified in the corresponding meta class.
231: * @param input the input.
232: * @return a String representation of the input.
233: */
234: public static String format(Currency input) {
235: return format(input, CurrencyFieldMetaData.getCurrencyFormat());
236: }
237:
238: /** Formats the input based on the input layout.
239: * @param input the input.
240: * @param layout the layout to be used for formatting the input.
241: * @return a String representation of the input.
242: */
243: public static String format(Currency input, String layout) {
244: String out = null;
245: if (input != null) {
246: if (layout == null)
247: layout = CurrencyFieldMetaData.getCurrencyFormat();
248: layout = LocaleHelper.determineLayout(layout);
249:
250: NumberFormat fmt;
251: if (LocaleContext.getLocale() != null)
252: fmt = NumberFormat.getCurrencyInstance(LocaleContext
253: .getLocale());
254: else
255: fmt = NumberFormat.getCurrencyInstance();
256: fmt.setCurrency(java.util.Currency.getInstance(input
257: .getCurrencyType()));
258: if (layout != null && layout.length() > 0
259: && fmt instanceof DecimalFormat)
260: ((DecimalFormat) fmt).applyPattern(layout);
261: out = fmt.format(input.getValue());
262: }
263: return out;
264: }
265:
266: /** Formats the input.
267: * @param input the input.
268: * @return a String representation of the input.
269: */
270: public static String format(String input) {
271: // Note: there is no layout for a String... just return
272: return input;
273: }
274:
275: /** Formats the input.
276: * @param input the input.
277: * @return a String representation of the input.
278: */
279: public static String format(byte[] input) {
280: // Note: there is no layout for a Raw... just return
281: if (input == null) {
282: return null;
283: } else {
284: return new String(input);
285: }
286: }
287:
288: /** Formats the input.
289: * @param input the input.
290: * @return a String representation of the input.
291: */
292: public static String format(Object input) {
293: return format(input, null);
294: }
295:
296: /** Formats the input.
297: * @param input the input.
298: * @param layout the layout to be used for formatting the input.
299: * @return a String representation of the input.
300: */
301: public static String format(Object input, String layout) {
302: String out = null;
303: if (input != null) {
304: if (input instanceof String)
305: out = format((String) input);
306: else if (input instanceof Boolean)
307: out = format((Boolean) input, layout);
308: else if (input instanceof Long)
309: out = format((Long) input, layout);
310: else if (input instanceof Double)
311: out = format((Double) input, layout);
312: else if (input instanceof DateOnly)
313: out = format((DateOnly) input, layout);
314: else if (input instanceof DateTime)
315: out = format((DateTime) input, layout);
316: else if (input instanceof Currency)
317: out = format((Currency) input, layout);
318: else if (input instanceof byte[])
319: out = format((byte[]) input);
320: else
321: out = input.toString();
322: }
323: return out;
324: }
325: }
|