XRootD
XrdXmlRdrTiny Class Reference

#include <XrdXmlRdrTiny.hh>

+ Inheritance diagram for XrdXmlRdrTiny:
+ Collaboration diagram for XrdXmlRdrTiny:

Public Member Functions

 XrdXmlRdrTiny (bool &aOK, const char *fname, const char *enc=0)
 Constructor & Destructor. More...
 
virtual ~XrdXmlRdrTiny ()
 
virtual bool GetAttributes (const char **aname, char **aval)
 
virtual int GetElement (const char **ename, bool reqd=false)
 
virtual const char * GetError (int &ecode)
 
virtual char * GetText (const char *ename, bool reqd=false)
 
- Public Member Functions inherited from XrdXmlReader
 XrdXmlReader ()
 Constructor & Destructor. More...
 
virtual ~XrdXmlReader ()
 

Static Public Member Functions

static bool Init ()
 
- Static Public Member Functions inherited from XrdXmlReader
static XrdXmlReaderGetReader (const char *fname, const char *enc=0, const char *impl=0)
 
static bool Init (const char *impl=0)
 

Detailed Description

Definition at line 42 of file XrdXmlRdrTiny.hh.

Constructor & Destructor Documentation

◆ XrdXmlRdrTiny()

XrdXmlRdrTiny::XrdXmlRdrTiny ( bool &  aOK,
const char *  fname,
const char *  enc = 0 
)

Constructor & Destructor.

Definition at line 83 of file XrdXmlRdrTiny.cc.

83  : reader(0) // make sure the pointer is nil initialized otherwise if stat fails the destructor segfaults
84 {
85  struct stat Stat;
86  const char *etext;
87 
88 // Initialize the standard values
89 //
90  curNode = 0;
91  curElem = 0;
92  elmNode = 0;
93  eCode = 0;
94  *eText = 0;
95  debug = getenv("XrdXmlDEBUG") != 0;
96 
97 // Make sure this file exists
98 //
99  if (stat(fname, &Stat))
100  {eCode = errno;
101  snprintf(eText,sizeof(eText),"%s opening %s", XrdSysE2T(errno), fname);
102  aOK = false;
103  return;
104  }
105 
106 // Get a file reader
107 //
108  reader = new TiXmlDocument(fname);
109  if (reader->LoadFile())
110  {curNode = (TiXmlNode *)reader;
111  curElem = 0;
112  elmNode = curNode;
113  aOK = true;
114  } else {
115  if (!(etext = reader->ErrorDesc()) || *etext)
116  {if ((eCode = errno)) etext = XrdSysE2T(errno);
117  else etext = "Unknown error";
118  }
119  snprintf(eText,sizeof(eText),"%s opening %s", etext, fname);
120  eCode = EINVAL;
121  aOK = false;
122  }
123 }
struct stat Stat
Definition: XrdCks.cc:49
int stat(const char *path, struct stat *buf)
const char * XrdSysE2T(int errcode)
Definition: XrdSysE2T.cc:104

References Stat, stat(), and XrdSysE2T().

+ Here is the call graph for this function:

◆ ~XrdXmlRdrTiny()

XrdXmlRdrTiny::~XrdXmlRdrTiny ( )
virtual

Definition at line 129 of file XrdXmlRdrTiny.cc.

130 {
131 
132 // Tear down the reader
133 //
134  if (reader)
135  {
136  delete reader;
137  reader = 0;
138  }
139 }

Member Function Documentation

◆ GetAttributes()

bool XrdXmlRdrTiny::GetAttributes ( const char **  aname,
char **  aval 
)
virtual

Get attributes from an XML tag. GetAttributes() should only be called after a successful GetElement() call.

Parameters
anamePointer to an array of attribute names whose values are to be returned. The last entry in the array must be nil.
avalPointer to an array where the corresponding attribute values are to be placed in 1-to-1 correspondence. The values must be freed using free().
Returns
true One or more attributes have been returned. false No specified attributes were found.

Implements XrdXmlReader.

Definition at line 161 of file XrdXmlRdrTiny.cc.

162 {
163  const char *value;
164  int i = 0;
165  bool found = false;
166 
167 // If we are not at the begining of an element, this is a sequence error
168 //
169  if (!curElem)
170  {snprintf(eText, sizeof(eText),
171  "Element not fetched when seeking attribute %s",aname[0]);
172  eCode = EILSEQ;
173  return false;
174  }
175 
176 // Find all the requested attributes
177 //
178  while(aname[i])
179  {if ((value = curElem->Attribute(aname[i])))
180  {if (aval[i]) free(aval[i]);
181  aval[i] = strdup(value);
182  found = true;
183  }
184  i++;
185  }
186 
187 // All done
188 //
189  return found;
190 }

◆ GetElement()

int XrdXmlRdrTiny::GetElement ( const char **  ename,
bool  reqd = false 
)
virtual

