-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
634 lines (558 loc) · 18.3 KB
/
main.go
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
package main
import (
"bytes"
"encoding/xml"
"errors"
"flag"
"go/format"
"io"
"net/http"
"net/url"
"os"
"sort"
"strings"
)
type WsdlDefinitions struct {
Name string `xml:"name,attr"`
TargetNamespace string `xml:"targetNamespace,attr"`
Imports []*WsdlImport `xml:"http://schemas.xmlsoap.org/wsdl/ import"`
Types []*WsdlTypes `xml:"http://schemas.xmlsoap.org/wsdl/ types"`
Messages []*WsdlMessage `xml:"http://schemas.xmlsoap.org/wsdl/ message"`
PortTypes []*WsdlPortTypes `xml:"http://schemas.xmlsoap.org/wsdl/ portType"`
Services []*WsdlService `xml:"http://schemas.xmlsoap.org/wsdl/ service"`
Bindings []*WsdlBinding `xml:"http://schemas.xmlsoap.org/wsdl/ binding"`
}
type WsdlBinding struct {
Name string `xml:"name,attr"`
Type string `xml:"type,attr"`
Operations []*WsdlOperation `xml:"http://schemas.xmlsoap.org/wsdl/ operation"`
SoapBindings []*SoapBinding `xml:"http://schemas.xmlsoap.org/wsdl/soap/ binding"`
}
type SoapBinding struct {
Transport string `xml:"transport,attr"`
}
type WsdlTypes struct {
XsdSchema []*XsdSchema `xml:"http://www.w3.org/2001/XMLSchema schema"`
}
type WsdlImport struct {
Namespace string `xml:"namespace,attr"`
Location string `xml:"location,attr"`
}
type WsdlMessage struct {
Name string `xml:"name,attr"`
Parts []*WsdlMessagePart `xml:"http://schemas.xmlsoap.org/wsdl/ part"`
}
type WsdlMessagePart struct {
Name string `xml:"name,attr"`
Element string `xml:"element,attr"`
}
type WsdlPortTypes struct {
Name string `xml:"name,attr"`
Operations []*WsdlOperation `xml:"http://schemas.xmlsoap.org/wsdl/ operation"`
}
type WsdlOperation struct {
Name string `xml:"name,attr"`
Inputs []*WsdlOperationInput `xml:"http://schemas.xmlsoap.org/wsdl/ input"`
Outputs []*WsdlOperationOutput `xml:"http://schemas.xmlsoap.org/wsdl/ output"`
Faults []*WsdlOperationFault `xml:"http://schemas.xmlsoap.org/wsdl/ fault"`
SoapOperations []*SoapOperation `xml:"http://schemas.xmlsoap.org/wsdl/soap/ operation"`
}
type WsdlOperationInput struct {
Message string `xml:"message,attr"`
WsawAction string `xml:"http://www.w3.org/2006/05/addressing/wsdl Action,attr"`
}
type WsdlOperationOutput struct {
Message string `xml:"message,attr"`
WsawAction string `xml:"http://www.w3.org/2006/05/addressing/wsdl Action,attr"`
}
type WsdlOperationFault struct {
Name string `xml:"name,attr"`
Message string `xml:"message,attr"`
WsawAction string `xml:"http://www.w3.org/2006/05/addressing/wsdl Action,attr"`
}
type WsdlService struct {
Name string `xml:"name,attr"`
Ports []*WsdlPort `xml:"http://schemas.xmlsoap.org/wsdl/ port"`
}
type WsdlPort struct {
Name string `xml:"name,attr"`
Binding string `xml:"binding,attr"`
SoapAddresses []*SoapAddress `xml:"http://schemas.xmlsoap.org/wsdl/soap/ address"`
}
type SoapAddress struct {
Location string `xml:"location,attr"`
}
type SoapOperation struct {
SoapAction string `xml:"soapAction,attr"`
Style string `xml:"style,attr"`
}
type SoapBody struct {
Use string `xml:"use,attr"`
}
type XsdSchema struct {
TargetNamespace string `xml:"targetNamespace,attr"`
ElementFormDefault string `xml:"elementFormDefault,attr"`
Imports []*XsdImport `xml:"http://www.w3.org/2001/XMLSchema import"`
Elements []*XsdElement `xml:"http://www.w3.org/2001/XMLSchema element"`
ComplexTypes []*XsdComplexType `xml:"http://www.w3.org/2001/XMLSchema complexType"`
}
type XsdImport struct {
SchemaLocation string `xml:"schemaLocation,attr"`
Namespace string `xml:"namespace,attr"`
}
type XsdElement struct {
Name string `xml:"name,attr"`
Nillable bool `xml:"nillable,attr"`
Type string `xml:"type,attr"`
MinOccurs string `xml:"minOccurs,attr"`
MaxOccurs string `xml:"maxOccurs,attr"`
ComplexType *XsdComplexType `xml:"http://www.w3.org/2001/XMLSchema complexType"`
SimpleType *XsdSimpleType `xml:"http://www.w3.org/2001/XMLSchema simpleType"`
}
type XsdComplexType struct {
Name string `xml:"name,attr"`
Sequence *XsdSequence `xml:"http://www.w3.org/2001/XMLSchema sequence"`
}
type XsdSimpleType struct {
Name string `xml:"name,attr"`
Sequence *XsdRestriction `xml:"http://www.w3.org/2001/XMLSchema restriction"`
}
type XsdSequence struct {
Elements []*XsdElement `xml:"http://www.w3.org/2001/XMLSchema element"`
}
type XsdRestriction struct {
Base string `xml:"base,attr"`
Pattern *XsdPattern `xml:"http://www.w3.org/2001/XMLSchema pattern"`
MinInclusive *XsdMinInclusive `xml:"http://www.w3.org/2001/XMLSchema minInclusive"`
MaxInclusive *XsdMaxInclusive `xml:"http://www.w3.org/2001/XMLSchema maxInclusive"`
}
type XsdPattern struct {
Value string `xml:"value,attr"`
}
type XsdMinInclusive struct {
Value string `xml:"value,attr"`
}
type XsdMaxInclusive struct {
Value string `xml:"value,attr"`
}
func main() {
flag.Parse()
wsdlRoot := flag.Arg(0)
wsdlMap := make(map[string]*WsdlDefinitions)
xsdMap := make(map[string]*XsdSchema)
// "http://na2.replicon.com/services/ProjectService1.svc?wsdl"
_, err := LoadWsdl(wsdlRoot, nil, wsdlMap, xsdMap)
if err != nil {
println("Error fetching WSDL:", err.Error())
return
}
w := &bytes.Buffer{}
//_, err = w.WriteString("package " + wsdl.Name)
_, err = w.WriteString("package main\n\n")
if err != nil {
println("Error writing header:", err.Error())
return
}
_, err = w.WriteString("import (\n")
_, err = w.WriteString("\"encoding/xml\"\n")
_, err = w.WriteString("\"math/big\"\n")
_, err = w.WriteString(")\n")
_, err = w.WriteString("var _ = big.MaxBase // Avoid potential unused-import error\n\n")
complexTypes := make(NamedComplexTypeSlice, 0)
for _, xsd := range xsdMap {
for _, element := range xsd.Elements {
if element.ComplexType == nil {
continue
}
complexTypes = append(complexTypes, NamedComplexType{element.Name, element.ComplexType, xsd})
}
for _, complexType := range xsd.ComplexTypes {
complexTypes = append(complexTypes, NamedComplexType{complexType.Name, complexType, xsd})
}
}
sort.Sort(&complexTypes)
for _, complexType := range complexTypes {
WriteComplexType(w, complexType.Name, complexType.ComplexType, complexType.Xsd)
}
formattedOutput, err := format.Source(w.Bytes())
if err != nil {
println("Error while goformat'ing code:", err.Error())
return
}
file, err := os.OpenFile("test-client/ProjectService1.go", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
println("Error opening OutTest.wsdl:", err.Error())
return
}
file.Write(formattedOutput)
file.Close()
}
type NamedComplexType struct {
Name string
ComplexType *XsdComplexType
Xsd *XsdSchema
}
type NamedComplexTypeSlice []NamedComplexType
func (s *NamedComplexTypeSlice) Len() int {
return len(*s)
}
func (s *NamedComplexTypeSlice) Less(i, j int) bool {
return strings.ToLower((*s)[i].Name) < strings.ToLower((*s)[j].Name)
}
func (s *NamedComplexTypeSlice) Swap(i, j int) {
(*s)[i], (*s)[j] = (*s)[j], (*s)[i]
}
func WriteComplexType(w io.Writer, elementName string, complexType *XsdComplexType, xsd *XsdSchema) {
io.WriteString(w, "type ")
io.WriteString(w, elementName)
io.WriteString(w, " struct {\n")
if complexType.Name == "" {
io.WriteString(w, "\tXMLName xml.Name `xml:\"")
io.WriteString(w, xsd.TargetNamespace)
io.WriteString(w, " ")
io.WriteString(w, elementName)
io.WriteString(w, "\"`\n")
}
for _, element := range complexType.Sequence.Elements {
io.WriteString(w, "\t")
io.WriteString(w, strings.Title(element.Name))
io.WriteString(w, " ")
if element.MaxOccurs == "unbounded" {
io.WriteString(w, "[]")
}
if element.MinOccurs == "0" {
io.WriteString(w, "*")
}
io.WriteString(w, FindType(element.Type))
io.WriteString(w, " `xml:\"")
io.WriteString(w, xsd.TargetNamespace)
io.WriteString(w, " ")
io.WriteString(w, element.Name)
io.WriteString(w, "\"`\n")
}
io.WriteString(w, "}\n\n")
}
func FindType(elementType string) string {
if strings.HasPrefix(elementType, "tns:") {
return elementType[4:]
} else if elementType == "xs:boolean" {
return "bool"
} else if elementType == "xs:string" {
return "string"
} else if elementType == "xs:byte" {
return "int8"
} else if elementType == "xs:short" {
return "int16"
} else if elementType == "xs:int" {
return "int32"
} else if elementType == "xs:long" {
return "int64"
} else if elementType == "xs:unsignedByte" {
return "uint8"
} else if elementType == "xs:unsignedShort" {
return "uint16"
} else if elementType == "xs:unsignedInt" {
return "uint32"
} else if elementType == "xs:unsignedLong" {
return "uint64"
} else if elementType == "xs:integer" {
return "big.Int"
} else if elementType == "xs:nonPositiveInteger" {
return "big.Int"
} else if elementType == "xs:negativeInteger" {
return "big.Int"
} else if elementType == "xs:nonNegativeInteger" {
return "big.Int"
} else if elementType == "xs:positiveInteger" {
return "big.Int"
} else if elementType == "xs:decimal" {
return "big.Rat"
} else if elementType == "xs:float" {
return "float32"
} else if elementType == "xs:double" {
return "float64"
}
/*
xs:hexBinary
xs:base64Binary
xs:duration
xs:dateTime
xs:time
xs:date
xs:gYearMonth
xs:gYear
xs:gMonthDay
xs:gDay
xs:gMonth
xs:anyURI
xs:QName
xs:NOTATION
xs:normalizedString
xs:token
xs:language
xs:ID
xs:IDREF
xs:IDREFS
xs:ENTITY
xs:ENTITIES
xs:NMTOKEN
xs:NMTOKENS
xs:Name
xs:NCName
*/
return "string"
}
func GetAbsoluteUrl(rawUrl string, relativeRawUrl *string) (*url.URL, error) {
targetUrl, err := url.Parse(rawUrl)
if err != nil {
return nil, err
}
if relativeRawUrl != nil {
relativeUrl, err := url.Parse(*relativeRawUrl)
if err != nil {
return nil, err
}
targetUrl = relativeUrl.ResolveReference(targetUrl)
}
return targetUrl, nil
}
func LoadUrl(absoluteUrl url.URL) (io.Reader, error) {
if absoluteUrl.Scheme == "http" || absoluteUrl.Scheme == "https" {
response, err := http.Get(absoluteUrl.String())
if err != nil {
return nil, err
}
return response.Body, nil
} else if absoluteUrl.Scheme == "file" {
if absoluteUrl.Host != "" && absoluteUrl.Host != "localhost" {
return nil, errors.New("file:// URL has unknown host " + absoluteUrl.Host)
}
path := absoluteUrl.String()[5:]
return os.Open(path)
}
return nil, errors.New("unable to understand url scheme:" + absoluteUrl.Scheme)
}
func LoadWsdl(url string, relativeUrl *string, wsdlMap map[string]*WsdlDefinitions, xsdMap map[string]*XsdSchema) (retval *WsdlDefinitions, err error) {
absoluteUrl, err := GetAbsoluteUrl(url, relativeUrl)
if err != nil {
return nil, err
}
existing := wsdlMap[absoluteUrl.String()]
if existing != nil {
return existing, nil
}
println("Loading WSDL:", absoluteUrl.String())
response, err := LoadUrl(*absoluteUrl)
if err != nil {
return nil, err
}
retval, err = ParseWsdl(response, "http://dws-na2-int.replicon.com/", "http://na2.replicon.com/services/")
if err != nil {
return nil, err
}
wsdlMap[absoluteUrl.String()] = retval
absoluteUrlString := absoluteUrl.String()
for _, imp := range retval.Imports {
wsdlMap[imp.Location], err = LoadWsdl(imp.Location, &absoluteUrlString, wsdlMap, xsdMap)
if err != nil {
return nil, err
}
}
for _, types := range retval.Types {
for _, schema := range types.XsdSchema {
for _, imp := range schema.Imports {
_, err = LoadXsd(imp.SchemaLocation, &absoluteUrlString, xsdMap)
if err != nil {
return nil, err
}
}
}
}
return retval, nil
}
func LoadXsd(url string, relativeUrl *string, xsdMap map[string]*XsdSchema) (retval *XsdSchema, err error) {
absoluteUrl, err := GetAbsoluteUrl(url, relativeUrl)
if err != nil {
return nil, err
}
existing := xsdMap[absoluteUrl.String()]
if existing != nil {
return existing, nil
}
println("Loading XSD:", absoluteUrl.String())
response, err := LoadUrl(*absoluteUrl)
if err != nil {
return nil, err
}
retval, err = ParseXsd(response, "http://dws-na2-int.replicon.com/", "http://na2.replicon.com/services/")
if err != nil {
return nil, err
}
xsdMap[absoluteUrl.String()] = retval
absoluteUrlString := absoluteUrl.String()
for _, imp := range retval.Imports {
_, err = LoadXsd(imp.SchemaLocation, &absoluteUrlString, xsdMap)
}
return retval, nil
}
func DebugPrintWsdl(wsdl *WsdlDefinitions) {
println("WSDL:", wsdl.TargetNamespace, wsdl.Name)
for _, imp := range wsdl.Imports {
println("Import:", imp.Location, imp.Namespace)
}
for _, types := range wsdl.Types {
for _, schema := range types.XsdSchema {
println("Schema:", schema.TargetNamespace)
for _, imp := range schema.Imports {
println("\tImport:", imp.SchemaLocation, imp.Namespace)
}
}
}
for _, message := range wsdl.Messages {
println("Message:", message.Name)
for _, part := range message.Parts {
println("\tPart:", part.Name, part.Element)
}
}
for _, portType := range wsdl.PortTypes {
println("PortType:", portType.Name)
for _, operation := range portType.Operations {
println("\tOperation:", operation.Name)
for _, input := range operation.Inputs {
println("\t\tInput:", input.Message, input.WsawAction)
}
for _, output := range operation.Outputs {
println("\t\tOutput:", output.Message, output.WsawAction)
}
for _, fault := range operation.Faults {
println("\t\tFault:", fault.Message, fault.WsawAction)
}
}
}
for _, service := range wsdl.Services {
println("Service:", service.Name)
for _, port := range service.Ports {
println("\tPort:", port.Name, port.Binding)
for _, address := range port.SoapAddresses {
println("\t\tAddress:", address.Location)
}
}
}
for _, binding := range wsdl.Bindings {
println("Binding:", binding.Name, binding.Type)
for _, soapBinding := range binding.SoapBindings {
println("\tSOAP Binding:", soapBinding.Transport)
}
for _, operation := range binding.Operations {
println("\tOperation:", operation.Name)
for _, soapOperation := range operation.SoapOperations {
println("\t\tSOAP Operation:", soapOperation.SoapAction, soapOperation.Style)
}
}
}
}
func ParseWsdl(reader io.Reader, urlSubstituteOld string, urlSubstituteNew string) (*WsdlDefinitions, error) {
decoder := xml.NewDecoder(reader)
obj := WsdlDefinitions{}
err := decoder.Decode(&obj)
if err != nil {
return nil, err
}
for _, imp := range obj.Imports {
imp.Location = strings.Replace(imp.Location, urlSubstituteOld, urlSubstituteNew, -1)
}
for _, types := range obj.Types {
for _, schema := range types.XsdSchema {
for _, imp := range schema.Imports {
imp.SchemaLocation = strings.Replace(imp.SchemaLocation, urlSubstituteOld, urlSubstituteNew, -1)
}
}
}
return &obj, nil
}
func ParseXsd(reader io.Reader, urlSubstituteOld string, urlSubstituteNew string) (*XsdSchema, error) {
decoder := xml.NewDecoder(reader)
obj := XsdSchema{}
err := decoder.Decode(&obj)
if err != nil {
return nil, err
}
for _, imp := range obj.Imports {
imp.SchemaLocation = strings.Replace(imp.SchemaLocation, urlSubstituteOld, urlSubstituteNew, -1)
}
return &obj, nil
}
// println("WSDL:", obj.TargetNamespace, obj.Name)
// for _, imp := range obj.Imports {
// println("Import:", strings.Replace(imp.Namespace, substituteOld, substituteNew, -1), imp.Location)
// }
// for _, types := range obj.Types {
// for _, schema := range types.XsdSchema {
// println("Schema:", schema.TargetNamespace)
// for _, imp := range schema.Imports {
// println("\tImport:", strings.Replace(imp.SchemaLocation, substituteOld, substituteNew, -1), imp.Namespace)
// }
// }
// }
// for _, message := range obj.Messages {
// println("Message:", message.Name)
// for _, part := range message.Parts {
// println("\tPart:", part.Name, part.Element)
// }
// }
// for _, portType := range obj.PortTypes {
// println("PortType:", portType.Name)
// for _, operation := range portType.Operations {
// println("\tOperation:", operation.Name)
// for _, input := range operation.Inputs {
// println("\t\tInput:", input.Message, input.WsawAction)
// }
// for _, output := range operation.Outputs {
// println("\t\tOutput:", output.Message, output.WsawAction)
// }
// for _, fault := range operation.Faults {
// println("\t\tFault:", fault.Message, fault.WsawAction)
// }
// }
// }
// for _, service := range obj.Services {
// println("Service:", service.Name)
// for _, port := range service.Ports {
// println("\tPort:", port.Name, port.Binding)
// for _, address := range port.SoapAddresses {
// println("\t\tAddress:", address.Location)
// }
// }
// }
// for _, binding := range obj.Bindings {
// println("Binding:", binding.Name, binding.Type)
// for _, soapBinding := range binding.SoapBindings {
// println("\tSOAP Binding:", soapBinding.Transport)
// }
// for _, operation := range binding.Operations {
// println("\tOperation:", operation.Name)
// for _, soapOperation := range operation.SoapOperations {
// println("\t\tSOAP Operation:", soapOperation.SoapAction, soapOperation.Style)
// }
// }
// }
// /*
// w, err := os.OpenFile("OutTest.wsdl", os.O_CREATE|os.O_WRONLY, 0644)
// if err != nil {
// println("Error opening OutTest.wsdl:", err.Error())
// return
// }
// _, err = w.WriteString(xml.Header)
// if err != nil {
// println("Error writing header:", err.Error())
// return
// }
// encoder := xml.NewEncoder(w)
// err = encoder.Encode(&obj)
// if err != nil {
// println("Error encoding document:", err.Error())
// return
// }
// w.Close()
// */
// }