Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TXMLParser.cxx
Go to the documentation of this file.
1// @(#)root/xmlparser:$Id$
2// Author: Jose Lo 12/1/2005
3
4/*************************************************************************
5 * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/**
13\class TXMLParser
14\ingroup IO
15
16TXMLParser is an abstract class which interfaces with Libxml2.
17Libxml2 is the XML C parser and toolkit developed for the Gnome
18project.
19The libxml library provides two interfaces to the parser, a DOM
20style tree interface and a SAX style event based interface.
21TXMLParser is parent class of TSAXParser and TDOMParser, which are
22a SAX interface and DOM interface of libxml.
23*/
24
25/*************************************************************************
26 This source is based on libxml++, a C++ wrapper for the libxml XML
27 parser library. Copyright (C) 2000 by Ari Johnson.
28
29 libxml++ are copyright (C) 2000 by Ari Johnson, and are covered by the
30 GNU Lesser General Public License, which should be included with
31 libxml++ as the file COPYING.
32 *************************************************************************/
33
34#include "TXMLParser.h"
35
36#include <libxml/parser.h>
37
38
39namespace {
40 // See https://lists.fedoraproject.org/pipermail/devel/2010-January/129117.html :
41 // "That function might delete TLS fields that belong to other libraries
42 // [...] if called twice."
43 // The same (though with less dramatic consequences) holds for xmlInitParser().
44 struct InitAndCleanupTheXMLParserOnlyOnceCommaEver {
45 InitAndCleanupTheXMLParserOnlyOnceCommaEver() {
46 xmlInitParser();
47 }
48 ~InitAndCleanupTheXMLParserOnlyOnceCommaEver() {
49 xmlCleanupParser();
50 }
51 } gInitAndCleanupTheXMLParserOnlyOnceCommaEver;
52}
53
55
56////////////////////////////////////////////////////////////////////////////////
57/// Initializes parser variables.
58
60 : fContext(nullptr), fValidate(kTRUE), fReplaceEntities(kFALSE), fStopError(kFALSE), fParseCode(0)
61{
62}
63
64////////////////////////////////////////////////////////////////////////////////
65/// Cleanup.
66
68{
70 fParseCode = 0;
71}
72
73////////////////////////////////////////////////////////////////////////////////
74/// The parser will validate the xml file if val = true.
75
77{
78 fValidate = val;
79}
80
81////////////////////////////////////////////////////////////////////////////////
82/// The parser will replace/expand entities.
83
85{
86 fReplaceEntities = val;
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// To release any existing document.
91
93{
94 if (fContext) {
95 fContext->_private = nullptr;
96 xmlFreeParserCtxt(fContext);
97 fContext = nullptr;
98 }
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// This function is called when an error from the parser has occurred.
103/// Message is the parse error.
104
106{
107 fValidateError += message;
108}
109
110////////////////////////////////////////////////////////////////////////////////
111/// This function is called when a warning from the parser has occurred.
112/// Message is the parse error.
113
115{
116 fValidateWarning += message;
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// Returns the parse code message.
121
122const char *TXMLParser::GetParseCodeMessage(Int_t parseCode) const
123{
124 switch (parseCode) {
125 case -1:
126 return "Attempt to parse a second file while a parse is in progress";
127 break;
128 case -2:
129 return "Parse context is not created";
130 break;
131 case -3:
132 return "An error occurred while parsing file";
133 break;
134 case -4:
135 return "A fatal error occurred while parsing file";
136 break;
137 case -5:
138 return "Document is not well-formed";
139 break;
140 case -6:
141 return "Document is not valid";
142 break;
143 default:
144 return "Parse code does not exist";
145 }
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// Initialize parser parameters, such as, disactivate non-standards libxml1
150/// features, on/off validation, clear error and warning messages.
151
153{
154 fContext->linenumbers = 1; // TRUE - This is the default anyway.
155 fContext->validate = fValidate ? 1 : 0;
156 fContext->replaceEntities = fReplaceEntities ? 1 : 0;
157 fContext->_private = this;
158
159 fValidateError = "";
160 fValidateWarning = "";
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Stops parsing.
165
167{
168 if (fContext)
169 xmlStopParser(fContext);
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// Set the parse code:
174/// - \b 0: Parse successful
175/// - \b -1: Attempt to parse a second file while a parse is in progress
176/// - \b -2: Parse context is not created
177/// - \b -3: An error occurred while parsing file
178/// - \b -4: A fatal error occurred while parsing file
179/// - \b -5: Document is not well-formed
180
182{
183 fParseCode = errorcode;
184}
185
186////////////////////////////////////////////////////////////////////////////////
187/// Set parser stops in case of error:
188/// - \b stop = true, stops on error
189/// - \b stop = false, continue parsing on error...
190
192{
193 fStopError = stop;
194}
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:377
Basic string class.
Definition TString.h:139
TXMLParser is an abstract class which interfaces with Libxml2.
Definition TXMLParser.h:24
TString fValidateWarning
Parse warning.
Definition TXMLParser.h:36
virtual void InitializeContext()
Initialize parser parameters, such as, disactivate non-standards libxml1 features,...
TXMLParser()
Initializes parser variables.
virtual void SetParseCode(Int_t code)
Set the parse code:
_xmlParserCtxt * fContext
Parse the xml file.
Definition TXMLParser.h:31
Int_t fParseCode
To keep track of the errorcodes.
Definition TXMLParser.h:37
void SetValidate(Bool_t val=kTRUE)
The parser will validate the xml file if val = true.
TString fValidateError
Parse error.
Definition TXMLParser.h:35
void SetReplaceEntities(Bool_t val=kTRUE)
The parser will replace/expand entities.
virtual void ReleaseUnderlying()
To release any existing document.
Bool_t fValidate
To validate the parse context.
Definition TXMLParser.h:32
const char * GetParseCodeMessage(Int_t parseCode) const
Returns the parse code message.
virtual void OnValidateWarning(const TString &message)
This function is called when a warning from the parser has occurred.
void SetStopOnError(Bool_t stop=kTRUE)
Set parser stops in case of error:
Bool_t fReplaceEntities
Replace entities.
Definition TXMLParser.h:33
virtual void StopParser()
Stops parsing.
virtual void OnValidateError(const TString &message)
This function is called when an error from the parser has occurred.
Bool_t fStopError
Stop when parse error occurs.
Definition TXMLParser.h:34
~TXMLParser() override
Cleanup.