1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/*
SPDX-FileCopyrightText: 2005 Tobias Koenig <tokoe@kde.org>
SPDX-License-Identifier: MIT
*/
#include "definitions.h"
#include <QDir>
#include <QFile>
#include <QUrl>
#include <common/fileprovider.h>
#include <common/messagehandler.h>
#include <common/nsmanager.h>
#include <common/parsercontext.h>
#include "../src/settings.h"
#include <wsdl/port.h>
#include <QDebug>
using namespace KWSDL;
Definitions::Definitions()
{
}
Definitions::~Definitions()
{
}
void Definitions::setName(const QString &name)
{
mName = name;
}
QString Definitions::name() const
{
return mName;
}
void Definitions::setTargetNamespace(const QString &targetNamespace)
{
mTargetNamespace = targetNamespace;
mType.setNameSpace(mTargetNamespace);
}
QString Definitions::targetNamespace() const
{
return mTargetNamespace;
}
#if 0
void Definitions::setBindings(const Binding::List &bindings)
{
mBindings = bindings;
}
#endif
Binding::List Definitions::bindings() const
{
return mBindings;
}
#if 0
void Definitions::setImports(const Import::List &imports)
{
mImports = imports;
}
Import::List Definitions::imports() const
{
return mImports;
}
#endif
void Definitions::setMessages(const Message::List &messages)
{
mMessages = messages;
}
Message::List Definitions::messages() const
{
return mMessages;
}
void Definitions::setPortTypes(const PortType::List &portTypes)
{
mPortTypes = portTypes;
}
PortType::List Definitions::portTypes() const
{
return mPortTypes;
}
#if 0
void Definitions::setService(const Service &service)
{
mService = service;
}
#endif
Service::List Definitions::services() const
{
return mServices;
}
void Definitions::setType(const Type &type)
{
mType = type;
}
Type Definitions::type() const
{
return mType;
}
bool Definitions::loadXML(ParserContext *context, const QDomElement &element)
{
setTargetNamespace(element.attribute(QLatin1String("targetNamespace")));
mName = element.attribute(QLatin1String("name"));
context->namespaceManager()->enterChild(element);
QDomElement child = element.firstChildElement();
while (!child.isNull()) {
NSManager namespaceManager(context, child);
const QName tagName(child.tagName());
if (tagName.localName() == QLatin1String("import")) {
QString oldTn = targetNamespace();
QString oldName = mName;
importDefinition(context, child.attribute(QLatin1String("location")));
setTargetNamespace(oldTn);
mName = std::move(oldName);
} else if (tagName.localName() == QLatin1String("types")) {
if (!mType.loadXML(context, child)) {
return false;
}
} else if (tagName.localName() == QLatin1String("message")) {
Message message(mTargetNamespace);
message.loadXML(context, child);
// qDebug() << "Definitions: found message" << message.name() << message.nameSpace();
mMessages.append(message);
} else if (tagName.localName() == QLatin1String("portType")) {
PortType portType(mTargetNamespace);
portType.loadXML(context, child);
mPortTypes.append(portType);
} else if (tagName.localName() == QLatin1String("binding")) {
Binding binding(mTargetNamespace);
binding.loadXML(context, child);
mBindings.append(binding);
} else if (tagName.localName() == QLatin1String("service")) {
const QString name = child.attribute(QLatin1String("name"));
// qDebug() << "Service:" << name << "looking for" << mWantedService;
// is this the service we want?
if (mWantedService.isEmpty() || mWantedService == name) {
Service service(mTargetNamespace);
service.loadXML(context, &mBindings, child);
mServices.append(service);
}
} else if (tagName.localName() == QLatin1String("documentation")) {
// ignore documentation for now
} else {
context->messageHandler()->warning(QString::fromLatin1("Definitions: unknown tag %1").arg(child.tagName()));
}
child = child.nextSiblingElement();
}
return true;
}
void Definitions::fixUpDefinitions(/*ParserContext *context, const QDomElement &element */)
{
if (mServices.isEmpty()) {
if (!mBindings.isEmpty()) {
qDebug() << "No service tag found in the wsdl file, generating one service per binding";
for (const Binding &bind : qAsConst(mBindings)) {
Service service(mTargetNamespace);
service.setName(bind.name() + "Service");
Port port(mTargetNamespace);
port.setName(bind.name() + "Port");
QName bindingName(bind.portTypeName().prefix() + ":" + bind.name());
bindingName.setNameSpace(bind.nameSpace());
port.setBindingName(bindingName);
Port::List portList;
portList.append(port);
service.setPorts(portList);
mServices.append(service);
}
} else {
Q_ASSERT(!mPortTypes.isEmpty());
qDebug() << "No service or binding tag found in the wsdl file, generating only messages";
for (const PortType &portType : qAsConst(mPortTypes)) {
Binding binding(mTargetNamespace);
binding.setName(portType.name() + "Binding");
binding.setPortTypeName(QName(portType.nameSpace(), portType.name()));
binding.setType(Binding::UnknownBinding);
mBindings.append(binding);
Port port(mTargetNamespace);
port.setName(portType.name());
port.setBindingName(QName(binding.nameSpace(), binding.name()));
Service service(mTargetNamespace);
service.setName(portType.name() + "Service");
service.setPorts(Port::List() << port);
mServices.append(service);
}
}
}
}
static QUrl urlForLocation(ParserContext *context, const QString &location)
{
QUrl url(location);
if ((url.scheme().isEmpty() || url.scheme() == QLatin1String("file"))) {
QDir dir(location);
if (dir.isRelative()) {
url = context->documentBaseUrl();
url.setPath(url.path() + QLatin1Char('/') + location);
}
}
return url;
}
void Definitions::importDefinition(ParserContext *context, const QString &location)
{
if (location.isEmpty()) {
context->messageHandler()->warning(QString::fromLatin1("Definitions import: location tag required: %1").arg(location));
return;
}
FileProvider provider(Settings::self()->useLocalFilesOnly(), Settings::self()->importPathList());
QString fileName;
const QUrl locationUrl = urlForLocation(context, location);
qDebug("Importing wsdl definition at %s", locationUrl.toEncoded().constData());
if (provider.get(locationUrl, fileName)) {
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
qDebug("Unable to open file %s", qPrintable(file.fileName()));
return;
}
QDomDocument doc(QLatin1String("kwsdl"));
QString errorMsg;
int errorLine, errorColumn;
bool ok = doc.setContent(&file, false, &errorMsg, &errorLine, &errorColumn);
if (!ok) {
qDebug("Error[%d:%d] %s", errorLine, errorColumn, qPrintable(errorMsg));
return;
}
// prepare the new context to avoid infinite recursion
QDomElement rootNode = doc.documentElement();
NSManager namespaceManager(context, rootNode);
const QName tagName(rootNode.tagName());
if (tagName.localName() == QLatin1String("definitions")) {
// recursivity
context->namespaceManager()->enterChild(rootNode);
const QUrl oldBaseUrl = context->documentBaseUrl();
context->setDocumentBaseUrlFromFileUrl(locationUrl);
loadXML(context, rootNode);
context->setDocumentBaseUrl(oldBaseUrl);
} else {
qDebug("No definition tag found in imported wsdl file %s", locationUrl.toEncoded().constData());
}
file.close();
provider.cleanUp();
}
}
#if 0
void Definitions::saveXML(ParserContext *context, QDomDocument &document) const
{
QDomElement element = document.createElement("definitions");
document.appendChild(element);
if (!mTargetNamespace.isEmpty()) {
element.setAttribute("targetNamespace", mTargetNamespace);
}
if (!mName.isEmpty()) {
element.setAttribute("name", mName);
}
{
Import::List::ConstIterator it(mImports.begin());
const Import::List::ConstIterator endIt(mImports.end());
for (; it != endIt; ++it) {
(*it).saveXML(context, document, element);
}
}
mType.saveXML(context, document, element);
{
Message::List::ConstIterator it(mMessages.begin());
const Message::List::ConstIterator endIt(mMessages.end());
for (; it != endIt; ++it) {
(*it).saveXML(context, document, element);
}
}
{
PortType::List::ConstIterator it(mPortTypes.begin());
const PortType::List::ConstIterator endIt(mPortTypes.end());
for (; it != endIt; ++it) {
(*it).saveXML(context, document, element);
}
}
{
Binding::List::ConstIterator it(mBindings.begin());
const Binding::List::ConstIterator endIt(mBindings.end());
for (; it != endIt; ++it) {
(*it).saveXML(context, document, element);
}
}
mService.saveXML(context, &mBindings, document, element);
}
#endif
void Definitions::setWantedService(const QString &name)
{
mWantedService = name;
}
|