001: /*
002: This file is part of BORG.
003:
004: BORG is free software; you can redistribute it and/or modify
005: it under the terms of the GNU General Public License as published by
006: the Free Software Foundation; either version 2 of the License, or
007: (at your option) any later version.
008:
009: BORG is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with BORG; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: Copyright 2003 by Mike Berger
019: */
020: package net.sf.borg.model.beans;
021:
022: import java.text.SimpleDateFormat;
023: import java.util.Date;
024: import java.util.Vector;
025:
026: import net.sf.borg.common.VMap;
027: import net.sf.borg.common.XTree;
028:
029: /**
030: *
031: * @author mbb
032: */
033: // BeanXMLAdapters are used by BeanDB to convert KeyedBeans to and from Row objects
034: // or XML
035: abstract public class BeanXMLAdapter {
036:
037: abstract public XTree toXml(KeyedBean bean);
038:
039: abstract public KeyedBean fromXml(XTree xt);
040:
041: private static SimpleDateFormat normalDateFormat_ = new SimpleDateFormat(
042: "MM/dd/yy hh:mm aa");
043:
044: protected static String toString(Date d) {
045: return (normalDateFormat_.format(d));
046: }
047:
048: protected static String toString(boolean b) {
049: if (b)
050: return ("true");
051: return ("false");
052: }
053:
054: protected static Date toDate(String s) {
055:
056: try {
057: Date d = normalDateFormat_.parse(s);
058: return (d);
059: } catch (Exception e) {
060: return (null);
061: }
062: }
063:
064: protected static boolean toBoolean(String s) {
065: if (s.equals("true"))
066: return (true);
067: return (false);
068: }
069:
070: protected static String toString(Integer i) {
071: return (i.toString());
072: }
073:
074: protected static Integer toInteger(String s) {
075: Integer i = null;
076: try {
077: i = Integer.decode(s);
078: } catch (Exception e) {
079: return (null);
080: }
081: return (i);
082: }
083:
084: protected static int toInt(String s) {
085: int i = 0;
086: try {
087: i = Integer.parseInt(s);
088: } catch (Exception e) {
089: return (0);
090: }
091: return (i);
092: }
093:
094: // yuck
095: protected static String toString(Vector v) {
096: VMap vm = new VMap();
097: for (int i = 0; i < v.size(); i++)
098: vm.put(v.elementAt(i), "v");
099:
100: return (vm.passivate());
101: }
102:
103: protected static Vector toVector(String s) {
104: if (s == null || s.equals(""))
105: return (null);
106: VMap vm = new VMap();
107: vm.activate(s);
108: Vector v = new Vector(vm.keySet());
109:
110: return (v);
111: }
112: }
|