| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "servicemetadata_p.h" |
| 41 | #include <QFile> |
| 42 | #include <QDataStream> |
| 43 | #include <QDebug> |
| 44 | #include <QRegularExpression> |
| 45 | #include "qserviceinterfacedescriptor_p.h" |
| 46 | |
| 47 | //XML tags and attributes |
| 48 | //General |
| 49 | #define NAME_TAG "name" |
| 50 | #define DESCRIPTION_TAG "description" |
| 51 | #define SERVICEFW_TAG "SFW" |
| 52 | #define XML_MAX "1.1" |
| 53 | |
| 54 | //Service related |
| 55 | #define SERVICE_TAG "service" |
| 56 | #define SERVICE_FILEPATH "filepath" |
| 57 | #define SERVICE_IPCADDRESS "ipcaddress" |
| 58 | |
| 59 | //Interface related |
| 60 | #define INTERFACE_TAG "interface" |
| 61 | #define INTERFACE_VERSION "version" |
| 62 | #define INTERFACE_CAPABILITY "capabilities" |
| 63 | #define INTERFACE_CUSTOM_PROPERTY "customproperty" |
| 64 | |
| 65 | //Service type prefix |
| 66 | #define SERVICE_IPC_PREFIX "_q_ipc_addr:" |
| 67 | |
| 68 | QT_BEGIN_NAMESPACE |
| 69 | |
| 70 | #ifndef QT_NO_DATASTREAM |
| 71 | SERVICEMETADATA_EXPORT QDataStream &operator<<(QDataStream &out, const ServiceMetaDataResults &r) |
| 72 | { |
| 73 | out << r.type << r.name << r.location; |
| 74 | out << r.description << r.interfaces << r.latestInterfaces; |
| 75 | |
| 76 | return out; |
| 77 | } |
| 78 | |
| 79 | SERVICEMETADATA_EXPORT QDataStream &operator>>(QDataStream &in, ServiceMetaDataResults &r) |
| 80 | { |
| 81 | in >> r.type >> r.name >> r.location; |
| 82 | in >> r.description >> r.interfaces >> r.latestInterfaces; |
| 83 | |
| 84 | return in; |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | /* |
| 89 | \class ServiceMetaData |
| 90 | |
| 91 | |
| 92 | Utility class (used by service database) that offers support for |
| 93 | parsing metadata service xml registry file during service registration. \n |
| 94 | |
| 95 | It uses QXMLStreamReader class for parsing. Supproted Operations are: |
| 96 | - Parse the service and interfaces defined in XML file |
| 97 | - name, version, capabilitiesList, description and filePath of service can be retrieved |
| 98 | - each interface can be retrieved |
| 99 | */ |
| 100 | |
| 101 | /* |
| 102 | * Class constructor |
| 103 | * |
| 104 | * @param aXmlFilePath path to the xml file that describes the service. |
| 105 | */ |
| 106 | ServiceMetaData::ServiceMetaData(const QString &aXmlFilePath) |
| 107 | : xmlDevice(new QFile(aXmlFilePath)), |
| 108 | ownsXmlDevice(true), |
| 109 | serviceType(QService::Plugin), |
| 110 | latestError(0) |
| 111 | {} |
| 112 | |
| 113 | /* |
| 114 | * Class constructor |
| 115 | * |
| 116 | * @param device QIODevice that contains the XML data that describes the service. |
| 117 | */ |
| 118 | ServiceMetaData::ServiceMetaData(QIODevice *device) |
| 119 | : xmlDevice(device), |
| 120 | ownsXmlDevice(false), |
| 121 | serviceType(QService::Plugin), |
| 122 | latestError(0) |
| 123 | {} |
| 124 | |
| 125 | /* |
| 126 | * Class destructor |
| 127 | * |
| 128 | */ |
| 129 | ServiceMetaData::~ServiceMetaData() |
| 130 | { |
| 131 | if (ownsXmlDevice) |
| 132 | delete xmlDevice; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | Sets the device containing the XML data that describes the service to \a device. |
| 137 | */ |
| 138 | void ServiceMetaData::setDevice(QIODevice *device) |
| 139 | { |
| 140 | clearMetadata(); |
| 141 | xmlDevice = device; |
| 142 | ownsXmlDevice = false; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | Returns the device containing the XML data that describes the service. |
| 147 | */ |
| 148 | QIODevice *ServiceMetaData::device() const |
| 149 | { |
| 150 | return xmlDevice; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Gets the service name |
| 155 | * |
| 156 | * @return service name or default value (empty string) if it is not available |
| 157 | */ |
| 158 | /*QString ServiceMetaData::name() const |
| 159 | { |
| 160 | return serviceName; |
| 161 | }*/ |
| 162 | |
| 163 | /* |
| 164 | * Gets the path of the service implementation file |
| 165 | * |
| 166 | * @return service implementation filepath |
| 167 | */ |
| 168 | /*QString ServiceMetaData::location() const |
| 169 | { |
| 170 | return serviceLocation; |
| 171 | }*/ |
| 172 | |
| 173 | /* |
| 174 | * Gets the service description |
| 175 | * |
| 176 | * @return service description or default value (empty string) if it is not available |
| 177 | */ |
| 178 | /*QString ServiceMetaData::description() const |
| 179 | { |
| 180 | return serviceDescription; |
| 181 | }*/ |
| 182 | |
| 183 | /* |
| 184 | Returns the metadata of the interace at \a index; otherwise |
| 185 | returns 0. |
| 186 | */ |
| 187 | /*QList<QServiceInterfaceDescriptor> ServiceMetaData::getInterfaces() const |
| 188 | { |
| 189 | return serviceInterfaces; |
| 190 | } */ |
| 191 | |
| 192 | /*! |
| 193 | \internal |
| 194 | |
| 195 | Returns a streamable object containing the results of the parsing. |
| 196 | */ |
| 197 | ServiceMetaDataResults ServiceMetaData::parseResults() const |
| 198 | { |
| 199 | ServiceMetaDataResults results; |
| 200 | results.type = serviceType; |
| 201 | results.location = serviceLocation; |
| 202 | results.name = serviceName; |
| 203 | results.description = serviceDescription; |
| 204 | results.interfaces = serviceInterfaces; |
| 205 | results.latestInterfaces = latestInterfaces(); |
| 206 | |
| 207 | return results; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | Parses the file and extracts the service metadata \n |
| 212 | Custom error codes: \n |
| 213 | SFW_ERROR_UNABLE_TO_OPEN_FILE in case can not open the XML file \n |
| 214 | SFW_ERROR_INVALID_XML_FILE in case service registry is not a valid XML file \n |
| 215 | SFW_ERROR_NO_SERVICE in case XML file has no service tag\n |
| 216 | @return true if the metadata was read properly, false if there is an error |
| 217 | */ |
| 218 | bool ServiceMetaData::extractMetadata() |
| 219 | { |
| 220 | Q_ASSERT(checkVersion(QLatin1String(XML_MAX))); |
| 221 | |
| 222 | latestError = 0; |
| 223 | clearMetadata(); |
| 224 | QXmlStreamReader xmlReader; |
| 225 | bool parseError = false; |
| 226 | //Open xml file |
| 227 | if (!xmlDevice->isOpen() && !xmlDevice->open(QIODevice::ReadOnly)) { |
| 228 | qWarning() << xmlDevice->errorString(); |
| 229 | latestError = ServiceMetaData::SFW_ERROR_UNABLE_TO_OPEN_FILE; |
| 230 | parseError = true; |
| 231 | } else { |
| 232 | //Load xml content |
| 233 | xmlReader.setDevice(xmlDevice); |
| 234 | // Read XML doc |
| 235 | while (!xmlReader.atEnd() && !parseError) { |
| 236 | xmlReader.readNext(); |
| 237 | //Found <SFW> xml versioning tag introduced in 1.1 |
| 238 | //If this tag is not found the XML parser version will be 1.0 |
| 239 | if (xmlReader.isStartElement() && xmlReader.name() == QLatin1String(SERVICEFW_TAG)) { |
| 240 | if (!processVersionElement(xmlReader)) { |
| 241 | parseError = true; |
| 242 | } |
| 243 | } |
| 244 | //Found a <service> node, read service related metadata |
| 245 | else if (xmlReader.isStartElement() && xmlReader.name() == QLatin1String(SERVICE_TAG)) { |
| 246 | if (!processServiceElement(xmlReader)) { |
| 247 | parseError = true; |
| 248 | } |
| 249 | } |
| 250 | else if (xmlReader.isStartElement() && xmlReader.name() != QLatin1String(SERVICE_TAG) |
| 251 | && xmlReader.name() != QLatin1String(SERVICEFW_TAG)) { |
| 252 | latestError = ServiceMetaData::SFW_ERROR_NO_SERVICE; |
| 253 | parseError = true; |
| 254 | } |
| 255 | else if (xmlReader.tokenType() == QXmlStreamReader::Invalid) { |
| 256 | latestError = ServiceMetaData::SFW_ERROR_INVALID_XML_FILE; |
| 257 | parseError = true; |
| 258 | } |
| 259 | } |
| 260 | if (ownsXmlDevice) |
| 261 | xmlDevice->close(); |
| 262 | } |
| 263 | |
| 264 | if (parseError) { |
| 265 | //provide better error reports |
| 266 | switch (latestError) { |
| 267 | case SFW_ERROR_NO_SERVICE: /* Can not find service root node in XML file*/ |
| 268 | qDebug() << "Missing <service> tag"; |
| 269 | break; |
| 270 | case SFW_ERROR_NO_SERVICE_NAME: /* Can not find service name in XML file */ |
| 271 | qDebug() << "Missing or empty <name> tag within <service>"; |
| 272 | break; |
| 273 | case SFW_ERROR_NO_SERVICE_PATH: /* Can not find service filepath or ipcaddress in XML file */ |
| 274 | if (greaterThan(xmlVersion, QLatin1String("1.0"))) |
| 275 | qDebug() << "Missing or empty <filepath> or <ipcaddress> tag within <service>"; |
| 276 | else |
| 277 | qDebug() << "Missing or empty <filepath> tag within <service>"; |
| 278 | break; |
| 279 | case SFW_ERROR_NO_SERVICE_INTERFACE: /* No interface for the service in XML file*/ |
| 280 | qDebug() << "Missing <interface> tag"; |
| 281 | break; |
| 282 | case SFW_ERROR_NO_INTERFACE_VERSION: /* Can not find interface version in XML file */ |
| 283 | qDebug() << "Missing or empty <version> tag within <interface>"; |
| 284 | break; |
| 285 | case SFW_ERROR_NO_INTERFACE_NAME: /* Can not find interface name in XML file*/ |
| 286 | qDebug() << "Missing or empty <name> tag within <interface>"; |
| 287 | break; |
| 288 | case SFW_ERROR_UNABLE_TO_OPEN_FILE: /* Error opening XML file*/ |
| 289 | qDebug() << "Unable to open service xml file"; |
| 290 | break; |
| 291 | case SFW_ERROR_INVALID_XML_FILE: /* Not a valid XML file*/ |
| 292 | qDebug() << "Not a valid service xml"; |
| 293 | break; |
| 294 | case SFW_ERROR_PARSE_SERVICE: /* Error parsing service node */ |
| 295 | qDebug().nospace() << "Invalid tag within <service> with the supplied version(" |
| 296 | << xmlVersion << ")"; |
| 297 | break; |
| 298 | case SFW_ERROR_PARSE_INTERFACE: /* Error parsing interface node */ |
| 299 | qDebug() << "Invalid tag within <interface> tags"; |
| 300 | break; |
| 301 | case SFW_ERROR_DUPLICATED_INTERFACE: /* The same interface is defined twice */ |
| 302 | qDebug() << "The same interface has been defined more than once"; |
| 303 | break; |
| 304 | case SFW_ERROR_INVALID_VERSION: |
| 305 | qDebug() << "Invalid version string, expected: x.y"; |
| 306 | break; |
| 307 | case SFW_ERROR_DUPLICATED_TAG: /* The tag appears twice */ |
| 308 | qDebug() << "XML tag appears twice"; |
| 309 | break; |
| 310 | case SFW_ERROR_INVALID_CUSTOM_TAG: /* The customproperty tag is not correctly formatted or otherwise incorrect*/ |
| 311 | qDebug() << "Invalid custom property tag"; |
| 312 | break; |
| 313 | case SFW_ERROR_DUPLICATED_CUSTOM_KEY: /* The customproperty appears twice*/ |
| 314 | qDebug() << "Same custom property appears multiple times"; |
| 315 | break; |
| 316 | case SFW_ERROR_MULTIPLE_SERVICE_TYPES: /* Both filepath and ipcaddress found in the XML file */ |
| 317 | qDebug() << "Cannot specify both <filepath> and <ipcaddress> tags within <service>"; |
| 318 | break; |
| 319 | case SFW_ERROR_INVALID_FILEPATH: /* Service path cannot contain IPC prefix */ |
| 320 | qDebug() << "Invalid service location, avoid private prefixes"; |
| 321 | break; |
| 322 | case SFW_ERROR_INVALID_XML_VERSION: /* Error parsing servicefw version node */ |
| 323 | qDebug() << "Invalid or missing version attribute in <SFW> tag"; |
| 324 | break; |
| 325 | case SFW_ERROR_UNSUPPORTED_IPC: /* Servicefw version doesn't support IPC */ |
| 326 | qDebug().nospace() << "Supplied service framework version(" << xmlVersion |
| 327 | << ") doesn't support the <ipcaddress> tag"; |
| 328 | break; |
| 329 | case SFW_ERROR_UNSUPPORTED_XML_VERSION: /* Unsupported servicefw version supplied */ |
| 330 | qDebug().nospace() << "Service framework version(" << xmlVersion |
| 331 | << ") is higher than available support(" << QLatin1String(XML_MAX) << ")"; |
| 332 | break; |
| 333 | } |
| 334 | clearMetadata(); |
| 335 | } |
| 336 | return !parseError; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | Gets the latest parsing error \n |
| 341 | @return parsing error(negative value) or 0 in case there is none |
| 342 | */ |
| 343 | int ServiceMetaData::getLatestError() const |
| 344 | { |
| 345 | return latestError; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | Parses the service framework xml version tag and continues to parse the service |
| 350 | */ |
| 351 | bool ServiceMetaData::processVersionElement(QXmlStreamReader &aXMLReader) |
| 352 | { |
| 353 | Q_ASSERT(aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(SERVICEFW_TAG)); |
| 354 | bool parseError = false; |
| 355 | |
| 356 | if (aXMLReader.attributes().hasAttribute(QLatin1String("version"))) { |
| 357 | xmlVersion = aXMLReader.attributes().value(QLatin1String("version")).toString(); |
| 358 | bool success = checkVersion(xmlVersion); |
| 359 | |
| 360 | if (xmlVersion.isEmpty() || !success) { |
| 361 | latestError = ServiceMetaData::SFW_ERROR_INVALID_XML_VERSION; |
| 362 | parseError = true; |
| 363 | } else { |
| 364 | if (greaterThan(xmlVersion, QLatin1String(XML_MAX))) { |
| 365 | latestError = ServiceMetaData::SFW_ERROR_UNSUPPORTED_XML_VERSION; |
| 366 | parseError = true; |
| 367 | } |
| 368 | } |
| 369 | } else { |
| 370 | latestError = ServiceMetaData::SFW_ERROR_INVALID_XML_VERSION; |
| 371 | parseError = true; |
| 372 | } |
| 373 | |
| 374 | while (!parseError && !aXMLReader.atEnd()) { |
| 375 | aXMLReader.readNext(); |
| 376 | //Found a <service> node, read service related metadata |
| 377 | if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(SERVICE_TAG)) { |
| 378 | if (!processServiceElement(aXMLReader)) { |
| 379 | parseError = true; |
| 380 | } |
| 381 | } |
| 382 | else if (aXMLReader.isEndElement() && aXMLReader.name() == QLatin1String(SERVICEFW_TAG)) { |
| 383 | //Found </SFW>, leave the loop |
| 384 | break; |
| 385 | } |
| 386 | else if (aXMLReader.isStartElement() && aXMLReader.name() != QLatin1String(SERVICE_TAG)) { |
| 387 | latestError = ServiceMetaData::SFW_ERROR_NO_SERVICE; |
| 388 | parseError = true; |
| 389 | } |
| 390 | else if (aXMLReader.tokenType() == QXmlStreamReader::Invalid) { |
| 391 | latestError = ServiceMetaData::SFW_ERROR_INVALID_XML_FILE; |
| 392 | parseError = true; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | return !parseError; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | Parses and extracts the service metadata from the current xml <service> node \n |
| 401 | */ |
| 402 | bool ServiceMetaData::processServiceElement(QXmlStreamReader &aXMLReader) |
| 403 | { |
| 404 | Q_ASSERT(aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(SERVICE_TAG)); |
| 405 | bool parseError = false; |
| 406 | |
| 407 | int dupSTags[4] = {0 //->tag name |
| 408 | ,0 //-> service description |
| 409 | ,0 //-> filepath |
| 410 | ,0 //-> ipcaddress |
| 411 | }; |
| 412 | while (!parseError && !aXMLReader.atEnd()) { |
| 413 | aXMLReader.readNext(); |
| 414 | if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(NAME_TAG)) { |
| 415 | //Found <name> tag |
| 416 | serviceName = aXMLReader.readElementText(); |
| 417 | dupSTags[0]++; |
| 418 | } else if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(DESCRIPTION_TAG)) { |
| 419 | //Found <description> tag |
| 420 | serviceDescription = aXMLReader.readElementText(); |
| 421 | dupSTags[1]++; |
| 422 | } else if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(SERVICE_FILEPATH) ) { |
| 423 | //Found <filepath> tag for plugin service |
| 424 | dupSTags[2]++; |
| 425 | serviceLocation = aXMLReader.readElementText(); |
| 426 | //Check if IPC prefix was used incorrectly here |
| 427 | if (serviceLocation.startsWith(QLatin1String(SERVICE_IPC_PREFIX))) { |
| 428 | latestError = ServiceMetaData::SFW_ERROR_INVALID_FILEPATH; |
| 429 | parseError = true; |
| 430 | } |
| 431 | } else if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(SERVICE_IPCADDRESS) ) { |
| 432 | //Found <ipcaddress> tag for IPC service |
| 433 | //Check if servicefw XML version supports IPC |
| 434 | if (greaterThan(xmlVersion, QLatin1String("1.0"))) { |
| 435 | dupSTags[3]++; |
| 436 | serviceLocation = aXMLReader.readElementText(); |
| 437 | //Check if IPC prefix was used incorrectly here |
| 438 | if (serviceLocation.startsWith(QLatin1String(SERVICE_IPC_PREFIX))) { |
| 439 | latestError = ServiceMetaData::SFW_ERROR_INVALID_FILEPATH; |
| 440 | parseError = true; |
| 441 | } |
| 442 | } else { |
| 443 | latestError = ServiceMetaData::SFW_ERROR_UNSUPPORTED_IPC; |
| 444 | parseError = true; |
| 445 | } |
| 446 | } else if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(INTERFACE_TAG)) { |
| 447 | //Found interface> node, read module related metadata |
| 448 | if (!processInterfaceElement(aXMLReader)) |
| 449 | parseError = true; |
| 450 | } else if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String("version")) { |
| 451 | //Found <version> tag on service level. We ignore this for now |
| 452 | aXMLReader.readElementText(); |
| 453 | } else if (aXMLReader.isEndElement() && aXMLReader.name() == QLatin1String(SERVICE_TAG)) { |
| 454 | //Found </service>, leave the loop |
| 455 | break; |
| 456 | } else if (aXMLReader.isEndElement() || aXMLReader.isStartElement()) { |
| 457 | latestError = ServiceMetaData::SFW_ERROR_PARSE_SERVICE; |
| 458 | parseError = true; |
| 459 | } else if (aXMLReader.tokenType() == QXmlStreamReader::Invalid) { |
| 460 | latestError = ServiceMetaData::SFW_ERROR_INVALID_XML_FILE; |
| 461 | parseError = true; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if ( !parseError ) { |
| 466 | if (serviceName.isEmpty()) { |
| 467 | latestError = ServiceMetaData::SFW_ERROR_NO_SERVICE_NAME; |
| 468 | parseError = true; |
| 469 | } else if (serviceLocation.isEmpty()) { |
| 470 | latestError = ServiceMetaData::SFW_ERROR_NO_SERVICE_PATH; |
| 471 | parseError = true; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (dupSTags[3] > 0) |
| 476 | serviceType = QService::InterProcess; |
| 477 | |
| 478 | if ((dupSTags[2] > 0) && (dupSTags[3] > 0)) { |
| 479 | latestError = SFW_ERROR_MULTIPLE_SERVICE_TYPES; |
| 480 | parseError = true; |
| 481 | } |
| 482 | |
| 483 | for (int i=0; !parseError && i<4; i++) { |
| 484 | if (dupSTags[i] > 1) { |
| 485 | latestError = SFW_ERROR_DUPLICATED_TAG; |
| 486 | parseError = true; |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | //update all interfaces with service data |
| 492 | const int icount = serviceInterfaces.count(); |
| 493 | if (icount == 0 && latestError == 0) { |
| 494 | latestError = ServiceMetaData::SFW_ERROR_NO_SERVICE_INTERFACE; |
| 495 | parseError = true; |
| 496 | } |
| 497 | for (int i = 0; i<icount; i++) { |
| 498 | serviceInterfaces.at(i).d->serviceName = serviceName; |
| 499 | serviceInterfaces.at(i).d->attributes[QServiceInterfaceDescriptor::Location] = serviceLocation; |
| 500 | serviceInterfaces.at(i).d->attributes[QServiceInterfaceDescriptor::ServiceDescription] = serviceDescription; |
| 501 | serviceInterfaces.at(i).d->attributes[QServiceInterfaceDescriptor::ServiceType] = serviceType; |
| 502 | } |
| 503 | |
| 504 | return !parseError; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | Parses and extracts the interface metadata from the current xml <interface> node \n |
| 509 | */ |
| 510 | bool ServiceMetaData::processInterfaceElement(QXmlStreamReader &aXMLReader) |
| 511 | { |
| 512 | Q_ASSERT(aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(INTERFACE_TAG)); |
| 513 | bool parseError = false; |
| 514 | |
| 515 | //Read interface parameter |
| 516 | QString tmp; |
| 517 | QServiceInterfaceDescriptor aInterface; |
| 518 | int dupITags[4] = { |
| 519 | 0, //->iface name tag |
| 520 | 0, //->version |
| 521 | 0, //->capabilities |
| 522 | 0 //->description |
| 523 | }; |
| 524 | aInterface.d = new QServiceInterfaceDescriptorPrivate; |
| 525 | |
| 526 | while (!parseError && !aXMLReader.atEnd()) { |
| 527 | aXMLReader.readNext(); |
| 528 | //Read interface description |
| 529 | if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(NAME_TAG)) { |
| 530 | aInterface.d->interfaceName = aXMLReader.readElementText(); |
| 531 | dupITags[0]++; |
| 532 | //Found <name> tag for interface |
| 533 | } else if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(DESCRIPTION_TAG)) { |
| 534 | //Found <description> tag |
| 535 | aInterface.d->attributes[QServiceInterfaceDescriptor::InterfaceDescription] = aXMLReader.readElementText(); |
| 536 | dupITags[3]++; |
| 537 | //Found </interface>, leave the loop |
| 538 | } else if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(INTERFACE_VERSION)) { |
| 539 | tmp.clear(); |
| 540 | tmp = aXMLReader.readElementText(); |
| 541 | if (tmp.isEmpty()) |
| 542 | continue; //creates NO_INTERFACE_VERSION error further below |
| 543 | bool success = checkVersion(tmp); |
| 544 | if ( success ) { |
| 545 | int majorVer = -1; |
| 546 | int minorVer = -1; |
| 547 | transformVersion(tmp, &majorVer, &minorVer); |
| 548 | aInterface.d->major = majorVer; |
| 549 | aInterface.d->minor = minorVer; |
| 550 | dupITags[1]++; |
| 551 | } else { |
| 552 | latestError = ServiceMetaData::SFW_ERROR_INVALID_VERSION; |
| 553 | parseError = true; |
| 554 | } |
| 555 | } else if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(INTERFACE_CAPABILITY)) { |
| 556 | tmp.clear(); |
| 557 | tmp= aXMLReader.readElementText(); |
| 558 | aInterface.d->attributes[QServiceInterfaceDescriptor::Capabilities] = tmp.split(QLatin1String(","), QString::SkipEmptyParts); |
| 559 | dupITags[2]++; |
| 560 | } else if (aXMLReader.isStartElement() && aXMLReader.name() == QLatin1String(INTERFACE_CUSTOM_PROPERTY)) { |
| 561 | parseError = true; |
| 562 | if (aXMLReader.attributes().hasAttribute(QLatin1String("key"))) { |
| 563 | const QString ref = aXMLReader.attributes().value(QLatin1String("key")).toString(); |
| 564 | if (!ref.isEmpty()) { |
| 565 | if (aInterface.d->customAttributes.contains(ref)) { |
| 566 | latestError = SFW_ERROR_DUPLICATED_CUSTOM_KEY; |
| 567 | continue; |
| 568 | } else { |
| 569 | QString value = aXMLReader.readElementText(); |
| 570 | if (value.isNull()) |
| 571 | value = QLatin1String(""); |
| 572 | aInterface.d->customAttributes[ref] = value; |
| 573 | parseError = false; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | if (parseError) |
| 578 | latestError = SFW_ERROR_INVALID_CUSTOM_TAG; |
| 579 | } else if (aXMLReader.isEndElement() && aXMLReader.name() == QLatin1String(INTERFACE_TAG)) { |
| 580 | break; |
| 581 | } else if (aXMLReader.isStartElement() || aXMLReader.isEndElement()) { |
| 582 | latestError = ServiceMetaData::SFW_ERROR_PARSE_INTERFACE; |
| 583 | parseError = true; |
| 584 | } else if (aXMLReader.tokenType() == QXmlStreamReader::Invalid) { |
| 585 | latestError = ServiceMetaData::SFW_ERROR_INVALID_XML_FILE; |
| 586 | parseError = true; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | if (!parseError) { |
| 591 | if (dupITags[1] == 0) { //no version tag found |
| 592 | latestError = ServiceMetaData::SFW_ERROR_NO_INTERFACE_VERSION; |
| 593 | parseError = true; |
| 594 | } else if (aInterface.d->interfaceName.isEmpty()) { |
| 595 | latestError = ServiceMetaData::SFW_ERROR_NO_INTERFACE_NAME; |
| 596 | parseError = true; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | for (int i=0;!parseError && i<4;i++) { |
| 601 | if (dupITags[i] > 1) { |
| 602 | parseError = true; |
| 603 | latestError = SFW_ERROR_DUPLICATED_TAG; |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (!parseError) { |
| 609 | const QString ident = aInterface.d->interfaceName |
| 610 | + QString::number(aInterface.majorVersion()) |
| 611 | + QLatin1String(".") |
| 612 | + QString::number(aInterface.minorVersion()); |
| 613 | if (duplicates.contains(ident.toLower())) { |
| 614 | latestError = ServiceMetaData::SFW_ERROR_DUPLICATED_INTERFACE; |
| 615 | parseError = true; |
| 616 | } else { |
| 617 | duplicates.insert(ident.toLower()); |
| 618 | serviceInterfaces.append(aInterface); |
| 619 | if (!m_latestIndex.contains(aInterface.d->interfaceName.toLower()) |
| 620 | || lessThan(latestInterfaceVersion(aInterface.d->interfaceName), aInterface)) |
| 621 | |
| 622 | { |
| 623 | m_latestIndex[aInterface.d->interfaceName.toLower()] = serviceInterfaces.count() - 1; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | return !parseError; |
| 628 | } |
| 629 | |
| 630 | QServiceInterfaceDescriptor ServiceMetaData::latestInterfaceVersion(const QString &interfaceName) |
| 631 | { |
| 632 | QServiceInterfaceDescriptor ret; |
| 633 | if (m_latestIndex.contains(interfaceName.toLower())) |
| 634 | return serviceInterfaces[m_latestIndex[interfaceName.toLower()]]; |
| 635 | else |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | QList<QServiceInterfaceDescriptor> ServiceMetaData::latestInterfaces() const |
| 640 | { |
| 641 | QList<QServiceInterfaceDescriptor> interfaces; |
| 642 | QHash<QString,int>::const_iterator i = m_latestIndex.constBegin(); |
| 643 | while (i != m_latestIndex.constEnd()) |
| 644 | { |
| 645 | interfaces.append(serviceInterfaces[i.value()]); |
| 646 | ++i; |
| 647 | } |
| 648 | return interfaces; |
| 649 | } |
| 650 | |
| 651 | bool ServiceMetaData::lessThan(const QServiceInterfaceDescriptor &d1, |
| 652 | const QServiceInterfaceDescriptor &d2) const |
| 653 | { |
| 654 | return (d1.majorVersion() < d2.majorVersion()) |
| 655 | || ( d1.majorVersion() == d2.majorVersion() |
| 656 | && d1.minorVersion() < d2.minorVersion()); |
| 657 | |
| 658 | } |
| 659 | |
| 660 | bool ServiceMetaData::greaterThan(const QString &v1, const QString &v2) const |
| 661 | { |
| 662 | int majorV1 = -1; |
| 663 | int minorV1 = -1; |
| 664 | transformVersion(v1, &majorV1, &minorV1); |
| 665 | |
| 666 | int majorV2 = -1; |
| 667 | int minorV2 = -1; |
| 668 | transformVersion(v2, &majorV2, &minorV2); |
| 669 | |
| 670 | return (majorV1 > majorV2 |
| 671 | || (majorV1 == majorV2 && minorV1 > minorV2)); |
| 672 | } |
| 673 | |
| 674 | bool ServiceMetaData::checkVersion(const QString &version) const |
| 675 | { |
| 676 | //match x.y as version format |
| 677 | QRegularExpression rx(QLatin1String("^([1-9][0-9]*)\\.(0+|[1-9][0-9]*)$")); |
| 678 | QRegularExpressionMatch match = rx.match(version); |
| 679 | int pos = match.capturedStart(); |
| 680 | QStringList list = match.capturedTexts(); |
| 681 | bool success = false; |
| 682 | if (pos == 0 && list.count() == 3 |
| 683 | && match.capturedLength() == version.length() ) |
| 684 | { |
| 685 | list[1].toInt(&success); |
| 686 | if ( success ) { |
| 687 | list[2].toInt(&success); |
| 688 | } |
| 689 | } |
| 690 | return success; |
| 691 | } |
| 692 | |
| 693 | void ServiceMetaData::transformVersion(const QString &version, int *major, int *minor) const |
| 694 | { |
| 695 | Q_ASSERT(major != NULL); |
| 696 | Q_ASSERT(minor != NULL); |
| 697 | if (!checkVersion(version)) { |
| 698 | *major = -1; |
| 699 | *minor = -1; |
| 700 | } else { |
| 701 | QRegularExpression rx(QLatin1String("^([1-9][0-9]*)\\.(0+|[1-9][0-9]*)$")); |
| 702 | QRegularExpressionMatch match = rx.match(version); |
| 703 | QStringList list = match.capturedTexts(); |
| 704 | Q_ASSERT(list.count() == 3); |
| 705 | *major = list[1].toInt(); |
| 706 | *minor = list[2].toInt(); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Clears the service metadata |
| 712 | * |
| 713 | */ |
| 714 | void ServiceMetaData::clearMetadata() |
| 715 | { |
| 716 | xmlVersion = QLatin1String("1.0"); |
| 717 | serviceName.clear(); |
| 718 | serviceLocation.clear(); |
| 719 | serviceDescription.clear(); |
| 720 | serviceInterfaces.clear(); |
| 721 | duplicates.clear(); |
| 722 | m_latestIndex.clear(); |
| 723 | serviceType = QService::Plugin; |
| 724 | } |
| 725 | |
| 726 | QT_END_NAMESPACE |