00001 // @(#)root/xmlparser:$Id: TDOMParser.cxx 23637 2008-05-02 11:12:04Z rdm $ 00002 // Author: Jose Lo 12/4/2005 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 ////////////////////////////////////////////////////////////////////////// 00013 // // 00014 // TDomParser // 00015 // // 00016 // DOM stands for the Document Object Model; this is an API for // 00017 // accessing XML or HTML structured documents. // 00018 // The Document Object Model is a platform and language-neutral // 00019 // interface that will allow programs and scripts to dynamically // 00020 // access and update the content, structure and style of documents. // 00021 // // 00022 // The parser returns a tree built during the document analysis. // 00023 // // 00024 ////////////////////////////////////////////////////////////////////////// 00025 00026 #include "TDOMParser.h" 00027 #include "TXMLDocument.h" 00028 00029 #include <libxml/tree.h> 00030 #include <libxml/parserInternals.h> 00031 00032 00033 ClassImp(TDOMParser); 00034 00035 //______________________________________________________________________________ 00036 TDOMParser::TDOMParser() : fTXMLDoc(0) 00037 { 00038 // TDOMParser constructor 00039 } 00040 00041 //______________________________________________________________________________ 00042 TDOMParser::~TDOMParser() 00043 { 00044 // TDOMParser destructor, it calls ReleaseUnderlying(). 00045 00046 ReleaseUnderlying(); 00047 } 00048 00049 //______________________________________________________________________________ 00050 void TDOMParser::ReleaseUnderlying() 00051 { 00052 // Release any existing document. 00053 00054 if (fTXMLDoc) { 00055 delete fTXMLDoc; 00056 fTXMLDoc = 0; 00057 } 00058 00059 SetParseCode(0); 00060 00061 TXMLParser::ReleaseUnderlying(); 00062 } 00063 00064 //______________________________________________________________________________ 00065 Int_t TDOMParser::ParseFile(const char *filename) 00066 { 00067 // Parse the XML file where filename is the XML file name. 00068 // It will create a TXMLDocument if the file is parsed without 00069 // any error. It returns parse code error in case of parse error, 00070 // see TXMLParser. 00071 00072 ReleaseUnderlying(); 00073 00074 fContext = xmlCreateFileParserCtxt(filename); 00075 00076 if (!fContext) { 00077 SetParseCode(-2); 00078 return -2; 00079 } 00080 00081 InitializeContext(); 00082 00083 if (!fContext->directory) { 00084 const char *dir = xmlParserGetDirectory(filename); 00085 fContext->directory = (char *)xmlStrdup((const xmlChar *)dir); 00086 } 00087 00088 return ParseContext(); 00089 } 00090 00091 //______________________________________________________________________________ 00092 Int_t TDOMParser::ParseBuffer(const char *buffer, Int_t len) 00093 { 00094 // It parses a buffer, much like ParseFile(). 00095 00096 ReleaseUnderlying(); 00097 00098 fContext = xmlCreateMemoryParserCtxt(buffer, len); 00099 00100 if (!fContext) { 00101 SetParseCode(-2); 00102 return -2; 00103 } 00104 00105 InitializeContext(); 00106 00107 return ParseContext(); 00108 } 00109 00110 //______________________________________________________________________________ 00111 Int_t TDOMParser::ParseContext() 00112 { 00113 // Creates a XML document for the parser. 00114 // It returns 0 on success, and 00115 // -1 if no XML document was created, 00116 // -5 if the document is not well formated, 00117 // -6 if document is not valid. 00118 00119 xmlParseDocument(fContext); 00120 00121 if (!fContext->myDoc) { 00122 SetParseCode(-1); 00123 return -1; 00124 } 00125 00126 if (!fContext->wellFormed) { 00127 SetParseCode(-5); 00128 return -5; 00129 } 00130 00131 if (!fContext->valid) { 00132 SetParseCode(-6); 00133 return -6; 00134 } 00135 00136 fTXMLDoc = new TXMLDocument(fContext->myDoc); 00137 00138 return 0; 00139 } 00140 00141 //______________________________________________________________________________ 00142 TXMLDocument *TDOMParser::GetXMLDocument() const 00143 { 00144 // Returns the TXMLDocument. 00145 00146 return fTXMLDoc; 00147 }