1: <?php
2:
3: 4: 5: 6: 7: 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: 20: 21: 22:
23: class ToxgeneXmlGeneratorDriver implements IXmlGeneratorDriver
24: {
25:
26:
27: 28: 29:
30: const MIN_HEAP = 64000000;
31:
32: 33: 34:
35: const TOXGENE_DEFAULT_SEED = 123456789;
36:
37: 38: 39:
40: const OS_WIN = 'WINNT';
41:
42: 43: 44:
45: const OS_LINUX = 'Linux';
46:
47:
48: private $tmpDir;
49:
50: 51: 52: 53: 54:
55: public function __construct($tmpDir)
56: {
57:
58: P::mcd($tmpDir, '/');
59: $this->tmpDir = substr($tmpDir, 0, -1);
60: }
61:
62:
63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74:
75: public function generate($outputPath, $templateDir, array $settings)
76: {
77: $home = P::m(LIBS, 'XmlGenerators/ToXGene/2.3');
78: $toxgene = P::m($home, 'toxgene.jar');
79: $xercesImpl = P::m($home, 'Xerces-2.6.2/xercesImpl.jar');
80: $xercesApis = P::m($home, 'Xerces-2.6.2/xml-apis.jar');
81: $xercesParser = P::m($home, 'Xerces-2.6.2/xmlParserAPIs.jar');
82:
83: switch (PHP_OS)
84: {
85: case self::OS_WIN:
86: $java = '"' . P::m(LIBS, 'Java/1.6.0_29/java.exe') . '"';
87: $home = '"' . $home . '"';
88: $class = '"' . $toxgene . '";"' . $xercesImpl . '";"' . $xercesApis . '";"' . $xercesParser . '"';
89: break;
90:
91: case self::OS_LINUX:
92:
93: $java = 'java';
94: $class = $toxgene . ':' . $xercesImpl . ':' . $xercesApis . ':' . $xercesParser;
95: break;
96: }
97:
98: $seed = self::TOXGENE_DEFAULT_SEED;
99: if (isset($settings['seed']))
100: {
101: $seed = $settings['seed'];
102: }
103:
104: $template = P::m($templateDir, $settings['template']);
105:
106:
107: if (isset($settings['document']))
108: {
109: $outputName = $settings['document'];
110: }
111: else
112: {
113: preg_match('/<tox-document name="([^"]*)">/', file_get_contents($template), $matches);
114: $outputName = $matches[1];
115: }
116:
117: $params =
118: '-s ' . $seed . ' ' .
119: '-i "' . $templateDir . '" ' .
120: '-d "' . $this->tmpDir . '"'
121: ;
122:
123: $command = $java . ' -Xmx' . self::MIN_HEAP . ' -DToXgene_home=' . $home . ' -classpath ' . $class . ' toxgene.ToXgene ' . $params . ' "' . $template . '" 2>&1';
124:
125: exec($command, $output);
126:
127:
128: $error = '';
129: $output = implode(' ', $output);
130: $errorPos = strpos($output, 'ERROR');
131: if ($errorPos != FALSE)
132: {
133: $error = substr($output, $errorPos);
134: }
135: else
136: {
137: $errorPos = strpos($output, 'Exception');
138: if ($errorPos != FALSE)
139: {
140: $error = substr($output, $errorPos);
141: }
142: }
143:
144: if ($error)
145: {
146: throw new \XSLTBenchmarking\GenerateXmlException($error);
147: }
148:
149:
150: $generatedFilePath = P::m($this->tmpDir, $outputName . '.xml');
151: if ($generatedFilePath !== $outputPath)
152: {
153: copy($generatedFilePath, $outputPath);
154: unlink($generatedFilePath);
155: }
156:
157: $content = file_get_contents($outputPath);
158:
159: $content = preg_replace('/<!-- generated by ToXgene [^-]* -->/', '', $content);
160:
161: $content = mb_convert_encoding($content, 'UTF-8', 'ASCII');
162: $content = str_replace('encoding="US-ASCII"', 'encoding="UTF-8"', $content);
163: file_put_contents($outputPath, $content);
164:
165:
166: if (!isset($settings['indent']) || $settings['indent'] === '1')
167: {
168: $contentSimpleXml = new \SimpleXMLElement($outputPath, 0, TRUE);
169: $contentDomXml = dom_import_simplexml($contentSimpleXml)->ownerDocument;
170: $contentDomXml->formatOutput = TRUE;
171: $contentDomXml->save($outputPath);
172: }
173: }
174:
175:
176: }
177: