/usr/include/OpenMS/FORMAT/HANDLERS/XMLHandler.h is in libopenms-dev 1.11.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | // --------------------------------------------------------------------------
// OpenMS -- Open-Source Mass Spectrometry
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
//
// This software is released under a three-clause BSD license:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of any author or any participating institution
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS.
// --------------------------------------------------------------------------
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// --------------------------------------------------------------------------
// $Maintainer: Chris Bielow $
// $Authors: Marc Sturm, Chris Bielow $
// --------------------------------------------------------------------------
#ifndef OPENMS_FORMAT_HANDLERS_XMLHANDLER_H
#define OPENMS_FORMAT_HANDLERS_XMLHANDLER_H
#include <iostream>
#include <OpenMS/CONCEPT/Types.h>
#include <OpenMS/CONCEPT/Macros.h>
#include <OpenMS/DATASTRUCTURES/DateTime.h>
#include <OpenMS/METADATA/MetaInfoInterface.h>
#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/sax/Locator.hpp>
#include <xercesc/sax2/Attributes.hpp>
#include <algorithm>
namespace OpenMS
{
namespace Internal
{
/// Helper class for XML parsing that handles the memory management for conversions of Xerces strings
class OPENMS_DLLAPI StringManager
{
public:
/// Constructor
StringManager();
/// Destructor. Destroys the strings in the various lists
~StringManager();
/// Frees memory of all owned strings
void clear();
/// Transcode the supplied C string to XMLCh* and take ownership of the XMLCh*
XMLCh * convert(const char * str) const;
/// Transcode the supplied C++ string to XMLCh* and take ownership of the XMLCh*
XMLCh * convert(const std::string & str) const;
/// Transcode the supplied OpenMS string to XMLCh* and take ownership of the XMLCh*
XMLCh * convert(const String & str) const;
/// Transcode the supplied XMLCh* to a C string and take ownership of the C string
char * convert(const XMLCh * str) const;
private:
mutable std::vector<XMLCh *> xml_strings_;
mutable std::vector<char *> c_strings_;
};
/**
@brief Base class for XML handlers.
*/
class OPENMS_DLLAPI XMLHandler :
public xercesc::DefaultHandler
{
public:
/// Exception that is thrown if the parsing is ended by some event (e.g. if only a prefix of the XML file is needed).
class OPENMS_DLLAPI EndParsingSoftly :
public Exception::BaseException
{
public:
EndParsingSoftly(const char * file, int line, const char * function) :
Exception::BaseException(file, line, function)
{
}
};
///Action to set the current mode (for error messages)
enum ActionMode
{
LOAD, ///< Loading a file
STORE ///< Storing a file
};
/// Default constructor
XMLHandler(const String & filename, const String & version);
/// Destructor
virtual ~XMLHandler();
/// Release internal memory used for parsing (call
void reset();
/**
@name Reimplemented XERCES-C error handlers
These methods forward the error message to our own error handlers below.
*/
//@{
void fatalError(const xercesc::SAXParseException & exception);
void error(const xercesc::SAXParseException & exception);
void warning(const xercesc::SAXParseException & exception);
//@}
/// Fatal error handler. Throws a ParseError exception
void fatalError(ActionMode mode, const String & msg, UInt line = 0, UInt column = 0) const;
/// Error handler for recoverable errors.
void error(ActionMode mode, const String & msg, UInt line = 0, UInt column = 0) const;
/// Warning handler.
void warning(ActionMode mode, const String & msg, UInt line = 0, UInt column = 0) const;
/// Parsing method for character data
virtual void characters(const XMLCh * const chars, const XMLSize_t length);
/// Parsing method for opening tags
virtual void startElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname, const xercesc::Attributes & attrs);
/// Parsing method for closing tags
virtual void endElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname);
/// Writes the contents to a stream.
virtual void writeTo(std::ostream & /*os*/)
{
}
/// Returns the last error description
String errorString();
protected:
/// Error message of the last error
mutable String error_message_;
/// File name
String file_;
/// Schema version
String version_;
/// Helper class for string conversion
StringManager sm_;
/**
@brief Stack of open XML tags
This member is used only in those XML parsers that need this information.
*/
std::vector<String> open_tags_;
/// Returns if two xerces strings are equal
inline bool equal_(const XMLCh * a, const XMLCh * b)
{
return xercesc::XMLString::compareString(a, b) == 0;
}
///@name General MetaInfo handling (for idXML, featureXML, consensusXML)
//@{
///Writes the content of MetaInfoInterface to the file
void writeUserParam_(const String & tag_name, std::ostream & os, const MetaInfoInterface & meta, UInt indent) const;
//@}
///@name controlled vocabulary handling methods
//@{
/// Array of CV term lists (one sublist denotes one term and it's children)
std::vector<std::vector<String> > cv_terms_;
/// Converts @p term to the index of the term in the cv_terms_ entry @p section
/// If the term is not found, @p result_on_error is returned (0 by default)
inline SignedSize cvStringToEnum_(const Size section, const String & term, const char * message, const SignedSize result_on_error = 0)
{
OPENMS_PRECONDITION(section < cv_terms_.size(), "cvStringToEnum_: Index overflow (section number too large)");
std::vector<String>::const_iterator it = std::find(cv_terms_[section].begin(), cv_terms_[section].end(), term);
if (it != cv_terms_[section].end())
{
return it - cv_terms_[section].begin();
}
else
{
warning(LOAD, String("Unexpected CV entry '") + message + "'='" + term + "'");
return result_on_error;
}
}
//@}
///@name String conversion
//@{
/// Conversion of a String to an integer value
inline Int asInt_(const String & in)
{
Int res = 0;
try
{
res = in.toInt();
}
catch (Exception::ConversionError)
{
error(LOAD, String("Int conversion error of \"") + in + "\"");
}
return res;
}
/// Conversion of a Xerces string to an integer value
inline Int asInt_(const XMLCh * in)
{
return xercesc::XMLString::parseInt(in);
}
/// Conversion of a String to an unsigned integer value
inline UInt asUInt_(const String & in)
{
UInt res = 0;
try
{
Int tmp = in.toInt();
if (tmp < 0)
{
Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__, "");
}
res = UInt(tmp);
}
catch (Exception::ConversionError)
{
error(LOAD, String("UInt conversion error of \"") + in + "\"");
}
return res;
}
/// Conversion of a String to a double value
inline double asDouble_(const String & in)
{
double res = 0.0;
try
{
res = in.toDouble();
}
catch (Exception::ConversionError)
{
error(LOAD, String("Double conversion error of \"") + in + "\"");
}
return res;
}
/// Conversion of a String to a float value
inline float asFloat_(const String & in)
{
float res = 0.0;
try
{
res = in.toFloat();
}
catch (Exception::ConversionError)
{
error(LOAD, String("Float conversion error of \"") + in + "\"");
}
return res;
}
/**
@brief Conversion of a string to a boolean value
'true', 'false', '1' and '0' are accpeted.
@n For all other values a parse error is produced.
*/
inline bool asBool_(const String & in)
{
if (in == "true" || in == "TRUE" || in == "True" || in == "1")
{
return true;
}
else if (in == "false" || in == "FALSE" || in == "False" || in == "0")
{
return false;
}
else
{
error(LOAD, String("Boolean conversion error of \"") + in + "\"");
}
return false;
}
/// Conversion of a xs:datetime string to a DataTime value
inline DateTime asDateTime_(String date_string)
{
DateTime date_time;
if (date_string != "")
{
try
{
//strip away milliseconds
date_string.trim();
date_string = date_string.substr(0, 19);
date_time.set(date_string);
}
catch (Exception::ParseError err)
{
error(LOAD, String("DateTime conversion error of \"") + date_string + "\"");
}
}
return date_time;
}
//@}
///@name Accessing attributes
//@{
/// Converts an attribute to a String
inline char * attributeAsString_(const xercesc::Attributes & a, const char * name) const
{
const XMLCh * val = a.getValue(sm_.convert(name));
if (val == 0) fatalError(LOAD, String("Required attribute '") + name + "' not present!");
return sm_.convert(val);
}
/// Converts an attribute to a Int
inline Int attributeAsInt_(const xercesc::Attributes & a, const char * name) const
{
const XMLCh * val = a.getValue(sm_.convert(name));
if (val == 0) fatalError(LOAD, String("Required attribute '") + name + "' not present!");
return xercesc::XMLString::parseInt(val);
}
/// Converts an attribute to a DoubleReal
inline DoubleReal attributeAsDouble_(const xercesc::Attributes & a, const char * name) const
{
const XMLCh * val = a.getValue(sm_.convert(name));
if (val == 0) fatalError(LOAD, String("Required attribute '") + name + "' not present!");
return String(sm_.convert(val)).toDouble();
}
/// Converts an attribute to a DoubleList
inline DoubleList attributeAsDoubleList_(const xercesc::Attributes & a, const char * name) const
{
String tmp(expectList_(attributeAsString_(a, name)));
return DoubleList::create(tmp.substr(1, tmp.size() - 2));
}
/// Converts an attribute to an IntList
inline IntList attributeAsIntList_(const xercesc::Attributes & a, const char * name) const
{
String tmp(expectList_(attributeAsString_(a, name)));
return IntList::create(tmp.substr(1, tmp.size() - 2));
}
/// Converts an attribute to an StringList
inline StringList attributeAsStringList_(const xercesc::Attributes & a, const char * name) const
{
String tmp(expectList_(attributeAsString_(a, name)));
return StringList::create(tmp.substr(1, tmp.size() - 2));
}
/**
@brief Assigns the attribute content to the String @a value if the attribute is present
@return if the attribute was present
*/
inline bool optionalAttributeAsString_(String & value, const xercesc::Attributes & a, const char * name) const
{
const XMLCh * val = a.getValue(sm_.convert(name));
if (val != 0)
{
value = sm_.convert(val);
return true;
}
return false;
}
/**
@brief Assigns the attribute content to the Int @a value if the attribute is present
@return if the attribute was present
*/
inline bool optionalAttributeAsInt_(Int & value, const xercesc::Attributes & a, const char * name) const
{
const XMLCh * val = a.getValue(sm_.convert(name));
if (val != 0)
{
value = xercesc::XMLString::parseInt(val);
return true;
}
return false;
}
/**
@brief Assigns the attribute content to the UInt @a value if the attribute is present
@return if the attribute was present
*/
inline bool optionalAttributeAsUInt_(UInt & value, const xercesc::Attributes & a, const char * name) const
{
const XMLCh * val = a.getValue(sm_.convert(name));
if (val != 0)
{
value = xercesc::XMLString::parseInt(val);
return true;
}
return false;
}
/**
@brief Assigns the attribute content to the DoubleReal @a value if the attribute is present
@return if the attribute was present
*/
inline bool optionalAttributeAsDouble_(DoubleReal & value, const xercesc::Attributes & a, const char * name) const
{
const XMLCh * val = a.getValue(sm_.convert(name));
if (val != 0)
{
value = String(sm_.convert(val)).toDouble();
return true;
}
return false;
}
/**
@brief Assigns the attribute content to the DoubleList @a value if the attribute is present
@return if the attribute was present
*/
inline bool optionalAttributeAsDoubleList_(DoubleList & value, const xercesc::Attributes & a, const char * name) const
{
const XMLCh * val = a.getValue(sm_.convert(name));
if (val != 0)
{
value = attributeAsDoubleList_(a, name);
return true;
}
return false;
}
/**
@brief Assigns the attribute content to the StringList @a value if the attribute is present
@return if the attribute was present
*/
inline bool optionalAttributeAsStringList_(StringList & value, const xercesc::Attributes & a, const char * name) const
{
const XMLCh * val = a.getValue(sm_.convert(name));
if (val != 0)
{
value = attributeAsStringList_(a, name);
return true;
}
return false;
}
/**
@brief Assigns the attribute content to the IntList @a value if the attribute is present
@return if the attribute was present
*/
inline bool optionalAttributeAsIntList_(IntList & value, const xercesc::Attributes & a, const char * name) const
{
const XMLCh * val = a.getValue(sm_.convert(name));
if (val != 0)
{
value = attributeAsIntList_(a, name);
return true;
}
return false;
}
/// Converts an attribute to a String
inline char * attributeAsString_(const xercesc::Attributes & a, const XMLCh * name) const
{
const XMLCh * val = a.getValue(name);
if (val == 0) fatalError(LOAD, String("Required attribute '") + sm_.convert(name) + "' not present!");
return sm_.convert(val);
}
/// Converts an attribute to a Int
inline Int attributeAsInt_(const xercesc::Attributes & a, const XMLCh * name) const
{
const XMLCh * val = a.getValue(name);
if (val == 0) fatalError(LOAD, String("Required attribute '") + sm_.convert(name) + "' not present!");
return xercesc::XMLString::parseInt(val);
}
/// Converts an attribute to a DoubleReal
inline DoubleReal attributeAsDouble_(const xercesc::Attributes & a, const XMLCh * name) const
{
const XMLCh * val = a.getValue(name);
if (val == 0) fatalError(LOAD, String("Required attribute '") + sm_.convert(name) + "' not present!");
return String(sm_.convert(val)).toDouble();
}
/// Converts an attribute to a DoubleList
inline DoubleList attributeAsDoubleList_(const xercesc::Attributes & a, const XMLCh * name) const
{
String tmp(expectList_(attributeAsString_(a, name)));
return DoubleList::create(tmp.substr(1, tmp.size() - 2));
}
/// Converts an attribute to a IntList
inline IntList attributeAsIntList_(const xercesc::Attributes & a, const XMLCh * name) const
{
String tmp(expectList_(attributeAsString_(a, name)));
return IntList::create(tmp.substr(1, tmp.size() - 2));
}
/// Converts an attribute to a StringList
inline StringList attributeAsStringList_(const xercesc::Attributes & a, const XMLCh * name) const
{
String tmp(expectList_(attributeAsString_(a, name)));
return StringList::create(tmp.substr(1, tmp.size() - 2));
}
/// Assigns the attribute content to the String @a value if the attribute is present
inline bool optionalAttributeAsString_(String & value, const xercesc::Attributes & a, const XMLCh * name) const
{
const XMLCh * val = a.getValue(name);
if (val != 0)
{
char * tmp2 = sm_.convert(val);
if (String(tmp2) != "")
{
value = tmp2;
return true;
}
}
return false;
}
/// Assigns the attribute content to the Int @a value if the attribute is present
inline bool optionalAttributeAsInt_(Int & value, const xercesc::Attributes & a, const XMLCh * name) const
{
const XMLCh * val = a.getValue(name);
if (val != 0)
{
value = xercesc::XMLString::parseInt(val);
return true;
}
return false;
}
/// Assigns the attribute content to the UInt @a value if the attribute is present
inline bool optionalAttributeAsUInt_(UInt & value, const xercesc::Attributes & a, const XMLCh * name) const
{
const XMLCh * val = a.getValue(name);
if (val != 0)
{
value = xercesc::XMLString::parseInt(val);
return true;
}
return false;
}
/// Assigns the attribute content to the DoubleReal @a value if the attribute is present
inline bool optionalAttributeAsDouble_(DoubleReal & value, const xercesc::Attributes & a, const XMLCh * name) const
{
const XMLCh * val = a.getValue(name);
if (val != 0)
{
value = String(sm_.convert(val)).toDouble();
return true;
}
return false;
}
/**
@brief Assigns the attribute content to the DoubleList @a value if the attribute is present
@return if the attribute was present
*/
inline bool optionalAttributeAsDoubleList_(DoubleList & value, const xercesc::Attributes & a, const XMLCh * name) const
{
const XMLCh * val = a.getValue(name);
if (val != 0)
{
value = attributeAsDoubleList_(a, name);
return true;
}
return false;
}
/**
@brief Assigns the attribute content to the IntList @a value if the attribute is present
@return if the attribute was present
*/
inline bool optionalAttributeAsIntList_(IntList & value, const xercesc::Attributes & a, const XMLCh * name) const
{
const XMLCh * val = a.getValue(name);
if (val != 0)
{
value = attributeAsIntList_(a, name);
return true;
}
return false;
}
/**
@brief Assigns the attribute content to the StringList @a value if the attribute is present
@return if the attribute was present
*/
inline bool optionalAttributeAsStringList_(StringList & value, const xercesc::Attributes & a, const XMLCh * name) const
{
const XMLCh * val = a.getValue(name);
if (val != 0)
{
value = attributeAsStringList_(a, name);
return true;
}
return false;
}
//@}
private:
/// Not implemented
XMLHandler();
inline String expectList_(const char * str) const
{
String tmp(str);
if (!(tmp.hasPrefix('[') && tmp.hasSuffix(']')))
{
fatalError(LOAD, String("List argument is not a string representation of a list!"));
}
return tmp;
}
};
} // namespace Internal
} // namespace OpenMS
#endif // OPENMS_FORMAT_HANDLERS_XMLHANDLER_H
|