Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This class is used to encode and decode binary elements ( blog, type/value pairs) |
| 3 | * |
| 4 | * @author: ucla-cs |
| 5 | */ |
| 6 | |
| 7 | var XML_EXT = 0x00; |
| 8 | |
| 9 | var XML_TAG = 0x01; |
| 10 | |
| 11 | var XML_DTAG = 0x02; |
| 12 | |
| 13 | var XML_ATTR = 0x03; |
| 14 | |
| 15 | var XML_DATTR = 0x04; |
| 16 | |
| 17 | var XML_BLOB = 0x05; |
| 18 | |
| 19 | var XML_UDATA = 0x06; |
| 20 | |
| 21 | var XML_CLOSE = 0x0; |
| 22 | |
| 23 | var XML_SUBTYPE_PROCESSING_INSTRUCTIONS = 16; |
| 24 | |
| 25 | |
| 26 | var XML_TT_BITS = 3; |
| 27 | var XML_TT_MASK = ((1 << XML_TT_BITS) - 1); |
| 28 | var XML_TT_VAL_BITS = XML_TT_BITS + 1; |
| 29 | var XML_TT_VAL_MASK = ((1 << (XML_TT_VAL_BITS)) - 1); |
| 30 | var XML_REG_VAL_BITS = 7; |
| 31 | var XML_REG_VAL_MASK = ((1 << XML_REG_VAL_BITS) - 1); |
| 32 | var XML_TT_NO_MORE = (1 << XML_REG_VAL_BITS); // 0x80 |
| 33 | var BYTE_MASK = 0xFF; |
| 34 | var LONG_BYTES = 8; |
| 35 | var LONG_BITS = 64; |
| 36 | |
| 37 | var bits_11 = 0x0000007FF; |
| 38 | var bits_18 = 0x00003FFFF; |
| 39 | var bits_32 = 0x0FFFFFFFF; |
| 40 | |
| 41 | |
| 42 | |
| 43 | //returns a string |
| 44 | tagToString = function(/*long*/ tagVal) { |
| 45 | if ((tagVal >= 0) && (tagVal < CCNProtocolDTagsStrings.length)) { |
| 46 | return CCNProtocolDTagsStrings[tagVal]; |
| 47 | } else if (tagVal == CCNProtocolDTags.CCNProtocolDataUnit) { |
| 48 | return CCNProtocolDTags.CCNPROTOCOL_DATA_UNIT; |
| 49 | } |
| 50 | return null; |
| 51 | }; |
| 52 | |
| 53 | //returns a Long |
| 54 | stringToTag = function(/*String*/ tagName) { |
| 55 | // the slow way, but right now we don't care.... want a static lookup for the forward direction |
| 56 | for (var i=0; i < CCNProtocolDTagsStrings.length; ++i) { |
| 57 | if ((null != CCNProtocolDTagsStrings[i]) && (CCNProtocolDTagsStrings[i] == tagName)) { |
| 58 | return i; |
| 59 | } |
| 60 | } |
| 61 | if (CCNProtocolDTags.CCNPROTOCOL_DATA_UNIT == tagName) { |
| 62 | return CCNProtocolDTags.CCNProtocolDataUnit; |
| 63 | } |
| 64 | return null; |
| 65 | }; |
| 66 | |
| 67 | //console.log(stringToTag(64)); |
| 68 | var BinaryXMLDecoder = function BinaryXMLDecoder(istream){ |
| 69 | var MARK_LEN=512; |
| 70 | var DEBUG_MAX_LEN = 32768; |
| 71 | |
| 72 | this.istream = istream; |
| 73 | //console.log('istream is '+ this.istream); |
| 74 | this.offset = 0; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | BinaryXMLDecoder.prototype.readStartElement =function( |
| 79 | //String |
| 80 | startTag, |
| 81 | //TreeMap<String, String> |
| 82 | attributes) { |
| 83 | |
| 84 | try { |
| 85 | //this.TypeAndVal |
| 86 | tv = this.decodeTypeAndVal(this.istream); |
| 87 | |
| 88 | if (null == tv) { |
| 89 | throw new Exception("Expected start element: " + startTag + " got something not a tag."); |
| 90 | } |
| 91 | |
| 92 | //String |
| 93 | decodedTag = null; |
| 94 | |
| 95 | if (tv.type() == XML_TAG) { |
| 96 | |
| 97 | decodedTag = this.decodeUString(this.Istream, tv.val()+1); |
| 98 | |
| 99 | } else if (tv.type() == XML_DTAG) { |
| 100 | decodedTag = tagToString(tv.val()); |
| 101 | } |
| 102 | |
| 103 | if ((null == decodedTag) || decodedTag != startTag) { |
| 104 | throw new Exception("Expected start element: " + startTag + " got: " + decodedTag + "(" + tv.val() + ")"); |
| 105 | } |
| 106 | |
| 107 | if (null != attributes) { |
| 108 | readAttributes(attributes); |
| 109 | } |
| 110 | |
| 111 | } catch (e) { |
| 112 | throw new Exception("readStartElement", e); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | BinaryXMLDecoder.prototype.readAttributes = function( |
| 117 | //TreeMap<String,String> |
| 118 | attributes){ |
| 119 | |
| 120 | if (null == attributes) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | try { |
| 125 | |
| 126 | //this.TypeAndVal |
| 127 | nextTV = this.peekTypeAndVal(this.istream); |
| 128 | |
| 129 | while ((null != nextTV) && ((XML_ATTR == nextTV.type()) || |
| 130 | (XML_DATTR == nextTV.type()))) { |
| 131 | |
| 132 | //this.TypeAndVal |
| 133 | thisTV = this.decodeTypeAndVal(this.Istream); |
| 134 | |
| 135 | var attributeName = null; |
| 136 | if (XML_ATTR == thisTV.type()) { |
| 137 | |
| 138 | attributeName = this.decodeUString(this.istream, thisTV.val()+1); |
| 139 | |
| 140 | } else if (XML_DATTR == thisTV.type()) { |
| 141 | // DKS TODO are attributes same or different dictionary? |
| 142 | attributeName = tagToString(thisTV.val()); |
| 143 | if (null == attributeName) { |
| 144 | throw new ContentDecodingException("Unknown DATTR value" + thisTV.val()); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | var attributeValue = this.decodeUString(this.istream); |
| 149 | |
| 150 | attributes.put(attributeName, attributeValue); |
| 151 | |
| 152 | nextTV = this.peekTypeAndVal(this.istream); |
| 153 | } |
| 154 | |
| 155 | } catch ( e) { |
| 156 | |
| 157 | throw new ContentDecodingException("readStartElement", e); |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | |
| 162 | BinaryXMLDecoder.prototype.initializeDecoding = function() { |
| 163 | //if (!this.istream.markSupported()) { |
| 164 | //throw new IllegalArgumentException(this.getClass().getName() + ": input stream must support marking!"); |
| 165 | //} |
| 166 | } |
| 167 | |
| 168 | BinaryXMLDecoder.prototype.readStartDocument = function(){ |
| 169 | // Currently no start document in binary encoding. |
| 170 | } |
| 171 | |
| 172 | BinaryXMLDecoder.prototype.readEndDocument = function() { |
| 173 | // Currently no end document in binary encoding. |
| 174 | }; |
| 175 | |
| 176 | BinaryXMLDecoder.prototype.readStartElement = function( |
| 177 | //String |
| 178 | startTag, |
| 179 | //TreeMap<String, String> |
| 180 | attributes) { |
| 181 | |
| 182 | |
| 183 | //NOT SURE |
| 184 | //if(typeof startTag == 'number') |
| 185 | //startTag = tagToString(startTag); |
| 186 | |
| 187 | //try { |
| 188 | //TypeAndVal |
| 189 | tv = this.decodeTypeAndVal(this.istream); |
| 190 | |
| 191 | if (null == tv) { |
| 192 | throw new Exception("Expected start element: " + startTag + " got something not a tag."); |
| 193 | } |
| 194 | |
| 195 | //String |
| 196 | decodedTag = null; |
| 197 | //console.log(tv); |
| 198 | //console.log(typeof tv); |
| 199 | |
| 200 | //console.log(XML_TAG); |
| 201 | if (tv.type() == XML_TAG) { |
| 202 | //console.log('got here'); |
| 203 | //Log.info(Log.FAC_ENCODING, "Unexpected: got tag in readStartElement; looking for tag " + startTag + " got length: " + (int)tv.val()+1); |
| 204 | // Tag value represents length-1 as tags can never be empty. |
| 205 | var valval ; |
| 206 | if(typeof tv.val() == 'string'){ |
| 207 | valval = (parseInt(tv.val())) + 1; |
| 208 | } |
| 209 | else |
| 210 | valval = (tv.val())+ 1; |
| 211 | |
| 212 | //console.log('valval is ' +valval); |
| 213 | |
| 214 | decodedTag = this.decodeUString(this.istream, valval); |
| 215 | |
| 216 | } else if (tv.type() == XML_DTAG) { |
| 217 | //console.log('gothere'); |
| 218 | //console.log(tv.val()); |
| 219 | //decodedTag = tagToString(tv.val()); |
| 220 | //console.log() |
| 221 | decodedTag = tv.val(); |
| 222 | } |
| 223 | |
| 224 | //console.log(decodedTag); |
| 225 | //console.log('startTag is '+startTag); |
| 226 | |
| 227 | |
| 228 | if ((null == decodedTag) || decodedTag != startTag ) { |
| 229 | console.log('expecting '+ startag + ' but got '+ decodedTag); |
| 230 | throw new Exception("Expected start element: " + startTag + " got: " + decodedTag + "(" + tv.val() + ")"); |
| 231 | } |
| 232 | |
| 233 | // DKS: does not read attributes out of stream if caller doesn't |
| 234 | // ask for them. Should possibly peek and skip over them regardless. |
| 235 | // TODO: fix this |
| 236 | if (null != attributes) { |
| 237 | readAttributes(attributes); |
| 238 | } |
| 239 | |
| 240 | //} catch ( e) { |
| 241 | //console.log(e); |
| 242 | //throw new Exception("readStartElement", e); |
| 243 | //} |
| 244 | } |
| 245 | |
| 246 | |
| 247 | BinaryXMLDecoder.prototype.readAttributes = function( |
| 248 | //TreeMap<String,String> |
| 249 | attributes) { |
| 250 | |
| 251 | if (null == attributes) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | try { |
| 256 | // Now need to get attributes. |
| 257 | //TypeAndVal |
| 258 | nextTV = this.peekTypeAndVal(this.istream); |
| 259 | |
| 260 | while ((null != nextTV) && ((XML_ATTR == nextTV.type()) || |
| 261 | (XML_DATTR == nextTV.type()))) { |
| 262 | |
| 263 | // Decode this attribute. First, really read the type and value. |
| 264 | //this.TypeAndVal |
| 265 | thisTV = this.decodeTypeAndVal(this.istream); |
| 266 | |
| 267 | //String |
| 268 | attributeName = null; |
| 269 | if (XML_ATTR == thisTV.type()) { |
| 270 | // Tag value represents length-1 as attribute names cannot be empty. |
| 271 | var valval ; |
| 272 | if(typeof tv.val() == 'string'){ |
| 273 | valval = (parseInt(tv.val())) + 1; |
| 274 | } |
| 275 | else |
| 276 | valval = (tv.val())+ 1; |
| 277 | |
| 278 | attributeName = this.decodeUString(this.istream,valval); |
| 279 | |
| 280 | } else if (XML_DATTR == thisTV.type()) { |
| 281 | // DKS TODO are attributes same or different dictionary? |
| 282 | attributeName = tagToString(thisTV.val()); |
| 283 | if (null == attributeName) { |
| 284 | throw new Exception("Unknown DATTR value" + thisTV.val()); |
| 285 | } |
| 286 | } |
| 287 | // Attribute values are always UDATA |
| 288 | //String |
| 289 | attributeValue = this.decodeUString(this.istream); |
| 290 | |
| 291 | // |
| 292 | attributes.push([attributeName, attributeValue]); |
| 293 | |
| 294 | nextTV = this.peekTypeAndVal(this.istream); |
| 295 | } |
| 296 | |
| 297 | } catch ( e) { |
| 298 | Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e); |
| 299 | throw new Exception("readStartElement", e); |
| 300 | } |
| 301 | }; |
| 302 | |
| 303 | //returns a string |
| 304 | BinaryXMLDecoder.prototype.peekStartElementAsString = function() { |
| 305 | //this.istream.mark(MARK_LEN); |
| 306 | |
| 307 | //String |
| 308 | decodedTag = null; |
| 309 | var previousOffset = this.offset; |
| 310 | try { |
| 311 | // Have to distinguish genuine errors from wrong tags. Could either use |
| 312 | // a special exception subtype, or redo the work here. |
| 313 | //this.TypeAndVal |
| 314 | tv = this.decodeTypeAndVal(this.istream); |
| 315 | |
| 316 | if (null != tv) { |
| 317 | |
| 318 | if (tv.type() == XML_TAG) { |
| 319 | /*if (tv.val()+1 > DEBUG_MAX_LEN) { |
| 320 | throw new ContentDecodingException("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!"); |
| 321 | }*/ |
| 322 | |
| 323 | // Tag value represents length-1 as tags can never be empty. |
| 324 | var valval ; |
| 325 | if(typeof tv.val() == 'string'){ |
| 326 | valval = (parseInt(tv.val())) + 1; |
| 327 | } |
| 328 | else |
| 329 | valval = (tv.val())+ 1; |
| 330 | |
| 331 | decodedTag = this.decodeUString(this.istream, valval); |
| 332 | |
| 333 | //Log.info(Log.FAC_ENCODING, "Unexpected: got text tag in peekStartElement; length: " + valval + " decoded tag = " + decodedTag); |
| 334 | |
| 335 | } else if (tv.type() == XML_DTAG) { |
| 336 | decodedTag = tagToString(tv.val()); |
| 337 | } |
| 338 | |
| 339 | } // else, not a type and val, probably an end element. rewind and return false. |
| 340 | |
| 341 | } catch ( e) { |
| 342 | |
| 343 | } finally { |
| 344 | try { |
| 345 | this.offset = previousOffset; |
| 346 | } catch ( e) { |
| 347 | Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e); |
| 348 | throw new ContentDecodingException("Cannot reset stream! " + e.getMessage(), e); |
| 349 | } |
| 350 | } |
| 351 | return decodedTag; |
| 352 | }; |
| 353 | |
| 354 | BinaryXMLDecoder.prototype.peekStartElement = function( |
| 355 | //String |
| 356 | startTag) { |
| 357 | //String |
| 358 | if(typeof startTag == 'string'){ |
| 359 | decodedTag = this.peekStartElementAsString(); |
| 360 | |
| 361 | if ((null != decodedTag) && decodedTag == startTag) { |
| 362 | return true; |
| 363 | } |
| 364 | return false; |
| 365 | } |
| 366 | else if(typeof startTag == 'number'){ |
| 367 | decodedTag = this.peekStartElementAsLong(); |
| 368 | if ((null != decodedTag) && decodedTag == startTag) { |
| 369 | return true; |
| 370 | } |
| 371 | return false; |
| 372 | } |
| 373 | else{ |
| 374 | throw new Exception("SHOULD BE STRING OR NUMBER"); |
| 375 | } |
| 376 | } |
| 377 | //returns Long |
| 378 | BinaryXMLDecoder.prototype.peekStartElementAsLong = function() { |
| 379 | //this.istream.mark(MARK_LEN); |
| 380 | |
| 381 | //Long |
| 382 | decodedTag = null; |
| 383 | |
| 384 | var previousOffset = this.offset; |
| 385 | |
| 386 | try { |
| 387 | // Have to distinguish genuine errors from wrong tags. Could either use |
| 388 | // a special exception subtype, or redo the work here. |
| 389 | //this.TypeAndVal |
| 390 | tv = this.decodeTypeAndVal(this.istream); |
| 391 | |
| 392 | if (null != tv) { |
| 393 | |
| 394 | if (tv.type() == XML_TAG) { |
| 395 | if (tv.val()+1 > DEBUG_MAX_LEN) { |
| 396 | throw new ContentDecodingException("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!"); |
| 397 | } |
| 398 | |
| 399 | var valval ; |
| 400 | if(typeof tv.val() == 'string'){ |
| 401 | valval = (parseInt(tv.val())) + 1; |
| 402 | } |
| 403 | else |
| 404 | valval = (tv.val())+ 1; |
| 405 | |
| 406 | // Tag value represents length-1 as tags can never be empty. |
| 407 | //String |
| 408 | strTag = this.decodeUString(this.istream, valval); |
| 409 | |
| 410 | decodedTag = stringToTag(strTag); |
| 411 | |
| 412 | //Log.info(Log.FAC_ENCODING, "Unexpected: got text tag in peekStartElement; length: " + valval + " decoded tag = " + decodedTag); |
| 413 | |
| 414 | } else if (tv.type() == XML_DTAG) { |
| 415 | decodedTag = tv.val(); |
| 416 | } |
| 417 | |
| 418 | } // else, not a type and val, probably an end element. rewind and return false. |
| 419 | |
| 420 | } catch ( e) { |
| 421 | |
| 422 | } finally { |
| 423 | try { |
| 424 | //this.istream.reset(); |
| 425 | this.offset = previousOffset; |
| 426 | } catch ( e) { |
| 427 | Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e); |
| 428 | throw new Exception("Cannot reset stream! " + e.getMessage(), e); |
| 429 | } |
| 430 | } |
| 431 | return decodedTag; |
| 432 | }; |
| 433 | |
| 434 | |
| 435 | // returns a byte[] |
| 436 | BinaryXMLDecoder.prototype.readBinaryElement = function( |
| 437 | //long |
| 438 | startTag, |
| 439 | //TreeMap<String, String> |
| 440 | attributes){ |
| 441 | //byte [] |
| 442 | blob = null; |
| 443 | |
| 444 | this.readStartElement(startTag, attributes); |
| 445 | blob = this.readBlob(); |
| 446 | |
| 447 | |
| 448 | return blob; |
| 449 | |
| 450 | }; |
| 451 | |
| 452 | |
| 453 | BinaryXMLDecoder.prototype.readEndElement = function(){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 454 | //try { |
| 455 | if(LOG>4)console.log('this.offset is '+this.offset); |
| 456 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 457 | var next = this.istream[this.offset]; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 458 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 459 | this.offset++; |
| 460 | //read(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 461 | |
| 462 | if(LOG>4)console.log('XML_CLOSE IS '+XML_CLOSE); |
| 463 | if(LOG>4)console.log('next is '+next); |
| 464 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 465 | if (next != XML_CLOSE) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 466 | console.log("Expected end element, got: " + next); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 467 | throw new ContentDecodingException("Expected end element, got: " + next); |
| 468 | } |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 469 | //} catch ( e) { |
| 470 | //throw new ContentDecodingException(e); |
| 471 | //} |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | |
| 475 | //String |
| 476 | BinaryXMLDecoder.prototype.readUString = function(){ |
| 477 | //String |
| 478 | ustring = this.decodeUString(this.istream); |
| 479 | this.readEndElement(); |
| 480 | return ustring; |
| 481 | |
| 482 | }; |
| 483 | |
| 484 | |
| 485 | //returns a byte[] |
| 486 | BinaryXMLDecoder.prototype.readBlob = function() { |
| 487 | //byte [] |
| 488 | |
| 489 | blob = this.decodeBlob(this.istream); |
| 490 | this.readEndElement(); |
| 491 | return blob; |
| 492 | |
| 493 | }; |
| 494 | |
| 495 | |
| 496 | //CCNTime |
| 497 | BinaryXMLDecoder.prototype.readDateTime = function( |
| 498 | //long |
| 499 | startTag) { |
| 500 | //byte [] |
| 501 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 502 | var byteTimestamp = this.readBinaryElement(startTag); |
| 503 | |
| 504 | //var lontimestamp = DataUtils.byteArrayToUnsignedLong(byteTimestamp); |
| 505 | |
| 506 | var byteTimestamp = DataUtils.toHex(byteTimestamp); |
| 507 | |
| 508 | |
| 509 | var byteTimestamp = parseInt(byteTimestamp, 16); |
| 510 | |
| 511 | lontimestamp = (byteTimestamp/ 4096) * 1000; |
| 512 | |
| 513 | //if(lontimestamp<0) lontimestamp = - lontimestamp; |
| 514 | |
| 515 | if(LOG>3) console.log('DECODED DATE WITH VALUE'); |
| 516 | if(LOG>3) console.log(lontimestamp); |
| 517 | |
| 518 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 519 | //CCNTime |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 520 | timestamp = new CCNTime(lontimestamp); |
Meki Cherkaoui | b21911b | 2012-05-18 16:54:37 -0700 | [diff] [blame] | 521 | //timestamp.setDateBinary(byteTimestamp); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 522 | |
| 523 | if (null == timestamp) { |
| 524 | throw new ContentDecodingException("Cannot parse timestamp: " + DataUtils.printHexBytes(byteTimestamp)); |
| 525 | } |
| 526 | return timestamp; |
| 527 | }; |
| 528 | |
| 529 | BinaryXMLDecoder.prototype.decodeTypeAndVal = function( |
| 530 | /*InputStream*/ |
| 531 | istream) { |
| 532 | |
| 533 | /*int*/next; |
| 534 | /*int*/type = -1; |
| 535 | /*long*/val = 0; |
| 536 | /*boolean*/more = true; |
| 537 | |
| 538 | |
| 539 | //var savedOffset = this.offset; |
| 540 | var count = 0; |
| 541 | |
| 542 | do { |
| 543 | |
| 544 | var next = this.istream[this.offset ]; |
| 545 | |
| 546 | |
| 547 | if (next < 0) { |
| 548 | return null; |
| 549 | } |
| 550 | |
| 551 | if ((0 == next) && (0 == val)) { |
| 552 | return null; |
| 553 | } |
| 554 | |
| 555 | more = (0 == (next & XML_TT_NO_MORE)); |
| 556 | |
| 557 | if (more) { |
| 558 | val = val << XML_REG_VAL_BITS; |
| 559 | val |= (next & XML_REG_VAL_MASK); |
| 560 | } else { |
| 561 | |
| 562 | type = next & XML_TT_MASK; |
| 563 | val = val << XML_TT_VAL_BITS; |
| 564 | val |= ((next >>> XML_TT_BITS) & XML_TT_VAL_MASK); |
| 565 | } |
| 566 | |
| 567 | this.offset++; |
| 568 | |
| 569 | } while (more); |
| 570 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 571 | if(LOG>3)console.log('TYPE is '+ type + ' VAL is '+ val); |
| 572 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 573 | return new TypeAndVal(type, val); |
| 574 | }; |
| 575 | |
| 576 | |
| 577 | |
| 578 | //TypeAndVal |
| 579 | BinaryXMLDecoder.peekTypeAndVal = function( |
| 580 | //InputStream |
| 581 | istream) { |
| 582 | //TypeAndVal |
| 583 | tv = null; |
| 584 | |
| 585 | //istream.mark(LONG_BYTES*2); |
| 586 | |
| 587 | var previousOffset = this.offset; |
| 588 | |
| 589 | try { |
| 590 | tv = this.decodeTypeAndVal(this.istream); |
| 591 | } finally { |
| 592 | //istream.reset(); |
| 593 | this.offset = previousOffset; |
| 594 | } |
| 595 | |
| 596 | return tv; |
| 597 | }; |
| 598 | |
| 599 | |
| 600 | //byte[] |
| 601 | BinaryXMLDecoder.prototype.decodeBlob = function( |
| 602 | //InputStream |
| 603 | istream, |
| 604 | //int |
| 605 | blobLength) { |
| 606 | |
| 607 | |
| 608 | if(null == blobLength){ |
| 609 | //TypeAndVal |
| 610 | tv = this.decodeTypeAndVal(this.istream); |
| 611 | |
| 612 | var valval ; |
Meki Cherkaoui | b21911b | 2012-05-18 16:54:37 -0700 | [diff] [blame] | 613 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 614 | if(typeof tv.val() == 'string'){ |
| 615 | valval = (parseInt(tv.val())); |
| 616 | } |
| 617 | else |
| 618 | valval = (tv.val()); |
| 619 | |
| 620 | //console.log('valval here is ' + valval); |
| 621 | return this.decodeBlob(this.istream, valval); |
| 622 | } |
| 623 | |
| 624 | // |
| 625 | //byte [] |
| 626 | |
| 627 | bytes = this.istream.slice(this.offset, this.offset+ blobLength); |
| 628 | this.offset += blobLength; |
| 629 | |
| 630 | //int |
| 631 | return bytes; |
| 632 | |
| 633 | count = 0; |
| 634 | |
| 635 | }; |
| 636 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 637 | var count =0; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 638 | |
| 639 | //String |
| 640 | BinaryXMLDecoder.prototype.decodeUString = function( |
| 641 | //InputStream |
| 642 | istream, |
| 643 | //int |
| 644 | byteLength) { |
| 645 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 646 | /* |
| 647 | console.log('COUNT IS '+count); |
| 648 | console.log('INPUT BYTELENGTH IS '+byteLength); |
| 649 | count++; |
| 650 | if(null == byteLength|| undefined == byteLength){ |
| 651 | console.log("!!!!"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 652 | tv = this.decodeTypeAndVal(this.istream); |
| 653 | var valval ; |
| 654 | if(typeof tv.val() == 'string'){ |
| 655 | valval = (parseInt(tv.val())); |
| 656 | } |
| 657 | else |
| 658 | valval = (tv.val()); |
| 659 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 660 | if(LOG>4) console.log('valval is ' + valval); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 661 | byteLength= this.decodeUString(this.istream, valval); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 662 | |
| 663 | //if(LOG>4) console.log('byte Length found in type val is '+ byteLength.charCodeAt(0)); |
| 664 | byteLength = parseInt(byteLength); |
| 665 | |
| 666 | |
| 667 | //byteLength = byteLength.charCodeAt(0); |
| 668 | //if(LOG>4) console.log('byte Length found in type val is '+ byteLength); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 669 | } |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 670 | if(LOG>4)console.log('byteLength is '+byteLength); |
| 671 | if(LOG>4)console.log('type of byteLength is '+typeof byteLength); |
| 672 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 673 | stringBytes = this.decodeBlob(this.istream, byteLength); |
| 674 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 675 | //console.log('String bytes are '+ stringBytes); |
| 676 | //console.log('stringBytes); |
| 677 | |
| 678 | if(LOG>4)console.log('byteLength is '+byteLength); |
| 679 | if(LOG>4)console.log('this.offset is '+this.offset); |
| 680 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 681 | tempBuffer = this.istream.slice(this.offset, this.offset+byteLength); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 682 | if(LOG>4)console.log('TEMPBUFFER IS' + tempBuffer); |
| 683 | if(LOG>4)console.log( tempBuffer); |
| 684 | |
| 685 | if(LOG>4)console.log('ADDING to offset value' + byteLength); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 686 | this.offset+= byteLength; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 687 | //if(LOG>3)console.log('read the String' + tempBuffer.toString('ascii')); |
| 688 | //return tempBuffer.toString('ascii');// |
| 689 | |
| 690 | |
| 691 | //if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.getUTF8StringFromBytes(stringBytes) ) ; |
| 692 | //if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.getUTF8StringFromBytes(tempBuffer) ) ; |
| 693 | //if(LOG>3)console.log(DataUtils.getUTF8StringFromBytes(tempBuffer) ) ; |
| 694 | //return DataUtils.getUTF8StringFromBytes(tempBuffer); |
| 695 | |
| 696 | if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.toString(stringBytes) ) ; |
| 697 | if(LOG>3)console.log( 'TYPE OF STRING READ IS '+ typeof DataUtils.toString(stringBytes) ) ; |
| 698 | |
| 699 | return DataUtils.toString(stringBytes);*/ |
| 700 | |
| 701 | if(null == byteLength ){ |
| 702 | var tempStreamPosition = this.offset; |
| 703 | |
| 704 | //TypeAndVal |
| 705 | tv = this.decodeTypeAndVal(istream); |
| 706 | |
| 707 | if(LOG>3)console.log('TV is '+tv); |
| 708 | if(LOG>3)console.log(tv); |
| 709 | |
| 710 | if(LOG>3)console.log('Type of TV is '+typeof tv); |
| 711 | |
| 712 | if ((null == tv) || (XML_UDATA != tv.type())) { // if we just have closers left, will get back null |
| 713 | //if (Log.isLoggable(Log.FAC_ENCODING, Level.FINEST)) |
| 714 | //Log.finest(Log.FAC_ENCODING, "Expected UDATA, got " + ((null == tv) ? " not a tag " : tv.type()) + ", assuming elided 0-length blob."); |
| 715 | |
| 716 | this.offset = tempStreamPosition; |
| 717 | |
| 718 | return ""; |
| 719 | } |
| 720 | |
| 721 | return this.decodeUString(istream, tv.val()); |
| 722 | } |
| 723 | else{ |
| 724 | //byte [] |
| 725 | stringBytes = this.decodeBlob(istream, byteLength); |
| 726 | |
| 727 | //return DataUtils.getUTF8StringFromBytes(stringBytes); |
| 728 | return DataUtils.toString(stringBytes); |
| 729 | |
| 730 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 731 | }; |
| 732 | |
| 733 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 734 | |
| 735 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 736 | //OBject containg a pair of type and value |
| 737 | var TypeAndVal = function TypeAndVal(_type,_val) { |
| 738 | this.t = _type; |
| 739 | this.v = _val; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 740 | }; |
| 741 | |
| 742 | TypeAndVal.prototype.type = function(){ |
| 743 | return this.t; |
| 744 | }; |
| 745 | |
| 746 | TypeAndVal.prototype.val = function(){ |
| 747 | return this.v; |
| 748 | }; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 749 | //TODO |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | BinaryXMLDecoder.prototype.readIntegerElement =function( |
| 757 | //String |
| 758 | startTag) { |
| 759 | |
| 760 | //String |
| 761 | if(LOG>4) console.log('READING INTEGER '+ startTag); |
| 762 | if(LOG>4) console.log('TYPE OF '+ typeof startTag); |
| 763 | |
| 764 | //try { |
| 765 | |
| 766 | strVal = this.readUTF8Element(startTag); |
| 767 | |
| 768 | //} |
| 769 | //catch (e) { |
| 770 | //throw new Exception("Cannot parse " + startTag + ": " + strVal); |
| 771 | //} |
| 772 | |
| 773 | return parseInt(strVal); |
| 774 | }; |
| 775 | |
| 776 | |
| 777 | BinaryXMLDecoder.prototype.readUTF8Element =function( |
| 778 | //String |
| 779 | startTag, |
| 780 | //TreeMap<String, String> |
| 781 | attributes) { |
| 782 | //throws ContentDecodingException |
| 783 | |
| 784 | this.readStartElement(startTag, attributes); // can't use getElementText, can't get attributes |
| 785 | //String |
| 786 | strElementText = this.readUString(); |
| 787 | return strElementText; |
| 788 | }; |