Mercurial > projects > orange
comparison orange/serialization/archives/XMLArchive.d @ 18:3d42ea434d46
Added an error callback. Fixes #3 and #4.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Thu, 12 Aug 2010 23:24:51 +0200 |
parents | 27c5b6c5425f |
children | 8ab9588b92bf 9a575087b961 |
comparison
equal
deleted
inserted
replaced
17:c4e7e64ffb67 | 18:3d42ea434d46 |
---|---|
103 auto set = doc.query[Tags.archiveTag][Tags.dataTag]; | 103 auto set = doc.query[Tags.archiveTag][Tags.dataTag]; |
104 | 104 |
105 if (set.nodes.length == 1) | 105 if (set.nodes.length == 1) |
106 lastElement = set.nodes[0]; | 106 lastElement = set.nodes[0]; |
107 | 107 |
108 else if (set.nodes.length == 0) | |
109 throw new ArchiveException(errorMessage!(ArchiveMode.unarchiving) ~ `The "` ~ to!(string)(Tags.dataTag) ~ `" tag could not be found.`, __FILE__, __LINE__); | |
110 | |
111 else | 108 else |
112 throw new ArchiveException(errorMessage!(ArchiveMode.unarchiving) ~ `There were more than one "` ~ to!(string)(Tags.dataTag) ~ `" tag.`, __FILE__, __LINE__); | 109 { |
110 if (errorCallback) | |
111 { | |
112 if (set.nodes.length == 0) | |
113 errorCallback(new ArchiveException(errorMessage!(ArchiveMode.unarchiving) ~ `The "` ~ to!(string)(Tags.dataTag) ~ `" tag could not be found.`, __FILE__, __LINE__), [Tags.dataTag]); | |
114 | |
115 else | |
116 errorCallback(new ArchiveException(errorMessage!(ArchiveMode.unarchiving) ~ `There were more than one "` ~ to!(string)(Tags.dataTag) ~ `" tag.`, __FILE__, __LINE__), [Tags.dataTag]); | |
117 } | |
118 } | |
113 } | 119 } |
114 } | 120 } |
115 | 121 |
116 public DataType data () | 122 public DataType data () |
117 { | 123 { |
348 { | 354 { |
349 lastElement = getElement(Tags.nullTag, key); | 355 lastElement = getElement(Tags.nullTag, key); |
350 callDelegate = false; | 356 callDelegate = false; |
351 return null; | 357 return null; |
352 } | 358 } |
353 | 359 |
354 lastElement = tmp; | 360 lastElement = tmp; |
355 | 361 |
356 auto runtimeType = getValueOfAttribute(Attributes.runtimeTypeAttribute); | 362 auto runtimeType = getValueOfAttribute(Attributes.runtimeTypeAttribute); |
357 auto name = fromDataType!(string)(runtimeType); | 363 auto name = fromDataType!(string)(runtimeType); |
358 id = getValueOfAttribute(Attributes.idAttribute); | 364 id = getValueOfAttribute(Attributes.idAttribute); |
373 return result; | 379 return result; |
374 } | 380 } |
375 | 381 |
376 private T unarchiveStruct (T) (DataType key) | 382 private T unarchiveStruct (T) (DataType key) |
377 { | 383 { |
378 lastElement = getElement(Tags.structTag, key); | 384 auto element = getElement(Tags.structTag, key); |
385 | |
386 if (element.isValid) | |
387 lastElement = element; | |
379 | 388 |
380 return T.init; | 389 return T.init; |
381 } | 390 } |
382 | 391 |
383 private T unarchiveString (T) (DataType key) | 392 private T unarchiveString (T) (DataType key) |
384 { | 393 { |
385 return fromDataType!(T)(getElement(Tags.stringTag, key).value); | 394 auto element = getElement(Tags.stringTag, key); |
395 | |
396 if (!element.isValid) | |
397 return T.init; | |
398 | |
399 return fromDataType!(T)(element.value); | |
386 } | 400 } |
387 | 401 |
388 private T unarchiveArray (T) (DataType key) | 402 private T unarchiveArray (T) (DataType key) |
389 { | 403 { |
390 T value; | 404 T value; |
391 | 405 |
392 lastElement = getElement(Tags.arrayTag, key); | 406 auto element = getElement(Tags.arrayTag, key); |
407 | |
408 if (!element.isValid) | |
409 return T.init; | |
410 | |
411 lastElement = element; | |
393 auto length = getValueOfAttribute(Attributes.lengthAttribute); | 412 auto length = getValueOfAttribute(Attributes.lengthAttribute); |
394 value.length = fromDataType!(size_t)(length); | 413 value.length = fromDataType!(size_t)(length); |
395 | 414 |
396 return value; | 415 return value; |
397 } | 416 } |
398 | 417 |
399 private T unarchiveAssociativeArray (T) (DataType key) | 418 private T unarchiveAssociativeArray (T) (DataType key) |
400 { | 419 { |
401 lastElement = getElement(Tags.associativeArrayTag, key); | 420 auto element = getElement(Tags.associativeArrayTag, key); |
421 | |
422 if (element.isValid) | |
423 lastElement = element; | |
402 | 424 |
403 return T.init; | 425 return T.init; |
404 } | 426 } |
405 | 427 |
406 private T unarchivePointer (T) (DataType key, ref bool callDelegate) | 428 private T unarchivePointer (T) (DataType key, ref bool callDelegate) |
410 if (auto reference = getUnarchivedReference!(T)(id)) | 432 if (auto reference = getUnarchivedReference!(T)(id)) |
411 { | 433 { |
412 callDelegate = false; | 434 callDelegate = false; |
413 return *reference; | 435 return *reference; |
414 } | 436 } |
415 | 437 |
416 lastElement = getElement(Tags.pointerTag, key); | 438 auto element = getElement(Tags.pointerTag, key); |
439 | |
440 if (!element.isValid) | |
441 return T.init; | |
442 | |
443 lastElement = element; | |
417 id = getValueOfAttribute(Attributes.idAttribute); | 444 id = getValueOfAttribute(Attributes.idAttribute); |
418 | 445 |
419 T result = new BaseTypeOfPointer!(T); | 446 T result = new BaseTypeOfPointer!(T); |
420 | 447 |
421 addUnarchivedReference(result, id); | 448 addUnarchivedReference(result, id); |
423 return result; | 450 return result; |
424 } | 451 } |
425 | 452 |
426 private T unarchiveEnum (T) (DataType key) | 453 private T unarchiveEnum (T) (DataType key) |
427 { | 454 { |
428 return fromDataType!(T)(getElement(Tags.enumTag, key).value); | 455 auto element = getElement(Tags.enumTag, key); |
456 | |
457 if (!element.isValid) | |
458 return T.init; | |
459 | |
460 return fromDataType!(T)(element.value); | |
429 } | 461 } |
430 | 462 |
431 private T unarchivePrimitive (T) (DataType key) | 463 private T unarchivePrimitive (T) (DataType key) |
432 { | 464 { |
433 return fromDataType!(T)(getElement(toDataType(T.stringof), key).value); | 465 auto element = getElement(toDataType(T.stringof), key); |
466 | |
467 if (!element.isValid) | |
468 return T.init; | |
469 | |
470 return fromDataType!(T)(element.value); | |
434 } | 471 } |
435 | 472 |
436 private T unarchiveTypeDef (T) (DataType key) | 473 private T unarchiveTypeDef (T) (DataType key) |
437 { | 474 { |
438 lastElement = getElement(Tags.typedefTag, key); | 475 auto element = getElement(Tags.typedefTag, key); |
476 | |
477 if (element.isValid) | |
478 lastElement = element; | |
439 | 479 |
440 return T.init; | 480 return T.init; |
441 } | 481 } |
442 | 482 |
443 public AssociativeArrayVisitor!(KeyTypeOfAssociativeArray!(T), ValueTypeOfAssociativeArray!(T)) unarchiveAssociativeArrayVisitor (T) () | 483 public AssociativeArrayVisitor!(KeyTypeOfAssociativeArray!(T), ValueTypeOfAssociativeArray!(T)) unarchiveAssociativeArrayVisitor (T) () |
452 .attribute(Attributes.keyAttribute, key); | 492 .attribute(Attributes.keyAttribute, key); |
453 } | 493 } |
454 | 494 |
455 public void unarchiveBaseClass (T : Object) (DataType key) | 495 public void unarchiveBaseClass (T : Object) (DataType key) |
456 { | 496 { |
457 lastElement = getElement(Tags.baseTag, key); | 497 auto element = getElement(Tags.baseTag, key); |
498 | |
499 if (element.isValid) | |
500 lastElement = element; | |
458 } | 501 } |
459 | 502 |
460 version (Tango) | 503 version (Tango) |
461 { | 504 { |
462 template errorMessage (ArchiveMode mode = ArchiveMode.archiving) | 505 template errorMessage (ArchiveMode mode = ArchiveMode.archiving) |
495 if (set.nodes.length == 1) | 538 if (set.nodes.length == 1) |
496 return set.nodes[0].parent; | 539 return set.nodes[0].parent; |
497 | 540 |
498 else | 541 else |
499 { | 542 { |
500 if (throwOnError) | 543 if (throwOnError && errorCallback) |
501 { | 544 { |
502 if (set.nodes.length == 0) | 545 if (set.nodes.length == 0) |
503 throw new ArchiveException(`Could not find an element "` ~ to!(string)(tag) ~ `" with the attribute "` ~ to!(string)(Attributes.keyAttribute) ~ `" with the value "` ~ to!(string)(key) ~ `".`, __FILE__, __LINE__); | 546 errorCallback(new ArchiveException(`Could not find an element "` ~ to!(string)(tag) ~ `" with the attribute "` ~ to!(string)(Attributes.keyAttribute) ~ `" with the value "` ~ to!(string)(key) ~ `".`, __FILE__, __LINE__), [tag, Attributes.keyAttribute, key]); |
504 | 547 |
505 else | 548 else |
506 throw new ArchiveException(`Could not unarchive the value with the key "` ~ to!(string)(key) ~ `" due to malformed data.`, __FILE__, __LINE__); | 549 errorCallback(new ArchiveException(`Could not unarchive the value with the key "` ~ to!(string)(key) ~ `" due to malformed data.`, __FILE__, __LINE__), [tag, Attributes.keyAttribute, key]); |
507 } | 550 } |
508 | 551 |
509 return doc.Node.invalid; | 552 return doc.Node.invalid; |
510 } | 553 } |
511 } | 554 } |
515 auto set = lastElement.query.attribute(attribute); | 558 auto set = lastElement.query.attribute(attribute); |
516 | 559 |
517 if (set.nodes.length == 1) | 560 if (set.nodes.length == 1) |
518 return set.nodes[0].value; | 561 return set.nodes[0].value; |
519 | 562 |
520 else if (set.nodes.length == 0) | |
521 throw new ArchiveException(`Could not find the attribute "` ~ to!(string)(attribute) ~ `".`, __FILE__, __LINE__); | |
522 | |
523 else | 563 else |
524 throw new ArchiveException(`Could not unarchive the value of the attribute "` ~ to!(string)(attribute) ~ `" due to malformed data.`, __FILE__, __LINE__); | 564 { |
565 if (errorCallback) | |
566 { | |
567 if (set.nodes.length == 0) | |
568 errorCallback(new ArchiveException(`Could not find the attribute "` ~ to!(string)(attribute) ~ `".`, __FILE__, __LINE__), [attribute]); | |
569 | |
570 else | |
571 errorCallback(new ArchiveException(`Could not unarchive the value of the attribute "` ~ to!(string)(attribute) ~ `" due to malformed data.`, __FILE__, __LINE__), [attribute]); | |
572 } | |
573 } | |
525 } | 574 } |
526 | 575 |
527 private void addArchivedReference (T) (T value, DataType id) | 576 private void addArchivedReference (T) (T value, DataType id) |
528 { | 577 { |
529 static assert(isReference!(T), format!(`The given type "`, T, `" is not a reference type, i.e. object or pointer.`)); | 578 static assert(isReference!(T), format!(`The given type "`, T, `" is not a reference type, i.e. object or pointer.`)); |