| 1 | //============================================================================= |
| 2 | // MuseScore sftools |
| 3 | // |
| 4 | // Copyright (C) 2002-2011 Werner Schweer |
| 5 | // |
| 6 | // This work is free software; you can redistribute it and/or |
| 7 | // modify it under the terms of the GNU Library General Public |
| 8 | // License as published by the Free Software Foundation; either |
| 9 | // version 2 of the License, or (at your option) any later version. |
| 10 | // |
| 11 | // In addition, as a special exception, licensor gives permission to |
| 12 | // link the code of this work with the OpenSSL Library (or with modified |
| 13 | // versions of OpenSSL that use the same license as OpenSSL), and |
| 14 | // distribute linked combinations including the two. You must obey the |
| 15 | // GNU General Public License in all respects for all of the code used |
| 16 | // other than OpenSSL. If you modify this file, you may extend this |
| 17 | // exception to your version of the file, but you are not obligated to |
| 18 | // do so. If you do not wish to do so, delete this exception statement |
| 19 | // from your version. (This exception is necessary should this work be |
| 20 | // included in a GPL-licenced work.) |
| 21 | // |
| 22 | // See COPYING.LIB for the licence text and disclaimer of warranty. |
| 23 | //============================================================================= |
| 24 | |
| 25 | #include "xml.h" |
| 26 | |
| 27 | #include <QtCore/QStringList> |
| 28 | |
| 29 | QString docName; |
| 30 | |
| 31 | //--------------------------------------------------------- |
| 32 | // Xml |
| 33 | //--------------------------------------------------------- |
| 34 | |
| 35 | Xml::Xml() |
| 36 | { |
| 37 | stack.clear(); |
| 38 | } |
| 39 | |
| 40 | Xml::Xml(QIODevice* device) |
| 41 | : QTextStream(device) |
| 42 | { |
| 43 | setCodec("UTF-8"); |
| 44 | stack.clear(); |
| 45 | } |
| 46 | |
| 47 | //--------------------------------------------------------- |
| 48 | // putLevel |
| 49 | //--------------------------------------------------------- |
| 50 | |
| 51 | void Xml::putLevel() |
| 52 | { |
| 53 | int level = stack.size(); |
| 54 | for (int i = 0; i < level * 2; ++i) |
| 55 | *this << ' '; |
| 56 | } |
| 57 | |
| 58 | //--------------------------------------------------------- |
| 59 | // header |
| 60 | //--------------------------------------------------------- |
| 61 | |
| 62 | void Xml::header() |
| 63 | { |
| 64 | *this << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
| 65 | } |
| 66 | |
| 67 | //--------------------------------------------------------- |
| 68 | // stag |
| 69 | // <mops attribute="value"> |
| 70 | //--------------------------------------------------------- |
| 71 | |
| 72 | void Xml::stag(const QString& s) |
| 73 | { |
| 74 | putLevel(); |
| 75 | *this << '<' << s << '>' << endl; |
| 76 | stack.append(s.split(' ')[0]); |
| 77 | } |
| 78 | |
| 79 | //--------------------------------------------------------- |
| 80 | // etag |
| 81 | // </mops> |
| 82 | //--------------------------------------------------------- |
| 83 | |
| 84 | void Xml::etag() |
| 85 | { |
| 86 | putLevel(); |
| 87 | *this << "</" << stack.takeLast() << '>' << endl; |
| 88 | } |
| 89 | |
| 90 | //--------------------------------------------------------- |
| 91 | // tagE |
| 92 | // <mops attribute="value"/> |
| 93 | //--------------------------------------------------------- |
| 94 | |
| 95 | void Xml::tagE(const char* format, ...) |
| 96 | { |
| 97 | va_list args; |
| 98 | va_start(args, format); |
| 99 | putLevel(); |
| 100 | *this << '<'; |
| 101 | char buffer[BS]; |
| 102 | vsnprintf(buffer, BS, format, args); |
| 103 | *this << buffer; |
| 104 | va_end(args); |
| 105 | *this << "/>" << endl; |
| 106 | } |
| 107 | |
| 108 | //--------------------------------------------------------- |
| 109 | // tagE |
| 110 | //--------------------------------------------------------- |
| 111 | |
| 112 | void Xml::tagE(const QString& s) |
| 113 | { |
| 114 | putLevel(); |
| 115 | *this << '<' << s << "/>\n"; |
| 116 | } |
| 117 | |
| 118 | //--------------------------------------------------------- |
| 119 | // ntag |
| 120 | // <mops> without newline |
| 121 | //--------------------------------------------------------- |
| 122 | |
| 123 | void Xml::ntag(const char* name) |
| 124 | { |
| 125 | putLevel(); |
| 126 | *this << "<" << name << ">"; |
| 127 | } |
| 128 | |
| 129 | //--------------------------------------------------------- |
| 130 | // netag |
| 131 | // </mops> without indentation |
| 132 | //--------------------------------------------------------- |
| 133 | |
| 134 | void Xml::netag(const char* s) |
| 135 | { |
| 136 | *this << "</" << s << '>' << endl; |
| 137 | } |
| 138 | |
| 139 | void Xml::tag(const QString& name, QVariant data) |
| 140 | { |
| 141 | QString ename(name.split(' ')[0]); |
| 142 | |
| 143 | putLevel(); |
| 144 | switch(data.type()) { |
| 145 | case QVariant::Bool: |
| 146 | case QVariant::Char: |
| 147 | case QVariant::Int: |
| 148 | case QVariant::UInt: |
| 149 | *this << "<" << name << ">"; |
| 150 | *this << data.toInt(); |
| 151 | *this << "</" << ename << ">\n"; |
| 152 | break; |
| 153 | case QVariant::Double: |
| 154 | *this << "<" << name << ">"; |
| 155 | *this << data.value<double>(); |
| 156 | *this << "</" << ename << ">\n"; |
| 157 | break; |
| 158 | case QVariant::String: |
| 159 | *this << "<" << name << ">"; |
| 160 | *this << xmlString(data.value<QString>()); |
| 161 | *this << "</" << ename << ">\n"; |
| 162 | break; |
| 163 | default: |
| 164 | qDebug("Xml::tag: unsupported type %d\n", data.type()); |
| 165 | // abort(); |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | //--------------------------------------------------------- |
| 171 | // xmlString |
| 172 | //--------------------------------------------------------- |
| 173 | |
| 174 | QString Xml::xmlString(const QString& ss) |
| 175 | { |
| 176 | QString s(ss); |
| 177 | s.replace('&', "&"); |
| 178 | s.replace('<', "<"); |
| 179 | s.replace('>', ">"); |
| 180 | s.replace('\'', "'"); |
| 181 | s.replace('"', """); |
| 182 | return s; |
| 183 | } |
| 184 | |
| 185 | //--------------------------------------------------------- |
| 186 | // dump |
| 187 | //--------------------------------------------------------- |
| 188 | |
| 189 | void Xml::dump(int len, const unsigned char* p) |
| 190 | { |
| 191 | putLevel(); |
| 192 | int col = 0; |
| 193 | setFieldWidth(5); |
| 194 | setNumberFlags(numberFlags() | QTextStream::ShowBase); |
| 195 | setIntegerBase(16); |
| 196 | for (int i = 0; i < len; ++i, ++col) { |
| 197 | if (col >= 16) { |
| 198 | setFieldWidth(0); |
| 199 | *this << endl; |
| 200 | col = 0; |
| 201 | putLevel(); |
| 202 | setFieldWidth(5); |
| 203 | } |
| 204 | *this << (p[i] & 0xff); |
| 205 | } |
| 206 | if (col) |
| 207 | *this << endl << dec; |
| 208 | setFieldWidth(0); |
| 209 | setIntegerBase(10); |
| 210 | } |
| 211 | |
| 212 | //--------------------------------------------------------- |
| 213 | // printDomElementPath |
| 214 | //--------------------------------------------------------- |
| 215 | |
| 216 | static QString domElementPath(const QDomElement& e) |
| 217 | { |
| 218 | QString s; |
| 219 | QDomNode dn(e); |
| 220 | while (!dn.parentNode().isNull()) { |
| 221 | dn = dn.parentNode(); |
| 222 | const QDomElement& e = dn.toElement(); |
| 223 | const QString k(e.tagName()); |
| 224 | if (!s.isEmpty()) |
| 225 | s += ":"; |
| 226 | s += k; |
| 227 | } |
| 228 | return s; |
| 229 | } |
| 230 | |
| 231 | //--------------------------------------------------------- |
| 232 | // domError |
| 233 | //--------------------------------------------------------- |
| 234 | |
| 235 | void domError(const QDomElement& e) |
| 236 | { |
| 237 | QString m; |
| 238 | QString s = domElementPath(e); |
| 239 | if (!docName.isEmpty()) |
| 240 | m = QString("<%1>:").arg(docName); |
| 241 | int ln = e.lineNumber(); |
| 242 | if (ln != -1) |
| 243 | m += QString("line:%1 ").arg(ln); |
| 244 | int col = e.columnNumber(); |
| 245 | if (col != -1) |
| 246 | m += QString("col:%1 ").arg(col); |
| 247 | m += QString("%1: Unknown Node <%2>, type %3").arg(s).arg(e.tagName()).arg(e.nodeType()); |
| 248 | if (e.isText()) |
| 249 | m += QString(" text node <%1>").arg(e.toText().data()); |
| 250 | qDebug("%s", qPrintable(m)); |
| 251 | } |
| 252 | |
| 253 | //--------------------------------------------------------- |
| 254 | // domNotImplemented |
| 255 | //--------------------------------------------------------- |
| 256 | |
| 257 | void domNotImplemented(const QDomElement& e) |
| 258 | { |
| 259 | QString s = domElementPath(e); |
| 260 | if (!docName.isEmpty()) |
| 261 | qDebug("<%s>:", qPrintable(docName)); |
| 262 | qDebug("%s: Node not implemented: <%s>, type %d\n", |
| 263 | qPrintable(s), qPrintable(e.tagName()), e.nodeType()); |
| 264 | if (e.isText()) |
| 265 | qDebug(" text node <%s>\n", qPrintable(e.toText().data())); |
| 266 | } |
| 267 | |
| 268 | //--------------------------------------------------------- |
| 269 | // writeHtml |
| 270 | //--------------------------------------------------------- |
| 271 | |
| 272 | void Xml::writeHtml(const QString& s) |
| 273 | { |
| 274 | QStringList sl(s.split("\n")); |
| 275 | // |
| 276 | // remove first line from html (DOCTYPE...) |
| 277 | // |
| 278 | for (int i = 1; i < sl.size(); ++i) |
| 279 | *this << sl[i] << "\n"; |
| 280 | } |