001: /**
002: * com.mckoi.database.global.ObjectTranslator 09 Feb 2001
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database.global;
024:
025: import java.util.Date;
026: import com.mckoi.util.BigNumber;
027: import java.io.*;
028:
029: /**
030: * This object compliments ObjectTransfer and provides a method to translate
031: * any object into a type the database engine can process.
032: *
033: * @author Tobias Downer
034: */
035:
036: public class ObjectTranslator {
037:
038: /**
039: * Translates the given object to a type the database can process.
040: */
041: public static Object translate(Object ob) {
042: if (ob == null) {
043: return null;
044: } else if (ob instanceof String) {
045: return StringObject.fromString((String) ob);
046: } else if (ob instanceof StringObject
047: || ob instanceof BigNumber || ob instanceof Date
048: || ob instanceof ByteLongObject
049: || ob instanceof Boolean
050: || ob instanceof StreamableObject) {
051: return ob;
052: } else if (ob instanceof byte[]) {
053: return new ByteLongObject((byte[]) ob);
054: } else if (ob instanceof Serializable) {
055: return serialize(ob);
056: } else {
057: // System.out.println("Ob is: (" + ob.getClass() + ") " + ob);
058: throw new Error("Unable to translate object. "
059: + "It is not a primitive type or serializable.");
060: }
061: }
062:
063: /**
064: * Serializes the Java object to a ByteLongObject.
065: */
066: public static ByteLongObject serialize(Object ob) {
067: try {
068: ByteArrayOutputStream bout = new ByteArrayOutputStream();
069: ObjectOutputStream ob_out = new ObjectOutputStream(bout);
070: ob_out.writeObject(ob);
071: ob_out.close();
072: return new ByteLongObject(bout.toByteArray());
073: } catch (IOException e) {
074: throw new Error("Serialization error: " + e.getMessage());
075: }
076: }
077:
078: /**
079: * Deserializes a ByteLongObject to a Java object.
080: */
081: public static Object deserialize(ByteLongObject blob) {
082: if (blob == null) {
083: return null;
084: } else {
085: try {
086: ByteArrayInputStream bin = new ByteArrayInputStream(
087: blob.getByteArray());
088: ObjectInputStream ob_in = new ObjectInputStream(bin);
089: Object ob = ob_in.readObject();
090: ob_in.close();
091: return ob;
092: } catch (ClassNotFoundException e) {
093: throw new Error("Class not found: " + e.getMessage());
094: } catch (IOException e) {
095: throw new Error("De-serialization error: "
096: + e.getMessage());
097: }
098: }
099: }
100:
101: }
|