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 java.util.*;
053: import org.jaffa.metadata.*;
054: import org.jaffa.datatypes.exceptions.*;
055: import java.text.*;
056: import org.apache.log4j.Logger;
057: import org.jaffa.presentation.portlet.session.LocaleContext;
058: import org.jaffa.util.LocaleHelper;
059:
060: /** This class has convenience methods to parse a string into the required objects.
061: */
062: public class Parser {
063:
064: private static final Logger log = Logger.getLogger(Parser.class);
065: private static final Collection c_booleanTrue = new ArrayList();
066: private static final Collection c_booleanFalse = new ArrayList();
067: static {
068: c_booleanTrue.add("true");
069: c_booleanTrue.add("t");
070: c_booleanTrue.add("yes");
071: c_booleanTrue.add("y");
072: c_booleanTrue.add("1");
073:
074: c_booleanFalse.add("false");
075: c_booleanFalse.add("f");
076: c_booleanFalse.add("no");
077: c_booleanFalse.add("n");
078: c_booleanFalse.add("0");
079: }
080:
081: /** Returns a Boolean object corresponding to the input String.
082: * It uses the default layout specified in the corrsponding meta class.
083: * If the layout is null, then a TRUE is returned if the input is true, t, yes, y, 1.
084: * If the layout is null, then a FALSE is returned if the input is false, f, no, n, 0.
085: * If the layout is null, and the input is none of the above, then it uses standard JAVA boolean parsing.
086: * If the layout is passed, then it is assumed to have the true/false strings separated by a comma.
087: * @param input the input.
088: * @return the parsed object.
089: */
090: public static Boolean parseBoolean(String input) {
091: return parseBoolean(input, null);
092: }
093:
094: /** Returns a Boolean object corresponding to the input String.
095: * If the layout is null, then a TRUE is returned if the input is true, t, yes, y, 1.
096: * If the layout is null, then a FALSE is returned if the input is false, f, no, n, 0.
097: * If the layout is null, and the input is none of the above, then it uses standard JAVA boolean parsing.
098: * If the layout is passed, then it is assumed to have the true/false strings separated by a comma.
099: * @param input the input.
100: * @param layout the layout.
101: * @return the parsed object.
102: */
103: public static Boolean parseBoolean(String input, String layout) {
104: Boolean out = null;
105: if (input != null && input.length() > 0) {
106: if (layout == null)
107: layout = BooleanFieldMetaData.DEFAULT_LAYOUT;
108: layout = LocaleHelper.determineLayout(layout);
109: if (layout == null) {
110: input = input.toLowerCase();
111: if (c_booleanTrue.contains(input))
112: out = Boolean.TRUE;
113: else if (c_booleanFalse.contains(input))
114: out = Boolean.FALSE;
115: else
116: out = Boolean.valueOf(input);
117: } else {
118: input = input.toUpperCase();
119: layout = layout.toUpperCase();
120:
121: // Assume that the true/false values are separated by a comma
122: String trueString = null;
123: int i = layout.indexOf(',');
124: trueString = layout.substring(0, i);
125:
126: if (input.equals(trueString))
127: out = new Boolean(true);
128: else
129: out = new Boolean(false);
130: }
131: }
132: return out;
133: }
134:
135: /** Returns a Long object corresponding to the input String.
136: * It uses the default layout specified in the corrsponding meta class.
137: * @param input the input.
138: * @throws FormatIntegerException if parsing fails.
139: * @return the parsed object.
140: */
141: public static Long parseInteger(String input)
142: throws FormatIntegerException {
143: return parseInteger(input, null);
144: }
145:
146: /** Returns a Long object corresponding to the input String.
147: * @param input the input.
148: * @param layout the layout.
149: * @throws FormatIntegerException if parsing fails.
150: * @return the parsed object.
151: */
152: public static Long parseInteger(String input, String layout)
153: throws FormatIntegerException {
154: try {
155: Long out = null;
156: if (input != null && input.length() > 0) {
157: if (layout == null)
158: layout = IntegerFieldMetaData.getIntegerFormat();
159: layout = LocaleHelper.determineLayout(layout);
160:
161: NumberFormat fmt;
162: if (LocaleContext.getLocale() != null)
163: fmt = NumberFormat.getIntegerInstance(LocaleContext
164: .getLocale());
165: else
166: fmt = NumberFormat.getIntegerInstance();
167: if (layout != null && fmt instanceof DecimalFormat)
168: ((DecimalFormat) fmt).applyPattern(layout);
169: out = new Long(fmt.parse(input).longValue());
170: }
171: return out;
172: } catch (ParseException e) {
173: throw new FormatIntegerException(null, null, e);
174: }
175: }
176:
177: /** Returns a Double object corresponding to the input String.
178: * It uses the default layout specified in the corrsponding meta class.
179: * @param input the input.
180: * @throws FormatDecimalException if parsing fails.
181: * @return the parsed object.
182: */
183: public static Double parseDecimal(String input)
184: throws FormatDecimalException {
185: return parseDecimal(input, null);
186: }
187:
188: /** Returns a Double object corresponding to the input String.
189: * @param input the input.
190: * @param layout the layout.
191: * @throws FormatDecimalException if parsing fails.
192: * @return the parsed object.
193: */
194: public static Double parseDecimal(String input, String layout)
195: throws FormatDecimalException {
196: try {
197: Double out = null;
198: if (input != null && input.length() > 0) {
199: if (layout == null)
200: layout = DecimalFieldMetaData.getDecimalFormat();
201: layout = LocaleHelper.determineLayout(layout);
202:
203: NumberFormat fmt;
204: if (LocaleContext.getLocale() != null)
205: fmt = NumberFormat.getNumberInstance(LocaleContext
206: .getLocale());
207: else
208: fmt = NumberFormat.getNumberInstance();
209: if (layout != null && fmt instanceof DecimalFormat)
210: ((DecimalFormat) fmt).applyPattern(layout);
211: out = new Double(fmt.parse(input).doubleValue());
212: }
213: return out;
214: } catch (ParseException e) {
215: throw new FormatDecimalException(null, null, e);
216: }
217: }
218:
219: /** Returns a DateOnly object corresponding to the input String.
220: * It uses the default layout specified in the corrsponding meta class.
221: * @param input the input.
222: * @throws FormatDateOnlyException if parsing fails.
223: * @return the parsed object.
224: */
225: public static DateOnly parseDateOnly(String input)
226: throws FormatDateOnlyException {
227: return parseDateOnly(input, null);
228: }
229:
230: /** Returns a DateOnly object corresponding to the input String.
231: * @param input the input.
232: * @param layout the layout.
233: * @throws FormatDateOnlyException if parsing fails.
234: * @return the parsed object.
235: */
236: public static DateOnly parseDateOnly(String input, String layout)
237: throws FormatDateOnlyException {
238: try {
239: return DateOnly.parse(input, layout);
240: } catch (ParseException e) {
241: throw new FormatDateOnlyException(
242: null,
243: new Object[] { (layout == null ? DateOnlyFieldMetaData
244: .getDateOnlyParse()
245: : layout) }, e);
246: }
247: }
248:
249: /** Returns a DateTime object corresponding to the input String.
250: * It uses the default layout specified in the corrsponding meta class.
251: * @param input the input.
252: * @throws FormatDateTimeException if parsing fails.
253: * @return the parsed object.
254: */
255: public static DateTime parseDateTime(String input)
256: throws FormatDateTimeException {
257: return parseDateTime(input, null);
258: }
259:
260: /** Returns a DateTime object corresponding to the input String.
261: * @param input the input.
262: * @param layout the layout.
263: * @throws FormatDateTimeException if parsing fails.
264: * @return the parsed object.
265: */
266: public static DateTime parseDateTime(String input, String layout)
267: throws FormatDateTimeException {
268: DateTime out = null;
269: try {
270: out = DateTime.parse(input, layout);
271: return out;
272: } catch (ParseException e) {
273: try {
274: if (log.isDebugEnabled())
275: log
276: .debug(
277: "The input "
278: + input
279: + " could not be parsed into a DateTime using the layout "
280: + layout
281: + ". Will try to convert into a DateOnly using the default layout.",
282: e);
283: DateOnly date = parseDateOnly(input);
284: out = DateOnly.toDateTime(date);
285: return out;
286: } catch (FormatDateOnlyException e1) {
287: throw new FormatDateTimeException(
288: null,
289: new Object[] { (layout == null ? DateTimeFieldMetaData
290: .getDateTimeParse()[0]
291: : layout) }, e1);
292: }
293: }
294: }
295:
296: /** Returns a Currency object corresponding to the input String.
297: * It uses the default layout specified in the corrsponding meta class.
298: * @param input the input.
299: * @throws FormatCurrencyException if parsing fails.
300: * @return the parsed object.
301: */
302: public static Currency parseCurrency(String input)
303: throws FormatCurrencyException {
304: return parseCurrency(input, null);
305: }
306:
307: /** Returns a Currency object corresponding to the input String.
308: * @param input the input.
309: * @param layout the layout.
310: * @throws FormatCurrencyException if parsing fails.
311: * @return the parsed object.
312: */
313: public static Currency parseCurrency(String input, String layout)
314: throws FormatCurrencyException {
315: try {
316: Currency out = null;
317: if (input != null && input.length() > 0) {
318: if (layout == null)
319: layout = CurrencyFieldMetaData.getCurrencyFormat();
320: layout = LocaleHelper.determineLayout(layout);
321:
322: NumberFormat fmt;
323: if (LocaleContext.getLocale() != null)
324: fmt = NumberFormat
325: .getCurrencyInstance(LocaleContext
326: .getLocale());
327: else
328: fmt = NumberFormat.getCurrencyInstance();
329: if (layout != null && fmt instanceof DecimalFormat)
330: ((DecimalFormat) fmt).applyPattern(layout);
331: out = new Currency(fmt.parse(input).doubleValue());
332: }
333: return out;
334: } catch (ParseException e) {
335: throw new FormatCurrencyException(null, null, e);
336: }
337: }
338:
339: /** Just returns the String as is.
340: * @param input the input.
341: * @return the String as is.
342: */
343: public static String parseString(String input) {
344: // Note: there is no layout for a String... just return
345: return input;
346: }
347:
348: /** Converts the input into an array of bytes
349: * @param input the input.
350: * @return the input as an array of bytes.
351: */
352: public static byte[] parseRaw(String input) {
353: // Note: there is no layout for a Raw... just return
354: if (input == null) {
355: return null;
356: } else {
357: return input.getBytes();
358: }
359: }
360:
361: }
|