Changeset 17362 in vbox for trunk/src/VBox/Main/xml
- Timestamp:
- Mar 4, 2009 6:10:58 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/xml/xml.cpp
r16538 r17362 4 4 5 5 /* 6 * Copyright (C) 2007-200 8Sun Microsystems, Inc.6 * Copyright (C) 2007-2009 Sun Microsystems, Inc. 7 7 * 8 8 * This file is part of VirtualBox Open Source Edition (OSE), as … … 42 42 #include "VBox/xml.h" 43 43 44 //////////////////////////////////////////////////////////////////////////////// 45 // 46 // globals 47 // 48 //////////////////////////////////////////////////////////////////////////////// 44 49 45 50 /** … … 94 99 { 95 100 96 ////////////////////////////////////////////////////////////////////////////// 101 //////////////////////////////////////////////////////////////////////////////// 102 // 97 103 // Exceptions 98 ////////////////////////////////////////////////////////////////////////////// 104 // 105 //////////////////////////////////////////////////////////////////////////////// 99 106 100 107 LogicError::LogicError(RT_SRC_POS_DECL) … … 111 118 { 112 119 if (!aErr) 113 throw EInvalidArg 120 throw EInvalidArg(RT_SRC_POS); 114 121 115 122 char *msg = Format(aErr); … … 148 155 } 149 156 150 ////////////////////////////////////////////////////////////////////////////// 157 //////////////////////////////////////////////////////////////////////////////// 158 // 151 159 // File Class 160 // 152 161 ////////////////////////////////////////////////////////////////////////////// 153 162 … … 283 292 } 284 293 285 ////////////////////////////////////////////////////////////////////////////// 294 //////////////////////////////////////////////////////////////////////////////// 295 // 286 296 // MemoryBuf Class 297 // 287 298 ////////////////////////////////////////////////////////////////////////////// 288 299 … … 349 360 } 350 361 351 / *352 * GlobalLock 353 * 354 * 355 */362 //////////////////////////////////////////////////////////////////////////////// 363 // 364 // GlobalLock class 365 // 366 //////////////////////////////////////////////////////////////////////////////// 356 367 357 368 struct GlobalLock::Data … … 392 403 } 393 404 394 / *395 * Node 396 * 397 * 398 */405 //////////////////////////////////////////////////////////////////////////////// 406 // 407 // Node class 408 // 409 //////////////////////////////////////////////////////////////////////////////// 399 410 400 411 struct Node::Data 401 412 { 402 413 xmlNode *plibNode; // != NULL if this is an element 403 xmlAttr *plibAttr; // != NULL if this is an element404 405 Node *pParent;// NULL only for the root element414 xmlAttr *plibAttr; // != NULL if this is an attribute 415 416 Node *pParent; // NULL only for the root element 406 417 const char *pcszName; // points either into plibNode or plibAttr 407 418 … … 790 801 } 791 802 792 / *793 * Document 794 * 795 * 796 */803 //////////////////////////////////////////////////////////////////////////////// 804 // 805 // Document class 806 // 807 //////////////////////////////////////////////////////////////////////////////// 797 808 798 809 struct Document::Data 799 810 { 800 xmlDocPtr p Document;801 Node *pRootElement;811 xmlDocPtr plibDocument; 812 Node *pRootElement; 802 813 803 814 Data() 804 815 { 805 p Document = NULL;816 plibDocument = NULL; 806 817 pRootElement = NULL; 807 818 } … … 814 825 void reset() 815 826 { 816 if (p Document)827 if (plibDocument) 817 828 { 818 xmlFreeDoc(p Document);819 p Document = NULL;829 xmlFreeDoc(plibDocument); 830 plibDocument = NULL; 820 831 } 821 832 if (pRootElement) … … 828 839 void copyFrom(const Document::Data *p) 829 840 { 830 if (p->p Document)841 if (p->plibDocument) 831 842 { 832 p Document = xmlCopyDoc(p->pDocument,833 1); // recursive == copy all843 plibDocument = xmlCopyDoc(p->plibDocument, 844 1); // recursive == copy all 834 845 } 835 846 } … … 867 878 { 868 879 m->pRootElement = new Node(); 869 m->pRootElement->m->plibNode = xmlDocGetRootElement(m->p Document);880 m->pRootElement->m->plibNode = xmlDocGetRootElement(m->plibDocument); 870 881 m->pRootElement->m->pcszName = (const char*)m->pRootElement->m->plibNode->name; 871 882 … … 873 884 } 874 885 886 /** 887 * Returns the root element of the document, or NULL if the document is empty. 888 * @return 889 */ 875 890 const Node* Document::getRootElement() const 876 891 { … … 878 893 } 879 894 880 /* 881 * XmlParserBase 882 * 883 * 884 */ 895 /** 896 * Creates a new element node and sets it as the root element. This will 897 * only work if the document is empty; otherwise EDocumentNotEmpty is thrown. 898 */ 899 Node* Document::createRootElement(const char *pcszRootElementName) 900 { 901 if (m->pRootElement) 902 throw EDocumentNotEmpty(RT_SRC_POS); 903 904 m->plibDocument = xmlNewDoc((const xmlChar*)"1.0"); 905 if (!(m->pRootElement = new Node())) 906 throw ENoMemory(); 907 Node::Data *pNodeData = m->pRootElement->m; 908 if (!(pNodeData->plibNode = xmlNewNode(NULL, // namespace 909 (const xmlChar*)pcszRootElementName))) 910 throw ENoMemory(); 911 pNodeData->pcszName = (const char*)pNodeData->plibNode->name; 912 913 return m->pRootElement; 914 } 915 916 //////////////////////////////////////////////////////////////////////////////// 917 // 918 // XmlParserBase class 919 // 920 //////////////////////////////////////////////////////////////////////////////// 885 921 886 922 XmlParserBase::XmlParserBase() … … 897 933 } 898 934 899 / *900 * XmlFileParser 901 * 902 * 903 */935 //////////////////////////////////////////////////////////////////////////////// 936 // 937 // XmlFileParser class 938 // 939 //////////////////////////////////////////////////////////////////////////////// 904 940 905 941 struct XmlFileParser::Data … … 931 967 } 932 968 933 struct Read Context969 struct ReadWriteContext 934 970 { 935 971 File file; 936 972 com::Utf8Str error; 937 973 938 Read Context(const char *pcszFilename)939 : file(File::Mode_Read, pcszFilename) 974 ReadWriteContext(const char *pcszFilename) 975 : file(File::Mode_Read, pcszFilename) // @todo must be write for writer 940 976 { 941 977 } … … 969 1005 m->strXmlFilename = pcszFilename; 970 1006 971 Read Context context(pcszFilename);1007 ReadWriteContext context(pcszFilename); 972 1008 doc.m->reset(); 973 if (!(doc.m->p Document = xmlCtxtReadIO(m->ctxt,974 ReadCallback,975 CloseCallback,976 &context,977 pcszFilename,978 NULL, // encoding = auto979 XML_PARSE_NOBLANKS)))1009 if (!(doc.m->plibDocument = xmlCtxtReadIO(m->ctxt, 1010 ReadCallback, 1011 CloseCallback, 1012 &context, 1013 pcszFilename, 1014 NULL, // encoding = auto 1015 XML_PARSE_NOBLANKS))) 980 1016 throw XmlError(xmlCtxtGetLastError(m->ctxt)); 981 1017 … … 986 1022 int XmlFileParser::ReadCallback(void *aCtxt, char *aBuf, int aLen) 987 1023 { 988 Read Context *pContext = static_cast<ReadContext*>(aCtxt);1024 ReadWriteContext *pContext = static_cast<ReadWriteContext*>(aCtxt); 989 1025 990 1026 /* To prevent throwing exceptions while inside libxml2 code, we catch … … 1010 1046 } 1011 1047 1048 //////////////////////////////////////////////////////////////////////////////// 1049 // 1050 // XmlFileWriter class 1051 // 1052 //////////////////////////////////////////////////////////////////////////////// 1053 1054 struct XmlFileWriter::Data 1055 { 1056 Document *pDoc; 1057 }; 1058 1059 XmlFileWriter::XmlFileWriter(Document &doc) 1060 { 1061 m = new Data(); 1062 m->pDoc = &doc; 1063 } 1064 1065 XmlFileWriter::~XmlFileWriter() 1066 { 1067 delete m; 1068 } 1069 1070 int XmlFileWriter::WriteCallback(void *aCtxt, const char *aBuf, int aLen) 1071 { 1072 ReadWriteContext *pContext = static_cast<ReadWriteContext*>(aCtxt); 1073 1074 /* To prevent throwing exceptions while inside libxml2 code, we catch 1075 * them and forward to our level using a couple of variables. */ 1076 try 1077 { 1078 return pContext->file.write(aBuf, aLen); 1079 } 1080 catch (const xml::EIPRTFailure &err) { pContext->setError(err); } 1081 catch (const xml::Error &err) { pContext->setError(err); } 1082 catch (const std::exception &err) { pContext->setError(err); } 1083 catch (...) { pContext->setError(xml::LogicError(RT_SRC_POS)); } 1084 1085 return -1 /* failure */; 1086 } 1087 1088 int XmlFileWriter::CloseCallback(void *aCtxt) 1089 { 1090 /// @todo to be written 1091 1092 return -1; 1093 } 1094 1095 void XmlFileWriter::write(const char *pcszFilename) 1096 { 1097 ReadWriteContext context(pcszFilename); 1098 1099 GlobalLock lock(); 1100 1101 /* serialize to the stream */ 1102 xmlIndentTreeOutput = 1; 1103 xmlTreeIndentString = " "; 1104 xmlSaveNoEmptyTags = 0; 1105 1106 xmlSaveCtxtPtr saveCtxt; 1107 if (!(saveCtxt = xmlSaveToIO(WriteCallback, 1108 CloseCallback, 1109 &context, 1110 NULL, 1111 XML_SAVE_FORMAT))) 1112 throw xml::LogicError(RT_SRC_POS); 1113 1114 long rc = xmlSaveDoc(saveCtxt, m->pDoc->m->plibDocument); 1115 if (rc == -1) 1116 { 1117 /* look if there was a forwared exception from the lower level */ 1118 // if (m->trappedErr.get() != NULL) 1119 // m->trappedErr->rethrow(); 1120 1121 /* there must be an exception from the Output implementation, 1122 * otherwise the save operation must always succeed. */ 1123 throw xml::LogicError(RT_SRC_POS); 1124 } 1125 1126 xmlSaveClose(saveCtxt); 1127 } 1128 1012 1129 1013 1130 } // end namespace xml
Note:
See TracChangeset
for help on using the changeset viewer.