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

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 13.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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
53QT_BEGIN_NAMESPACE
54
55static QDBusIntrospection::Annotations
56parseAnnotations(const QDomElement& elem)
57{
58 QDBusIntrospection::Annotations retval;
59 QDomNodeList list = elem.elementsByTagName(QLatin1String("annotation"));
60 for (int i = 0; i < list.count(); ++i)
61 {
62 QDomElement ann = list.item(i).toElement();
63 if (ann.isNull())
64 continue;
65
66 QString name = ann.attribute(QLatin1String("name")),
67 value = ann.attribute(QLatin1String("value"));
68
69 if (!QDBusUtil::isValidInterfaceName(name)) {
70 qWarning("Invalid D-BUS annotation '%s' found while parsing introspection",
71 qPrintable(name));
72 continue;
73 }
74
75 retval.insert(name, value);
76 }
77
78 return retval;
79}
80
81static QDBusIntrospection::Arguments
82parseArgs(const QDomElement& elem, const QLatin1String& direction, bool acceptEmpty)
83{
84 QDBusIntrospection::Arguments retval;
85 QDomNodeList list = elem.elementsByTagName(QLatin1String("arg"));
86 for (int i = 0; i < list.count(); ++i)
87 {
88 QDomElement arg = list.item(i).toElement();
89 if (arg.isNull())
90 continue;
91
92 if ((acceptEmpty && !arg.hasAttribute(QLatin1String("direction"))) ||
93 arg.attribute(QLatin1String("direction")) == direction) {
94
95 QDBusIntrospection::Argument argData;
96 if (arg.hasAttribute(QLatin1String("name")))
97 argData.name = arg.attribute(QLatin1String("name")); // can be empty
98 argData.type = arg.attribute(QLatin1String("type"));
99 if (!QDBusUtil::isValidSingleSignature(argData.type)) {
100 qWarning("Invalid D-BUS type signature '%s' found while parsing introspection",
101 qPrintable(argData.type));
102 continue;
103 }
104
105 retval << argData;
106 }
107 }
108 return retval;
109}
110
111QDBusXmlParser::QDBusXmlParser(const QString& service, const QString& path,
112 const QString& xmlData)
113 : m_service(service), m_path(path)
114{
115 QDomDocument doc;
116 doc.setContent(xmlData);
117 m_node = doc.firstChildElement(QLatin1String("node"));
118}
119
120QDBusXmlParser::QDBusXmlParser(const QString& service, const QString& path,
121 const QDomElement& node)
122 : m_service(service), m_path(path), m_node(node)
123{
124}
125
126QDBusIntrospection::Interfaces
127QDBusXmlParser::interfaces() const
128{
129 QDBusIntrospection::Interfaces retval;
130
131 if (m_node.isNull())
132 return retval;
133
134 QDomNodeList interfaceList = m_node.elementsByTagName(QLatin1String("interface"));
135 for (int i = 0; i < interfaceList.count(); ++i)
136 {
137 QDomElement iface = interfaceList.item(i).toElement();
138 QString ifaceName = iface.attribute(QLatin1String("name"));
139 if (iface.isNull())
140 continue; // for whatever reason
141 if (!QDBusUtil::isValidInterfaceName(ifaceName)) {
142 qWarning("Invalid D-BUS interface name '%s' found while parsing introspection",
143 qPrintable(ifaceName));
144 continue;
145 }
146
147 QDBusIntrospection::Interface *ifaceData = new QDBusIntrospection::Interface;
148 ifaceData->name = ifaceName;
149 {
150 // save the data
151 QTextStream ts(&ifaceData->introspection);
152 iface.save(ts,2);
153 }
154
155 // parse annotations
156 ifaceData->annotations = parseAnnotations(iface);
157
158 // parse methods
159 QDomNodeList list = iface.elementsByTagName(QLatin1String("method"));
160 for (int j = 0; j < list.count(); ++j)
161 {
162 QDomElement method = list.item(j).toElement();
163 QString methodName = method.attribute(QLatin1String("name"));
164 if (method.isNull())
165 continue;
166 if (!QDBusUtil::isValidMemberName(methodName)) {
167 qWarning("Invalid D-BUS member name '%s' found in interface '%s' while parsing introspection",
168 qPrintable(methodName), qPrintable(ifaceName));
169 continue;
170 }
171
172 QDBusIntrospection::Method methodData;
173 methodData.name = methodName;
174
175 // parse arguments
176 methodData.inputArgs = parseArgs(method, QLatin1String("in"), true);
177 methodData.outputArgs = parseArgs(method, QLatin1String("out"), false);
178 methodData.annotations = parseAnnotations(method);
179
180 // add it
181 ifaceData->methods.insert(methodName, methodData);
182 }
183
184 // parse signals
185 list = iface.elementsByTagName(QLatin1String("signal"));
186 for (int j = 0; j < list.count(); ++j)
187 {
188 QDomElement signal = list.item(j).toElement();
189 QString signalName = signal.attribute(QLatin1String("name"));
190 if (signal.isNull())
191 continue;
192 if (!QDBusUtil::isValidMemberName(signalName)) {
193 qWarning("Invalid D-BUS member name '%s' found in interface '%s' while parsing introspection",
194 qPrintable(signalName), qPrintable(ifaceName));
195 continue;
196 }
197
198 QDBusIntrospection::Signal signalData;
199 signalData.name = signalName;
200
201 // parse data
202 signalData.outputArgs = parseArgs(signal, QLatin1String("out"), true);
203 signalData.annotations = parseAnnotations(signal);
204
205 // add it
206 ifaceData->signals_.insert(signalName, signalData);
207 }
208
209 // parse properties
210 list = iface.elementsByTagName(QLatin1String("property"));
211 for (int j = 0; j < list.count(); ++j)
212 {
213 QDomElement property = list.item(j).toElement();
214 QString propertyName = property.attribute(QLatin1String("name"));
215 if (property.isNull())
216 continue;
217 if (!QDBusUtil::isValidMemberName(propertyName)) {
218 qWarning("Invalid D-BUS member name '%s' found in interface '%s' while parsing introspection",
219 qPrintable(propertyName), qPrintable(ifaceName));
220 continue;
221 }
222
223 QDBusIntrospection::Property propertyData;
224
225 // parse data
226 propertyData.name = propertyName;
227 propertyData.type = property.attribute(QLatin1String("type"));
228 propertyData.annotations = parseAnnotations(property);
229
230 if (!QDBusUtil::isValidSingleSignature(propertyData.type)) {
231 // cannot be!
232 qWarning("Invalid D-BUS type signature '%s' found in property '%s.%s' while parsing introspection",
233 qPrintable(propertyData.type), qPrintable(ifaceName),
234 qPrintable(propertyName));
235 continue;
236 }
237
238 QString access = property.attribute(QLatin1String("access"));
239 if (access == QLatin1String("read"))
240 propertyData.access = QDBusIntrospection::Property::Read;
241 else if (access == QLatin1String("write"))
242 propertyData.access = QDBusIntrospection::Property::Write;
243 else if (access == QLatin1String("readwrite"))
244 propertyData.access = QDBusIntrospection::Property::ReadWrite;
245 else {
246 qWarning("Invalid D-BUS property access '%s' found in property '%s.%s' while parsing introspection",
247 qPrintable(access), qPrintable(ifaceName),
248 qPrintable(propertyName));
249 continue; // invalid one!
250 }
251
252 // add it
253 ifaceData->properties.insert(propertyName, propertyData);
254 }
255
256 // add it
257 retval.insert(ifaceName, QSharedDataPointer<QDBusIntrospection::Interface>(ifaceData));
258 }
259
260 return retval;
261}
262
263QSharedDataPointer<QDBusIntrospection::Object>
264QDBusXmlParser::object() const
265{
266 if (m_node.isNull())
267 return QSharedDataPointer<QDBusIntrospection::Object>();
268
269 QDBusIntrospection::Object* objData;
270 objData = new QDBusIntrospection::Object;
271 objData->service = m_service;
272 objData->path = m_path;
273
274 // check if we have anything to process
275 if (objData->introspection.isNull() && !m_node.firstChild().isNull()) {
276 // yes, introspect this object
277 QTextStream ts(&objData->introspection);
278 m_node.save(ts,2);
279
280 QDomNodeList objects = m_node.elementsByTagName(QLatin1String("node"));
281 for (int i = 0; i < objects.count(); ++i) {
282 QDomElement obj = objects.item(i).toElement();
283 QString objName = obj.attribute(QLatin1String("name"));
284 if (obj.isNull())
285 continue; // for whatever reason
286 if (!QDBusUtil::isValidObjectPath(m_path + QLatin1Char('/') + objName)) {
287 qWarning("Invalid D-BUS object path '%s/%s' found while parsing introspection",
288 qPrintable(m_path), qPrintable(objName));
289 continue;
290 }
291
292 objData->childObjects.append(objName);
293 }
294
295 QDomNodeList interfaceList = m_node.elementsByTagName(QLatin1String("interface"));
296 for (int i = 0; i < interfaceList.count(); ++i) {
297 QDomElement iface = interfaceList.item(i).toElement();
298 QString ifaceName = iface.attribute(QLatin1String("name"));
299 if (iface.isNull())
300 continue;
301 if (!QDBusUtil::isValidInterfaceName(ifaceName)) {
302 qWarning("Invalid D-BUS interface name '%s' found while parsing introspection",
303 qPrintable(ifaceName));
304 continue;
305 }
306
307 objData->interfaces.append(ifaceName);
308 }
309 } else {
310 objData->introspection = QLatin1String("<node/>\n");
311 }
312
313 QSharedDataPointer<QDBusIntrospection::Object> retval;
314 retval = objData;
315 return retval;
316}
317
318QSharedDataPointer<QDBusIntrospection::ObjectTree>
319QDBusXmlParser::objectTree() const
320{
321 QSharedDataPointer<QDBusIntrospection::ObjectTree> retval;
322
323 if (m_node.isNull())
324 return retval;
325
326 retval = new QDBusIntrospection::ObjectTree;
327
328 retval->service = m_service;
329 retval->path = m_path;
330
331 QTextStream ts(&retval->introspection);
332 m_node.save(ts,2);
333
334 // interfaces are easy:
335 retval->interfaceData = interfaces();
336 retval->interfaces = retval->interfaceData.keys();
337
338 // sub-objects are slightly more difficult:
339 QDomNodeList objects = m_node.elementsByTagName(QLatin1String("node"));
340 for (int i = 0; i < objects.count(); ++i) {
341 QDomElement obj = objects.item(i).toElement();
342 QString objName = obj.attribute(QLatin1String("name"));
343 if (obj.isNull() || objName.isEmpty())
344 continue; // for whatever reason
345
346 // check if we have anything to process
347 if (!obj.firstChild().isNull()) {
348 // yes, introspect this object
349 QString xml;
350 QTextStream ts2(&xml);
351 obj.save(ts2,0);
352
353 // parse it
354 QString objAbsName = m_path;
355 if (!objAbsName.endsWith(QLatin1Char('/')))
356 objAbsName.append(QLatin1Char('/'));
357 objAbsName += objName;
358
359 QDBusXmlParser parser(m_service, objAbsName, obj);
360 retval->childObjectData.insert(objName, parser.objectTree());
361 }
362
363 retval->childObjects << objName;
364 }
365
366 return QSharedDataPointer<QDBusIntrospection::ObjectTree>( retval );
367}
368
369QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.