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() {
47 }
50 }
52}
53
54
55////////////////////////////////////////////////////////////////////////////////
56/// Initializes parser variables.
57
59 : fContext(nullptr), fValidate(kTRUE), fReplaceEntities(kFALSE), fStopError(kFALSE), fParseCode(0)
60{
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Cleanup.
65
71
72////////////////////////////////////////////////////////////////////////////////
73/// The parser will validate the xml file if val = true.
74
76{
77 fValidate = val;
78}
79
80////////////////////////////////////////////////////////////////////////////////
81/// The parser will replace/expand entities.
82
87
88////////////////////////////////////////////////////////////////////////////////
89/// To release any existing document.
90
92{
93 if (fContext) {
94 fContext->_private = nullptr;
96 fContext = nullptr;
97 }
98}
99
100////////////////////////////////////////////////////////////////////////////////
101/// This function is called when an error from the parser has occurred.
102/// Message is the parse error.
103
105{
106 fValidateError += message;
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// This function is called when a warning from the parser has occurred.
111/// Message is the parse error.
112
114{
115 fValidateWarning += message;
116}
117
118////////////////////////////////////////////////////////////////////////////////
119/// Returns the parse code message.
120
122{
123 switch (parseCode) {
124 case -1:
125 return "Attempt to parse a second file while a parse is in progress";
126 break;
127 case -2:
128 return "Parse context is not created";
129 break;
130 case -3:
131 return "An error occurred while parsing file";
132 break;
133 case -4:
134 return "A fatal error occurred while parsing file";
135 break;
136 case -5:
137 return "Document is not well-formed";
138 break;
139 case -6:
140 return "Document is not valid";
141 break;
142 default:
143 return "Parse code does not exist";
144 }
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// Initialize parser parameters, such as, disactivate non-standards libxml1
149/// features, on/off validation, clear error and warning messages.
150
152{
153 if (fValidate)
154 fContext->options |= XML_PARSE_DTDVALID;
155 else
156 fContext->options &= ~XML_PARSE_DTDVALID;
157
159 fContext->options |= XML_PARSE_NOENT;
160 else
161 fContext->options &= ~XML_PARSE_NOENT;
162
163 fContext->_private = this;
164
165 fValidateError = "";
166 fValidateWarning = "";
167}
168
169////////////////////////////////////////////////////////////////////////////////
170/// Stops parsing.
171
177
178////////////////////////////////////////////////////////////////////////////////
179/// Set the parse code:
180/// - \b 0: Parse successful
181/// - \b -1: Attempt to parse a second file while a parse is in progress
182/// - \b -2: Parse context is not created
183/// - \b -3: An error occurred while parsing file
184/// - \b -4: A fatal error occurred while parsing file
185/// - \b -5: Document is not well-formed
186
191
192////////////////////////////////////////////////////////////////////////////////
193/// Set parser stops in case of error:
194/// - \b stop = true, stops on error
195/// - \b stop = false, continue parsing on error...
196
198{
199 fStopError = stop;
200}
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Basic string class.
Definition TString.h:138
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.