|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
|
9
|
|
|
* |
|
10
|
|
|
* @license GNU General Public License version 3 or later. |
|
11
|
|
|
* For the full copyright and license information, please read the |
|
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Kitodo\Dlf\Validation; |
|
16
|
|
|
|
|
17
|
|
|
use DOMDocument; |
|
18
|
|
|
use Exception; |
|
19
|
|
|
use InvalidArgumentException; |
|
20
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
21
|
|
|
use SimpleXMLElement; |
|
22
|
|
|
use Symfony\Component\Process\Process; |
|
23
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The validator validates the DOMDocument against an XSL Schematron and converts error output to validation errors. |
|
27
|
|
|
* |
|
28
|
|
|
* @package TYPO3 |
|
29
|
|
|
* @subpackage dlf |
|
30
|
|
|
* |
|
31
|
|
|
* @access public |
|
32
|
|
|
*/ |
|
33
|
|
|
class SaxonXslToSvrlValidator extends AbstractDlfValidator implements LoggerAwareInterface |
|
34
|
|
|
{ |
|
35
|
|
|
private string $jar; |
|
36
|
|
|
|
|
37
|
|
|
private string $xsl; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(array $configuration) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct(DOMDocument::class); |
|
42
|
|
|
$this->jar = GeneralUtility::getFileAbsFileName($configuration["jar"] ?? ''); |
|
43
|
|
|
$this->xsl = GeneralUtility::getFileAbsFileName($configuration["xsl"] ?? ''); |
|
44
|
|
|
if (empty($this->jar)) { |
|
45
|
|
|
$this->logger->error('Saxon JAR file not found.'); |
|
46
|
|
|
throw new InvalidArgumentException('Saxon JAR file not found.', 1723121212747); |
|
47
|
|
|
} |
|
48
|
|
|
if (empty($this->xsl)) { |
|
49
|
|
|
$this->logger->error('XSL Schematron file not found.'); |
|
50
|
|
|
throw new InvalidArgumentException('XSL Schematron file not found.', 1723121212747); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param mixed $value |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function isValid(mixed $value): void |
|
60
|
|
|
{ |
|
61
|
|
|
$svrl = $this->process($value); |
|
62
|
|
|
$this->addErrorsOfSvrl($svrl); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param mixed $value |
|
67
|
|
|
* |
|
68
|
|
|
* @throws InvalidArgumentException |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function process(mixed $value): string |
|
73
|
|
|
{ |
|
74
|
|
|
// using source from standard input |
|
75
|
|
|
$process = new Process(['java', '-jar', $this->jar, '-xsl:' . $this->xsl, '-s:-'], null, null, $value->saveXML()); |
|
76
|
|
|
$process->run(); |
|
77
|
|
|
// executes after the command finish |
|
78
|
|
|
if (!$process->isSuccessful()) { |
|
79
|
|
|
$this->logger->error('Processing exits with code "' . $process->getExitCode() . '"'); |
|
80
|
|
|
throw new InvalidArgumentException('Processing was not successful.', 1724862680); |
|
81
|
|
|
} |
|
82
|
|
|
return $process->getOutput(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Add errors of schematron output. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $svrl |
|
89
|
|
|
* |
|
90
|
|
|
* @throws InvalidArgumentException |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
private function addErrorsOfSvrl(string $svrl): void |
|
95
|
|
|
{ |
|
96
|
|
|
try { |
|
97
|
|
|
$xml = new SimpleXMLElement($svrl); |
|
98
|
|
|
$results = $xml->xpath("/svrl:schematron-output/svrl:failed-assert/svrl:text"); |
|
99
|
|
|
|
|
100
|
|
|
foreach ($results as $error) { |
|
101
|
|
|
$this->addError($error->__toString(), 1724405095); |
|
102
|
|
|
} |
|
103
|
|
|
} catch (Exception $e) { |
|
104
|
|
|
throw new InvalidArgumentException('Schematron output XML could not be parsed.', 1724754882); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|