1 /*
2     Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
3 
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2 of the License, or (at your option) any later version.
8 
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13 
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19 
20 #include <QCoreApplication>
21 #include <QFile>
22 #include <QDebug>
23 
24 #include <wsdl/wsdl.h>
25 
26 #include <common/fileprovider.h>
27 #include <common/messagehandler.h>
28 #include <common/parsercontext.h>
29 
30 #include "converter.h"
31 #include "creator.h"
32 #include "settings.h"
33 
34 #include "compiler.h"
35 
36 using namespace KWSDL;
37 
Compiler()38 Compiler::Compiler()
39     : QObject(nullptr)
40 {
41 }
42 
run()43 void Compiler::run()
44 {
45     download();
46 }
47 
download()48 void Compiler::download()
49 {
50     FileProvider provider;
51 
52     QString fileName;
53     if (provider.get(Settings::self()->wsdlUrl(), fileName)) {
54         QFile file(fileName);
55         if (!file.open(QIODevice::ReadOnly)) {
56             qDebug("Unable to download file %s", Settings::self()->wsdlUrl().toEncoded().constData());
57             provider.cleanUp();
58             QCoreApplication::exit(1);
59             return;
60         }
61 
62         //qDebug() << "parsing" << fileName;
63         QXmlInputSource source(&file);
64         QXmlSimpleReader reader;
65         reader.setFeature(QLatin1String("http://xml.org/sax/features/namespace-prefixes"), true);
66 
67         QString errorMsg;
68         int errorLine, errorCol;
69         QDomDocument doc;
70         if (!doc.setContent(&source, &reader, &errorMsg, &errorLine, &errorCol)) {
71             qDebug("%s at (%d,%d)", qPrintable(errorMsg), errorLine, errorCol);
72             QCoreApplication::exit(2);
73             return;
74         }
75 
76         parse(doc.documentElement());
77 
78         provider.cleanUp();
79     } else {
80         qDebug("Unable to download file %s", Settings::self()->wsdlUrl().toEncoded().constData());
81         provider.cleanUp();
82         QCoreApplication::exit(1);
83         return;
84     }
85 }
86 
parse(const QDomElement & element)87 void Compiler::parse(const QDomElement &element)
88 {
89     NSManager namespaceManager;
90 
91     // we don't parse xml.xsd, so hardcode the xml prefix (for xml:lang)
92     namespaceManager.setPrefix(QLatin1String("xml"), NSManager::xmlNamespace());
93 
94     MessageHandler messageHandler;
95     ParserContext context;
96     context.setNamespaceManager(&namespaceManager);
97     context.setMessageHandler(&messageHandler);
98     context.setDocumentBaseUrl(QUrl(Settings::self()->wsdlBaseUrl()));
99 
100     Definitions definitions;
101     definitions.setWantedService(Settings::self()->wantedService());
102     if (definitions.loadXML(&context, element)) {
103 
104         definitions.fixUpDefinitions(/*&context, element*/);
105 
106 
107         WSDL wsdl;
108         wsdl.setDefinitions(definitions);
109         wsdl.setNamespaceManager(namespaceManager);
110 
111         KODE::Code::setDefaultIndentation(4);
112         KWSDL::Converter converter;
113         converter.setWSDL(wsdl);
114 
115         if (!converter.convert()) {
116             QCoreApplication::exit(4);
117         } else {
118             KWSDL::Creator creator;
119             creator.setOutputDirectory(Settings::self()->outputDirectory());
120             creator.setSourceFile(Settings::self()->wsdlFileName());
121             creator.setHeaderFileName(Settings::self()->headerFileName());
122             creator.setImplementationFileName(Settings::self()->implementationFileName());
123 
124             creator.setClasses(converter.classes());
125             if (Settings::self()->generateHeader()) {
126                 creator.createHeader();
127             }
128             if (Settings::self()->generateImplementation()) {
129                 creator.createImplementation();
130             }
131 
132             QCoreApplication::exit(0);
133         }
134     } else {
135         QCoreApplication::exit(3);
136     }
137 }
138 
139 #include "moc_compiler.cpp"
140