Find an XML tag element.

Parameters
enamePointer to an array of tag names any of which should be searched for. The last entry in the array must be nil. The first element of the array should contain the name of the context tag. Elements are searched only within the scope of that tag. When searching for the first desired tag, use a null string to indicate document scope.
reqdWhen true one of the tag elements listed in ename must be found otherwise an error is generated.
Returns
=0 No specified tag was found. Note that this corresponds to encountering the tag present in ename[0], i.e. scope end. >0 A tag was found, the return value is the index into ename that corresponds to the tag's name.

Implements XrdXmlReader.

Definition at line 196 of file XrdXmlRdrTiny.cc.

197 {
198  TiXmlNode *theChild;
199  const char *name = (curNode ? curNode->Value() : 0);
200  int i;
201 
202 // If we are positioned either at the current node or the last node we returned
203 // Complain if that is not the case.
204 //
205  if (*ename[0])
206  {if (name && strcmp(name, ename[0]))
207  {if (curElem && !strcmp(elmNode->Value(),ename[0])) curNode = elmNode;
208  else {snprintf(eText, sizeof(eText),
209  "Current context '%s' does not match stated scope '%s'",
210  (name ? name : ""), ename[0]);
211  eCode = EILSEQ;
212  return false;
213  }
214  }
215  }
216 
217 // Sequence to the next node at appropriate level.
218 //
219  if (curNode == elmNode ) theChild = curNode->FirstChild();
220  else if (elmNode) theChild = elmNode->NextSibling();
221  else theChild = curNode->NextSibling();
222 
223 
224 // Scan over to the first wanted element
225 //
226  while(theChild)
227  {if ((name = theChild->Value()) && theChild->Type() == ntElmBeg)
228  {i = 1;
229  while(ename[i] && strcmp(name, ename[i])) i++;
230  if (ename[i])
231  {if (debug) Debug("getelem:",ename[i],name,ename[0],ntElmBeg);
232  curElem = theChild->ToElement();
233  elmNode = theChild;
234  return i;
235  }
236  }
237  theChild = theChild->NextSibling();
238  }
239 
240 // We didn't find any wanted tag here in this scope. Transition to the element's
241 // parent we finished the tag
242 //
243  if (debug) Debug("getelem:",ename[1],ename[0],ename[0],ntElmEnd);
244  elmNode = curNode;
245  curNode = curNode->Parent();
246  curElem = 0;
247  return 0;
248 
249 // This is an error if this element was required
250 //
251  if (reqd)
252  {snprintf(eText,sizeof(eText),"Required element '%s' not found in '%s'",
253  (ename[1] ? ename[1] : "???"), ename[0]);
254  eCode = ESRCH;
255  }
256  return 0;
257 }

References Macaroons::Debug.

◆ GetError()

virtual const char* XrdXmlRdrTiny::GetError ( int &  ecode)
inlinevirtual

Get the description of the last error encountered.

Parameters
ecodeThe error code associated with the error.
Returns
Pointer to text describing the error. The text may be destroyed on a subsequent call to any other method. Otherwise it is stable. A nil pointer indicates that no error is present.

Implements XrdXmlReader.

Definition at line 51 of file XrdXmlRdrTiny.hh.

51 {return ((ecode = eCode) ? eText : 0);}

◆ GetText()

char * XrdXmlRdrTiny::GetText ( const char *  ename,
bool  reqd = false 
)
virtual

Get the text portion of an XML tag element. GetText() should only be called after a successful call to GetElement() with a possibly intervening call to GetAttributes().

Parameters
enamePointer to the corresponding tag name.
reqdWhen true text must exist and not be null. Otherwise, an error is generated if the text is missing or null.
Returns
=0 No text found.
!0 Pointer to the tag's text field. It must be free using free().

Implements XrdXmlReader.

Definition at line 263 of file XrdXmlRdrTiny.cc.

264 {
265  const char *value;
266  char *sP;
267 
268 // If we are not at the begining of an element, this is a sequence error
269 //
270  if (!curElem)
271  {snprintf(eText, sizeof(eText),
272  "Illegal position seeking text for tag %s",ename);
273  eCode = EILSEQ;
274  return 0;
275  }
276 
277 // Get the text associated with element (simple text only)
278 //
279  value = curElem->GetText();
280 
281 // We did not find a value. If not required return.
282 //
283  if (value || !reqd)
284  {if (!value) return 0;
285  sP = strdup(value);
286  return sP;
287  }
288 
289 // Create error message
290 //
291  snprintf(eText, sizeof(eText), "Required %s tag value not found", ename);
292  eCode = ENOMSG;
293  return 0;
294 }

◆ Init()

bool XrdXmlRdrTiny::Init ( )
static

Definition at line 300 of file XrdXmlRdrTiny.cc.

300 {return true;}

The documentation for this class was generated from the following files: