QString XmlDomElement::getAttribute<QString>(const QString& name, bool throwIfEmpty, const QString& defaultValue) const throw (Exception) { Q_UNUSED(defaultValue); Q_ASSERT(defaultValue == QString()); // defaultValue makes no sense in this method if (!mAttributes.contains(name)) { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, QString(), QString(tr("Attribute \"%1\" not found in node \"%2\".")).arg(name, mName)); } if (mAttributes.value(name).isEmpty() && throwIfEmpty) { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, QString(), QString(tr("Attribute \"%1\" in node \"%2\" must not be empty.")).arg(name, mName)); } return mAttributes.value(name); }
QString XmlDomElement::getText<QString>(bool throwIfEmpty, const QString& defaultValue) const throw (Exception) { Q_UNUSED(defaultValue); Q_ASSERT(defaultValue == QString()); // defaultValue makes no sense in this method if (hasChilds()) { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, mName, tr("A node with child elements cannot have a text.")); } if (mText.isEmpty() && throwIfEmpty) { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, mName, tr("The node text must not be empty.")); } return mText; }
XmlDomElement* XmlDomElement::getFirstChild(bool throwIfNotFound) const throw (Exception) { if (!mChilds.isEmpty()) return mChilds.first(); else if (!throwIfNotFound) return nullptr; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, QString(), QString(tr("No child in node \"%1\" found.")).arg(mName)); } }
Uuid XmlDomElement::getAttribute<Uuid>(const QString& name, bool throwIfEmpty, const Uuid& defaultValue) const throw (Exception) { QString attr = getAttribute<QString>(name, throwIfEmpty); Uuid obj(attr); if (!obj.isNull()) return obj; else if ((attr.isEmpty()) && (!throwIfEmpty)) return defaultValue; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, attr, QString(tr("Invalid UUID attribute \"%1\" in node \"%2\".")).arg(name, mName)); } }
Version XmlDomElement::getText<Version>(bool throwIfEmpty, const Version& defaultValue) const throw (Exception) { QString text = getText<QString>(throwIfEmpty); Version obj(text); if (obj.isValid()) return obj; else if ((text.isEmpty()) && (!throwIfEmpty)) return defaultValue; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, text, QString(tr("Invalid version number in node \"%1\".")).arg(mName)); } }
Uuid XmlDomElement::getText<Uuid>(bool throwIfEmpty, const Uuid& defaultValue) const throw (Exception) { QString text = getText<QString>(throwIfEmpty); Uuid obj(text); if (!obj.isNull()) return obj; else if ((text.isEmpty()) && (!throwIfEmpty)) return defaultValue; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, text, QString(tr("Invalid UUID in node \"%1\".")).arg(mName)); } }
QDateTime XmlDomElement::getText<QDateTime>(bool throwIfEmpty, const QDateTime& defaultValue) const throw (Exception) { QString text = getText<QString>(throwIfEmpty); QDateTime obj = QDateTime::fromString(text, Qt::ISODate).toLocalTime(); if (obj.isValid()) return obj; else if ((text.isEmpty()) && (!throwIfEmpty)) return defaultValue; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, text, QString(tr("Invalid date/time in node \"%1\".")).arg(mName)); } }
int XmlDomElement::getAttribute<int>(const QString& name, bool throwIfEmpty, const int& defaultValue) const throw (Exception) { QString attr = getAttribute<QString>(name, throwIfEmpty); bool ok = false; int value = attr.toInt(&ok); if (ok) return value; else if ((attr.isEmpty()) && (!throwIfEmpty)) return defaultValue; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, attr, QString(tr("Invalid integer attribute \"%1\" in node \"%2\".")).arg(name, mName)); } }
bool XmlDomElement::getAttribute<bool>(const QString& name, bool throwIfEmpty, const bool& defaultValue) const throw (Exception) { QString attr = getAttribute<QString>(name, throwIfEmpty); if (attr == "true") return true; else if (attr == "false") return false; else if ((attr.isEmpty()) && (!throwIfEmpty)) return defaultValue; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, attr, QString(tr("Invalid boolean attribute \"%1\" in node \"%2\".")).arg(name, mName)); } }
bool XmlDomElement::getText<bool>(bool throwIfEmpty, const bool& defaultValue) const throw (Exception) { QString text = getText<QString>(throwIfEmpty); if (text == "true") return true; else if (text == "false") return false; else if ((text.isEmpty()) && (!throwIfEmpty)) return defaultValue; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, text, QString(tr("Invalid boolean value in node \"%1\".")).arg(mName)); } }
qreal XmlDomElement::getText<qreal>(bool throwIfEmpty, const qreal& defaultValue) const throw (Exception) { QString text = getText<QString>(throwIfEmpty); bool ok = false; static_assert(sizeof(qreal) == sizeof(double), "Unsupported size of qreal type!"); qreal value = text.toDouble(&ok); if (ok) return value; else if ((text.isEmpty()) && (!throwIfEmpty)) return defaultValue; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, text, QString(tr("Invalid number in node \"%1\".")).arg(mName)); } }
VAlign XmlDomElement::getAttribute<VAlign>(const QString& name, bool throwIfEmpty, const VAlign& defaultValue) const throw (Exception) { QString attr = getAttribute<QString>(name, throwIfEmpty); try { VAlign obj = VAlign::fromString(attr); return obj; } catch (Exception& exc) { if ((attr.isEmpty()) && (!throwIfEmpty)) return defaultValue; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, attr, QString(tr("Invalid vertical align attribute \"%1\" in node \"%2\".")).arg(name, mName)); } } }
Length XmlDomElement::getText<Length>(bool throwIfEmpty, const Length& defaultValue) const throw (Exception) { QString text = getText<QString>(throwIfEmpty); try { Length obj = Length::fromMm(text); return obj; } catch (Exception& exc) { if ((text.isEmpty()) && (!throwIfEmpty)) return defaultValue; else { throw FileParseError(__FILE__, __LINE__, getDocFilePath(), -1, -1, text, QString(tr("Invalid length in node \"%1\".")).arg(mName)); } } }