summaryrefslogtreecommitdiff
path: root/cpp/src/slice2wsdl/Gen.cpp
blob: 503975aaf5af1907041cedcb1c66368f5dbfe0b3 (plain)
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
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <IceUtil/Functional.h>
#include <IceUtil/OutputUtil.h>

#include <Slice/CPlusPlusUtil.h> // TODO: ???

#include <Gen.h>

using namespace std;
using namespace Slice;
using namespace IceUtil;

static const string internalId = "_internal.";

Slice::Gen::Gen(const string& name, const string& base, const string& include,
		const vector<string>& includePaths, const string& dir) :
    _name(name),
    _base(base),
    _include(include),
    _includePaths(includePaths),
    _dir(dir)
{
    _orgName = "http://www.noorg.org"; // TODO: argument!
    for(vector<string>::iterator p = _includePaths.begin(); p != _includePaths.end(); ++p)
    {
	if(p->length() && (*p)[p->length() - 1] != '/')
	{
	    *p += '/';
	}
    }

    string::size_type pos = _base.rfind('/');
    if(pos != string::npos)
    {
	_base.erase(0, pos + 1);
    }
}

Slice::Gen::~Gen()
{
}

bool
Slice::Gen::visitClassDefStart(const ClassDefPtr& p)
{
    XMLOutput O;

    //string fileO = _base + ".wsdl";
    string fileO = containedToId(p) + p->name() + ".wsdl";
    if(!_dir.empty())
    {
	fileO = _dir + '/' + fileO;
    }

    O.open(fileO.c_str());
    if(!O)
    {
	cerr << _name << ": can't open `" << fileO << "' for writing: " << strerror(errno) << endl;
	return false;
    }

    printHeader(O);
    O << "\n<!-- Generated from file `" << changeInclude(_base, _includePaths) << ".ice' -->\n";

    string scopeId = containedToId(p);

    //
    // TODO: It would be better if start() aligned the attributes
    // correctly.
    //
    ostringstream os;
    os << "wsdl:definitions name=\"" << scopeId << p->name() << "\""
       << "\n                 xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\""
       << "\n                 xmlns:xsd1=\"" << _orgName << "/schemas\""
       << "\n                 xmlns:tns=\"" << _orgName << "/definitions\""
       << "\n                 targetNamespace=\"" << _orgName << "/definitions\"";

    O << se(os.str());

    // TODO: schemaLocation?
    O << sp << nl << "<wsdl:import namespace=\"" << _orgName << "/schemas\" location=\"" << _base << ".xsd\"/>";


    OperationList ops = p->allOperations();
    for(OperationList::const_iterator q = ops.begin(); q != ops.end(); ++q)
    {
	emitMessage(O, *q);
    }

    O << sp;

    os.str("");
    os << "wsdl:portType name=\"" << scopeId << "PortType\"";
    O << se(os.str());

    for(OperationList::const_iterator r = ops.begin(); r != ops.end(); ++r)
    {
	emitOperation(O, *r);
    }

    O << ee; // PortType

    O << ee; // definitions

    return false;
}

void
Slice::Gen::emitMessage(XMLOutput& O, const OperationPtr& p)
{
    O << sp;

    string scopeId = containedToId(p);
    
    ostringstream os;
    os << "wsdl:message name=\"input." << p->name() << "\"";
    O << se(os.str());

    O << nl << "<wsdl:part name=\"body\" element=\"xsd1:" << scopeId << "request." << p->name() << "\"/>";

    O << ee; // message

    os.str("");
    os << "wsdl:message name=\"output." << p->name() << "\"";
    O << se(os.str());

    O << nl << "<wsdl:part name=\"body\" element=\"xsd1:" << scopeId << "reply." << p->name() << "\"/>";

    O << ee; // message

    os.str("");
    os << "wsdl:message name=\"metadata." << p->name() << "\"";
    O << se(os.str());

    list<string> metaData = p->getMetaData();
    for(list<string>::iterator iter = metaData.begin(); iter != metaData.end(); ++iter)
    {
        O << nl << "<wsdl:part name=\"" << *iter << "\"/>";
    }

    O << ee; // message
}

void
Slice::Gen::emitOperation(XMLOutput& O, const OperationPtr& p)
{
    string scopeId = containedToId(p);
    
    ostringstream os;
    os << "wsdl:operation name=\"" << p->name() << "\"";
    O << se(os.str());

    O << nl << "<wsdl:input message=\"tns:input." << p->name() << "\"/>";
    O << nl << "<wsdl:output message=\"tns:output." << p->name() << "\"/>";

    O << ee; // operation
}

void
Slice::Gen::printHeader(XMLOutput& O)
{
    static const char* header =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!--\n"
"**********************************************************************\n"
"Copyright (c) 2001\n"
"MutableRealms, Inc.\n"
"Huntsville, AL, USA\n"
"\n"
"All Rights Reserved\n"
"\n"
"Generated by the `slice2wsdl' converter\n"
"**********************************************************************\n"
"-->";
    
    O.zeroIndent();
    O << header;
    O << "\n<!-- Ice version " << ICE_STRING_VERSION << " -->";
    O.restoreIndent();
}


string
Slice::Gen::containedToId(const ContainedPtr& contained)
{
    assert(contained);

    string scoped = contained->scope();
    if(scoped[0] == ':')
    {
	scoped.erase(0, 2);
    }

    string id;

    id.reserve(scoped.size());

    for(unsigned int i = 0; i < scoped.size(); ++i)
    {
	if(scoped[i] == ':')
	{
	    id += '.';
	    ++i;
	}
	else
	{
	    id += scoped[i];
	}
    }

    return id;
}