source: trunk/src/dbus/qdbusxmlparser.cpp@ 890

Last change on this file since 890 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 13.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtDBus module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qdbusxmlparser_p.h"
43#include "qdbusinterface.h"
44#include "qdbusinterface_p.h"
45#include "qdbusconnection_p.h"
46#include "qdbusutil_p.h"
47
48#include <QtXml/qdom.h>
49#include <QtCore/qmap.h>
50#include <QtCore/qvariant.h>
51#include <QtCore/qtextstream.h>
52
53#ifndef QT_NO_DBUS
54
55QT_BEGIN_NAMESPACE
56
57static QDBusIntrospection::Annotations
58parseAnnotations(const QDomElement& elem)
59{
60 QDBusIntrospection::Annotations retval;
61 QDomNodeList list = elem.elementsByTagName(QLatin1String("annotation"));
62 for (int i = 0; i < list.count(); ++i)
63 {
64 QDomElement ann = list.item(i).toElement();
65 if (ann.isNull())
66 continue;
67
68 QString name = ann.attribute(QLatin1String("name")),
69 value = ann.attribute(QLatin1String("value"));
70
71 if (!QDBusUtil::isValidInterfaceName(name)) {
72 qWarning("Invalid D-BUS annotation '%s' found while parsing introspection",
73 qPrintable(name));
74 continue;
75 }
76
77 retval.insert(name, value);
78 }
79
80 return retval;
81}
82
83static QDBusIntrospection::Arguments
84parseArgs(const QDomElement& elem, const QLatin1String& direction, bool acceptEmpty)
85{
86 QDBusIntrospection::Arguments retval;
87 QDomNodeList list = elem.elementsByTagName(QLatin1String("arg"));
88 for (int i = 0; i < list.count(); ++i)
89 {
90 QDomElement arg = list.item(i).toElement();
91 if (arg.isNull())
92 continue;
93
94 if ((acceptEmpty && !arg.hasAttribute(QLatin1String("direction"))) ||
95 arg.attribute(QLatin1String("direction")) == direction) {
96
97 QDBusIntrospection::Argument argData;
98 if (arg.hasAttribute(QLatin1String("name")))
99 argData.name = arg.attribute(QLatin1String("name")); // can be empty
100 argData.type = arg.attribute(QLatin1String("type"));
101 if (!QDBusUtil::isValidSingleSignature(argData.type)) {
102 qWarning("Invalid D-BUS type signature '%s' found while parsing introspection",
103 qPrintable(argData.type));
104 continue;
105 }
106
107 retval << argData;
108 }
109 }
110 return retval;
111}
112
113QDBusXmlParser::QDBusXmlParser(const QString& service, const QString& path,
114 const QString& xmlData)
115 : m_service(service), m_path(path)
116{
117 QDomDocument doc;
118 doc.setContent(xmlData);
119 m_node = doc.firstChildElement(QLatin1String("node"));
120}
121
122QDBusXmlParser::QDBusXmlParser(const QString& service, const QString& path,
123 const QDomElement& node)
124 : m_service(service), m_path(path), m_node(node)
125{
126}
127
128QDBusIntrospection::Interfaces
129QDBusXmlParser::interfaces() const
130{
131 QDBusIntrospection::Interfaces retval;
132
133 if (m_node.isNull())
134 return retval;
135
136 QDomNodeList interfaceList = m_node.elementsByTagName(QLatin1String("interface"));
137 for (int i = 0; i < interfaceList.count(); ++i)
138 {
139 QDomElement iface = interfaceList.item(i).toElement();
140 QString ifaceName = iface.attribute(QLatin1String("name"));
141 if (iface.isNull())
142 continue; // for whatever reason
143 if (!QDBusUtil::isValidInterfaceName(ifaceName)) {
144 qWarning("Invalid D-BUS interface name '%s' found while parsing introspection",
145 qPrintable(ifaceName));
146 continue;
147 }
148
149 QDBusIntrospection::Interface *ifaceData = new QDBusIntrospection::Interface;
150 ifaceData->name = ifaceName;
151 {
152 // save the data
153 QTextStream ts(&ifaceData->introspection);
154 iface.save(ts,2);
155 }
156
157 // parse annotations
158 ifaceData->annotations = parseAnnotations(iface);
159
160 // parse methods
161 QDomNodeList list = iface.elementsByTagName(QLatin1String("method"));
162 for (int j = 0; j < list.count(); ++j)
163 {
164 QDomElement method = list.item(j).toElement();
165 QString methodName = method.attribute(QLatin1String("name"));
166 if (method.isNull())
167 continue;
168 if (!QDBusUtil::isValidMemberName(methodName)) {
169 qWarning("Invalid D-BUS member name '%s' found in interface '%s' while parsing introspection",
170 qPrintable(methodName), qPrintable(ifaceName));
171 continue;
172 }
173
174 QDBusIntrospection::Method methodData;
175 methodData.name = methodName;
176
177 // parse arguments
178 methodData.inputArgs = parseArgs(method, QLatin1String("in"), true);
179 methodData.outputArgs = parseArgs(method, QLatin1String("out"), false);
180 methodData.annotations = parseAnnotations(method);
181
182 // add it
183 ifaceData->methods.insert(methodName, methodData);
184 }
185
186 // parse signals
187 list = iface.elementsByTagName(QLatin1String("signal"));
188 for (int j = 0; j < list.count(); ++j)
189 {
190 QDomElement signal = list.item(j).toElement();
191 QString signalName = signal.attribute(QLatin1String("name"));
192 if (signal.isNull())
193 continue;
194 if (!QDBusUtil::isValidMemberName(signalName)) {
195 qWarning("Invalid D-BUS member name '%s' found in interface '%s' while parsing introspection",
196 qPrintable(signalName), qPrintable(ifaceName));
197 continue;
198 }
199
200 QDBusIntrospection::Signal signalData;
201 signalData.name = signalName;
202
203 // parse data
204 signalData.outputArgs = parseArgs(signal, QLatin1String("out"), true);
205 signalData.annotations = parseAnnotations(signal);
206
207 // add it
208 ifaceData->signals_.insert(signalName, signalData);
209 }
210
211 // parse properties
212 list = iface.elementsByTagName(QLatin1String("property"));
213 for (int j = 0; j < list.count(); ++j)
214 {
215 QDomElement property = list.item(j).toElement();
216 QString propertyName = property.attribute(QLatin1String("name"));
217 if (property.isNull())
218 continue;
219 if (!QDBusUtil::isValidMemberName(propertyName)) {
220 qWarning("Invalid D-BUS member name '%s' found in interface '%s' while parsing introspection",
221 qPrintable(propertyName), qPrintable(ifaceName));
222 continue;
223 }
224
225 QDBusIntrospection::Property propertyData;
226
227 // parse data
228 propertyData.name = propertyName;
229 propertyData.type = property.attribute(QLatin1String("type"));
230 propertyData.annotations = parseAnnotations(property);
231
232 if (!QDBusUtil::isValidSingleSignature(propertyData.type)) {
233 // cannot be!
234 qWarning("Invalid D-BUS type signature '%s' found in property '%s.%s' while parsing introspection",
235 qPrintable(propertyData.type), qPrintable(ifaceName),
236 qPrintable(propertyName));
237 continue;
238 }
239
240 QString access = property.attribute(QLatin1String("access"));
241 if (access == QLatin1String("read"))
242 propertyData.access = QDBusIntrospection::Property::Read;
243 else if (access == QLatin1String("write"))
244 propertyData.access = QDBusIntrospection::Property::Write;
245 else if (access == QLatin1String("readwrite"))
246 propertyData.access = QDBusIntrospection::Property::ReadWrite;
247 else {
248 qWarning("Invalid D-BUS property access '%s' found in property '%s.%s' while parsing introspection",
249 qPrintable(access), qPrintable(ifaceName),
250 qPrintable(propertyName));
251 continue; // invalid one!
252 }
253
254 // add it
255 ifaceData->properties.insert(propertyName, propertyData);
256 }
257
258 // add it
259 retval.insert(ifaceName, QSharedDataPointer<QDBusIntrospection::Interface>(ifaceData));
260 }
261
262 return retval;
263}
264
265QSharedDataPointer<QDBusIntrospection::Object>
266QDBusXmlParser::object() const
267{
268 if (m_node.isNull())
269 return QSharedDataPointer<QDBusIntrospection::Object>();
270
271 QDBusIntrospection::Object* objData;
272 objData = new QDBusIntrospection::Object;
273 objData->service = m_service;
274 objData->path = m_path;
275
276 // check if we have anything to process
277 if (objData->introspection.isNull() && !m_node.firstChild().isNull()) {
278 // yes, introspect this object
279 QTextStream ts(&objData->introspection);
280 m_node.save(ts,2);
281
282 QDomNodeList objects = m_node.elementsByTagName(QLatin1String("node"));
283 for (int i = 0; i < objects.count(); ++i) {
284 QDomElement obj = objects.item(i).toElement();
285 QString objName = obj.attribute(QLatin1String("name"));
286 if (obj.isNull())
287 continue; // for whatever reason
288 if (!QDBusUtil::isValidObjectPath(m_path + QLatin1Char('/') + objName)) {
289 qWarning("Invalid D-BUS object path '%s/%s' found while parsing introspection",
290 qPrintable(m_path), qPrintable(objName));
291 continue;
292 }
293
294 objData->childObjects.append(objName);
295 }
296
297 QDomNodeList interfaceList = m_node.elementsByTagName(QLatin1String("interface"));
298 for (int i = 0; i < interfaceList.count(); ++i) {
299 QDomElement iface = interfaceList.item(i).toElement();
300 QString ifaceName = iface.attribute(QLatin1String("name"));
301 if (iface.isNull())
302 continue;
303 if (!QDBusUtil::isValidInterfaceName(ifaceName)) {
304 qWarning("Invalid D-BUS interface name '%s' found while parsing introspection",
305 qPrintable(ifaceName));
306 continue;
307 }
308
309 objData->interfaces.append(ifaceName);
310 }
311 } else {
312 objData->introspection = QLatin1String("<node/>\n");
313 }
314
315 QSharedDataPointer<QDBusIntrospection::Object> retval;
316 retval = objData;
317 return retval;
318}
319
320QSharedDataPointer<QDBusIntrospection::ObjectTree>
321QDBusXmlParser::objectTree() const
322{
323 QSharedDataPointer<QDBusIntrospection::ObjectTree> retval;
324
325 if (m_node.isNull())
326 return retval;
327
328 retval = new QDBusIntrospection::ObjectTree;
329
330 retval->service = m_service;
331 retval->path = m_path;
332
333 QTextStream ts(&retval->introspection);
334 m_node.save(ts,2);
335
336 // interfaces are easy:
337 retval->interfaceData = interfaces();
338 retval->interfaces = retval->interfaceData.keys();
339
340 // sub-objects are slightly more difficult:
341 QDomNodeList objects = m_node.elementsByTagName(QLatin1String("node"));
342 for (int i = 0; i < objects.count(); ++i) {
343 QDomElement obj = objects.item(i).toElement();
344 QString objName = obj.attribute(QLatin1String("name"));
345 if (obj.isNull() || objName.isEmpty())
346 continue; // for whatever reason
347
348 // check if we have anything to process
349 if (!obj.firstChild().isNull()) {
350 // yes, introspect this object
351 QString xml;
352 QTextStream ts2(&xml);
353 obj.save(ts2,0);
354
355 // parse it
356 QString objAbsName = m_path;
357 if (!objAbsName.endsWith(QLatin1Char('/')))
358 objAbsName.append(QLatin1Char('/'));
359 objAbsName += objName;
360
361 QDBusXmlParser parser(m_service, objAbsName, obj);
362 retval->childObjectData.insert(objName, parser.objectTree());
363 }
364
365 retval->childObjects << objName;
366 }
367
368 return QSharedDataPointer<QDBusIntrospection::ObjectTree>( retval );
369}
370
371QT_END_NAMESPACE
372
373#endif // QT_NO_DBUS
Note: See TracBrowser for help on using the repository browser.