~ubuntu-branches/ubuntu/raring/muse/raring

« back to all changes in this revision

Viewing changes to al/xml.cpp

  • Committer: Package Import Robot
  • Author(s): Alessio Treglia
  • Date: 2011-08-12 11:16:41 UTC
  • mfrom: (1.1.9) (10.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20110812111641-sjz0e93fr0ozfv0b
Tags: 2.0~beta2-1
* New maintainer (see bug 637185#12), thanks Daniel!
* New upstream release (Closes: #627371):
  - New interface Qt4-based (Closes: #604584)
* Refresh packaging:
  - Move to DH7 and source format 3.0 (quilt).
  - Refresh patchset.
  - Fix a bunch lintian warnings.
  - debian/postinst:
    + Use set -e in the body rather than pass -e on the #! line to make it
      have actually effect if it is run by hand with "sh /path/to/script".
  - Update instructions on how-to increase RTC clock frequency.
    Thanks to Torquil Macdonald Sørensen for the report (Closes: #570833).
  - Bump Standards.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//=============================================================================
 
2
//  AL
 
3
//  Audio Utility Library
 
4
//  $Id:$
 
5
//
 
6
//  Copyright (C) 2002-2006 by Werner Schweer and others
 
7
//
 
8
//  This program is free software; you can redistribute it and/or modify
 
9
//  it under the terms of the GNU General Public License version 2.
 
10
//
 
11
//  This program is distributed in the hope that it will be useful,
 
12
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//  GNU General Public License for more details.
 
15
//
 
16
//  You should have received a copy of the GNU General Public License
 
17
//  along with this program; if not, write to the Free Software
 
18
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
//=============================================================================
 
20
 
 
21
#include "xml.h"
 
22
#include "al.h"
 
23
 
 
24
#include <QDomElement>
 
25
#include <QMetaProperty>
 
26
#include <QWidget>
 
27
 
 
28
namespace AL {
 
29
 
 
30
//---------------------------------------------------------
 
31
//   Xml
 
32
//---------------------------------------------------------
 
33
 
 
34
Xml::Xml()
 
35
      {
 
36
      level = 0;
 
37
      }
 
38
 
 
39
Xml::Xml(QIODevice* device)
 
40
   : QTextStream(device)
 
41
      {
 
42
      setCodec("utf8");
 
43
      level = 0;
 
44
      }
 
45
 
 
46
//---------------------------------------------------------
 
47
//   putLevel
 
48
//---------------------------------------------------------
 
49
 
 
50
void Xml::putLevel()
 
51
      {
 
52
      for (int i = 0; i < level*2; ++i)
 
53
            *this << ' ';
 
54
      }
 
55
 
 
56
//---------------------------------------------------------
 
57
//   header
 
58
//---------------------------------------------------------
 
59
 
 
60
void Xml::header()
 
61
      {
 
62
      *this << "<?xml version=\"1.0\" encoding=\"utf8\"?>" << endl;
 
63
      }
 
64
 
 
65
//---------------------------------------------------------
 
66
//   put
 
67
//---------------------------------------------------------
 
68
 
 
69
void Xml::put(const QString& s)
 
70
      {
 
71
      putLevel();
 
72
        *this << xmlString(s) << endl;
 
73
      }
 
74
 
 
75
//---------------------------------------------------------
 
76
//   stag
 
77
//    <mops attribute="value">
 
78
//---------------------------------------------------------
 
79
 
 
80
void Xml::stag(const QString& s)
 
81
      {
 
82
      putLevel();
 
83
      *this << '<' << s << '>' << endl;
 
84
      ++level;
 
85
      }
 
86
 
 
87
//---------------------------------------------------------
 
88
//   etag
 
89
//---------------------------------------------------------
 
90
 
 
91
void Xml::etag(const char* s)
 
92
      {
 
93
      putLevel();
 
94
      *this << "</" << s << '>' << endl;
 
95
      --level;
 
96
      }
 
97
 
 
98
//---------------------------------------------------------
 
99
//   tagE
 
100
//    <mops attribute="value"/>
 
101
//---------------------------------------------------------
 
102
 
 
103
void Xml::tagE(const QString& s)
 
104
      {
 
105
      putLevel();
 
106
      *this << '<' << s << "/>" << endl;
 
107
      }
 
108
 
 
109
void Xml::tag(const char* name, int val)
 
110
      {
 
111
      putLevel();
 
112
      *this << '<' << name << '>' << val << "</" << name << '>' << endl;
 
113
      }
 
114
 
 
115
void Xml::tag(const char* name, unsigned val)
 
116
      {
 
117
      putLevel();
 
118
      *this << '<' << name << '>' << val << "</" << name << '>' << endl;
 
119
      }
 
120
 
 
121
void Xml::tag(const char* name, float val)
 
122
      {
 
123
      putLevel();
 
124
      *this << '<' << name << '>' << val << "</" << name << '>' << endl;
 
125
      }
 
126
 
 
127
void Xml::tag(const char* name, const double& val)
 
128
      {
 
129
      putLevel();
 
130
      *this << '<' << name << '>' << val << "</" << name << '>' << endl;
 
131
      }
 
132
 
 
133
void Xml::tag(const char* name, const QString& val)
 
134
      {
 
135
      putLevel();
 
136
      *this << "<" << name << ">" << xmlString(val) << "</" << name << '>' << endl;
 
137
      }
 
138
 
 
139
void Xml::tag(const char* name, const QColor& color)
 
140
      {
 
141
      putLevel();
 
142
        *this << QString("<%1 r=\"%2\" g=\"%3\" b=\"%4\"/>")
 
143
         .arg(name).arg(color.red()).arg(color.green()).arg(color.blue()) << endl;
 
144
      }
 
145
 
 
146
void Xml::tag(const char* name, const QWidget* g)
 
147
      {
 
148
      tag(name, QRect(g->pos(), g->size()));
 
149
      }
 
150
 
 
151
void Xml::tag(const char* name, const QRect& r)
 
152
      {
 
153
      putLevel();
 
154
        *this << "<" << name;
 
155
      *this << QString(" x=\"%1\" y=\"%2\" w=\"%3\" h=\"%4\"/>")
 
156
         .arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height()) << endl;
 
157
      }
 
158
 
 
159
//---------------------------------------------------------
 
160
//   xmlString
 
161
//---------------------------------------------------------
 
162
 
 
163
QString Xml::xmlString(const QString& ss)
 
164
      {
 
165
      QString s(ss);
 
166
      s.replace('&', "&amp;");
 
167
      s.replace('<', "&lt;");
 
168
      s.replace('>', "&gt;");
 
169
      s.replace('\'', "&apos;");
 
170
      s.replace('"', "&quot;");
 
171
      return s;
 
172
      }
 
173
 
 
174
//---------------------------------------------------------
 
175
//   readGeometry
 
176
//---------------------------------------------------------
 
177
 
 
178
QRect readGeometry(QDomNode node)
 
179
      {
 
180
      QDomElement e = node.toElement();
 
181
      int x = e.attribute("x","0").toInt();
 
182
      int y = e.attribute("y","0").toInt();
 
183
      int w = e.attribute("w","50").toInt();
 
184
      int h = e.attribute("h","50").toInt();
 
185
      return QRect(x, y, w, h);
 
186
      }
 
187
 
 
188
//---------------------------------------------------------
 
189
//   writeProperties
 
190
//---------------------------------------------------------
 
191
 
 
192
void Xml::writeProperties(const QObject* o)
 
193
      {
 
194
      const QMetaObject* meta = o->metaObject();
 
195
 
 
196
        //
 
197
      // start from dummy "muse" property, assuming this is the
 
198
      // first muse propertie in widget hierarchy
 
199
      //
 
200
        int from = meta->indexOfProperty("muse") + 1;
 
201
        int n    = meta->propertyCount();
 
202
      for (int i = from; i < n; ++i) {
 
203
            QMetaProperty p  = meta->property(i);
 
204
                if (!p.isScriptable())
 
205
                continue;
 
206
            const char* name = p.name();
 
207
            QVariant v       = p.read(o);
 
208
            switch(v.type()) {
 
209
                case QVariant::Bool:
 
210
                case QVariant::Int:
 
211
                        tag(name, v.toInt());
 
212
                        break;
 
213
                  case QVariant::Double:
 
214
                        tag(name, v.toDouble());
 
215
                        break;
 
216
                  case QVariant::String:
 
217
                        tag(name, v.toString());
 
218
                        break;
 
219
                  case QVariant::Rect:
 
220
                        tag(name, v.toRect());
 
221
                        break;
 
222
                  case QVariant::Point:
 
223
                        {
 
224
                        QPoint p = v.toPoint();
 
225
                        putLevel();
 
226
                                *this << "<" << name << QString(" x=\"%1\" y=\"%2\" />")
 
227
                                   .arg(p.x()).arg(p.y()) << endl;
 
228
                        }
 
229
                        break;
 
230
 
 
231
                  default:
 
232
                        printf("MusE:%s type %d not implemented\n",
 
233
                           meta->className(), v.type());
 
234
                        break;
 
235
                }
 
236
            }
 
237
      }
 
238
 
 
239
//---------------------------------------------------------
 
240
//   readProperties
 
241
//---------------------------------------------------------
 
242
 
 
243
void readProperties(QObject* o, QDomNode node)
 
244
      {
 
245
      const QMetaObject* meta = o->metaObject();
 
246
 
 
247
      QDomElement e = node.toElement();
 
248
      QString tag(e.tagName());
 
249
      int idx = meta->indexOfProperty(tag.toLatin1().constData());
 
250
      if (idx == -1) {
 
251
            printf("MusE:%s: unknown tag %s\n",
 
252
               meta->className(), tag.toLatin1().constData());
 
253
            return;
 
254
            }
 
255
      QMetaProperty p = meta->property(idx);
 
256
      QVariant v;
 
257
      switch(p.type()) {
 
258
            case QVariant::Int:
 
259
            case QVariant::Bool:
 
260
                  v.setValue(e.text().toInt());
 
261
                  break;
 
262
            case QVariant::Double:
 
263
                  v.setValue(e.text().toDouble());
 
264
                  break;
 
265
            case QVariant::String:
 
266
                  v.setValue(e.text());
 
267
                  break;
 
268
            case QVariant::Rect:
 
269
                  v.setValue(AL::readGeometry(node));
 
270
                  break;
 
271
            case QVariant::Point:
 
272
                  {
 
273
                        int x = e.attribute("x","0").toInt();
 
274
                        int y = e.attribute("y","0").toInt();
 
275
                        v.setValue(QPoint(x, y));
 
276
                }
 
277
                  break;
 
278
            default:
 
279
                  printf("MusE:%s type %d not implemented\n",
 
280
                     meta->className(), p.type());
 
281
                  return;
 
282
            }
 
283
      if (p.isWritable())
 
284
            p.write(o, v);
 
285
      }
 
286
 
 
287
//---------------------------------------------------------
 
288
//   dump
 
289
//---------------------------------------------------------
 
290
 
 
291
void Xml::dump(int len, const unsigned char* p)
 
292
      {
 
293
      putLevel();
 
294
      int col = 0;
 
295
      setFieldWidth(5);
 
296
      setNumberFlags(numberFlags() | QTextStream::ShowBase);
 
297
      setIntegerBase(16);
 
298
      for (int i = 0; i < len; ++i, ++col) {
 
299
            if (col >= 16) {
 
300
                  setFieldWidth(0);
 
301
                  *this << endl;
 
302
                  col = 0;
 
303
                  putLevel();
 
304
                  setFieldWidth(5);
 
305
                  }
 
306
            *this << (p[i] & 0xff);
 
307
            }
 
308
      if (col)
 
309
            *this << endl << dec;
 
310
      setFieldWidth(0);
 
311
      setIntegerBase(10);
 
312
      }
 
313
 
 
314
//---------------------------------------------------------
 
315
//   domError
 
316
//---------------------------------------------------------
 
317
 
 
318
void domError(QDomNode node)
 
319
      {
 
320
      QDomElement e = node.toElement();
 
321
      QString tag(e.tagName());
 
322
      QString s;
 
323
      QDomNode dn(node);
 
324
      while (!dn.parentNode().isNull()) {
 
325
            dn = dn.parentNode();
 
326
            const QDomElement e = dn.toElement();
 
327
            const QString k(e.tagName());
 
328
            if (!s.isEmpty())
 
329
                  s += ":";
 
330
            s += k;
 
331
            }
 
332
      fprintf(stderr, "%s: Unknown Node <%s>, type %d\n",
 
333
         s.toLatin1().constData(), tag.toLatin1().constData(), node.nodeType());
 
334
      if (node.isText()) {
 
335
            fprintf(stderr, "  text node <%s>\n", node.toText().data().toLatin1().constData());
 
336
            }
 
337
      }
 
338
 
 
339
//---------------------------------------------------------
 
340
//   domNotImplemented
 
341
//---------------------------------------------------------
 
342
 
 
343
void domNotImplemented(QDomNode node)
 
344
      {
 
345
      if (!AL::debugMsg)
 
346
            return;
 
347
      QDomElement e = node.toElement();
 
348
      QString tag(e.tagName());
 
349
      QString s;
 
350
      QDomNode dn(node);
 
351
      while (!dn.parentNode().isNull()) {
 
352
            dn = dn.parentNode();
 
353
            const QDomElement e = dn.toElement();
 
354
            const QString k(e.tagName());
 
355
            if (!s.isEmpty())
 
356
                  s += ":";
 
357
            s += k;
 
358
            }
 
359
      fprintf(stderr, "%s: Node not implemented: <%s>, type %d\n",
 
360
         s.toLatin1().constData(), tag.toLatin1().constData(), node.nodeType());
 
361
      if (node.isText()) {
 
362
            fprintf(stderr, "  text node <%s>\n", node.toText().data().toLatin1().constData());
 
363
            }
 
364
      }
 
365
}
 
366