00001 // @(#)root/xmlparser:$Id: TXMLParser.cxx 34913 2010-08-20 19:18:35Z pcanal $ 00002 // Author: Jose Lo 12/1/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 // TXMLParser // 00015 // // 00016 // TXMLParser is an abstract class which interfaces with Libxml2. // 00017 // Libxml2 is the XML C parser and toolkit developed for the Gnome // 00018 // project. // 00019 // // 00020 // The libxml library provides two interfaces to the parser, a DOM // 00021 // style tree interface and a SAX style event based interface. // 00022 // // 00023 // TXMLParser is parent class of TSAXParser and TDOMParser, which are // 00024 // a SAX interface and DOM interface of libxml. // 00025 // // 00026 ////////////////////////////////////////////////////////////////////////// 00027 00028 /************************************************************************* 00029 This source is based on libxml++, a C++ wrapper for the libxml XML 00030 parser library. Copyright (C) 2000 by Ari Johnson. 00031 00032 libxml++ are copyright (C) 2000 by Ari Johnson, and are covered by the 00033 GNU Lesser General Public License, which should be included with 00034 libxml++ as the file COPYING. 00035 *************************************************************************/ 00036 00037 #include "Riostream.h" 00038 #include "TXMLParser.h" 00039 00040 #include <libxml/parser.h> 00041 00042 00043 ClassImp(TXMLParser); 00044 00045 //______________________________________________________________________________ 00046 TXMLParser::TXMLParser() 00047 : fContext(0), fValidate(kTRUE), fReplaceEntities(kFALSE), fStopError(kFALSE), fParseCode(0) 00048 { 00049 // Initializes parser variables. 00050 00051 xmlInitParser(); 00052 } 00053 00054 //______________________________________________________________________________ 00055 TXMLParser::~TXMLParser() 00056 { 00057 // Cleanup. 00058 00059 ReleaseUnderlying(); 00060 fParseCode = 0; 00061 } 00062 00063 //______________________________________________________________________________ 00064 void TXMLParser::SetValidate(Bool_t val) 00065 { 00066 // The parser will validate the xml file if val = true. 00067 00068 fValidate = val; 00069 } 00070 00071 //______________________________________________________________________________ 00072 void TXMLParser::SetReplaceEntities(Bool_t val) 00073 { 00074 // The parser will replace/expand entities. 00075 00076 fReplaceEntities = val; 00077 } 00078 00079 //______________________________________________________________________________ 00080 void TXMLParser::ReleaseUnderlying() 00081 { 00082 // To release any existing document. 00083 00084 if (fContext) { 00085 fContext->_private = 0; 00086 xmlFreeParserCtxt(fContext); 00087 fContext = 0; 00088 } 00089 xmlCleanupParser(); 00090 } 00091 00092 //______________________________________________________________________________ 00093 void TXMLParser::OnValidateError(const TString& message) 00094 { 00095 // This function is called when an error from the parser has occured. 00096 // Message is the parse error. 00097 00098 fValidateError += message; 00099 } 00100 00101 //______________________________________________________________________________ 00102 void TXMLParser::OnValidateWarning(const TString& message) 00103 { 00104 // This function is called when a warning from the parser has occured. 00105 // Message is the parse error. 00106 00107 fValidateWarning += message; 00108 } 00109 00110 //______________________________________________________________________________ 00111 const char *TXMLParser::GetParseCodeMessage(Int_t parseCode) const 00112 { 00113 // Returns the parse code message. 00114 00115 switch (parseCode) { 00116 case -1: 00117 return "Attempt to parse a second file while a parse is in progress"; 00118 break; 00119 case -2: 00120 return "Parse context is not created"; 00121 break; 00122 case -3: 00123 return "An error occured while parsing file"; 00124 break; 00125 case -4: 00126 return "A fatal error occured while parsing file"; 00127 break; 00128 case -5: 00129 return "Document is not well-formed"; 00130 break; 00131 case -6: 00132 return "Document is not valid"; 00133 break; 00134 default: 00135 return "Parse code does not exist"; 00136 } 00137 } 00138 00139 //______________________________________________________________________________ 00140 void TXMLParser::InitializeContext() 00141 { 00142 // Initialize parser parameters, such as, disactivate non-standards libxml1 00143 // features, on/off validation, clear error and warning messages. 00144 00145 fContext->linenumbers = 1; // TRUE - This is the default anyway. 00146 fContext->validate = fValidate ? 1 : 0; 00147 fContext->replaceEntities = fReplaceEntities ? 1 : 0; 00148 fContext->_private = this; 00149 00150 fValidateError = ""; 00151 fValidateWarning = ""; 00152 } 00153 00154 //______________________________________________________________________________ 00155 void TXMLParser::StopParser() 00156 { 00157 // Stops parsing. 00158 00159 if (fContext) 00160 xmlStopParser(fContext); 00161 } 00162 00163 //______________________________________________________________________________ 00164 void TXMLParser::SetParseCode(Int_t errorcode) 00165 { 00166 // Set the parse code: 00167 // 0: Parse succesfull 00168 // -1: Attempt to parse a second file while a parse is in progress 00169 // -2: Parse context is not created 00170 // -3: An error occured while parsing file 00171 // -4: A fatal error occured while parsing file 00172 // -5: Document is not well-formed 00173 00174 fParseCode = errorcode; 00175 } 00176 00177 //______________________________________________________________________________ 00178 void TXMLParser::SetStopOnError(Bool_t stop) 00179 { 00180 // Set parser stops in case of error: 00181 // stop = true, stops on error 00182 // stop = false, continue parsing on error... 00183 00184 fStopError = stop; 00185 }