35 #ifndef OPENMS_FORMAT_HANDLERS_XMLHANDLER_H 36 #define OPENMS_FORMAT_HANDLERS_XMLHANDLER_H 48 #include <xercesc/sax2/DefaultHandler.hpp> 49 #include <xercesc/sax/Locator.hpp> 50 #include <xercesc/sax2/Attributes.hpp> 83 XMLCh * convert(
const char * str)
const;
86 XMLCh * convert(
const std::string & str)
const;
89 XMLCh * convert(
const String & str)
const;
92 char * convert(
const XMLCh * str)
const;
100 static void appendASCII(
const XMLCh * str,
const XMLSize_t length,
String & result);
111 public xercesc::DefaultHandler
121 Exception::BaseException(file, line, function)
149 void fatalError(
const xercesc::SAXParseException &
exception);
150 void error(
const xercesc::SAXParseException &
exception);
151 void warning(
const xercesc::SAXParseException &
exception);
162 virtual void characters(
const XMLCh *
const chars,
const XMLSize_t length);
164 virtual void startElement(
const XMLCh *
const uri,
const XMLCh *
const localname,
const XMLCh *
const qname,
const xercesc::Attributes & attrs);
166 virtual void endElement(
const XMLCh *
const uri,
const XMLCh *
const localname,
const XMLCh *
const qname);
169 virtual void writeTo(std::ostream & );
215 inline bool equal_(
const XMLCh * a,
const XMLCh * b)
const 217 return xercesc::XMLString::compareString(a, b) == 0;
238 OPENMS_PRECONDITION(section < cv_terms_.size(),
"cvStringToEnum_: Index overflow (section number too large)");
240 std::vector<String>::const_iterator it = std::find(cv_terms_[section].begin(), cv_terms_[section].end(), term);
241 if (it != cv_terms_[section].end())
243 return it - cv_terms_[section].begin();
247 warning(LOAD,
String(
"Unexpected CV entry '") + message +
"'='" + term +
"'");
248 return result_on_error;
267 error(LOAD,
String(
"Int conversion error of \"") + in +
"\"");
275 return xercesc::XMLString::parseInt(in);
293 error(LOAD,
String(
"UInt conversion error of \"") + in +
"\"");
308 error(LOAD,
String(
"Double conversion error of \"") + in +
"\"");
323 error(LOAD,
String(
"Float conversion error of \"") + in +
"\"");
337 if (in ==
"true" || in ==
"TRUE" || in ==
"True" || in ==
"1")
341 else if (in ==
"false" || in ==
"FALSE" || in ==
"False" || in ==
"0")
347 error(LOAD,
String(
"Boolean conversion error of \"") + in +
"\"");
356 if (date_string !=
"")
362 date_string = date_string.
substr(0, 19);
363 date_time.
set(date_string);
367 error(LOAD,
String(
"DateTime conversion error of \"") + date_string +
"\"");
381 const XMLCh * val = a.getValue(sm_.
convert(name));
382 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + name +
"' not present!");
389 const XMLCh * val = a.getValue(sm_.
convert(name));
390 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + name +
"' not present!");
391 return xercesc::XMLString::parseInt(val);
397 const XMLCh * val = a.getValue(sm_.
convert(name));
398 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + name +
"' not present!");
405 String tmp(expectList_(attributeAsString_(a, name)));
406 return ListUtils::create<double>(tmp.
substr(1, tmp.size() - 2));
412 String tmp(expectList_(attributeAsString_(a, name)));
413 return ListUtils::create<Int>(tmp.
substr(1, tmp.size() - 2));
419 String tmp(expectList_(attributeAsString_(a, name)));
420 return ListUtils::create<String>(tmp.
substr(1, tmp.size() - 2));
430 const XMLCh * val = a.getValue(sm_.
convert(name));
446 const XMLCh * val = a.getValue(sm_.
convert(name));
449 value = xercesc::XMLString::parseInt(val);
462 const XMLCh * val = a.getValue(sm_.
convert(name));
465 value = xercesc::XMLString::parseInt(val);
478 const XMLCh * val = a.getValue(sm_.
convert(name));
494 const XMLCh * val = a.getValue(sm_.
convert(name));
497 value = attributeAsDoubleList_(a, name);
510 const XMLCh * val = a.getValue(sm_.
convert(name));
513 value = attributeAsStringList_(a, name);
526 const XMLCh * val = a.getValue(sm_.
convert(name));
529 value = attributeAsIntList_(a, name);
538 const XMLCh * val = a.getValue(name);
539 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + sm_.
convert(name) +
"' not present!");
546 const XMLCh * val = a.getValue(name);
547 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + sm_.
convert(name) +
"' not present!");
548 return xercesc::XMLString::parseInt(val);
554 const XMLCh * val = a.getValue(name);
555 if (val == 0) fatalError(LOAD,
String(
"Required attribute '") + sm_.
convert(name) +
"' not present!");
562 String tmp(expectList_(attributeAsString_(a, name)));
563 return ListUtils::create<double>(tmp.
substr(1, tmp.size() - 2));
569 String tmp(expectList_(attributeAsString_(a, name)));
570 return ListUtils::create<Int>(tmp.
substr(1, tmp.size() - 2));
576 String tmp(expectList_(attributeAsString_(a, name)));
577 return ListUtils::create<String>(tmp.
substr(1, tmp.size() - 2));
583 const XMLCh * val = a.getValue(name);
586 char * tmp2 = sm_.
convert(val);
599 const XMLCh * val = a.getValue(name);
602 value = xercesc::XMLString::parseInt(val);
611 const XMLCh * val = a.getValue(name);
614 value = xercesc::XMLString::parseInt(val);
623 const XMLCh * val = a.getValue(name);
639 const XMLCh * val = a.getValue(name);
642 value = attributeAsDoubleList_(a, name);
655 const XMLCh * val = a.getValue(name);
658 value = attributeAsIntList_(a, name);
671 const XMLCh * val = a.getValue(name);
674 value = attributeAsStringList_(a, name);
691 fatalError(LOAD,
String(
"List argument is not a string representation of a list!"));
701 #endif // OPENMS_FORMAT_HANDLERS_XMLHANDLER_H bool equal_(const XMLCh *a, const XMLCh *b) const
Returns if two Xerces strings are equal.
Definition: XMLHandler.h:215
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:637
std::vector< String > open_tags_
Stack of open XML tags.
Definition: XMLHandler.h:212
char * attributeAsString_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a String.
Definition: XMLHandler.h:536
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:444
IntList attributeAsIntList_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to an IntList.
Definition: XMLHandler.h:410
bool has(Byte byte) const
true if String contains the byte, false otherwise
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:116
Int asInt_(const XMLCh *in)
Conversion of a Xerces string to an integer value.
Definition: XMLHandler.h:273
String version_
Schema version.
Definition: XMLHandler.h:202
std::vector< double > DoubleList
Vector of double precision real types.
Definition: ListUtils.h:66
StringList attributeAsStringList_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to an StringList.
Definition: XMLHandler.h:417
void set(UInt month, UInt day, UInt year, UInt hour, UInt minute, UInt second)
sets data from six integers
SignedSize cvStringToEnum_(const Size section, const String &term, const char *message, const SignedSize result_on_error=0)
Definition: XMLHandler.h:236
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:653
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:107
XMLCh * convert(const char *str) const
Transcode the supplied C string to XMLCh* and take ownership of the XMLCh*.
unsigned int UInt
Unsigned integer type.
Definition: Types.h:95
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:476
std::vector< Int > IntList
Vector of signed integers.
Definition: ListUtils.h:59
Base class for XML handlers.
Definition: XMLHandler.h:110
DateTime asDateTime_(String date_string)
Conversion of a xs:datetime string to a DateTime value.
Definition: XMLHandler.h:353
UInt asUInt_(const String &in)
Conversion of a String to an unsigned integer value.
Definition: XMLHandler.h:279
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:135
ActionMode
Action to set the current mode (for error messages)
Definition: XMLHandler.h:128
double attributeAsDouble_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to a double.
Definition: XMLHandler.h:395
Definition: XMLHandler.h:70
Int attributeAsInt_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a Int.
Definition: XMLHandler.h:544
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:492
Int attributeAsInt_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to a Int.
Definition: XMLHandler.h:387
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
static String writeXMLEscape(const String &to_escape)
Escapes a string and returns the escaped string.
Definition: XMLHandler.h:181
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:524
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...
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:621
String error_message_
Error message of the last error.
Definition: XMLHandler.h:196
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:581
EndParsingSoftly(const char *file, int line, const char *function)
Definition: XMLHandler.h:120
std::vector< XMLCh * > xml_strings_
Definition: XMLHandler.h:103
DoubleList attributeAsDoubleList_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a DoubleList.
Definition: XMLHandler.h:560
Int toInt() const
Conversion to int.
char * attributeAsString_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to a String.
Definition: XMLHandler.h:379
double toDouble() const
Conversion to double.
int exception
(Used by various macros. Indicates a rough category of the exception being caught.)
float asFloat_(const String &in)
Conversion of a String to a float value.
Definition: XMLHandler.h:314
IntList attributeAsIntList_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a IntList.
Definition: XMLHandler.h:567
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
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:669
bool asBool_(const String &in)
Conversion of a string to a boolean value.
Definition: XMLHandler.h:335
float toFloat() const
Conversion to float.
String file_
File name.
Definition: XMLHandler.h:199
Exception base class.
Definition: Exception.h:90
Invalid conversion exception.
Definition: Exception.h:363
Loading a file.
Definition: XMLHandler.h:130
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:299
StringManager sm_
Helper class for string conversion.
Definition: XMLHandler.h:205
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:609
Int asInt_(const String &in)
Conversion of a String to an integer value.
Definition: XMLHandler.h:258
bool hasPrefix(const String &string) const
true if String begins with string, false otherwise
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
DateTime Class.
Definition: DateTime.h:55
String & substitute(char from, char to)
Replaces all occurrences of the character from by the character to.
StringList attributeAsStringList_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a StringList.
Definition: XMLHandler.h:574
String expectList_(const char *str) const
Definition: XMLHandler.h:686
std::vector< std::vector< String > > cv_terms_
Array of CV term lists (one sublist denotes one term and it's children)
Definition: XMLHandler.h:232
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:460
std::vector< char * > c_strings_
Definition: XMLHandler.h:104
int Int
Signed integer type.
Definition: Types.h:103
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:597
double attributeAsDouble_(const xercesc::Attributes &a, const XMLCh *name) const
Converts an attribute to a double.
Definition: XMLHandler.h:552
DoubleList attributeAsDoubleList_(const xercesc::Attributes &a, const char *name) const
Converts an attribute to a DoubleList.
Definition: XMLHandler.h:403
bool hasSuffix(const String &string) const
true if String ends with string, false otherwise
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:508
Parse Error exception.
Definition: Exception.h:623
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:428