|
5 | 5 | namespace SimpleSAML\XML; |
6 | 6 |
|
7 | 7 | use DOMDocument; |
8 | | -use Exception; |
9 | | -use LibXMLError; |
10 | 8 | use SimpleSAML\Assert\Assert; |
11 | 9 | use SimpleSAML\XML\Exception\IOException; |
12 | 10 | use SimpleSAML\XML\Exception\RuntimeException; |
13 | 11 | use SimpleSAML\XML\Exception\SchemaViolationException; |
14 | 12 | use SimpleSAML\XML\Exception\UnparseableXMLException; |
15 | | -use XMLReader; |
16 | 13 |
|
17 | 14 | use function array_unique; |
| 15 | +use function file_exists; |
18 | 16 | use function file_get_contents; |
19 | 17 | use function func_num_args; |
20 | 18 | use function implode; |
21 | 19 | use function libxml_clear_errors; |
22 | | -use function libxml_get_last_error; |
| 20 | +use function libxml_get_errors; |
23 | 21 | use function libxml_set_external_entity_loader; |
24 | 22 | use function libxml_use_internal_errors; |
25 | 23 | use function sprintf; |
@@ -150,29 +148,19 @@ public static function schemaValidation( |
150 | 148 | string $schemaFile, |
151 | 149 | int $options = self::DEFAULT_OPTIONS, |
152 | 150 | ): void { |
153 | | - $xmlReader = XMLReader::XML($xml, null, $options); |
154 | | - Assert::notFalse($xmlReader, SchemaViolationException::class); |
155 | | - |
156 | | - libxml_use_internal_errors(true); |
157 | | - |
158 | | - try { |
159 | | - $xmlReader->setSchema($schemaFile); |
160 | | - } catch (Exception) { |
161 | | - $err = libxml_get_last_error(); |
162 | | - throw new SchemaViolationException(trim($err->message) . ' on line ' . $err->line); |
| 151 | + if (!file_exists($schemaFile)) { |
| 152 | + throw new IOException('File not found.'); |
163 | 153 | } |
164 | 154 |
|
165 | | - $msgs = []; |
166 | | - while ($xmlReader->read()) { |
167 | | - if (!$xmlReader->isValid()) { |
168 | | - $err = libxml_get_last_error(); |
169 | | - if ($err instanceof LibXMLError) { |
170 | | - $msgs[] = trim($err->message) . ' on line ' . $err->line; |
171 | | - } |
| 155 | + $document = DOMDocumentFactory::fromString($xml); |
| 156 | + $result = $document->schemaValidate($schemaFile); |
| 157 | + |
| 158 | + if ($result === false) { |
| 159 | + $msgs = []; |
| 160 | + foreach (libxml_get_errors() as $err) { |
| 161 | + $msgs[] = trim($err->message) . ' on line ' . $err->line; |
172 | 162 | } |
173 | | - } |
174 | 163 |
|
175 | | - if ($msgs) { |
176 | 164 | throw new SchemaViolationException(sprintf( |
177 | 165 | "XML schema validation errors:\n - %s", |
178 | 166 | implode("\n - ", array_unique($msgs)), |
|
0 commit comments