KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > global > ObjectTranslator


1 /**
2  * com.mckoi.database.global.ObjectTranslator 09 Feb 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.global;
26
27 import java.util.Date JavaDoc;
28 import com.mckoi.util.BigNumber;
29 import java.io.*;
30
31 /**
32  * This object compliments ObjectTransfer and provides a method to translate
33  * any object into a type the database engine can process.
34  *
35  * @author Tobias Downer
36  */

37
38 public class ObjectTranslator {
39
40   /**
41    * Translates the given object to a type the database can process.
42    */

43   public static Object JavaDoc translate(Object JavaDoc ob) {
44     if (ob == null) {
45       return null;
46     }
47     else if (ob instanceof String JavaDoc) {
48       return StringObject.fromString((String JavaDoc) ob);
49     }
50     else if (ob instanceof StringObject ||
51              ob instanceof BigNumber ||
52              ob instanceof Date JavaDoc ||
53              ob instanceof ByteLongObject ||
54              ob instanceof Boolean JavaDoc ||
55              ob instanceof StreamableObject) {
56       return ob;
57     }
58     else if (ob instanceof byte[]) {
59       return new ByteLongObject((byte[]) ob);
60     }
61     else if (ob instanceof Serializable) {
62       return serialize(ob);
63     }
64     else {
65 // System.out.println("Ob is: (" + ob.getClass() + ") " + ob);
66
throw new Error JavaDoc("Unable to translate object. " +
67                       "It is not a primitive type or serializable.");
68     }
69   }
70
71   /**
72    * Serializes the Java object to a ByteLongObject.
73    */

74   public static ByteLongObject serialize(Object JavaDoc ob) {
75     try {
76       ByteArrayOutputStream bout = new ByteArrayOutputStream();
77       ObjectOutputStream ob_out = new ObjectOutputStream(bout);
78       ob_out.writeObject(ob);
79       ob_out.close();
80       return new ByteLongObject(bout.toByteArray());
81     }
82     catch (IOException e) {
83       throw new Error JavaDoc("Serialization error: " + e.getMessage());
84     }
85   }
86
87   /**
88    * Deserializes a ByteLongObject to a Java object.
89    */

90   public static Object JavaDoc deserialize(ByteLongObject blob) {
91     if (blob == null) {
92       return null;
93     }
94     else {
95       try {
96         ByteArrayInputStream bin =
97                               new ByteArrayInputStream(blob.getByteArray());
98         ObjectInputStream ob_in = new ObjectInputStream(bin);
99         Object JavaDoc ob = ob_in.readObject();
100         ob_in.close();
101         return ob;
102       }
103       catch (ClassNotFoundException JavaDoc e) {
104         throw new Error JavaDoc("Class not found: " + e.getMessage());
105       }
106       catch (IOException e) {
107         throw new Error JavaDoc("De-serialization error: " + e.getMessage());
108       }
109     }
110   }
111
112 }
113
Popular Tags