11 |
13 Sep 07 |
nicklas |
1 |
///////////////////////////////////////////////////////////////// |
11 |
13 Sep 07 |
nicklas |
2 |
// |
11 |
13 Sep 07 |
nicklas |
// Copyright (C) 2005 Affymetrix, Inc. |
11 |
13 Sep 07 |
nicklas |
4 |
// |
11 |
13 Sep 07 |
nicklas |
// This library is free software; you can redistribute it and/or modify |
11 |
13 Sep 07 |
nicklas |
// it under the terms of the GNU Lesser General Public License as published |
11 |
13 Sep 07 |
nicklas |
// by the Free Software Foundation; either version 2.1 of the License, |
11 |
13 Sep 07 |
nicklas |
// or (at your option) any later version. |
11 |
13 Sep 07 |
nicklas |
9 |
// |
11 |
13 Sep 07 |
nicklas |
// This library is distributed in the hope that it will be useful, but |
11 |
13 Sep 07 |
nicklas |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 |
13 Sep 07 |
nicklas |
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
11 |
13 Sep 07 |
nicklas |
// for more details. |
11 |
13 Sep 07 |
nicklas |
14 |
// |
11 |
13 Sep 07 |
nicklas |
// You should have received a copy of the GNU Lesser General Public License |
11 |
13 Sep 07 |
nicklas |
// along with this library; if not, write to the Free Software Foundation, Inc., |
11 |
13 Sep 07 |
nicklas |
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
11 |
13 Sep 07 |
nicklas |
18 |
// |
11 |
13 Sep 07 |
nicklas |
19 |
///////////////////////////////////////////////////////////////// |
11 |
13 Sep 07 |
nicklas |
20 |
|
11 |
13 Sep 07 |
nicklas |
21 |
package affymetrix.calvin.parameter; |
11 |
13 Sep 07 |
nicklas |
22 |
|
11 |
13 Sep 07 |
nicklas |
23 |
import affymetrix.portability.*; |
11 |
13 Sep 07 |
nicklas |
24 |
import java.nio.*; |
11 |
13 Sep 07 |
nicklas |
25 |
import java.lang.*; |
11 |
13 Sep 07 |
nicklas |
26 |
|
11 |
13 Sep 07 |
nicklas |
/** A class to hold a parameter name/value/type. |
11 |
13 Sep 07 |
nicklas |
* This class will convert several built-in types between the MIME string and their native types. |
11 |
13 Sep 07 |
nicklas |
29 |
*/ |
11 |
13 Sep 07 |
nicklas |
30 |
public class ParameterNameValue { |
11 |
13 Sep 07 |
nicklas |
31 |
|
11 |
13 Sep 07 |
nicklas |
/** an 8 bit integer. */ |
11 |
13 Sep 07 |
nicklas |
33 |
public static final int Int8Type = 0; |
11 |
13 Sep 07 |
nicklas |
34 |
|
11 |
13 Sep 07 |
nicklas |
/** an 8 bit unsigned integer. */ |
11 |
13 Sep 07 |
nicklas |
36 |
public static final int UInt8Type = 1; |
11 |
13 Sep 07 |
nicklas |
37 |
|
11 |
13 Sep 07 |
nicklas |
/** a 16 bit integer. */ |
11 |
13 Sep 07 |
nicklas |
39 |
public static final int Int16Type = 2; |
11 |
13 Sep 07 |
nicklas |
40 |
|
11 |
13 Sep 07 |
nicklas |
/** a 16 bit unsigned integer. */ |
11 |
13 Sep 07 |
nicklas |
42 |
public static final int UInt16Type = 3; |
11 |
13 Sep 07 |
nicklas |
43 |
|
11 |
13 Sep 07 |
nicklas |
/** a 32 bit integer. */ |
11 |
13 Sep 07 |
nicklas |
45 |
public static final int Int32Type = 4; |
11 |
13 Sep 07 |
nicklas |
46 |
|
11 |
13 Sep 07 |
nicklas |
/** a 32 bit unsigned integer. */ |
11 |
13 Sep 07 |
nicklas |
48 |
public static final int UInt32Type = 5; |
11 |
13 Sep 07 |
nicklas |
49 |
|
11 |
13 Sep 07 |
nicklas |
/** a 32 bit floating point. */ |
11 |
13 Sep 07 |
nicklas |
51 |
public static final int FloatType = 6; |
11 |
13 Sep 07 |
nicklas |
52 |
|
11 |
13 Sep 07 |
nicklas |
/** a 16 bit character. */ |
11 |
13 Sep 07 |
nicklas |
54 |
public static final int TextType = 7; |
11 |
13 Sep 07 |
nicklas |
55 |
|
11 |
13 Sep 07 |
nicklas |
/** an 8 bit character. */ |
11 |
13 Sep 07 |
nicklas |
57 |
public static final int AsciiType = 8; |
11 |
13 Sep 07 |
nicklas |
58 |
|
11 |
13 Sep 07 |
nicklas |
/** an 8 bit integer. */ |
11 |
13 Sep 07 |
nicklas |
60 |
public static final int UnknownType = 9; |
11 |
13 Sep 07 |
nicklas |
61 |
|
11 |
13 Sep 07 |
nicklas |
/** Creates a new instance of ParameterNameValue */ |
11 |
13 Sep 07 |
nicklas |
63 |
public ParameterNameValue() { |
11 |
13 Sep 07 |
nicklas |
64 |
name = ""; |
11 |
13 Sep 07 |
nicklas |
65 |
type = ""; |
11 |
13 Sep 07 |
nicklas |
66 |
value = null; |
11 |
13 Sep 07 |
nicklas |
67 |
} |
11 |
13 Sep 07 |
nicklas |
68 |
|
11 |
13 Sep 07 |
nicklas |
/** Constructor. Useful when reading in from a file |
11 |
13 Sep 07 |
nicklas |
* @param n Parameter name |
11 |
13 Sep 07 |
nicklas |
* @param mimeValue MIME encoded value in a buffer |
11 |
13 Sep 07 |
nicklas |
* @param mimeValueSize The size in bytes of the MIME encoded value. |
11 |
13 Sep 07 |
nicklas |
* @param mimeType The value type. |
11 |
13 Sep 07 |
nicklas |
74 |
*/ |
11 |
13 Sep 07 |
nicklas |
75 |
public ParameterNameValue(String n, byte[] mimeValue, int mimeValueSize, String mimeType) { |
11 |
13 Sep 07 |
nicklas |
76 |
name = n; |
11 |
13 Sep 07 |
nicklas |
77 |
value = new MIMEValue(); |
11 |
13 Sep 07 |
nicklas |
78 |
value.setValue(mimeValue, mimeValueSize); |
11 |
13 Sep 07 |
nicklas |
79 |
type = mimeType; |
11 |
13 Sep 07 |
nicklas |
80 |
} |
11 |
13 Sep 07 |
nicklas |
81 |
|
11 |
13 Sep 07 |
nicklas |
/** Constructor. Useful when reading in from a file |
11 |
13 Sep 07 |
nicklas |
* @param n Parameter name |
11 |
13 Sep 07 |
nicklas |
* @param mimeValue MIME encoded value |
11 |
13 Sep 07 |
nicklas |
* @param mimeType |
11 |
13 Sep 07 |
nicklas |
86 |
*/ |
11 |
13 Sep 07 |
nicklas |
87 |
public ParameterNameValue(String n, MIMEValue mimeValue, String mimeType) { |
11 |
13 Sep 07 |
nicklas |
88 |
name = n; |
11 |
13 Sep 07 |
nicklas |
89 |
value = mimeValue; |
11 |
13 Sep 07 |
nicklas |
90 |
type = mimeType; |
11 |
13 Sep 07 |
nicklas |
91 |
} |
11 |
13 Sep 07 |
nicklas |
92 |
|
11 |
13 Sep 07 |
nicklas |
/** A copy constructor. |
11 |
13 Sep 07 |
nicklas |
* @param param The parameter to copy. |
11 |
13 Sep 07 |
nicklas |
95 |
*/ |
11 |
13 Sep 07 |
nicklas |
96 |
public ParameterNameValue(ParameterNameValue param) { |
11 |
13 Sep 07 |
nicklas |
97 |
name = param.getName(); |
11 |
13 Sep 07 |
nicklas |
98 |
value = param.getMIMEValue(); |
11 |
13 Sep 07 |
nicklas |
99 |
type = param.getMIMEType(); |
11 |
13 Sep 07 |
nicklas |
100 |
} |
11 |
13 Sep 07 |
nicklas |
101 |
|
11 |
13 Sep 07 |
nicklas |
/** An equality operator. Compares the parameter name. |
11 |
13 Sep 07 |
nicklas |
* @param param The parameter to compare. |
11 |
13 Sep 07 |
nicklas |
* @return True if the parameter names are the same. |
11 |
13 Sep 07 |
nicklas |
105 |
*/ |
11 |
13 Sep 07 |
nicklas |
106 |
boolean equals(ParameterNameValue param) { |
11 |
13 Sep 07 |
nicklas |
107 |
return equals(param.getName()); |
11 |
13 Sep 07 |
nicklas |
108 |
} |
11 |
13 Sep 07 |
nicklas |
109 |
|
11 |
13 Sep 07 |
nicklas |
/** An equality operator. Compares the parameter name. |
11 |
13 Sep 07 |
nicklas |
* @param n A parameter name to compare. |
11 |
13 Sep 07 |
nicklas |
* @return True if the parameter names are the same. |
11 |
13 Sep 07 |
nicklas |
113 |
*/ |
11 |
13 Sep 07 |
nicklas |
114 |
public boolean equals(String n) { |
11 |
13 Sep 07 |
nicklas |
115 |
return (name.compareTo(n) == 0); |
11 |
13 Sep 07 |
nicklas |
116 |
} |
11 |
13 Sep 07 |
nicklas |
117 |
|
11 |
13 Sep 07 |
nicklas |
/** get the name of the parameter |
11 |
13 Sep 07 |
nicklas |
* @return The name of the parameter |
11 |
13 Sep 07 |
nicklas |
120 |
*/ |
11 |
13 Sep 07 |
nicklas |
121 |
public String getName() { return name; } |
11 |
13 Sep 07 |
nicklas |
122 |
|
11 |
13 Sep 07 |
nicklas |
/** set the name of the parameter. |
11 |
13 Sep 07 |
nicklas |
* @param v The name of the parameter. |
11 |
13 Sep 07 |
nicklas |
125 |
*/ |
11 |
13 Sep 07 |
nicklas |
126 |
public void setName( String v) { name = v; } |
11 |
13 Sep 07 |
nicklas |
127 |
|
11 |
13 Sep 07 |
nicklas |
/** get the parameter type |
11 |
13 Sep 07 |
nicklas |
* @return The parameter type of the object.*/ |
11 |
13 Sep 07 |
nicklas |
130 |
public int getParameterType() { |
11 |
13 Sep 07 |
nicklas |
131 |
|
11 |
13 Sep 07 |
nicklas |
/** Type table. Order matches the ParameterType enum */ |
11 |
13 Sep 07 |
nicklas |
133 |
final String[] typeTable = { |
11 |
13 Sep 07 |
nicklas |
134 |
MIMEValue.Int8MIMEType, |
11 |
13 Sep 07 |
nicklas |
135 |
MIMEValue.UInt8MIMEType, |
11 |
13 Sep 07 |
nicklas |
136 |
MIMEValue.Int16MIMEType, |
11 |
13 Sep 07 |
nicklas |
137 |
MIMEValue.UInt16MIMEType, |
11 |
13 Sep 07 |
nicklas |
138 |
MIMEValue.Int32MIMEType, |
11 |
13 Sep 07 |
nicklas |
139 |
MIMEValue.UInt32MIMEType, |
11 |
13 Sep 07 |
nicklas |
140 |
MIMEValue.FloatMIMEType, |
11 |
13 Sep 07 |
nicklas |
141 |
MIMEValue.TextMIMEType, |
11 |
13 Sep 07 |
nicklas |
142 |
MIMEValue.AsciiMIMEType |
11 |
13 Sep 07 |
nicklas |
143 |
}; |
11 |
13 Sep 07 |
nicklas |
144 |
|
11 |
13 Sep 07 |
nicklas |
145 |
for (int i=0; i<UnknownType; ++i) |
11 |
13 Sep 07 |
nicklas |
146 |
{ |
11 |
13 Sep 07 |
nicklas |
147 |
if (type.compareTo(typeTable[i]) == 0) |
11 |
13 Sep 07 |
nicklas |
148 |
return i; |
11 |
13 Sep 07 |
nicklas |
149 |
} |
11 |
13 Sep 07 |
nicklas |
150 |
return UnknownType; |
11 |
13 Sep 07 |
nicklas |
151 |
} |
11 |
13 Sep 07 |
nicklas |
152 |
|
11 |
13 Sep 07 |
nicklas |
/** gets value as a int8_t |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an int8_t |
11 |
13 Sep 07 |
nicklas |
155 |
*/ |
11 |
13 Sep 07 |
nicklas |
156 |
public byte getValueInt8() { return (byte)valueToInt(value); } |
11 |
13 Sep 07 |
nicklas |
157 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the value as an int8_t |
11 |
13 Sep 07 |
nicklas |
* @param v |
11 |
13 Sep 07 |
nicklas |
160 |
*/ |
11 |
13 Sep 07 |
nicklas |
161 |
public void setValueInt8(byte v) { |
11 |
13 Sep 07 |
nicklas |
162 |
value = new MIMEValue(); |
11 |
13 Sep 07 |
nicklas |
163 |
type = MIMEValue.Int8MIMEType; |
11 |
13 Sep 07 |
nicklas |
164 |
intToValue(v, value); |
11 |
13 Sep 07 |
nicklas |
165 |
} |
11 |
13 Sep 07 |
nicklas |
166 |
|
11 |
13 Sep 07 |
nicklas |
/** gets value as a byte |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an byte |
11 |
13 Sep 07 |
nicklas |
169 |
*/ |
11 |
13 Sep 07 |
nicklas |
170 |
public byte getValueUInt8() { return (byte)valueToInt(value); } |
11 |
13 Sep 07 |
nicklas |
171 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the value as a byte |
11 |
13 Sep 07 |
nicklas |
* @param v |
11 |
13 Sep 07 |
nicklas |
174 |
*/ |
11 |
13 Sep 07 |
nicklas |
175 |
public void setValueUInt8(byte v) { |
11 |
13 Sep 07 |
nicklas |
176 |
value = new MIMEValue(); |
11 |
13 Sep 07 |
nicklas |
177 |
type = MIMEValue.UInt8MIMEType; |
11 |
13 Sep 07 |
nicklas |
178 |
intToValue(v, value); |
11 |
13 Sep 07 |
nicklas |
179 |
} |
11 |
13 Sep 07 |
nicklas |
180 |
|
11 |
13 Sep 07 |
nicklas |
/** gets value as a int16_t |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an int16_t |
11 |
13 Sep 07 |
nicklas |
183 |
*/ |
11 |
13 Sep 07 |
nicklas |
184 |
public short getValueInt16() { return (short) valueToInt(value); } |
11 |
13 Sep 07 |
nicklas |
185 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the value as an int16_t |
11 |
13 Sep 07 |
nicklas |
* @param v |
11 |
13 Sep 07 |
nicklas |
188 |
*/ |
11 |
13 Sep 07 |
nicklas |
189 |
public void setValueInt16(short v) { |
11 |
13 Sep 07 |
nicklas |
190 |
value = new MIMEValue(); |
11 |
13 Sep 07 |
nicklas |
191 |
type = MIMEValue.Int16MIMEType; |
11 |
13 Sep 07 |
nicklas |
192 |
intToValue(v, value); |
11 |
13 Sep 07 |
nicklas |
193 |
} |
11 |
13 Sep 07 |
nicklas |
194 |
|
11 |
13 Sep 07 |
nicklas |
/** gets value as a u_int16_t |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an u_int16_t |
11 |
13 Sep 07 |
nicklas |
197 |
*/ |
11 |
13 Sep 07 |
nicklas |
198 |
public short getValueUInt16() { return (short) valueToInt(value); } |
11 |
13 Sep 07 |
nicklas |
199 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the value as a u_int16_t |
11 |
13 Sep 07 |
nicklas |
* @param v |
11 |
13 Sep 07 |
nicklas |
202 |
*/ |
11 |
13 Sep 07 |
nicklas |
203 |
public void setValueUInt16(short v) { |
11 |
13 Sep 07 |
nicklas |
204 |
value = new MIMEValue(); |
11 |
13 Sep 07 |
nicklas |
205 |
type = MIMEValue.UInt16MIMEType; |
11 |
13 Sep 07 |
nicklas |
206 |
intToValue(v, value); |
11 |
13 Sep 07 |
nicklas |
207 |
} |
11 |
13 Sep 07 |
nicklas |
208 |
|
11 |
13 Sep 07 |
nicklas |
/** gets value as a int |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an int |
11 |
13 Sep 07 |
nicklas |
211 |
*/ |
11 |
13 Sep 07 |
nicklas |
212 |
public int getValueInt32() { return (int) valueToInt(value); } |
11 |
13 Sep 07 |
nicklas |
213 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the value as an int |
11 |
13 Sep 07 |
nicklas |
* @param v |
11 |
13 Sep 07 |
nicklas |
216 |
*/ |
11 |
13 Sep 07 |
nicklas |
217 |
public void setValueInt32(int v) { |
11 |
13 Sep 07 |
nicklas |
218 |
value = new MIMEValue(); |
11 |
13 Sep 07 |
nicklas |
219 |
type = MIMEValue.Int32MIMEType; |
11 |
13 Sep 07 |
nicklas |
220 |
intToValue(v, value); |
11 |
13 Sep 07 |
nicklas |
221 |
} |
11 |
13 Sep 07 |
nicklas |
222 |
|
11 |
13 Sep 07 |
nicklas |
/** gets value as a int |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an int |
11 |
13 Sep 07 |
nicklas |
225 |
*/ |
11 |
13 Sep 07 |
nicklas |
226 |
public int getValueUInt32() { return (int) valueToInt(value); } |
11 |
13 Sep 07 |
nicklas |
227 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the value as a int |
11 |
13 Sep 07 |
nicklas |
* @param v |
11 |
13 Sep 07 |
nicklas |
230 |
*/ |
11 |
13 Sep 07 |
nicklas |
231 |
public void setValueUInt32(int v) { |
11 |
13 Sep 07 |
nicklas |
232 |
value = new MIMEValue(); |
11 |
13 Sep 07 |
nicklas |
233 |
type = MIMEValue.UInt32MIMEType; |
11 |
13 Sep 07 |
nicklas |
234 |
intToValue(v, value); |
11 |
13 Sep 07 |
nicklas |
235 |
} |
11 |
13 Sep 07 |
nicklas |
236 |
|
11 |
13 Sep 07 |
nicklas |
/** gets value as a float |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an float |
11 |
13 Sep 07 |
nicklas |
239 |
*/ |
11 |
13 Sep 07 |
nicklas |
240 |
public float getValueFloat() { |
11 |
13 Sep 07 |
nicklas |
241 |
int ival = valueToInt(value); |
11 |
13 Sep 07 |
nicklas |
242 |
return Float.intBitsToFloat(ival); |
11 |
13 Sep 07 |
nicklas |
243 |
} |
11 |
13 Sep 07 |
nicklas |
244 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the value as a float |
11 |
13 Sep 07 |
nicklas |
* @param v |
11 |
13 Sep 07 |
nicklas |
247 |
*/ |
11 |
13 Sep 07 |
nicklas |
248 |
public void setValueFloat(float v) { |
11 |
13 Sep 07 |
nicklas |
249 |
value = new MIMEValue(); |
11 |
13 Sep 07 |
nicklas |
250 |
type = MIMEValue.FloatMIMEType; |
11 |
13 Sep 07 |
nicklas |
251 |
intToValue(Float.floatToIntBits(v), value); |
11 |
13 Sep 07 |
nicklas |
252 |
} |
11 |
13 Sep 07 |
nicklas |
253 |
|
11 |
13 Sep 07 |
nicklas |
/** Gets the value as a wstring |
11 |
13 Sep 07 |
nicklas |
* @param val The MIME value object. |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an wstring |
11 |
13 Sep 07 |
nicklas |
257 |
*/ |
11 |
13 Sep 07 |
nicklas |
258 |
protected static String getTextFromValueObject(MIMEValue val) { |
11 |
13 Sep 07 |
nicklas |
259 |
byte[] b = val.getBytes(); |
11 |
13 Sep 07 |
nicklas |
260 |
int len = val.size()/DataSizes.SHORT_SIZE; |
11 |
13 Sep 07 |
nicklas |
261 |
String str = ""; |
11 |
13 Sep 07 |
nicklas |
262 |
for (int ichar=0, ibyte=0; ichar<len; ichar++, ibyte+=2) |
11 |
13 Sep 07 |
nicklas |
263 |
{ |
11 |
13 Sep 07 |
nicklas |
264 |
if (b[ibyte] == 0 && b[ibyte+1] == 0) |
11 |
13 Sep 07 |
nicklas |
265 |
continue; |
11 |
13 Sep 07 |
nicklas |
266 |
|
11 |
13 Sep 07 |
nicklas |
267 |
char c; |
11 |
13 Sep 07 |
nicklas |
268 |
if (b[ibyte] < 0) |
11 |
13 Sep 07 |
nicklas |
269 |
c = (char)((b[ibyte]+256) << 8); |
11 |
13 Sep 07 |
nicklas |
270 |
else |
11 |
13 Sep 07 |
nicklas |
271 |
c = (char)(b[ibyte] << 8); |
11 |
13 Sep 07 |
nicklas |
272 |
|
11 |
13 Sep 07 |
nicklas |
273 |
if (b[ibyte+1] < 0) |
11 |
13 Sep 07 |
nicklas |
274 |
c += (char)(b[ibyte+1] + 256); |
11 |
13 Sep 07 |
nicklas |
275 |
else |
11 |
13 Sep 07 |
nicklas |
276 |
c += (char)b[ibyte+1]; |
11 |
13 Sep 07 |
nicklas |
277 |
|
11 |
13 Sep 07 |
nicklas |
278 |
str += c; |
11 |
13 Sep 07 |
nicklas |
279 |
} |
11 |
13 Sep 07 |
nicklas |
280 |
return str.trim(); |
11 |
13 Sep 07 |
nicklas |
281 |
} |
11 |
13 Sep 07 |
nicklas |
282 |
|
11 |
13 Sep 07 |
nicklas |
/** gets value as a wstring |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an wstring |
11 |
13 Sep 07 |
nicklas |
285 |
*/ |
11 |
13 Sep 07 |
nicklas |
286 |
public String getValueText() { |
11 |
13 Sep 07 |
nicklas |
287 |
return getTextFromValueObject(value); |
11 |
13 Sep 07 |
nicklas |
288 |
} |
11 |
13 Sep 07 |
nicklas |
289 |
|
11 |
13 Sep 07 |
nicklas |
/** Sets the value as a text type. |
11 |
13 Sep 07 |
nicklas |
* @param str The string value. |
11 |
13 Sep 07 |
nicklas |
* @param val String representation of the value. |
11 |
13 Sep 07 |
nicklas |
293 |
*/ |
11 |
13 Sep 07 |
nicklas |
294 |
protected static void setValueObjectFromText(String str, MIMEValue val) { |
11 |
13 Sep 07 |
nicklas |
295 |
byte[] b = new byte[str.length()*DataSizes.SHORT_SIZE]; |
11 |
13 Sep 07 |
nicklas |
296 |
for (int ichar=0, ibyte=0; ichar<str.length(); ichar++, ibyte+=2) |
11 |
13 Sep 07 |
nicklas |
297 |
{ |
11 |
13 Sep 07 |
nicklas |
298 |
char c = str.charAt(ichar); |
11 |
13 Sep 07 |
nicklas |
299 |
b[ibyte] = (byte) (c >> 8); |
11 |
13 Sep 07 |
nicklas |
300 |
b[ibyte+1] = (byte) c; |
11 |
13 Sep 07 |
nicklas |
301 |
} |
11 |
13 Sep 07 |
nicklas |
302 |
val.setValue(b, str.length()*DataSizes.SHORT_SIZE); |
11 |
13 Sep 07 |
nicklas |
303 |
} |
11 |
13 Sep 07 |
nicklas |
304 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the value as a text type. |
11 |
13 Sep 07 |
nicklas |
* @param v String representation of the value. |
11 |
13 Sep 07 |
nicklas |
307 |
*/ |
11 |
13 Sep 07 |
nicklas |
308 |
public void setValueText(String v) { |
11 |
13 Sep 07 |
nicklas |
309 |
type = MIMEValue.TextMIMEType; |
11 |
13 Sep 07 |
nicklas |
310 |
value = new MIMEValue(); |
11 |
13 Sep 07 |
nicklas |
311 |
setValueObjectFromText(v, value); |
11 |
13 Sep 07 |
nicklas |
312 |
} |
11 |
13 Sep 07 |
nicklas |
313 |
|
11 |
13 Sep 07 |
nicklas |
/** gets value as a string |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an string |
11 |
13 Sep 07 |
nicklas |
316 |
*/ |
11 |
13 Sep 07 |
nicklas |
317 |
protected static String getAsciiFromValueObject(MIMEValue val) { |
11 |
13 Sep 07 |
nicklas |
318 |
byte[] b = val.getBytes(); |
11 |
13 Sep 07 |
nicklas |
319 |
int len = val.size(); |
11 |
13 Sep 07 |
nicklas |
320 |
String str = ""; |
11 |
13 Sep 07 |
nicklas |
321 |
for (int i = 0; i < len; ++i) |
11 |
13 Sep 07 |
nicklas |
322 |
{ |
11 |
13 Sep 07 |
nicklas |
323 |
str += (char)b[i]; |
11 |
13 Sep 07 |
nicklas |
324 |
} |
11 |
13 Sep 07 |
nicklas |
325 |
return str; |
11 |
13 Sep 07 |
nicklas |
326 |
} |
11 |
13 Sep 07 |
nicklas |
327 |
|
11 |
13 Sep 07 |
nicklas |
/** gets value as a string |
11 |
13 Sep 07 |
nicklas |
* @return Value of the parameter as an string |
11 |
13 Sep 07 |
nicklas |
330 |
*/ |
11 |
13 Sep 07 |
nicklas |
331 |
public String getValueAscii() { |
11 |
13 Sep 07 |
nicklas |
332 |
return getAsciiFromValueObject(value); |
11 |
13 Sep 07 |
nicklas |
333 |
} |
11 |
13 Sep 07 |
nicklas |
334 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the value as a text type. |
11 |
13 Sep 07 |
nicklas |
* @param str String representation of the value. |
11 |
13 Sep 07 |
nicklas |
337 |
*/ |
11 |
13 Sep 07 |
nicklas |
338 |
protected void setValueObjectFromAscii(String str, MIMEValue val) { |
11 |
13 Sep 07 |
nicklas |
339 |
int len = str.length(); |
11 |
13 Sep 07 |
nicklas |
340 |
byte[] buf = new byte[len]; |
11 |
13 Sep 07 |
nicklas |
341 |
for (int i=0; i<len; i++) |
11 |
13 Sep 07 |
nicklas |
342 |
{ |
11 |
13 Sep 07 |
nicklas |
343 |
buf[i] = (byte)str.charAt(i); |
11 |
13 Sep 07 |
nicklas |
344 |
} |
11 |
13 Sep 07 |
nicklas |
345 |
val.setValue(buf, len); |
11 |
13 Sep 07 |
nicklas |
346 |
} |
11 |
13 Sep 07 |
nicklas |
347 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the value as a text type. |
11 |
13 Sep 07 |
nicklas |
* @param v String representation of the value. |
11 |
13 Sep 07 |
nicklas |
350 |
*/ |
11 |
13 Sep 07 |
nicklas |
351 |
public void setValueAscii(String v) { |
11 |
13 Sep 07 |
nicklas |
352 |
type = MIMEValue.AsciiMIMEType; |
11 |
13 Sep 07 |
nicklas |
353 |
value = new MIMEValue(); |
11 |
13 Sep 07 |
nicklas |
354 |
setValueObjectFromAscii(v, value); |
11 |
13 Sep 07 |
nicklas |
355 |
} |
11 |
13 Sep 07 |
nicklas |
356 |
|
11 |
13 Sep 07 |
nicklas |
// Raw MIME methods |
11 |
13 Sep 07 |
nicklas |
/** Returns the mime type without interpretation. |
11 |
13 Sep 07 |
nicklas |
* @return String of the MIME type |
11 |
13 Sep 07 |
nicklas |
360 |
*/ |
11 |
13 Sep 07 |
nicklas |
361 |
public String getMIMEType() { return type; } |
11 |
13 Sep 07 |
nicklas |
362 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the mime type without attempting to interpret it. |
11 |
13 Sep 07 |
nicklas |
* @param v The mime type. |
11 |
13 Sep 07 |
nicklas |
365 |
*/ |
11 |
13 Sep 07 |
nicklas |
366 |
public void setMIMEType( String v) { type = v; } |
11 |
13 Sep 07 |
nicklas |
367 |
|
11 |
13 Sep 07 |
nicklas |
/** Returns the mime value without interpretation. |
11 |
13 Sep 07 |
nicklas |
* @return MIME encoded string. |
11 |
13 Sep 07 |
nicklas |
370 |
*/ |
11 |
13 Sep 07 |
nicklas |
371 |
public MIMEValue getMIMEValue() { return value; } |
11 |
13 Sep 07 |
nicklas |
372 |
|
11 |
13 Sep 07 |
nicklas |
/** sets the mime value without attempting to interpret it. |
11 |
13 Sep 07 |
nicklas |
* @param v MIME encoded string. |
11 |
13 Sep 07 |
nicklas |
375 |
*/ |
11 |
13 Sep 07 |
nicklas |
376 |
public void setMIMEValue( MIMEValue v) { value = v; } |
11 |
13 Sep 07 |
nicklas |
377 |
|
11 |
13 Sep 07 |
nicklas |
/** Converts known types to a string |
11 |
13 Sep 07 |
nicklas |
* @return A string representation of the value |
11 |
13 Sep 07 |
nicklas |
380 |
*/ |
11 |
13 Sep 07 |
nicklas |
381 |
public String toString() { |
11 |
13 Sep 07 |
nicklas |
382 |
String str = ""; |
11 |
13 Sep 07 |
nicklas |
383 |
switch(getParameterType()) |
11 |
13 Sep 07 |
nicklas |
384 |
{ |
11 |
13 Sep 07 |
nicklas |
385 |
case Int8Type: |
11 |
13 Sep 07 |
nicklas |
386 |
return String.valueOf(getValueInt8()); |
11 |
13 Sep 07 |
nicklas |
387 |
|
11 |
13 Sep 07 |
nicklas |
388 |
case Int16Type: |
11 |
13 Sep 07 |
nicklas |
389 |
return String.valueOf(getValueInt16()); |
11 |
13 Sep 07 |
nicklas |
390 |
|
11 |
13 Sep 07 |
nicklas |
391 |
case Int32Type: |
11 |
13 Sep 07 |
nicklas |
392 |
return String.valueOf(getValueInt32()); |
11 |
13 Sep 07 |
nicklas |
393 |
|
11 |
13 Sep 07 |
nicklas |
394 |
case UInt8Type: |
11 |
13 Sep 07 |
nicklas |
395 |
return String.valueOf(getValueUInt8()); |
11 |
13 Sep 07 |
nicklas |
396 |
|
11 |
13 Sep 07 |
nicklas |
397 |
case UInt16Type: |
11 |
13 Sep 07 |
nicklas |
398 |
return String.valueOf(getValueUInt16()); |
11 |
13 Sep 07 |
nicklas |
399 |
|
11 |
13 Sep 07 |
nicklas |
400 |
case UInt32Type: |
11 |
13 Sep 07 |
nicklas |
401 |
return String.valueOf(getValueUInt32()); |
11 |
13 Sep 07 |
nicklas |
402 |
|
11 |
13 Sep 07 |
nicklas |
403 |
case FloatType: |
11 |
13 Sep 07 |
nicklas |
404 |
return String.valueOf(getValueFloat()); |
11 |
13 Sep 07 |
nicklas |
405 |
|
11 |
13 Sep 07 |
nicklas |
406 |
case TextType: |
11 |
13 Sep 07 |
nicklas |
407 |
return getValueText(); |
11 |
13 Sep 07 |
nicklas |
408 |
|
11 |
13 Sep 07 |
nicklas |
409 |
case AsciiType: |
11 |
13 Sep 07 |
nicklas |
410 |
return getValueAscii(); |
11 |
13 Sep 07 |
nicklas |
411 |
} |
11 |
13 Sep 07 |
nicklas |
412 |
return null; |
11 |
13 Sep 07 |
nicklas |
413 |
} |
11 |
13 Sep 07 |
nicklas |
414 |
|
11 |
13 Sep 07 |
nicklas |
/** Converts a value to an integer. |
11 |
13 Sep 07 |
nicklas |
* @param val The mime representation. |
11 |
13 Sep 07 |
nicklas |
* @return The integer representation. |
11 |
13 Sep 07 |
nicklas |
418 |
*/ |
11 |
13 Sep 07 |
nicklas |
419 |
protected static int valueToInt(MIMEValue val) { |
11 |
13 Sep 07 |
nicklas |
420 |
int ival = 0; |
11 |
13 Sep 07 |
nicklas |
421 |
byte[] buf = val.getBytes(); |
11 |
13 Sep 07 |
nicklas |
422 |
if (buf[0] < 0) |
11 |
13 Sep 07 |
nicklas |
423 |
ival = ((buf[0]+256) << 24); |
11 |
13 Sep 07 |
nicklas |
424 |
else |
11 |
13 Sep 07 |
nicklas |
425 |
ival = (buf[0] << 24); |
11 |
13 Sep 07 |
nicklas |
426 |
|
11 |
13 Sep 07 |
nicklas |
427 |
if (buf[1] < 0) |
11 |
13 Sep 07 |
nicklas |
428 |
ival += ((buf[1]+256) << 16); |
11 |
13 Sep 07 |
nicklas |
429 |
else |
11 |
13 Sep 07 |
nicklas |
430 |
ival += (buf[1] << 16); |
11 |
13 Sep 07 |
nicklas |
431 |
|
11 |
13 Sep 07 |
nicklas |
432 |
if (buf[2] < 0) |
11 |
13 Sep 07 |
nicklas |
433 |
ival += ((buf[2]+256) << 8); |
11 |
13 Sep 07 |
nicklas |
434 |
else |
11 |
13 Sep 07 |
nicklas |
435 |
ival += (buf[2] << 8); |
11 |
13 Sep 07 |
nicklas |
436 |
|
11 |
13 Sep 07 |
nicklas |
437 |
if (buf[3] < 0) |
11 |
13 Sep 07 |
nicklas |
438 |
ival += (buf[3] + 256); |
11 |
13 Sep 07 |
nicklas |
439 |
else |
11 |
13 Sep 07 |
nicklas |
440 |
ival += buf[3]; |
11 |
13 Sep 07 |
nicklas |
441 |
return ival; |
11 |
13 Sep 07 |
nicklas |
442 |
} |
11 |
13 Sep 07 |
nicklas |
443 |
|
11 |
13 Sep 07 |
nicklas |
/** Converts an integer to a value. |
11 |
13 Sep 07 |
nicklas |
* @param v The integer representation. |
11 |
13 Sep 07 |
nicklas |
* @param val The value. |
11 |
13 Sep 07 |
nicklas |
447 |
*/ |
11 |
13 Sep 07 |
nicklas |
448 |
protected static void intToValue(int v, MIMEValue val) { |
11 |
13 Sep 07 |
nicklas |
449 |
byte[] buf = new byte[DataSizes.INT_SIZE]; |
11 |
13 Sep 07 |
nicklas |
450 |
buf[0] = (byte) (v >> 24); |
11 |
13 Sep 07 |
nicklas |
451 |
buf[1] = (byte) (v >> 16); |
11 |
13 Sep 07 |
nicklas |
452 |
buf[2] = (byte) (v >> 8); |
11 |
13 Sep 07 |
nicklas |
453 |
buf[3] = (byte) v; |
11 |
13 Sep 07 |
nicklas |
454 |
val.setValue(buf, DataSizes.INT_SIZE); |
11 |
13 Sep 07 |
nicklas |
455 |
} |
11 |
13 Sep 07 |
nicklas |
456 |
|
11 |
13 Sep 07 |
nicklas |
/** The name of the parameter */ |
11 |
13 Sep 07 |
nicklas |
458 |
protected String name; |
11 |
13 Sep 07 |
nicklas |
459 |
|
11 |
13 Sep 07 |
nicklas |
/** The MIME type of the parameter */ |
11 |
13 Sep 07 |
nicklas |
461 |
protected String type; |
11 |
13 Sep 07 |
nicklas |
462 |
|
11 |
13 Sep 07 |
nicklas |
/** The MIME value of the parameter */ |
11 |
13 Sep 07 |
nicklas |
464 |
protected MIMEValue value; |
11 |
13 Sep 07 |
nicklas |
465 |
} |