Bsoft  2.1.0
Bernard's software package
rwxml.cpp
Go to the documentation of this file.
1 
9 #ifdef HAVE_XML
10 
11 #include "rwxml.h"
12 #include "utilities.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
27 int xml_check_for_node(xmlNodePtr parent, const char* tag)
28 {
29  xmlNodePtr node;
30 
31  for ( node = parent->xmlChildrenNode; node; node=node->next )
32  if ( !xmlStrcmp(node->name, BAD_CAST tag) )
33  return 1;
34 
35  return 0;
36 }
37 
48 int xml_check_for_node_in_tree(xmlNodePtr parent, const char* tag)
49 {
50  xmlNodePtr node;
51 
52  int result(0);
53 
54  for ( node = parent->xmlChildrenNode; node && result==0; node=node->next ) {
55  if ( !xmlStrcmp(node->name, BAD_CAST tag) )
56  result = 1;
57  else
58  result = xml_check_for_node_in_tree(node, tag);
59  }
60 
61  return result;
62 }
63 
74 xmlNodePtr xml_find_node(xmlNodePtr parent, const char* tag)
75 {
76  xmlNodePtr node = NULL;
77 
78  for ( node = parent->xmlChildrenNode; node; node = node->next )
79  if ( !xmlStrcmp(node->name, BAD_CAST tag) )
80  return node;
81 
82  if ( verbose & VERB_FULL )
83  cerr << "Node " << tag << " of parent " << parent->name << " not found!" << endl;
84 
85  return node;
86 }
87 
99 int xml_copy_string(char* string, xmlNodePtr parent, const char* tag)
100 {
101  string[0] = 0;
102 
103  xmlNodePtr node = xml_find_node(parent, tag);
104 
105  if ( !node ) return -1;
106 
107  xmlChar* content = xmlNodeGetContent(node);
108 
109  if ( !content ) return 0;
110 
111  strcpy(string, (char *)content);
112 
113  xmlFree(content);
114 
115  if ( verbose & VERB_DEBUG )
116  cout << "DEBUG xml_copy_string: tag=" << tag << " string=" << string << " (" << strlen(string) << ")" << endl;
117 
118  return 0;
119 }
120 
131 string xml_get_string_attribute(xmlNodePtr parent, const char* tag)
132 {
133  xmlChar* content = xmlGetProp(parent, BAD_CAST tag);
134 
135  string value("");
136 
137  if ( !content ) return value;
138 
139  value = (char *)content;
140 
141  xmlFree(content);
142 
143  if ( verbose & VERB_DEBUG )
144  cout << "DEBUG xml_get_string_attribute: tag=" << tag << " value=" << value << endl;
145 
146  return value;
147 }
148 
159 long xml_get_integer_attribute(xmlNodePtr parent, const char* tag)
160 {
161  xmlChar* content = xmlGetProp(parent, BAD_CAST tag);
162 
163  if ( !content ) return 0;
164 
165  long value = atol((char *)content);
166 
167  xmlFree(content);
168 
169  if ( verbose & VERB_DEBUG )
170  cout << "DEBUG xml_get_integer_attribute: tag=" << tag << " value=" << value << endl;
171 
172  return value;
173 }
174 
185 double xml_get_real_attribute(xmlNodePtr parent, const char* tag)
186 {
187  xmlChar* content = xmlGetProp(parent, BAD_CAST tag);
188 
189  if ( !content ) return 0;
190 
191  double value = atof((char *)content);
192 
193  xmlFree(content);
194 
195  if ( verbose & VERB_DEBUG )
196  cout << "DEBUG xml_get_real_attribute: tag=" << tag << " value=" << value << endl;
197 
198  return value;
199 }
200 
212 string xml_get_string(xmlNodePtr parent, const char* tag)
213 {
214  string s;
215 
216  xmlNodePtr node = xml_find_node(parent, tag);
217 
218  if ( !node ) return s;
219 
220  xmlChar* content = xmlNodeGetContent(node);
221 
222  if ( !content ) return s;
223 
224  s = (char *) content;
225 
226  xmlFree(content);
227 
228  if ( verbose & VERB_DEBUG )
229  cout << "DEBUG xml_get_string: tag=" << tag << " s=" << s << " (" << s.length() << ")" << endl;
230 
231  return s;
232 }
233 
244 long xml_get_integer(xmlNodePtr parent, const char* tag)
245 {
246  long value(0);
247 
248  xmlNodePtr node = xml_find_node(parent, tag);
249 
250  if ( !node ) return value;
251 
252  xmlChar* content = xmlNodeGetContent(node);
253 
254  if ( !content ) return value;
255 
256  value = atol((char *)content);
257 
258  xmlFree(content);
259 
260  if ( verbose & VERB_DEBUG )
261  cout << "DEBUG xml_get_integer: tag=" << tag << " value=" << value << endl;
262 
263  return value;
264 }
265 
276 double xml_get_real(xmlNodePtr parent, const char* tag)
277 {
278  double value(0);
279 
280  xmlNodePtr node = xml_find_node(parent, tag);
281 
282  if ( !node ) return value;
283 
284  xmlChar* content = xmlNodeGetContent(node);
285 
286  if ( !content ) return value;
287 
288  value = atof((char *)content);
289 
290  xmlFree(content);
291 
292  if ( verbose & VERB_DEBUG )
293  cout << "DEBUG xml_get_real: tag=" << tag << " value=" << value << endl;
294 
295  return value;
296 }
297 
310 xmlAttrPtr xml_set_integer_attribute(xmlNodePtr parent, const char* tag, long value, const char* format)
311 {
312  xmlChar s[256];
313 
314  xmlStrPrintf(s, 256, (xmlFormat) format, value);
315 
316  xmlAttrPtr attr = xmlNewProp(parent, BAD_CAST tag, BAD_CAST s);
317 
318  if ( verbose & VERB_DEBUG )
319  cout << "DEBUG xml_set_integer_attribute: tag=" << tag << " string=" << s << " (" << xmlStrlen(s) << ")" << endl;
320 
321  return attr;
322 }
323 
336 xmlAttrPtr xml_set_real_attribute(xmlNodePtr parent, const char* tag, double value, const char* format)
337 {
338  xmlChar s[256];
339 
340  xmlStrPrintf(s, 256, (xmlFormat) format, value);
341 
342  xmlAttrPtr attr = xmlNewProp(parent, BAD_CAST tag, BAD_CAST s);
343 
344  if ( verbose & VERB_DEBUG )
345  cout << "DEBUG xml_set_real_attribute: tag=" << tag << " string=" << s << " (" << xmlStrlen(s) << ")" << endl;
346 
347  return attr;
348 }
349 
362 xmlNodePtr xml_set_integer(xmlNodePtr parent, const char* tag, long value, const char* format)
363 {
364  xmlChar s[256];
365 
366  xmlStrPrintf(s, 256, (xmlFormat) format, value);
367 
368  xmlNodePtr node = xmlNewChild(parent, NULL, BAD_CAST tag, BAD_CAST s);
369 
370  if ( verbose & VERB_DEBUG )
371  cout << "DEBUG xml_set_integer: tag=" << tag << " string=" << s << " (" << xmlStrlen(s) << ")" << endl;
372 
373  return node;
374 }
375 
388 xmlNodePtr xml_set_real(xmlNodePtr parent, const char* tag, double value, const char* format)
389 {
390  xmlChar s[256];
391 
392  xmlStrPrintf(s, 256, (xmlFormat) format, value);
393 
394  xmlNodePtr node = xmlNewChild(parent, NULL, BAD_CAST tag, BAD_CAST s);
395 
396  if ( verbose & VERB_DEBUG )
397  cout << "DEBUG xml_set_real: tag=" << tag << " string=" << s << " (" << xmlStrlen(s) << ")" << endl;
398 
399  return node;
400 }
401 
412 int xml_validate(const xmlDocPtr doc, Bstring& xsdfile)
413 {
414  if ( xsdfile.length() < 1 ) return 0;
415 
416  xmlDocPtr schema_doc = xmlParseFile(xsdfile.c_str());
417 
418  if ( !schema_doc ) {
419  cerr << "Error: the schema cannot be loaded or is not well-formed" << endl;
420  return -1;
421  }
422 
423  xmlSchemaParserCtxtPtr parser_ctxt = xmlSchemaNewDocParserCtxt(schema_doc);
424  if ( !parser_ctxt ) {
425  cerr << "Error: unable to create a parser context for the schema" << endl;
426  xmlFreeDoc(schema_doc);
427  return -2;
428  }
429 
430  xmlSchemaPtr schema = xmlSchemaParse(parser_ctxt);
431  if ( !schema ) {
432  cerr << "Error: the schema is not valid" << endl;
433  xmlSchemaFreeParserCtxt(parser_ctxt);
434  xmlFreeDoc(schema_doc);
435  return -3;
436  }
437 
438  xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema);
439  if ( !valid_ctxt ) {
440  cerr << "Error: unable to create a validation context for the schema" << endl;
441  xmlSchemaFree(schema);
442  xmlSchemaFreeParserCtxt(parser_ctxt);
443  xmlFreeDoc(schema_doc);
444  return -4;
445  }
446 
447  if ( verbose & VERB_DEBUG )
448  cout << "DEBUG xml_validate: file=" << xsdfile << endl;
449 
450  int is_valid = (xmlSchemaValidateDoc(valid_ctxt, doc) == 0);
451 
452  if ( verbose ) {
453  cout << "File is ";
454  if ( is_valid ) cout << "valid";
455  else cout << "not valid";
456  cout << " (" << xsdfile << ")" << endl;
457  }
458 
459  xmlSchemaFreeValidCtxt(valid_ctxt);
460  xmlSchemaFree(schema);
461  xmlSchemaFreeParserCtxt(parser_ctxt);
462  xmlFreeDoc(schema_doc);
463 
464  /* force the return value to be non-negative on success */
465  return is_valid ? 1 : 0;
466 }
467 
468 
469 #endif
470 
xml_get_string
string xml_get_string(xmlNodePtr parent, const char *tag)
xml_check_for_node_in_tree
int xml_check_for_node_in_tree(xmlNodePtr parent, const char *tag)
rwxml.h
Reads and writes micrograph XML files.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bstring
Definition: Bstring.h:29
xml_get_string_attribute
string xml_get_string_attribute(xmlNodePtr parent, const char *tag)
xml_set_integer
xmlNodePtr xml_set_integer(xmlNodePtr parent, const char *tag, long value, const char *format)
xml_get_real
double xml_get_real(xmlNodePtr parent, const char *tag)
xml_check_for_node
int xml_check_for_node(xmlNodePtr parent, const char *tag)
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
xml_copy_string
int xml_copy_string(char *string, xmlNodePtr parent, const char *tag)
xml_validate
int xml_validate(const xmlDocPtr doc, Bstring &xsdfile)
xml_get_real_attribute
double xml_get_real_attribute(xmlNodePtr parent, const char *tag)
xml_find_node
xmlNodePtr xml_find_node(xmlNodePtr parent, const char *tag)
verbose
int verbose
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
xmlFormat
const char * xmlFormat
Definition: rwxml.h:16
xml_set_real
xmlNodePtr xml_set_real(xmlNodePtr parent, const char *tag, double value, const char *format)
xml_get_integer_attribute
long xml_get_integer_attribute(xmlNodePtr parent, const char *tag)
xml_set_integer_attribute
xmlAttrPtr xml_set_integer_attribute(xmlNodePtr parent, const char *tag, long value, const char *format)
xml_get_integer
long xml_get_integer(xmlNodePtr parent, const char *tag)
xml_set_real_attribute
xmlAttrPtr xml_set_real_attribute(xmlNodePtr parent, const char *tag, double value, const char *format)