Overview

Namespaces

  • PHP
  • XSLTBenchmarking
    • Reports
    • RunnerConsole
    • TestsGenerator
    • TestsRunner

Classes

  • EasyXmlGeneratorDriver
  • Generator
  • Params
  • SimpleTemplatingDriver
  • SmartyTemplatingDriver
  • SmartyXmlGeneratorDriver
  • Templating
  • Test
  • ToxgeneTemplatingDriver
  • ToxgeneXmlGeneratorDriver
  • XmlGenerator
  • XmlParamsDriver

Interfaces

  • IParamsDriver
  • ITemplatingDriver
  • IXmlGeneratorDriver
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  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:         P::mcd($tmpDir, '/');
 59:         $this->tmpDir = substr($tmpDir, 0, -1);
 60:     }
 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:         $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:                 // we assume installing java
 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:         // get output name - default first document, other set name
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:         // detect error
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:         // make sure about output path
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:         // remove comment
159:         $content = preg_replace('/<!-- generated by ToXgene [^-]* -->/', '', $content);
160:         // change encoding to UTF-8
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:         // make indent (default 'yes')
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: 
XSTL Benchmarking API documentation generated by ApiGen.
Generated using the TokenReflection library.