-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEDEDParser.php
More file actions
executable file
·207 lines (174 loc) · 7.31 KB
/
EDEDParser.php
File metadata and controls
executable file
·207 lines (174 loc) · 7.31 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
<?php
class EDEDParser
{
private $msgXML;
private $errors = [];
private $warnings = [];
public function __construct ($filePath)
{
$this->msgXML = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" standalone="yes"?><data_elements></data_elements>');
try {
$this->validateInput($filePath);
$this->process($filePath);
} catch (Exception $e) {
$this->errors[] = "Critical error in EDEDParser: " . $e->getMessage();
throw $e;
}
$msg = $this->msgXML->asXML();
$dom = new DOMDocument('1.0', 'utf-8');
$dom->xmlStandalone = true;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom_xml = dom_import_simplexml($this->msgXML);
$dom_xml = $dom->importNode($dom_xml, true);
$dom_xml = $dom->appendChild($dom_xml);
$result = $dom->saveXML();
$this->msgXML = $result;
}
private function validateInput($filePath)
{
if (!file_exists($filePath)) {
throw new Exception("EDED file not found: $filePath");
}
if (!is_readable($filePath)) {
throw new Exception("EDED file is not readable: $filePath");
}
$fileSize = filesize($filePath);
if ($fileSize === 0) {
throw new Exception("EDED file is empty: $filePath");
}
if ($fileSize > 50 * 1024 * 1024) { // 50MB limit
throw new Exception("EDED file is too large: $filePath (" . round($fileSize / 1024 / 1024, 2) . " MB)");
}
}
private function process ($filePath) {
$fileLines = file_get_contents($filePath);
if ($fileLines === false) {
throw new Exception("Failed to read file: $filePath");
}
$fileLines = preg_replace('/[\xC4]/', '-', $fileLines);
$ededArr = preg_split('/[-]{70}/', $fileLines);
if (count($ededArr) < 2) {
$this->warnings[] = "File may not be properly formatted - found only " . count($ededArr) . " sections";
}
unset($ededArr[0]);
foreach ($ededArr as $ededElm) {
$elmArr = preg_split('/[\r\n]+/', $ededElm);
$elementStatus='';
$elementCode = '';
$elementTitle = '';
$elementUse='';
$elementDescription ='';
$elementType='';
$elementMaxSize='';
$elementNote='';
$defXML = $this->msgXML->addChild("data_element");
$i = 0;
for ($i=0;$i<count($elmArr);) {
$row = $elmArr[$i];
if (strlen($row) < 1) {
$i++;
continue;
}
if ($elementCode === '') {
$result = preg_match("/^(.{5})([0-9\s]{6})(.{56})\[([A-Z]?)\]/", $row, $codeArr);
if(!isset($codeArr[1])) {
$result = preg_match("/^(.{5})([0-9\s]{6})(.*)/", $row, $codeArr);
if (!isset($codeArr[1])) {
$this->warnings[] = "Could not parse element header: $row";
break;
}
$elementStatus = trim($codeArr[1]);
$elementCode = trim($codeArr[2]);
$elementTitle = trim($codeArr[3]);
$i++;
if ($i >= count($elmArr)) {
$this->warnings[] = "Unexpected end of section while parsing element header continuation";
break;
}
$result = preg_match("/^[\s]{11}(.*)\[([A-Z]?)\]/", $elmArr[$i], $codeArr2);
if (!$result) {
$this->warnings[] = "Could not parse element usage: " . $elmArr[$i];
$elementTitle .= " " . trim($elmArr[$i]);
} else {
$elementTitle .= " ".trim($codeArr2[1]);
$elementUse = $codeArr2[2];
}
$i++;
continue;
}
$elementStatus = trim($codeArr[1]);
$elementCode = trim($codeArr[2]);
$elementTitle = trim($codeArr[3]);
$elementUse = $codeArr[4];
$i++;
continue;
}
if($elementDescription === '' && preg_match("/.{1}\s{4}Desc: (.*)/", $row, $matches)) {
$elementDescription = $matches[1];
$i++;
while (strlen($elmArr[$i])>1) {
if (preg_match("/^[\s]{11}(.*)/", $elmArr[$i], $matches)) {
$elementDescription .= " ".$matches[1];
$i++;
} else {
break;
}
}
continue;
}
if ($elementType === '') {
$result = preg_match("/^.{1}\s{4}Repr: (a?n?)[\.]*(\d+)/", $row, $codeArr);
if(!isset($codeArr[1])) {
$this->warnings[] = "Could not parse representation: $row";
} else {
$elementType = trim($codeArr[1]);
$elementMaxSize = trim($codeArr[2]);
}
$i++;
continue;
}
if($elementNote === '' && preg_match("/[\s]{5}Note:/", $row, $matches)) {
$elementNote = "";
$i++;
while (strlen($elmArr[$i])>1) {
if (preg_match("/^[\s]{11}(.*)/", $elmArr[$i], $matches)) {
if ($elementNote != '') {
$elementNote .= " ";
}
$elementNote .= $matches[1];
$i++;
} else {
break;
}
}
continue;
}
$i++;
}
$defXML->addAttribute('id', $elementCode);
$elementTitle = lcfirst(str_replace(" ", "", ucwords(strtolower($elementTitle))));
$elementTitle = str_replace("/", "Or", $elementTitle);
$defXML->addAttribute('name', $elementTitle);
$defXML->addAttribute('usage', $elementUse);
$defXML->addAttribute('desc', $elementDescription);
$defXML->addAttribute('type', $elementType);
$defXML->addAttribute('maxlength', $elementMaxSize);
}
}
public function getXML() {
return $this->msgXML;
}
public function getErrors() {
return $this->errors;
}
public function getWarnings() {
return $this->warnings;
}
public function hasErrors() {
return !empty($this->errors);
}
public function hasWarnings() {
return !empty($this->warnings);
}
}