/[cvs]/nfo/php/libs/org.netfrag.elib/xml/wddx/wddx_serializer.library
ViewVC logotype

Contents of /nfo/php/libs/org.netfrag.elib/xml/wddx/wddx_serializer.library

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Wed Jan 23 17:40:38 2002 UTC (22 years, 6 months ago) by cvsjoko
Branch: nfo, MAIN
CVS Tags: v003, HEAD
Changes since 1.1: +0 -0 lines
initial

1 <?
2
3 function wddx_ext_serialize_singlevalue($sVal) {
4
5 if (!isset($sVal)) { return '<null/><null/>'; }
6
7 if (is_string($sVal)) { return '<string>' . $sVal . '</string>'; }
8 if (is_array($sVal)) { return '<struct>' . $sVal . '</struct>'; }
9
10 if (
11 is_long($sVal) || is_int($sVal) || is_integer($sVal) ||
12 is_float($sVal) || is_double($sVal) || is_real($sVal) || is_numeric($sVal)
13 ) { return '<number>' . $sVal . '</number>'; }
14
15 if (is_bool($sVal)) { return '<boolean>' . $sVal . '</boolean>'; }
16
17 }
18
19
20
21
22 ///////////////////////////////////////////////////////////////////////////
23 //
24 // WddxSerializer
25 //
26 ///////////////////////////////////////////////////////////////////////////
27
28 class WddxSerializer {
29
30 var $wddxPacket = '';
31
32
33 ///////////////////////////////////////////////////////////////////////////
34 // WddxSerializer() binds the function properties of the object
35 function WddxSerializer()
36 {
37 // Compatibility section
38 // if (navigator.appVersion != "" && navigator.appVersion.indexOf("MSIE 3.") == -1)
39 if (1==1)
40 {
41 // Character encoding table
42
43 // Encoding table for strings (CDATA)
44 $et = array();
45
46 // Numbers to characters table and
47 // characters to numbers table
48 $n2c = array();
49 $c2n = array();
50
51 // Encoding table for attributes (i.e. var=str)
52 $at = array();
53
54 for ($i = 0; $i < 256; ++$i)
55 {
56 // Build a character from octal code
57 // $d1 = Math.floor(i/64);
58 $d1 = intval($i/64);
59 // $d2 = Math.floor((i%64)/8);
60 $d2 = intval(($i%64)/8);
61 $d3 = $i%8;
62 // $c = eval("\"\\" + d1.toString(10) + d2.toString(10) + d3.toString(10) + "\"");
63 // $c = eval("\"\\" . $d1 . $d2 . $d3 . "\"");
64 $c = $d1 . $d2 . $d3;
65
66 // Modify character-code conversion tables
67 $n2c[$i] = $c;
68 $c2n[$c] = $i;
69
70 // Modify encoding table
71 if ($i < 32 && $i != 9 && $i != 10 && $i != 13)
72 {
73 // Control characters that are not tabs, newlines, and carriage returns
74
75 // Create a two-character hex code representation
76 $hex = $i;
77 if (strlen($hex) == 1)
78 {
79 $hex = "0" . $hex;
80 }
81
82 $et[$n2c[$i]] = "<char code='" + $hex + "'/>";
83
84 // strip control chars from inside attrs
85 $at[$n2c[$i]] = "";
86
87 }
88 else if ($i < 128)
89 {
90 // Low characters that are not special control characters
91 $et[$n2c[$i]] = $n2c[$i];
92
93 // attr table
94 $at[$n2c[$i]] = $n2c[$i];
95 }
96 else
97 {
98 // High characters
99 $et[$n2c[$i]] = "&#x" . $i . ";";
100 $at[$n2c[$i]] = "&#x" . $i . ";";
101 }
102 }
103
104 // Special escapes for CDATA encoding
105 $et["<"] = "&lt;";
106 $et[">"] = "&gt;";
107 $et["&"] = "&amp;";
108
109 // Special escapes for attr encoding
110 $at["<"] = "&lt;";
111 $at[">"] = "&gt;";
112 $at["&"] = "&amp;";
113 $at["'"] = "&apos;";
114 $at["\""] = "&quot;";
115
116 // Store tables
117 $this->n2c = $n2c;
118 $this->c2n = $c2n;
119 $this->et = $et;
120 $this->at = $at;
121
122 // The browser is not MSIE 3.x
123 // $this->serializeString = &wddxSerializer_serializeString();
124 // $this->serializeAttr = &wddxSerializer_serializeAttr();
125 }
126 else
127 {
128 /*
129 // The browser is most likely MSIE 3.x, it is NS 2.0 compatible
130 $this->serializeString = wddxSerializer_serializeStringOld;
131 $this->serializeAttr = wddxSerializer_serializeAttrOld;
132 */
133 }
134
135 /*
136 // Setup timezone information
137
138 $tzOffset = (new Date()).getTimezoneOffset();
139
140 // Invert timezone offset to convert local time to UTC time
141 if (tzOffset >= 0)
142 {
143 this.timezoneString = '-';
144 }
145 else
146 {
147 this.timezoneString = '+';
148 }
149 this.timezoneString += Math.floor(Math.abs(tzOffset) / 60) + ":" + (Math.abs(tzOffset) % 60);
150 */
151
152 // Common properties
153 $this->preserveVarCase = false;
154 $this->useTimezoneInfo = true;
155
156 /*
157 // Common functions
158 $this->serialize = wddxSerializer_serialize;
159 $this->serializeValue = wddxSerializer_serializeValue;
160 $this->serializeVariable = wddxSerializer_serializeVariable;
161 $this->write = wddxSerializer_write;
162 */
163
164 }
165
166
167 ///////////////////////////////////////////////////////////////////////////
168 // serializeValue() serializes any value that can be serialized
169 // returns true/false
170 function serializeValue($obj)
171 {
172
173 $this->write(wddx_ext_serialize_singlevalue($obj));
174
175 $bSuccess = true;
176
177 /*
178 var bSuccess = true;
179 var val;
180
181 if (obj == null)
182 {
183 // Null value
184 this.write("<null/>");
185 }
186 else if (typeof(val = obj.valueOf()) == "string")
187 {
188 // String value
189 this.serializeString(val);
190 }
191 else if (typeof(val = obj.valueOf()) == "number")
192 {
193 // Distinguish between numbers and date-time values
194
195 if (
196 typeof(obj.getTimezoneOffset) == "function" &&
197 typeof(obj.toGMTString) == "function")
198 {
199 // Possible Date
200 // Note: getYear() fix is from David Flanagan's
201 // "JS: The Definitive Guide". This code is Y2K safe.
202 this.write("<dateTime>" +
203 (obj.getYear() < 1000 ? 1900+obj.getYear() : obj.getYear()) + "-" + (obj.getMonth() + 1) + "-" + obj.getDate() +
204 "T" + obj.getHours() + ":" + obj.getMinutes() + ":" + obj.getSeconds());
205 if (this.useTimezoneInfo)
206 {
207 this.write(this.timezoneString);
208 }
209 this.write("</dateTime>");
210 }
211 else
212 {
213 // Number value
214 this.write("<number>" + val + "</number>");
215 }
216 }
217 else if (typeof(val = obj.valueOf()) == "boolean")
218 {
219 // Boolean value
220 this.write("<boolean value='" + val + "'/>");
221 }
222 else if (typeof(obj) == "object")
223 {
224 if (typeof(obj.wddxSerialize) == "function")
225 {
226 // Object knows how to serialize itself
227 bSuccess = obj.wddxSerialize(this);
228 }
229 else if (
230 typeof(obj.join) == "function" &&
231 typeof(obj.reverse) == "function" &&
232 typeof(obj.sort) == "function" &&
233 typeof(obj.length) == "number")
234 {
235 // Possible Array
236 this.write("<array length='" + obj.length + "'>");
237 for (var i = 0; bSuccess && i < obj.length; ++i)
238 {
239 bSuccess = this.serializeValue(obj[i]);
240 }
241 this.write("</array>");
242 }
243 else
244 {
245 // Some generic object; treat it as a structure
246
247 // Use the wddxSerializationType property as a guide as to its type
248 if (typeof(obj.wddxSerializationType) == 'string')
249 {
250 this.write('<struct type="'+ obj.wddxSerializationType +'">')
251 }
252 else
253 {
254 this.write("<struct>");
255 }
256
257 for (var prop in obj)
258 {
259 if (prop != 'wddxSerializationType')
260 {
261 bSuccess = this.serializeVariable(prop, obj[prop]);
262 if (! bSuccess)
263 {
264 break;
265 }
266 }
267 }
268
269 this.write("</struct>");
270 }
271 }
272 else
273 {
274 // Error: undefined values or functions
275 bSuccess = false;
276 }
277 */
278
279 // Successful serialization
280 return $bSuccess;
281 }
282
283
284 /*
285 ///////////////////////////////////////////////////////////////////////////
286 // serializeAttr() serializes an attribute (such as a var tag) using JavaScript
287 // functionality available in NS 3.0 and above
288 function wddxSerializer_serializeAttr(s)
289 {
290 for (var i = 0; i < s.length; ++i)
291 {
292 this.write(this.at[s.charAt(i)]);
293 }
294 }
295
296
297 ///////////////////////////////////////////////////////////////////////////
298 // serializeAttrOld() serializes a string using JavaScript functionality
299 // available in IE 3.0. We don't support special characters for IE3, so
300 // just throw the unencoded text and hope for the best
301 function wddxSerializer_serializeAttrOld(s)
302 {
303 this.write(s);
304 }
305 */
306
307 ///////////////////////////////////////////////////////////////////////////
308 // serializeString() serializes a string using JavaScript functionality
309 // available in NS 3.0 and above
310 function wddxSerializer_serializeString($s)
311 {
312 $this->write("<string>");
313 for ($i = 0; $i < strlen($s); ++$i)
314 {
315 $this->write($this->et[substr($s, $i, 1)]);
316 }
317 $this->write("</string>");
318 }
319
320
321
322 ///////////////////////////////////////////////////////////////////////////
323 // write() appends text to the wddxPacket buffer
324 function write($str)
325 {
326 $this->wddxPacket .= $str;
327 }
328
329
330
331 ///////////////////////////////////////////////////////////////////////////
332 // serialize() creates a WDDX packet for a given object
333 // returns the packet on success or null on failure
334 function serialize($rootObj)
335 {
336 $this->wddxPacket = "";
337
338 $this->write("<wddxPacket version='1.0'><header/><data>");
339 $bSuccess = $this->serializeValue($rootObj);
340 $this->write("</data></wddxPacket>");
341
342 // return $this->wddxPacket;
343
344 if ($bSuccess)
345 {
346 return $this->wddxPacket;
347 }
348 else
349 {
350 return null;
351 }
352
353 }
354
355
356 ///////////////////////////////////////////////////////////////////////////
357 // envelope() creates a WDDX envelope around a given string
358 // returns the packet on success or null on failure
359 function packEnvelope()
360 {
361 $this->wddxPacket =
362 "<wddxPacket version='1.0'><header/><data>" .
363 $this->wddxPacket .
364 "</data></wddxPacket>";
365
366 return $this->wddxPacket;
367
368 }
369
370 }
371
372
373 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed