Logo ROOT   6.21/01
Reference Guide
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 
16 TXMLParser is an abstract class which interfaces with Libxml2.
17 Libxml2 is the XML C parser and toolkit developed for the Gnome
18 project.
19 The libxml library provides two interfaces to the parser, a DOM
20 style tree interface and a SAX style event based interface.
21 TXMLParser is parent class of TSAXParser and TDOMParser, which are
22 a 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 "Riostream.h"
35 #include "TXMLParser.h"
36 
37 #include <libxml/parser.h>
38 
39 
40 namespace {
41  // See https://lists.fedoraproject.org/pipermail/devel/2010-January/129117.html :
42  // "That function might delete TLS fields that belong to other libraries
43  // [...] if called twice."
44  // The same (though with less dramatic consequences) holds for xmlInitParser().
45  struct InitAndCleanupTheXMLParserOnlyOnceCommaEver {
46  InitAndCleanupTheXMLParserOnlyOnceCommaEver() {
47  xmlInitParser();
48  }
49  ~InitAndCleanupTheXMLParserOnlyOnceCommaEver() {
50  xmlCleanupParser();
51  }
52  } gInitAndCleanupTheXMLParserOnlyOnceCommaEver;
53 }
54 
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Initializes parser variables.
59 
61  : fContext(0), fValidate(kTRUE), fReplaceEntities(kFALSE), fStopError(kFALSE), fParseCode(0)
62 {
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Cleanup.
67 
69 {
71  fParseCode = 0;
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// The parser will validate the xml file if val = true.
76 
78 {
79  fValidate = val;
80 }
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 /// The parser will replace/expand entities.
84 
86 {
87  fReplaceEntities = val;
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// To release any existing document.
92 
94 {
95  if (fContext) {
96  fContext->_private = 0;
97  xmlFreeParserCtxt(fContext);
98  fContext = 0;
99  }
100 }
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 /// This function is called when an error from the parser has occured.
104 /// Message is the parse error.
105 
107 {
108  fValidateError += message;
109 }
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// This function is called when a warning from the parser has occured.
113 /// Message is the parse error.
114 
116 {
117  fValidateWarning += message;
118 }
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 /// Returns the parse code message.
122 
123 const char *TXMLParser::GetParseCodeMessage(Int_t parseCode) const
124 {
125  switch (parseCode) {
126  case -1:
127  return "Attempt to parse a second file while a parse is in progress";
128  break;
129  case -2:
130  return "Parse context is not created";
131  break;
132  case -3:
133  return "An error occured while parsing file";
134  break;
135  case -4:
136  return "A fatal error occured while parsing file";
137  break;
138  case -5:
139  return "Document is not well-formed";
140  break;
141  case -6:
142  return "Document is not valid";
143  break;
144  default:
145  return "Parse code does not exist";
146  }
147 }
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 /// Initialize parser parameters, such as, disactivate non-standards libxml1
151 /// features, on/off validation, clear error and warning messages.
152 
154 {
155  fContext->linenumbers = 1; // TRUE - This is the default anyway.
156  fContext->validate = fValidate ? 1 : 0;
157  fContext->replaceEntities = fReplaceEntities ? 1 : 0;
158  fContext->_private = this;
159 
160  fValidateError = "";
161  fValidateWarning = "";
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Stops parsing.
166 
168 {
169  if (fContext)
170  xmlStopParser(fContext);
171 }
172 
173 ////////////////////////////////////////////////////////////////////////////////
174 /// Set the parse code:
175 /// - \b 0: Parse successful
176 /// - \b -1: Attempt to parse a second file while a parse is in progress
177 /// - \b -2: Parse context is not created
178 /// - \b -3: An error occured while parsing file
179 /// - \b -4: A fatal error occured while parsing file
180 /// - \b -5: Document is not well-formed
181 
183 {
184  fParseCode = errorcode;
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// Set parser stops in case of error:
189 /// - \b stop = true, stops on error
190 /// - \b stop = false, continue parsing on error...
191 
193 {
194  fStopError = stop;
195 }
virtual void InitializeContext()
Initialize parser parameters, such as, disactivate non-standards libxml1 features, on/off validation, clear error and warning messages.
Definition: TXMLParser.cxx:153
void SetReplaceEntities(Bool_t val=kTRUE)
The parser will replace/expand entities.
Definition: TXMLParser.cxx:85
virtual void ReleaseUnderlying()
To release any existing document.
Definition: TXMLParser.cxx:93
Int_t fParseCode
To keep track of the errorcodes.
Definition: TXMLParser.h:37
Basic string class.
Definition: TString.h:131
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SetValidate(Bool_t val=kTRUE)
The parser will validate the xml file if val = true.
Definition: TXMLParser.cxx:77
_xmlParserCtxt * fContext
Parse the xml file.
Definition: TXMLParser.h:31
virtual void SetParseCode(Int_t code)
Set the parse code:
Definition: TXMLParser.cxx:182
virtual void OnValidateError(const TString &message)
This function is called when an error from the parser has occured.
Definition: TXMLParser.cxx:106
const char * GetParseCodeMessage(Int_t parseCode) const
Returns the parse code message.
Definition: TXMLParser.cxx:123
void SetStopOnError(Bool_t stop=kTRUE)
Set parser stops in case of error:
Definition: TXMLParser.cxx:192
TXMLParser()
Initializes parser variables.
Definition: TXMLParser.cxx:60
virtual void StopParser()
Stops parsing.
Definition: TXMLParser.cxx:167
TXMLParser is an abstract class which interfaces with Libxml2.
Definition: TXMLParser.h:24
const Bool_t kFALSE
Definition: RtypesCore.h:88
virtual ~TXMLParser()
Cleanup.
Definition: TXMLParser.cxx:68
#define ClassImp(name)
Definition: Rtypes.h:365
Bool_t fValidate
To validate the parse context.
Definition: TXMLParser.h:32
TString fValidateWarning
Parse warning.
Definition: TXMLParser.h:36
Bool_t fReplaceEntities
Replace entities.
Definition: TXMLParser.h:33
Bool_t fStopError
Stop when parse error occurs.
Definition: TXMLParser.h:34
TString fValidateError
Parse error.
Definition: TXMLParser.h:35
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual void OnValidateWarning(const TString &message)
This function is called when a warning from the parser has occured.
Definition: TXMLParser.cxx:115