35 #ifndef OPENMS_FORMAT_HANDLERS_XMLHANDLER_H 36 #define OPENMS_FORMAT_HANDLERS_XMLHANDLER_H 47 #include <xercesc/sax2/DefaultHandler.hpp> 48 #include <xercesc/sax/Locator.hpp> 49 #include <xercesc/sax2/Attributes.hpp> 72 XMLCh * convert(
const char * str)
const;
75 XMLCh * convert(
const std::string & str)
const;
78 XMLCh * convert(
const String & str)
const;
81 char * convert(
const XMLCh * str)
const;
89 static void appendASCII(
const XMLCh * str,
const XMLSize_t length,
String & result);
100 public xercesc::DefaultHandler
109 Exception::BaseException(file, line, function)
137 void fatalError(
const xercesc::SAXParseException &
exception);
138 void error(
const xercesc::SAXParseException &
exception);
139 void warning(
const xercesc::SAXParseException &
exception);
150 virtual void characters(
const XMLCh *
const chars,
const XMLSize_t length);
152 virtual void startElement(
const XMLCh *
const uri,
const XMLCh *
const localname,
const XMLCh *
const qname,
const xercesc::Attributes & attrs);
154 virtual void endElement(
const XMLCh *
const uri,
const XMLCh *
const localname,
const XMLCh *
const qname);
157 virtual void writeTo(std::ostream & );
203 inline bool equal_(
const XMLCh * a,
const XMLCh * b)
const 205 return xercesc::XMLString::compareString(a, b) == 0;
226 OPENMS_PRECONDITION(section < cv_terms_.size(),
"cvStringToEnum_: Index overflow (section number too large)");
228 std::vector<String>::const_iterator it = std::find(cv_terms_[section].begin(), cv_terms_[section].end(), term);
229 if (it != cv_terms_[section].end())
231 return it - cv_terms_[section].begin();
235 warning(LOAD,
String(
"Unexpected CV entry '") + message +
"'='" + term +
"'");
236 return result_on_error;
255 error(LOAD,
String(
"Int conversion error of \"") + in +
"\"");
263 return xercesc::XMLString::parseInt(in);
281 error(LOAD,
String(
"UInt conversion error of \"") + in +
"\"");
296 error(LOAD,
String(
"Double conversion error of \"") + in +
"\"");
311 error(LOAD,
String(
"Float conversion error of \"") + in +
"\"");
324 if (in ==
"true" || in ==
"TRUE" || in ==
"True" || in ==
"1")
328 else if (in ==
"false" || in ==
"FALSE" || in ==
"False" || in ==
"0")
334 error(LOAD,
String(
"Boolean conversion error of \"") + in +
"\"");
343 if (date_string !=
"")
349 date_string = date_string.
substr(0, 19);
350 date_time.
set(date_string);
354 error(LOAD,
String(
"DateTime conversion error of \"") + date_string +
"\"");
368 const XMLCh * val = a.getValue(sm_.
convert(name));
369 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + name +
"' not present!");
376 const XMLCh * val = a.getValue(sm_.
convert(name));
377 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + name +
"' not present!");
378 return xercesc::XMLString::parseInt(val);
384 const XMLCh * val = a.getValue(sm_.
convert(name));
385 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + name +
"' not present!");
392 String tmp(expectList_(attributeAsString_(a, name)));
393 return ListUtils::create<double>(tmp.
substr(1, tmp.size() - 2));
399 String tmp(expectList_(attributeAsString_(a, name)));
400 return ListUtils::create<Int>(tmp.
substr(1, tmp.size() - 2));
406 String tmp(expectList_(attributeAsString_(a, name)));
407 return ListUtils::create<String>(tmp.
substr(1, tmp.size() - 2));
417 const XMLCh * val = a.getValue(sm_.
convert(name));
433 const XMLCh * val = a.getValue(sm_.
convert(name));
436 value = xercesc::XMLString::parseInt(val);
449 const XMLCh * val = a.getValue(sm_.
convert(name));
452 value = xercesc::XMLString::parseInt(val);
465 const XMLCh * val = a.getValue(sm_.
convert(name));
481 const XMLCh * val = a.getValue(sm_.
convert(name));
484 value = attributeAsDoubleList_(a, name);
497 const XMLCh * val = a.getValue(sm_.
convert(name));
500 value = attributeAsStringList_(a, name);
513 const XMLCh * val = a.getValue(sm_.
convert(name));
516 value = attributeAsIntList_(a, name);
525 const XMLCh * val = a.getValue(name);
526 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + sm_.
convert(name) +
"' not present!");
533 const XMLCh * val = a.getValue(name);
534 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + sm_.
convert(name) +
"' not present!");
535 return xercesc::XMLString::parseInt(val);
541 const XMLCh * val = a.getValue(name);
542 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + sm_.
convert(name) +
"' not present!");
549 String tmp(expectList_(attributeAsString_(a, name)));
550 return ListUtils::create<double>(tmp.
substr(1, tmp.size() - 2));
556 String tmp(expectList_(attributeAsString_(a, name)));
557 return ListUtils::create<Int>(tmp.
substr(1, tmp.size() - 2));
563 String tmp(expectList_(attributeAsString_(a, name)));
564 return ListUtils::create<String>(tmp.
substr(1, tmp.size() - 2));
570 const XMLCh * val = a.getValue(name);
573 char * tmp2 = sm_.
convert(val);
586 const XMLCh * val = a.getValue(name);
589 value = xercesc::XMLString::parseInt(val);
598 const XMLCh * val = a.getValue(name);
601 value = xercesc::XMLString::parseInt(val);
610 const XMLCh * val = a.getValue(name);
626 const XMLCh * val = a.getValue(name);
629 value = attributeAsDoubleList_(a, name);
642 const XMLCh * val = a.getValue(name);
645 value = attributeAsIntList_(a, name);
658 const XMLCh * val = a.getValue(name);
661 value = attributeAsStringList_(a, name);
678 fatalError(LOAD,
String(
"List argument is not a string representation of a list!"));
688 #endif // OPENMS_FORMAT_HANDLERS_XMLHANDLER_H
std::vector< String > open_tags_
Stack of open XML tags.
Definition: XMLHandler.h:200
IntList attributeAsIntList_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to an IntList.
Definition: XMLHandler.h:397
bool optionalAttributeAsString_(String &value, const xercesc::Attributes &a, const char *name) const
Assigns the attribute content to the String value if the attribute is present.
Definition: XMLHandler.h:415
bool optionalAttributeAsDoubleList_(DoubleList &value, const xercesc::Attributes &a, const XMLCh *name) const
Assigns the attribute content to the DoubleList value if the attribute is present.
Definition: XMLHandler.h:624
A more convenient string class.
Definition: String.h:57
Exception that is thrown if the parsing is ended by some event (e.g. if only a prefix of the XML file...
Definition: XMLHandler.h:104
Int asInt_(const XMLCh *in)
Conversion of a Xerces string to an integer value.
Definition: XMLHandler.h:261
String version_
Schema version.
Definition: XMLHandler.h:190
std::vector< double > DoubleList
Vector of double precision real types.
Definition: ListUtils.h:66
IntList attributeAsIntList_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a IntList.
Definition: XMLHandler.h:554
void set(UInt month, UInt day, UInt year, UInt hour, UInt minute, UInt second)
sets data from six integers
bool optionalAttributeAsIntList_(IntList &value, const xercesc::Attributes &a, const XMLCh *name) const
Assigns the attribute content to the IntList value if the attribute is present.
Definition: XMLHandler.h:640
SignedSize cvStringToEnum_(const Size section, const String &term, const char *message, const SignedSize result_on_error=0)
Definition: XMLHandler.h:224
bool optionalAttributeAsInt_(Int &value, const xercesc::Attributes &a, const XMLCh *name) const
Assigns the attribute content to the Int value if the attribute is present.
Definition: XMLHandler.h:584
bool hasSuffix(const String &string) const
true if String ends with string, false otherwise
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:107
unsigned int UInt
Unsigned integer type.
Definition: Types.h:88
char * attributeAsString_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a String.
Definition: XMLHandler.h:523
bool optionalAttributeAsInt_(Int &value, const xercesc::Attributes &a, const char *name) const
Assigns the attribute content to the Int value if the attribute is present.
Definition: XMLHandler.h:431
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:59
Base class for XML handlers.
Definition: XMLHandler.h:99
DateTime asDateTime_(String date_string)
Conversion of a xs:datetime string to a DataTime value.
Definition: XMLHandler.h:340
UInt asUInt_(const String &in)
Conversion of a String to an unsigned integer value.
Definition: XMLHandler.h:267
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:128
ActionMode
Action to set the current mode (for error messages)
Definition: XMLHandler.h:116
Helper class for XML parsing that handles the memory management for conversions of Xerces strings...
Definition: XMLHandler.h:59
Int attributeAsInt_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to a Int.
Definition: XMLHandler.h:374
bool optionalAttributeAsUInt_(UInt &value, const xercesc::Attributes &a, const XMLCh *name) const
Assigns the attribute content to the UInt value if the attribute is present.
Definition: XMLHandler.h:596
bool has(Byte byte) const
true if String contains the byte, false otherwise
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
bool optionalAttributeAsStringList_(StringList &value, const xercesc::Attributes &a, const char *name) const
Assigns the attribute content to the StringList value if the attribute is present.
Definition: XMLHandler.h:495
static String writeXMLEscape(const String &to_escape)
Escapes a string and returns the escaped string.
Definition: XMLHandler.h:169
double toDouble() const
Conversion to double.
bool equal_(const XMLCh *a, const XMLCh *b) const
Returns if two xerces strings are equal.
Definition: XMLHandler.h:203
String error_message_
Error message of the last error.
Definition: XMLHandler.h:184
Int toInt() const
Conversion to int.
EndParsingSoftly(const char *file, int line, const char *function)
Definition: XMLHandler.h:108
String expectList_(const char *str) const
Definition: XMLHandler.h:673
std::vector< XMLCh * > xml_strings_
Definition: XMLHandler.h:92
DoubleList attributeAsDoubleList_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to a DoubleList.
Definition: XMLHandler.h:390
int exception
(Used by various macros. Indicates a rough category of the exception being caught.)
bool optionalAttributeAsDouble_(double &value, const xercesc::Attributes &a, const XMLCh *name) const
Assigns the attribute content to the double value if the attribute is present.
Definition: XMLHandler.h:608
StringList attributeAsStringList_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to an StringList.
Definition: XMLHandler.h:404
float asFloat_(const String &in)
Conversion of a String to a float value.
Definition: XMLHandler.h:302
bool optionalAttributeAsUInt_(UInt &value, const xercesc::Attributes &a, const char *name) const
Assigns the attribute content to the UInt value if the attribute is present.
Definition: XMLHandler.h:447
double attributeAsDouble_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to a double.
Definition: XMLHandler.h:382
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
XMLCh * convert(const char *str) const
Transcode the supplied C string to XMLCh* and take ownership of the XMLCh*.
bool asBool_(const String &in)
Conversion of a string to a boolean value.
Definition: XMLHandler.h:322
String file_
File name.
Definition: XMLHandler.h:187
DoubleList attributeAsDoubleList_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a DoubleList.
Definition: XMLHandler.h:547
char * attributeAsString_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to a String.
Definition: XMLHandler.h:366
Exception base class.
Definition: Exception.h:90
double attributeAsDouble_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a double.
Definition: XMLHandler.h:539
Invalid conversion exception.
Definition: Exception.h:363
Loading a file.
Definition: XMLHandler.h:118
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:74
double asDouble_(const String &in)
Conversion of a String to a double value.
Definition: XMLHandler.h:287
StringManager sm_
Helper class for string conversion.
Definition: XMLHandler.h:193
bool optionalAttributeAsDouble_(double &value, const xercesc::Attributes &a, const char *name) const
Assigns the attribute content to the double value if the attribute is present.
Definition: XMLHandler.h:463
Int asInt_(const String &in)
Conversion of a String to an integer value.
Definition: XMLHandler.h:246
bool optionalAttributeAsString_(String &value, const xercesc::Attributes &a, const XMLCh *name) const
Assigns the attribute content to the String value if the attribute is present.
Definition: XMLHandler.h:568
DateTime Class.
Definition: DateTime.h:55
String & substitute(char from, char to)
Replaces all occurrences of the character from by the character to.
String substr(size_t pos=0, size_t n=npos) const
Wrapper for the STL substr() method. Returns a String object with its contents initialized to a subst...
float toFloat() const
Conversion to float.
Int attributeAsInt_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a Int.
Definition: XMLHandler.h:531
std::vector< std::vector< String > > cv_terms_
Array of CV term lists (one sublist denotes one term and it's children)
Definition: XMLHandler.h:220
std::vector< char * > c_strings_
Definition: XMLHandler.h:93
int Int
Signed integer type.
Definition: Types.h:96
bool optionalAttributeAsIntList_(IntList &value, const xercesc::Attributes &a, const char *name) const
Assigns the attribute content to the IntList value if the attribute is present.
Definition: XMLHandler.h:511
bool optionalAttributeAsDoubleList_(DoubleList &value, const xercesc::Attributes &a, const char *name) const
Assigns the attribute content to the DoubleList value if the attribute is present.
Definition: XMLHandler.h:479
StringList attributeAsStringList_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a StringList.
Definition: XMLHandler.h:561
bool hasPrefix(const String &string) const
true if String begins with string, false otherwise
Parse Error exception.
Definition: Exception.h:608
bool optionalAttributeAsStringList_(StringList &value, const xercesc::Attributes &a, const XMLCh *name) const
Assigns the attribute content to the StringList value if the attribute is present.
Definition: XMLHandler.h:656