- Timestamp:
- Dec 2, 2008 5:39:34 PM (16 years ago)
- Location:
- trunk/src/VBox/Main
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/VirtualBoxImpl.cpp
r14904 r14922 3812 3812 aFile.uri(), err.rc()); 3813 3813 } 3814 catch (const XmlTreeBackend::Error &err)3814 catch (const xml::RuntimeError &err) 3815 3815 { 3816 3816 Assert (err.what() != NULL); -
trunk/src/VBox/Main/xml/Settings.cpp
r14904 r14922 518 518 ////////////////////////////////////////////////////////////////////////////// 519 519 520 class XmlTreeBackend::XmlError : public XmlTreeBackend::Error521 {522 public:523 524 XmlError (xmlErrorPtr aErr)525 {526 if (!aErr)527 throw xml::EInvalidArg (RT_SRC_POS);528 529 char *msg = Format (aErr);530 setWhat (msg);531 RTStrFree (msg);532 }533 534 /**535 * Composes a single message for the given error. The caller must free the536 * returned string using RTStrFree() when no more necessary.537 */538 static char *Format (xmlErrorPtr aErr)539 {540 const char *msg = aErr->message ? aErr->message : "<none>";541 size_t msgLen = strlen (msg);542 /* strip spaces, trailing EOLs and dot-like char */543 while (msgLen && strchr (" \n.?!", msg [msgLen - 1]))544 -- msgLen;545 546 char *finalMsg = NULL;547 RTStrAPrintf (&finalMsg, "%.*s.\nLocation: '%s', line %d (%d), column %d",548 msgLen, msg, aErr->file, aErr->line, aErr->int1, aErr->int2);549 550 return finalMsg;551 }552 };553 554 520 struct XmlTreeBackend::Data 555 521 { … … 693 659 m->trappedErr->rethrow(); 694 660 695 throw XmlError(xmlCtxtGetLastError (m->ctxt));661 throw xml::XmlError(xmlCtxtGetLastError (m->ctxt)); 696 662 } 697 663 … … 734 700 m->trappedErr->rethrow(); 735 701 736 throw XmlError(xmlCtxtGetLastError (m->ctxt));702 throw xml::XmlError(xmlCtxtGetLastError (m->ctxt)); 737 703 } 738 704 … … 767 733 { 768 734 xmlFreeDoc (newDoc); 769 throw Error(errorStr);735 throw xml::RuntimeError(errorStr); 770 736 /* errorStr is freed in catch(...) below */ 771 737 } … … 861 827 throw xml::LogicError (RT_SRC_POS); 862 828 863 throw Error(errorStr);829 throw xml::RuntimeError(errorStr); 864 830 /* errorStr is freed in catch(...) below */ 865 831 } … … 1107 1073 char * &str = *(char * *) aCtxt; 1108 1074 1109 char *newMsg = XmlError::Format (aErr);1075 char *newMsg = xml::XmlError::Format (aErr); 1110 1076 AssertReturnVoid (newMsg != NULL); 1111 1077 -
trunk/src/VBox/Main/xml/xml.cpp
r14904 r14922 95 95 96 96 LogicError::LogicError(RT_SRC_POS_DECL) 97 : Error(NULL) 97 98 { 98 99 char *msg = NULL; … … 103 104 } 104 105 106 XmlError::XmlError(xmlErrorPtr aErr) 107 { 108 if (!aErr) 109 throw EInvalidArg (RT_SRC_POS); 110 111 char *msg = Format (aErr); 112 setWhat (msg); 113 RTStrFree (msg); 114 } 115 116 /** 117 * Composes a single message for the given error. The caller must free the 118 * returned string using RTStrFree() when no more necessary. 119 */ 120 // static 121 char *XmlError::Format(xmlErrorPtr aErr) 122 { 123 const char *msg = aErr->message ? aErr->message : "<none>"; 124 size_t msgLen = strlen (msg); 125 /* strip spaces, trailing EOLs and dot-like char */ 126 while (msgLen && strchr (" \n.?!", msg [msgLen - 1])) 127 -- msgLen; 128 129 char *finalMsg = NULL; 130 RTStrAPrintf (&finalMsg, "%.*s.\nLocation: '%s', line %d (%d), column %d", 131 msgLen, msg, aErr->file, aErr->line, aErr->int1, aErr->int2); 132 133 return finalMsg; 134 } 135 136 EIPRTFailure::EIPRTFailure(int aRC) 137 : RuntimeError(NULL), 138 mRC(aRC) 139 { 140 char *newMsg = NULL; 141 RTStrAPrintf(&newMsg, "Runtime error: %d (%s)", aRC, RTErrGetShort(aRC)); 142 setWhat(newMsg); 143 RTStrFree(newMsg); 144 } 105 145 106 146 ////////////////////////////////////////////////////////////////////////////// … … 118 158 }; 119 159 120 File::File 160 File::File(Mode aMode, const char *aFileName) 121 161 : m (new Data()) 122 162 { … … 401 441 } 402 442 443 struct ReadContext 444 { 445 File file; 446 std::string error; 447 448 ReadContext(const char *pcszFilename) 449 : file(File::Mode_Read, pcszFilename) 450 { 451 } 452 453 void setError(const xml::Error &x) 454 { 455 error = x.what(); 456 } 457 458 void setError(const std::exception &x) 459 { 460 error = x.what(); 461 } 462 }; 463 403 464 void XmlFileParser::read(const char *pcszFilename) 404 465 { 405 466 GlobalLock lock(); 467 // global.setExternalEntityLoader(ExternalEntityLoader); 406 468 407 469 xmlDocPtr doc = NULL; 470 ReadContext *pContext = NULL; 408 471 409 472 m->strXmlFilename = pcszFilename; 410 473 411 /* Note: when parsing we use XML_PARSE_NOBLANKS to instruct libxml2 to 412 * remove text nodes that contain only blanks. This is important because 413 * otherwise xmlSaveDoc() won't be able to do proper indentation on 414 * output. */ 415 416 /* parse the stream */ 417 /* NOTE: new InputCtxt instance will be deleted when the stream is closed by 418 * the libxml2 API (e.g. when calling xmlFreeParserCtxt()) */ 419 // doc = xmlCtxtReadIO(m->ctxt, 420 // ReadCallback, 421 // CloseCallback, 422 // new Data::InputCtxt (&aInput, m->trappedErr), 423 // aInput.uri(), NULL, 424 // XML_PARSE_NOBLANKS); 425 if (doc == NULL) 426 { 427 /* look if there was a forwared exception from the lower level */ 428 // if (m->trappedErr.get() != NULL) 429 // m->trappedErr->rethrow(); 430 431 // throw XmlError (xmlCtxtGetLastError (m->ctxt)); 432 } 433 } 474 try 475 { 476 pContext = new ReadContext(pcszFilename); 477 doc = xmlCtxtReadIO(m->ctxt, 478 ReadCallback, 479 CloseCallback, 480 pContext, 481 pcszFilename, 482 NULL, // encoding 483 XML_PARSE_NOBLANKS); 484 if (doc == NULL) 485 { 486 throw XmlError(xmlCtxtGetLastError(m->ctxt)); 487 } 488 489 xmlFreeDoc(doc); 490 doc = NULL; 491 492 delete pContext; 493 pContext = NULL; 494 } 495 catch (...) 496 { 497 if (doc != NULL) 498 xmlFreeDoc(doc); 499 500 if (pContext) 501 delete pContext; 502 503 throw; 504 } 505 } 506 507 // static 508 int XmlFileParser::ReadCallback(void *aCtxt, char *aBuf, int aLen) 509 { 510 ReadContext *pContext = static_cast<ReadContext*>(aCtxt); 511 512 /* To prevent throwing exceptions while inside libxml2 code, we catch 513 * them and forward to our level using a couple of variables. */ 514 515 try 516 { 517 return pContext->file.read(aBuf, aLen); 518 } 519 catch (const xml::EIPRTFailure &err) { pContext->setError(err); } 520 catch (const xml::Error &err) { pContext->setError(err); } 521 catch (const std::exception &err) { pContext->setError(err); } 522 catch (...) { pContext->setError(xml::LogicError(RT_SRC_POS)); } 523 524 return -1 /* failure */; 525 } 526 527 int XmlFileParser::CloseCallback(void *aCtxt) 528 { 529 /// @todo to be written 530 531 return -1; 532 } 533 434 534 435 535 } // end namespace xml
Note:
See TracChangeset
for help on using the changeset viewer.