forked from DigDes/SoapCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaFromFile.cs
More file actions
166 lines (144 loc) · 4.49 KB
/
MetaFromFile.cs
File metadata and controls
166 lines (144 loc) · 4.49 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace SoapCore.Meta
{
public class MetaFromFile
{
/// <summary>
/// Gets or sets the CurrentWebService
/// </summary>
public string CurrentWebService { get; set; }
/// <summary>
/// Gets or sets the currentWebServer
/// </summary>
public string CurrentWebServer { get; set; }
/// <summary>
/// Gets or sets reads and returns the XSDFolder appsetting key form web.config
/// </summary>
public string XsdFolder { get; set; }
/// <summary>
/// Gets or sets the WSDLFolder appsetting key from web.config
/// </summary>
public string WSDLFolder { get; set; }
/// <summary>
/// Gets or sets the XSDURL appsetting key from web.config
/// </summary>
public string ServerUrl { get; set; }
#if NETSTANDARD
public async Task<string> ReadLocalFileAsync(string path)
{
if (!File.Exists(path))
{
return string.Empty;
}
using var reader = File.OpenText(path);
return await reader.ReadToEndAsync();
}
#else
public Task<string> ReadLocalFileAsync(string path)
{
if (!File.Exists(path))
{
return Task.FromResult(string.Empty);
}
return File.ReadAllTextAsync(path);
}
#endif
private XmlAttribute EnsureAttribute(XmlDocument xmlDoc, XmlNode node, string attributeName)
{
var attribute = node.Attributes[attributeName];
if (attribute == null)
{
attribute = xmlDoc.CreateAttribute(attributeName);
node.Attributes.Append(attribute);
}
return attribute;
}
public string ModifyWSDLAddRightSchemaPath(string xmlString)
{
var xmlDoc = new XmlDocument() { XmlResolver = null };
var sr = new StringReader(xmlString);
var reader = XmlReader.Create(sr, new XmlReaderSettings() { XmlResolver = null });
xmlDoc.Load(reader);
foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
{
if (WSDLFolder != null && node.Prefix == xmlDoc.DocumentElement.Prefix && node.LocalName == "import")
{
var attribute = EnsureAttribute(xmlDoc, node, "location");
string name = attribute.InnerText.Replace("./", string.Empty);
attribute.InnerText = WebServiceLocation() + "?import&name=" + name;
}
if (XsdFolder != null && node.Prefix == xmlDoc.DocumentElement.Prefix && node.LocalName == "types")
{
foreach (XmlNode schemaNode in node.ChildNodes)
{
if (schemaNode.LocalName == "schema")
{
foreach (XmlNode importOrIncludeNode in schemaNode.ChildNodes)
{
if ((importOrIncludeNode.LocalName == "import" && importOrIncludeNode.Attributes["schemaLocation"] != null)
|| importOrIncludeNode.LocalName == "include")
{
var attribute = EnsureAttribute(xmlDoc, importOrIncludeNode, "schemaLocation");
string name = attribute.InnerText.Replace("./", string.Empty);
attribute.InnerText = SchemaLocation() + "&name=" + name;
}
}
}
}
}
if (node.Prefix == xmlDoc.DocumentElement.Prefix && node.LocalName == "service")
{
foreach (XmlNode schemaNode in node.ChildNodes)
{
if (schemaNode.LocalName == "port")
{
foreach (XmlNode portNode in schemaNode.ChildNodes)
{
if (portNode.LocalName == "address")
{
var attribute = EnsureAttribute(xmlDoc, portNode, "location");
attribute.InnerText = WebServiceLocation();
break;
}
}
}
}
}
}
return xmlDoc.InnerXml;
}
public string ModifyXSDAddRightSchemaPath(string xmlString)
{
var xmlDoc = new XmlDocument() { XmlResolver = null };
var sr = new StringReader(xmlString);
var reader = XmlReader.Create(sr, new XmlReaderSettings() { XmlResolver = null });
xmlDoc.Load(reader);
foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
{
if ((node.LocalName == "import" && node.Attributes["schemaLocation"] != null)
|| node.LocalName == "include")
{
var attribute = EnsureAttribute(xmlDoc, node, "schemaLocation");
string name = attribute.InnerText.Replace("./", string.Empty);
attribute.InnerText = SchemaLocation() + "&name=" + name;
}
}
return xmlDoc.InnerXml;
}
private string SchemaLocation()
{
var schemaLocation = ServerUrl + CurrentWebServer + CurrentWebService + "?xsd";
return schemaLocation;
}
private string WebServiceLocation()
{
var webServiceLocation = ServerUrl + CurrentWebServer + CurrentWebService;
return webServiceLocation;
}
}
}