1 : <?php
2 :
3 : /**
4 : * XSLT Benchmarking
5 : * @link https://github.com/masicek/XSLT-Benchmarking
6 : * @author Viktor Mašíček <viktor@masicek.net>
7 : * @license "New" BSD License
8 : */
9 :
10 : namespace XSLTBenchmarking\TestsGenerator;
11 :
12 : require_once __DIR__ . '/IXmlGeneratorDriver.php';
13 : require_once LIBS . '/PhpPath/PhpPath.min.php';
14 : require_once ROOT . '/Exceptions.php';
15 :
16 : use PhpPath\P;
17 :
18 : /**
19 : * Driver for generating XML files by ToXGene.
20 : *
21 : * @author Viktor Mašíček <viktor@masicek.net>
22 : */
23 : class ToxgeneXmlGeneratorDriver implements IXmlGeneratorDriver
24 : {
25 :
26 :
27 : /**
28 : * It is the minimum heap size for the java runtime
29 : */
30 : const MIN_HEAP = 64000000;
31 :
32 : /**
33 : * Default random seed for running ToXGene
34 : */
35 : const TOXGENE_DEFAULT_SEED = 123456789;
36 :
37 : /**
38 : * Value of PHP_OS for Windows
39 : */
40 : const OS_WIN = 'WINNT';
41 :
42 : /**
43 : * Value of PHP_OS for Linux
44 : */
45 : const OS_LINUX = 'Linux';
46 :
47 :
48 : private $tmpDir;
49 :
50 : /**
51 : * Object configuration
52 : *
53 : * @param string $tmpDir The path of the temporary directory
54 : */
55 : public function __construct($tmpDir)
56 : {
57 : // ToXGene option '-d' have to get directory without end '/' eventually '\'
58 0 : P::mcd($tmpDir, '/');
59 0 : $this->tmpDir = substr($tmpDir, 0, -1);
60 0 : }
61 :
62 :
63 : /**
64 : * Generate xml file
65 : *
66 : * @param string $outputPath The path of the output xml file
67 : * @param array $settings The list of settings specific by selected xml generator
68 : * - template = ToXGene template
69 : * - document = select witch tox-document will by selected (default = first)
70 : * - indent = set/unset (0/1) indent of generated XML file (default = 1)
71 : * - seed = random seed for generating (default self::TOXGENE_DEFAULT_SEED)
72 : *
73 : * @return void
74 : */
75 : public function generate($outputPath, $templateDir, array $settings)
76 : {
77 1 : $home = P::m(LIBS, 'XmlGenerators/ToXGene/2.3');
78 1 : $toxgene = P::m($home, 'toxgene.jar');
79 1 : $xercesImpl = P::m($home, 'Xerces-2.6.2/xercesImpl.jar');
80 1 : $xercesApis = P::m($home, 'Xerces-2.6.2/xml-apis.jar');
81 1 : $xercesParser = P::m($home, 'Xerces-2.6.2/xmlParserAPIs.jar');
82 :
83 1 : switch (PHP_OS)
84 : {
85 1 : case self::OS_WIN:
86 1 : $java = '"' . P::m(LIBS, 'Java/1.6.0_29/java.exe') . '"';
87 1 : $home = '"' . $home . '"';
88 1 : $class = '"' . $toxgene . '";"' . $xercesImpl . '";"' . $xercesApis . '";"' . $xercesParser . '"';
89 1 : break;
90 :
91 0 : case self::OS_LINUX:
92 : // we assume installing java
93 0 : $java = 'java';
94 0 : $class = $toxgene . ':' . $xercesImpl . ':' . $xercesApis . ':' . $xercesParser;
95 0 : break;
96 0 : }
97 :
98 1 : $seed = self::TOXGENE_DEFAULT_SEED;
99 1 : if (isset($settings['seed']))
100 1 : {
101 1 : $seed = $settings['seed'];
102 1 : }
103 :
104 1 : $template = P::m($templateDir, $settings['template']);
105 :
106 : // get output name - default first document, other set name
107 1 : if (isset($settings['document']))
108 1 : {
109 0 : $outputName = $settings['document'];
110 0 : }
111 : else
112 : {
113 1 : preg_match('/<tox-document name="([^"]*)">/', file_get_contents($template), $matches);
114 1 : $outputName = $matches[1];
115 : }
116 :
117 : $params =
118 1 : '-s ' . $seed . ' ' .
119 1 : '-i "' . $templateDir . '" ' .
120 1 : '-d "' . $this->tmpDir . '"'
121 1 : ;
122 :
123 1 : $command = $java . ' -Xmx' . self::MIN_HEAP . ' -DToXgene_home=' . $home . ' -classpath ' . $class . ' toxgene.ToXgene ' . $params . ' "' . $template . '" 2>&1';
124 :
125 1 : exec($command, $output);
126 :
127 : // detect error
128 1 : $error = '';
129 1 : $output = implode(' ', $output);
130 1 : $errorPos = strpos($output, 'ERROR');
131 1 : if ($errorPos != FALSE)
132 1 : {
133 0 : $error = substr($output, $errorPos);
134 0 : }
135 : else
136 : {
137 1 : $errorPos = strpos($output, 'Exception');
138 1 : if ($errorPos != FALSE)
139 1 : {
140 0 : $error = substr($output, $errorPos);
141 0 : }
142 : }
143 :
144 : if ($error)
145 1 : {
146 0 : throw new \XSLTBenchmarking\GenerateXmlException($error);
147 : }
148 :
149 : // make sure about output path
150 1 : $generatedFilePath = P::m($this->tmpDir, $outputName . '.xml');
151 1 : if ($generatedFilePath !== $outputPath)
152 1 : {
153 1 : copy($generatedFilePath, $outputPath);
154 1 : unlink($generatedFilePath);
155 1 : }
156 :
157 1 : $content = file_get_contents($outputPath);
158 : // remove comment
159 1 : $content = preg_replace('/<!-- generated by ToXgene [^-]* -->/', '', $content);
160 : // change encoding to UTF-8
161 1 : $content = mb_convert_encoding($content, 'UTF-8', 'ASCII');
162 1 : $content = str_replace('encoding="US-ASCII"', 'encoding="UTF-8"', $content);
163 1 : file_put_contents($outputPath, $content);
164 :
165 : // make indent (default 'yes')
166 1 : if (!isset($settings['indent']) || $settings['indent'] === '1')
167 1 : {
168 1 : $contentSimpleXml = new \SimpleXMLElement($outputPath, 0, TRUE);
169 1 : $contentDomXml = dom_import_simplexml($contentSimpleXml)->ownerDocument;
170 1 : $contentDomXml->formatOutput = TRUE;
171 1 : $contentDomXml->save($outputPath);
172 1 : }
173 1 : }
174 :
175 :
176 : }
|