-
Notifications
You must be signed in to change notification settings - Fork 4
/
transform.php
executable file
·88 lines (73 loc) · 2.2 KB
/
transform.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
#!/usr/bin/env php
<?php
use PHPUnit\Framework\ExpectationFailedException as ExpectationFailedException;
class TestShouldFailException extends Exception { }
$opts = getopt('', ['xml:', 'out:', 'xsl:', 'secref:', 'article-defaults:', 'test']);
if (key_exists('test', $opts)) {
require __DIR__ . '/tests/XmlOutputTest.php';
}
if (!key_exists('xml', $opts)) {
usageAndExit();
}
if (!key_exists('xsl', $opts) and key_exists('test', $opts)) {
try {
$test_should_fail = new XmlOutputTest($opts['xml']);
$test_should_fail->testXml();
throw new TestShouldFailException();
} catch (ExpectationFailedException $e) {
echo "Caught ExpectationFailedException\n";
}
$test_should_pass = new XmlOutputTest($opts['out']);
$test_should_pass->testXml();
echo "ok";
exit();
}
$xsldoc = new DOMDocument();
$xsldoc->load($opts['xsl']);
$xsl = new XSLTProcessor();
$xsl->importStyleSheet($xsldoc);
if (is_dir($opts['xml']) && is_dir($opts['out'])) {
$dh = opendir($opts['xml']);
while ($file = readdir($dh)) {
echo $file . "\n";
if ($file === '.') continue;
if ($file === '..') continue;
transform($opts['xml'] . '/' . $file, $opts['out'] . '/' . $file);
}
closedir($dh);
} else {
transform($opts['xml'], $opts['out']);
}
function transform($source, $target = null) {
global $opts;
global $xsl;
$xmldoc = new DOMDocument();
$xmldoc->load($source);
if (key_exists('article-defaults', $opts)) {
$defaults = file_get_contents($opts['article-defaults']);
foreach (explode("\n", $defaults) as $default) {
$default = explode("=", trim($default));
if (sizeof($default) == 2) {
$xsl->setParameter('', array_shift($default), array_shift($default));
}
}
}
if (key_exists('secref', $opts)) {
$xsl->setParameter('', 'section_ref', $opts['secref']);
}
if ($target) {
file_put_contents($target, $xsl->transformToXML($xmldoc));
} else {
echo $xsl->transformToXML($xmldoc);
}
}
# Test transformed XML...
if (key_exists('xsl', $opts) and key_exists('test', $opts)) {
$test_should_pass = new XmlOutputTest($opts['out']);
$test_should_pass->testXml();
}
function usageAndExit() {
echo "USAGE: transform.php --xml /path/to/input.xml --out /path/to/output.xml [ --xsl transformer.xsl ] [ --test ]";
exit(0);
}
?>