summaryrefslogtreecommitdiff
path: root/orkbasej/java/net/sf/oreka/serializers/OrkSerializer.java
blob: f4b0d54eb94b92f7bea616a31621d524a8030ac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

package net.sf.oreka.serializers;

import net.sf.oreka.*;

/**
 * 
 * Base class for all serializers.
 * Serializers have to implement their own serialize and deserialize methods
 * since the data can be read from and written to many different formats
 * 
 * Enum and Boolean value are case insensitive (all enum values must be defined 
 * lower case in the project, otherwise they won't be recognized at all)
 *
 */
public abstract class OrkSerializer {
	
	boolean deserialize = false;
	String classKey = "class";
	
	public void setClassKey(String classKey)
	{
		this.classKey = classKey;
	}
	
	public int intValue(String key, int value, boolean required) throws OrkException
	{
		if (deserialize)
		{
			return getInt(key, value, required);
		}
		else
		{
			addInt(key, value);
			return value;
		}
	}

	public double doubleValue(String key, double value, boolean required) throws OrkException
	{
		if (deserialize)
		{
			return getDouble(key, value, required);
		}
		else
		{
			addDouble(key, value);
			return value;
		}		
	}
	
	public String stringValue(String key, String value, boolean required ) throws OrkException
	{
		if (deserialize)
		{
			return getString(key, value, required);
		}
		else
		{
			addString(key, value);
			return value;
		}
	}
	
	public boolean booleanValue(String key, boolean value, boolean required ) throws OrkException
	{
		if (deserialize)
		{
			return getBoolean(key, value, required);
		}
		else
		{
			addBoolean(key, value);
			return value;
		}
	}
	
	public Enum enumValue(String key, Enum value, boolean required ) throws OrkException
	{
		if (deserialize)
		{
			return getEnum(key, value, required);
		}
		else
		{
			addEnum(key, value);
			return value;
		}
	}
	
	public abstract OrkObject objectValue(String key, OrkObject value, boolean required ) throws OrkException;
	
	public abstract void addClassName(String value);
	
	void addInt(String key, int value)
	{
		String valueString = String.valueOf(value);
		addString(key, valueString);
	}
	
	void addDouble(String key, double value)
	{
		String valueString = String.valueOf(value);
		addString(key, valueString);
	}
	
	void addBoolean(String key, boolean value)
	{
		if(value)
		{
			addString(key, "true");
		}
		else
		{
			addString(key, "false");
		}
	}
	
	void addEnum(String key, Enum value)
	{
		addString(key, value.name());
	}
	
	abstract void addString(String key, String value);

	int getInt(String key, int oldValue, boolean required) throws OrkException
	{
		int value = 0;
		String stringValue = getString(key, null, required);
		
		if (stringValue == null)
		{
			value = oldValue;
		}
		else
		{
			try
			{
				value = Integer.valueOf(stringValue);
			}
			catch (Exception e)
			{
				throw (new OrkException("Serializer: value:" + stringValue + " not an int for key:" + key));
			}
		}
		return value;
	}
	
	double getDouble(String key, double oldValue, boolean required) throws OrkException
	{
		double value = 0.0;
		String stringValue = getString(key, null, required);
		
		if (stringValue == null)
		{
			value = oldValue;
		}
		else
		{
			try
			{
				value = Double.valueOf(stringValue);
			}
			catch (Exception e)
			{
				throw (new OrkException("Serializer: value:" + stringValue + " not a double for key:" + key));
			}
		}
		return value;
	}
	
	boolean getBoolean(String key, boolean oldValue, boolean required) throws OrkException
	{
		boolean value;
		String stringValue = getString(key, null, required);

		if (stringValue == null)
		{
			value = oldValue;
		}
		else
		{
			stringValue = stringValue.toLowerCase();
			
			if(	stringValue.equals("true") || stringValue.equals("yes") || stringValue.equals("1") )
			{
				value = true;
			}
			else if( stringValue.equals("false") || stringValue.equals("no") || stringValue.equals("0") )
			{
				value = false;
			}
			else
			{
				throw(new OrkException("Serializer: Invalid boolean value:" + stringValue + " for key:" + key));
			}
		}
		return value;
	}

	Enum getEnum(String key, Enum oldValue, boolean required) throws OrkException
	{
		Enum value = null;
		String stringValue = getString(key, null, required);

		if (stringValue == null)
		{
			value = oldValue;
		}
		else
		{
			stringValue = stringValue.toLowerCase();
			Class valueClass = oldValue.getClass();
			try
			{
				value = Enum.valueOf(valueClass, stringValue);
			}
			catch (IllegalArgumentException e)
			{
				throw (new OrkException("Serializer: Invalid enum value:" + stringValue + " for parameter:" + key));
			}
		}
		return value;
	}
	
	abstract String getString(String key, String oldValue, boolean required ) throws OrkException;
}