-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathXsdParser.php
More file actions
259 lines (231 loc) · 8.54 KB
/
Copy pathXsdParser.php
File metadata and controls
259 lines (231 loc) · 8.54 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
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
<?php
namespace WsSys\DtoGeneratorBundle\Generator\Parser;
use WsSys\DtoGeneratorBundle\Generator\Parser;
use WsSys\DtoGeneratorBundle\Generator\DataMapper;
use WsSys\DtoGeneratorBundle\Generator\Parser\Interfaces\ParserInterface;
/**
* Reads Xsd and it's elements
*/
class XsdParser implements ParserInterface
{
/**
* @var \DOMDocument
*/
protected $dom;
/**
* @var \DOMElement
*/
protected $start;
/**
* Reads the XSD from given source
* @param string $source
*/
public function parse($source)
{
$this->dom = new \DOMDocument();
$this->dom->load($source);
$this->start = $this->dom->documentElement;
}
/**
* Return First Element from Xsd
*
* @return Element | null if not found
*/
public function getFirstElementWithChildren()
{
$nodes = $this->start->childNodes;
foreach ($nodes as $node) {
if ($node->nodeType === XML_ELEMENT_NODE) {
$element = new Parser\Element\ComplexTypeElement();
$element->setName($node->getAttribute('name'))
->setElementAsFirst(true);
$this->addChildrenElements($node, $element);
return $element;
}
}
return null;
}
/**
* Add Children Elements to the given parentElement
*
* @param DOMNode $parentNode
* @param Element $parentElement
*/
protected function addChildrenElements($parentNode, &$parentElement)
{
$children = $parentNode->childNodes;
foreach ($children as $node) {
if ($node->nodeType === XML_ELEMENT_NODE) {
switch ($node->localName) {
case 'complexType':
case 'all':
case 'sequence':
/**
* Recursively looking at element(localName=element)node
*/
$this->addChildrenElements($node, $parentElement);
break;
case 'element':
if ($this->isComplexTypeNode($node)) {
$element = new Parser\Element\ComplexTypeElement();
$element->setName($node->getAttribute('name'));
$this->setComplexTypeChildrenRecursively($node, $element);
$parentElement->addChild($element);
} elseif ($this->isCustomTypeNode($node)) {
$element = new Parser\Element\ComplexTypeElement();
$element->setName($node->getAttribute('name'));
$this->setCustomTypeChildrenRecursively($node, $element);
$parentElement->addChild($element);
} else {
$element = new Parser\Element\Element();
$element->setName($node->getAttribute('name'));
if ($node->getAttribute('type')) {
$element->setDataType(DataMapper\DataTypeMapper::XsdToDto($node->getAttribute('type')));
} else {
$this->setElementsDataType($node, $element);
}
$parentElement->addChild($element);
}
break;
}
}
}
}
/**
* Recursively looks at an element which has DataType and sets the type of the element
*
* @param DOMNode $node
* @param Element $element
*/
protected function setElementsDataType(\DomNode $node, &$element)
{
$childNodes = $node->childNodes;
foreach ($childNodes as $childNode) {
$localName = $childNode->localName;
if ($localName == 'simpleType') {
$this->setElementsDataType($childNode, $element);
} elseif ($localName == 'restriction' ) {
$element->setDataType(DataMapper\DataTypeMapper::XsdToDto($childNode->getAttribute('base')));
return;
}
}
}
/**
* Checks if the node is complex type of node
* @param DomNode $node
*
* @return boolean
*/
protected function isComplexTypeNode(\DomNode $node)
{
$childNodes = $node->childNodes;
foreach ($childNodes as $childNode) {
$localName = $childNode->localName;
if ($localName == 'complexType') {
return true;
}
}
return false;
}
/**
* Sets Children of Complex Type Recursively
* @param DomNode $node
* @param Element $element
*/
protected function setComplexTypeChildrenRecursively(\DomNode $node, &$element)
{
$childNodes = $node->childNodes;
foreach ($childNodes as $childNode) {
$localName = $childNode->localName;
if ($childNode->nodeType === XML_ELEMENT_NODE && $localName == 'complexType') {
$this->addChildrenElements($childNode, $element);
}
}
}
/**
* Checks if the node is custom Type which is user Defined.
* It is a Complex type but name given by user.
*
* If given type of the element is not ComplexType but there is root level element with that name,
* it is supposed to be custom Node..
*
* Example of XML
* <?xml version="1.0" encoding="UTF-8"?>
* <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
* <xs:element name="oUm">
* <xs:annotation>
* <xs:documentation>This is an example of complex Element</xs:documentation>
* </xs:annotation>
* <xs:complexType>
* <xs:sequence>
* <xs:element name="id">
* <xs:annotation>
* <xs:documentation>Unique id</xs:documentation>
* </xs:annotation>
* <xs:simpleType>
* <xs:restriction base="xs:string">
* <xs:maxLength value="100"/>
* <xs:minLength value="1"/>
* </xs:restriction>
* </xs:simpleType>
* </xs:element>
* <xs:element name="billToAddress" type="addressType" minOccurs="0">
* <xs:annotation>
* <xs:documentation>Bill to address</xs:documentation>
* </xs:annotation>
* </xs:element>
* </xs:sequence>
* </xs:complexType>
* </xs:element>
* <xs:element name="address" type="addressType"/>
* <xs:complexType name="addressType">
* <xs:all>
* <xs:element name="title" type="xs:string" minOccurs="0">
* <xs:annotation>
* <xs:documentation>Customer title</xs:documentation>
* </xs:annotation>
* </xs:element>
* </xs:all>
* </xs:complexType>
* </xs:schema>
*
* @param DomNode $node
*
* @return boolean
*/
protected function isCustomTypeNode(\DomNode $node)
{
$nodes = $this->start->childNodes;
$nodeType = $node->nodeType;
$typeAttribute = $node->getAttribute('type');
if ($nodeType === XML_ELEMENT_NODE && !empty($typeAttribute)) {
foreach ($nodes as $node) {
//If any Node exists with the same name as Attribute Type defined for any element
if ($node->hasAttributes()) {
if ($typeAttribute === $node->getAttribute('name')) {
return true;
}
}
}
}
return false;
}
/**
* Sets Children of Custom Type Recursively
* @param DomNode $node
* @param Element $element
*/
protected function setCustomTypeChildrenRecursively(\DomNode $node, &$element)
{
$nodes = $this->start->childNodes;
$typeAttribute = $node->getAttribute('type');
foreach ($nodes as $node) {
//If any Node exists with the same name as Attribute Type defined for any element
if (method_exists($node, 'getAttribute')) {
if ($typeAttribute === $node->getAttribute('name')) {
$this->addChildrenElements($node, $element);
}
}
}
}
}