forked from EzerIT/BibleOL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbxml2json.php
More file actions
625 lines (499 loc) · 17.3 KB
/
Copy pathdbxml2json.php
File metadata and controls
625 lines (499 loc) · 17.3 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
<?php
// Copyright © 2013 by Ezer IT Consulting. All rights reserved. E-mail: claus@ezer.dk
// This is for command line usage
// Usage:
// php dbxml2json.php <DBXML-file>
// Outputs a JSON version of the database information to stdout.
// Note may of these classes omit declaration of member variables. This is in order to ensure that
// those member variables are only inluded in the JSON output if they are set.
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 'stderr');
ini_set('track_errors', true);
mb_internal_encoding('UTF-8');
require_once('myapp/helpers/xmlhandler_helper.php');
class SubsetOf extends XmlHandler {
public $name;
/** Name of superset property file. */
public $properties;
/** Paths within the superset database provided by this subset. */
public $provides = array();
function open_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'provides':
$this->provides[] = $elem['value'];
break;
case 'name':
$this->name = $elem['value'];
break;
case 'properties':
$this->properties = $elem['value'];
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
function close_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'provides':
case 'name':
case 'properties':
// Ignore
break;
case 'subsetof':
parent::close_handler($handlers, $elem);
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
}
class DatabaseInfo extends XmlHandler {
/** Version number found in the {@literal <databasedescription>} element. */
public $version;
/** The name of the database file. Together with {@link #propertiesName} this uniquely
* identifies a DatabaseInfo object. */
public $databaseName;
/** The name of the properties file. Together with {@link #databaseName} this uniquely
* identifies a DatabaseInfo object. */
public $propertiesName; // Name of properties file
/** Database version. */
public $databaseVersion = "Unknown"; //$NON-NLS-1$
/** The name of the Emdros object that represents the amount of text displayed in a single
* question. This is typically "sentence".
*/
public $granularity;
/** The name of the Emdros feature that represents the actual text to display. */
public $surfaceFeature;
/** The name of the Emdros object that has the feature in {@link #surfaceFeature}. This Emdros
* object must correspond to a single monad.
*/
public $objHasSurface;
/** The name of the Emdros feature that represents a string to display after the {@link #surfaceFeature}. */
public $suffixFeature;
/** The {@link CharSet} used by the database. */
public $charSet;
/** {@link ObjectSetting}s associated with Emdros objects in this database. */
public $objectSettings; //ChronologyMap<String, ObjectSetting>
/** The universe hierarchy levels used with this database. */
public $universeHierarchy;//Vector<UHTriple>
/** Set to the basic URL of the associated picture database, if any (e.g. "http://dbilinux.dbi.edu/picdb") */
public $picDb;
/** The list of sentence grammar information displayable in this database. */
//public List<SentenceGrammar> sentenceGrammarList;
public $sentencegrammar = array();
public $subsetOf = null; // TODO: Not used now
/**
* Serialising constructor.
*/
function __construct($elem) {
parent::__construct($elem);
$this->version = intval($elem['attributes']['version']);
}
/** Finalizes the construction of {@link #sentenceGrammarList} by adding the bottom and top
* level of the hierarchy. */
private function completeSentenceGrammarList() {
// Add top level:
// Construct pseudo element:
$pselem = array('attributes'=>array('object'=>$this->granularity),
'tag'=>null,
'level'=>null,
'value'=>null);
$this->sentencegrammar[] = new SentenceGrammar($pselem);
}
function open_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'dbname':
$this->databaseName = $elem['value'];
if (strncmp($this->databaseName, 'WIVU', strlen('WIVU'))===0)
$this->useSofPasuq = true; // TODO: Move to dbxml file
break;
case 'version':
$this->databaseVersion = $elem['value'];
break;
case 'properties':
$this->propertiesName = $elem['value'];
break;
case 'picdb':
$this->picDb = $elem['value'];
break;
case 'granularity':
$this->granularity = $elem['value'];
break;
case 'surfacefeature':
$this->surfaceFeature = $elem['value'];
break;
case 'surfaceobj':
$this->objHasSurface = $elem['value'];
break;
case 'suffixfeature':
$this->suffixFeature = isset($elem['value']) ? $elem['value'] : null;
break;
case 'charset':
$this->charSet = $elem['value'];
break;
case 'subsetof':
$ss =
$this->subsetOf = new SubsetOf($elem);
$handlers[] = $this->subsetOf;
break;
case 'execmode':
case 'mqlcommand':
case 'mqlhost':
case 'mqlhostpath':
case 'mqlport':
case 'disabled':
case 'dbid':
// Ignore
break;
case 'objectsetting':
$os = new ObjectSetting($elem);
$this->objectSettings[$elem['attributes']['object']] = $os;
$handlers[] = $os;
break;
case 'sentencegrammar':
$sg = new SentenceGrammar($elem);
$this->sentencegrammar[] = $sg;
$handlers[] = $sg;
break;
case 'universelevel':
$this->universeHierarchy[] = array('type'=>$elem['attributes']['object'],
'feat'=>$elem['attributes']['feature']);
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
function close_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'dbname':
case 'version':
case 'properties':
case 'picdb':
case 'granularity':
case 'surfacefeature':
case 'surfaceobj':
case 'suffixfeature':
case 'charset':
case 'execmode':
case 'mqlcommand':
case 'mqlhost':
case 'mqlhostpath':
case 'mqlport':
case 'disabled':
case 'dbid':
case 'universelevel':
// Ignore
break;
case 'databasedescription':
if (!isset($this->propertiesName))
$this->propertiesName = $this->databaseName;
$this->completeSentenceGrammarList();
parent::close_handler($handlers, $elem);
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
}
class ObjectSetting extends XmlHandler {
/**
* Constructs a new {@code ObjectSetting} object.
*/
function __construct($elem) {
parent::__construct($elem);
}
function open_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'featuresetting':
if (!isset($this->featuresetting))
$this->featuresetting = array();
$fs = new FeatureSetting($elem);
$this->featuresetting[$elem['attributes']['name']] = $fs;
$handlers[] = $fs;
break;
case 'mayselect':
if ($elem['value']==='true')
$this->mayselect = true;
break;
case 'additionalfeatures':
if (!isset($this->additionalfeatures))
$this->additionalfeatures = array();
$this->additionalfeatures[] = $elem['value'];
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
function close_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'featuresetting':
case 'mayselect':
case 'additionalfeatures':
// Ignore
break;
case 'objectsetting':
parent::close_handler($handlers, $elem);
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
}
class FeatureSetting extends XmlHandler {
/**
* Constructs a new {@code FeatureSetting} object.
*/
function __construct($elem) {
parent::__construct($elem);
if (isset($elem['attributes']['default']) &&
($elem['attributes']['default']==='yes' || $elem['attributes']['default']==='true'))
$this->isDefault = true;
}
function open_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'ignoreselect':
if ($elem['value']==='true')
$this->ignoreSelect = true;
break;
case 'ignoreshowrequest':
if ($elem['value']==='true')
$this->ignoreShowRequest = true;
break;
case 'hideword':
if ($elem['value']==='true')
$this->hideWord = true;
break;
case 'hidevalue':
if (!isset($this->hideValues))
$this->hideValues = array();
$this->hideValues[] = $elem['value'];
break;
case 'othervalue':
if (!isset($this->otherValues))
$this->otherValues = array();
$this->otherValues[] = $elem['value'];
break;
case 'isrange':
if ($elem['value']==='true')
$this->isRange = true;
break;
case 'matchregexp':
$this->matchregexp = '/^' . str_replace('%s','{0}',$elem['value']) . '$/i';
break;
case 'alternateshowrequest':
$this->alternateshowrequestDb = $elem['attributes']['db'];
$this->alternateshowrequestSql = $elem['value'];
break;
case 'foreign':
if ($elem['value']==='true')
$this->foreignText = true;
if ($elem['value']==='transliterated')
$this->transliteratedText = true;
break;
case 'color':
// TODO: Add color
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
function close_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'ignoreselect':
case 'ignoreshowrequest':
case 'hideword':
case 'hidevalue':
case 'othervalue':
case 'isrange':
case 'matchregexp':
case 'alternateshowrequest':
case 'foreign':
case 'color':
// Ignore
break;
case 'featuresetting':
parent::close_handler($handlers, $elem);
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
}
class GrammarGroup extends XmlHandler {
public $mytype;
public $name;
public $items = array();
/**
* Constructs a new {@code GrammarGroup} object.
*/
public function __construct(array $elem) {
parent::__construct($elem);
$this->mytype = 'GrammarGroup';
$this->name = $elem['attributes']['name'];
}
function open_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'grammarfeature':
$gf = new GrammarFeature($elem);
$this->items[] = $gf;
$handlers[] = $gf;
break;
case 'grammargroup':
$gg = new GrammarGroup($elem);
$this->items[] = $gg;
$handlers[] = $gg;
break;
case 'grammarmetafeature':
$gf = new GrammarMetaFeature($elem);
$this->items[] = $gf;
$handlers[] = $gf;
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
function close_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'grammargroup':
parent::close_handler($handlers, $elem);
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
}
class GrammarFeature extends XmlHandler {
public $mytype;
/**
* Constructs a new {@code GrammarFeature} object.
*/
public function __construct(array $elem) {
parent::__construct($elem);
$this->mytype = 'GrammarFeature';
$this->name = $elem['attributes']['name'];
}
function open_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'color':
if (!isset($this->coloring))
$this->coloring = array();
$this->coloring[$elem['attributes']['name']] = array(intval($elem['attributes']['r']),
intval($elem['attributes']['g']),
intval($elem['attributes']['b']));
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
function close_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'color':
// Ignore
break;
case 'grammarfeature':
parent::close_handler($handlers, $elem);
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
}
class GrammarMetaFeature extends XmlHandler {
public $mytype;
/**
* Constructs a new {@code GrammarMetaFeature} object.
*/
public function __construct(array $elem) {
parent::__construct($elem);
$this->mytype = 'GrammarMetaFeature';
$this->name = $elem['attributes']['name'];
}
function open_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'grammarsubfeature':
$this->items[] = array('mytype'=>'GrammarSubFeature',
'name'=>$elem['attributes']['name']);
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
function close_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'grammarsubfeature':
// Ignore
break;
case 'grammarmetafeature':
parent::close_handler($handlers, $elem);
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
}
class SentenceGrammar extends XmlHandler {
public $mytype;
/**
* Constructs a new {@code SentenceGrammar} object.
*/
public function __construct(array $elem) {
parent::__construct($elem);
$this->mytype = 'SentenceGrammar';
$this->objType = $elem['attributes']['object'];
}
function open_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'grammarfeature':
$gf = new GrammarFeature($elem);
$this->items[] = $gf;
$handlers[] = $gf;
break;
case 'grammargroup':
$gg = new GrammarGroup($elem);
$this->items[] = $gg;
$handlers[] = $gg;
break;
case 'grammarmetafeature':
$gf = new GrammarMetaFeature($elem);
$this->items[] = $gf;
$handlers[] = $gf;
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
function close_handler(&$handlers, $elem) {
switch ($elem['tag']) {
case 'sentencegrammar':
parent::close_handler($handlers, $elem);
break;
default:
PANIC(__FILE__,__LINE__ . " elem $elem[tag]");
break;
}
}
}
if ($_SERVER['argc']!==2) {
print "Usage: php dbxml2json.php <DBXML-file>\n";
die;
}
$contents = @file_get_contents($_SERVER['argv'][1])
or die ("Failed opening file {$_SERVER['argv'][1]}:\nError was '$php_errormsg'\n");
$decoded_xml = harvest($contents);
print json_encode($decoded_xml);
?>