-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALDPackageDefinition.php
301 lines (231 loc) · 7.97 KB
/
ALDPackageDefinition.php
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
<?php
require_once dirname(__FILE__) . '/lib/exceptions.php';
class ALDPackageDefinition {
const XMLNS = 'ald://package/schema/2012';
const DefinitionFile = 'definition.ald';
/************************************************************************************************/
private static $schema = NULL;
public static function SetSchemaLocation($path) {
if (!file_exists($path)) {
throw new FileNotFoundException();
}
self::$schema = file_get_contents($path);
}
/************************************************************************************************/
public function __construct($source) {
if (self::$schema === NULL) {
throw new UnknownSchemaException();
}
$this->document = new DOMDocument();
$this->document->loadXML($source);
$this->validate();
$this->xpath = new DOMXPath($this->document);
$this->xpath->registerNamespace('ald', self::XMLNS);
}
public function GetFiles() {
$files = array_merge($this->GetSourceFiles(), $this->GetDocFiles());
if (!in_array($logo = $this->GetLogo(), $files) && $logo !== NULL) {
$files[] = $t;
}
return $files;
}
public function GetSourceFiles() {
return $this->fileList('src');
}
public function GetDocFiles() {
return $this->fileList('doc');
}
public function GetFilesHierarchy() {
return array('src' => $this->GetSourceFilesHierarchy(), 'doc' => $this->GetDocFilesHierarchy());
}
public function GetSourceFilesHierarchy() {
return $this->fileHierarchy('src');
}
public function GetDocFilesHierarchy() {
return $this->fileHierarchy('doc');
}
public function GetID() {
return $this->readAttribute('id');
}
public function GetName() {
return $this->readAttribute('name');
}
public function GetVersion() {
return $this->readAttribute('version');
}
public function GetType() {
return $this->readAttribute('type');
}
public function GetHomepage() {
return $this->readAttribute('homepage');
}
public function GetLogo() {
return $this->readAttribute('logo-image');
}
public function GetDescription() {
return $this->readTag('description');
}
public function GetSummary() {
return $this->readTag('summary');
}
public function GetAuthors() {
return $this->readArray('ald:authors/ald:author', array('name', 'user-name', 'homepage', 'email'));
}
public function GetDependencies() {
return $this->readArray('ald:dependencies/ald:dependency', array('name'), '.');
}
public function GetTargetsHierarchy() {
return $this->readArrayRecursive('ald:targets', 'ald:target',
array('id', 'message', 'language-architecture', 'language-encoding', 'system-architecture', 'system-version', 'system-type'),
'./ald:language-version', 'language-version');
}
public function GetTargets() {
$targets = array();
foreach ($this->GetTargetsHierarchy() AS $target) {
$targets = array_merge($targets, $this->flattenTarget($target));
}
return $targets;
}
public function GetTags() {
$tags = array();
foreach ($this->readArray('ald:tags/ald:tag', array('name')) AS $tag) {
$tags[] = $tag['name'];
}
return $tags;
}
public function GetDevelopment() {
$repos = array();
foreach ($this->xpath->query('/*/ald:development/ald:repository') AS $node) {
$repo = array('type' => $this->readAttribute('type', $node), 'view-url' => $this->readAttribute('view-url', $node));
$fragment = substr($node->getNodePath(), strlen($this->document->documentElement->getNodePath()) + 1) . '/ald:url';
$repo['urls'] = $this->readArray($fragment, array('access'), NULL, 'url');
$repos[] = $repo;
}
return $repos;
}
public function GetLinks() {
return $this->readArray('ald:links/ald:link', array('name', 'description', 'href'));
}
/************************************************************************************************/
private $document;
private $xpath;
private function validate() {
if (!@$this->document->schemaValidateSource(self::$schema)) {
throw new InvalidXmlException();
}
}
private function fileList($list) {
$files = array();
$list_root = $this->xpath->query('/*/ald:files/ald:' . $list)->item(0);
foreach ($this->xpath->query('.//ald:file', $list_root) AS $node) {
$path = $node->getAttribute('ald:path');
$curr_node = $node;
while ($curr_node->parentNode->tagName == 'ald:file-set') {
$curr_node = $curr_node->parentNode;
$path = $curr_node->getAttribute('ald:src') . '/' . $path;
}
$files[] = $path;
}
return $files;
}
private function fileHierarchy($xpath, $top = true) {
$files = array('files' => array(), 'sets' => array());
$list_root = $this->xpath->query(($top ? '/*/ald:files/ald:' : '') . $xpath)->item(0);
if (!$top) {
$files['src'] = $list_root->getAttribute('ald:src');
$files['targets'] = array();
foreach ($this->xpath->query('./ald:target', $list_root) AS $node) {
$files['targets'][] = $node->getAttribute('ald:ref');
}
}
foreach ($this->xpath->query('./ald:file', $list_root) AS $node) {
$files['files'][] = $node->getAttribute('ald:path');
}
foreach ($this->xpath->query('./ald:file-set', $list_root) AS $node) {
$files['sets'][] = $this->fileHierarchy($node->getNodePath(), false);
}
return $files;
}
private function flattenTarget($obj, $parent = array()) {
$list = array();
unset($parent['id']); # so it doesn't overwrite
$target = array_merge($obj, $parent); # parent values overwrite child values
unset($target[0]); # so we don't have it in the output
$list[] = $target;
if (isset($obj[0]) && is_array($obj[0])) {
foreach ($obj[0] AS $child) {
$list = array_merge($list, $this->flattenTarget($child, $target));
}
}
return $list;
}
private function readAttribute($attr, $context = NULL) {
return $this->readNode('@ald:' . $attr, $context);
}
private function readTag($tag, $context = NULL) {
return $this->readNode('ald:' . $tag, $context);
}
private function readNode($xpath, $context = NULL) {
$list = $this->xpath->query($xpath, $context);
if ($list->length > 0) {
return $list->item(0)->nodeValue;
}
return NULL;
}
private function readAttributeList($node, $list) {
$data = array();
foreach ($list AS $attr) {
if (($t = $this->readAttribute($attr, $node)) !== NULL) {
$data[$attr] = $t;
}
}
return $data;
}
private function readArray($fragment, $attributes, $version_tag = NULL, $value_as = NULL) {
$arr = array();
foreach ($this->xpath->query('/*/' . $fragment) AS $node) {
$item = $this->readAttributeList($node, $attributes);
if ($version_tag !== NULL) {
$item = array_merge($item, $this->readVersionTag($this->xpath->query($version_tag, $node)->item(0)));
}
if ($value_as !== NULL) {
$item[$value_as] = $node->nodeValue;
}
$arr[] = $item;
}
return $arr;
}
private function readArrayRecursive($root, $path, $attributes, $version_tag = NULL, $version_tag_key = NULL) {
$arr = array();
foreach ($this->xpath->query($root . '/' . $path) AS $node) {
$item = $this->readAttributeList($node, $attributes);
$children = $this->readArrayRecursive($node->getNodePath(), $path, $attributes, $version_tag, $version_tag_key);
if (count($children) > 0) {
$item[] = $children;
}
if ($version_tag !== NULL) {
$list = $this->xpath->query($version_tag, $node);
if ($list->length > 0) {
$item[$version_tag_key] = $this->readVersionTag($list->item(0));
}
}
$arr[] = $item;
}
return $arr;
}
private function readVersionTag($node) {
$tag = array();
if ($list = $this->xpath->query('ald:version-list', $node)->item(0)) {
$tag['version-list'] = array();
foreach ($this->xpath->query('ald:version/@ald:value', $list) AS $version) {
$tag['version-list'][] = $version->nodeValue;
}
} else if ($range = $this->xpath->query('ald:version-range', $node)->item(0)) {
$tag['version-range'] = array('min' => $this->readAttribute('min-version', $range), 'max' => $this->readAttribute('max-version', $range));
} else {
$tag['version'] = $this->xpath->query('ald:version/@ald:value', $node)->item(0)->nodeValue;
}
return $tag;
}
}
?>