1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.web.soap.service.parser.serviceparser;
7 
8 import std.array : split;
9 import std..string : format;
10 
11 import diamond.dom;
12 import diamond.xml;
13 
14 package(diamond.web.soap.service.parser):
15 /**
16 * Parses the services of the wsdl file.
17 * Params:
18 *   root =       The root node of the wsdl file.
19 */
20 string parseServices(XmlNode root)
21 {
22   auto services = root.getByTagName("wsdl:service");
23 
24   if (!services || !services.length)
25   {
26     services = root.getByTagName("xs:service");
27   }
28 
29   if (!services || !services.length)
30   {
31     services = root.getByTagName("xsd:service");
32   }
33 
34   if (!services || !services.length)
35   {
36     services = root.getByTagName("soap:service");
37   }
38 
39   if (!services || !services.length)
40   {
41     services = root.getByTagName("service");
42   }
43 
44   auto result = q{
45 private static immutable(string[string]) __services;
46 
47 static this()
48 {
49 %s
50 }
51   };
52 
53   string endpoints = "";
54 
55   foreach (service; services)
56   {
57     auto ports = service.getByTagName("wsdl:port");
58 
59     if (!ports || !ports.length)
60     {
61       ports = service.getByTagName("xs:port");
62     }
63 
64     if (!ports || !ports.length)
65     {
66       ports = service.getByTagName("xsd:port");
67     }
68 
69     if (!ports || !ports.length)
70     {
71       ports = service.getByTagName("port");
72     }
73 
74     if (!ports || !ports.length)
75     {
76       ports = service.getByTagName("soap:port");
77     }
78 
79     foreach (port; ports)
80     {
81       auto bindingAttribute = port.getAttribute("binding");
82       auto binding = bindingAttribute.value.split(":").length == 2 ? bindingAttribute.value.split(":")[1] : bindingAttribute.value;
83 
84       auto locations = port.getByAttributeName("location");
85 
86       if (locations && locations.length)
87       {
88         endpoints ~= "\t__services[\"" ~ binding ~ "\"] = \"" ~ locations[0].getAttribute("location").value ~ "\";\r\n";
89       }
90     }
91   }
92 
93   return result.format(endpoints);
94 }