88use SimpleSAML \Assert \Assert ;
99use SimpleSAML \XML \Exception \IOException ;
1010use SimpleSAML \XML \Exception \RuntimeException ;
11- use SimpleSAML \XML \Exception \SchemaViolationException ;
1211use SimpleSAML \XML \Exception \UnparseableXMLException ;
1312
14- use function array_unique ;
15- use function file_exists ;
1613use function file_get_contents ;
1714use function func_num_args ;
18- use function implode ;
1915use function libxml_clear_errors ;
20- use function libxml_get_errors ;
2116use function libxml_set_external_entity_loader ;
2217use function libxml_use_internal_errors ;
2318use function sprintf ;
24- use function trim ;
2519
2620/**
2721 * @package simplesamlphp/xml-common
@@ -37,14 +31,12 @@ final class DOMDocumentFactory
3731
3832 /**
3933 * @param string $xml
40- * @param string|null $schemaFile
4134 * @param non-negative-int $options
4235 *
4336 * @return \DOMDocument
4437 */
4538 public static function fromString (
4639 string $ xml ,
47- ?string $ schemaFile = null ,
4840 int $ options = self ::DEFAULT_OPTIONS ,
4941 ): DOMDocument {
5042 libxml_set_external_entity_loader (null );
@@ -64,11 +56,6 @@ public static function fromString(
6456 $ options |= LIBXML_NO_XXE ;
6557 }
6658
67- // Perform optional schema validation
68- if (!empty ($ schemaFile )) {
69- self ::schemaValidation ($ xml , $ schemaFile , $ options );
70- }
71-
7259 $ domDocument = self ::create ();
7360 $ loaded = $ domDocument ->loadXML ($ xml , $ options );
7461
@@ -97,7 +84,6 @@ public static function fromString(
9784
9885 /**
9986 * @param string $file
100- * @param string|null $schemaFile
10187 * @param non-negative-int $options
10288 *
10389 * @return \DOMDocument
@@ -117,9 +103,7 @@ public static function fromFile(
117103 }
118104
119105 Assert::notWhitespaceOnly ($ xml , sprintf ('File "%s" does not have content ' , $ file ), RuntimeException::class);
120- return (func_num_args () < 3 )
121- ? static ::fromString ($ xml , $ schemaFile )
122- : static ::fromString ($ xml , $ schemaFile , $ options );
106+ return (func_num_args () < 2 ) ? static ::fromString ($ xml ) : static ::fromString ($ xml , $ options );
123107 }
124108
125109
@@ -132,39 +116,4 @@ public static function create(string $version = '1.0', string $encoding = 'UTF-8
132116 {
133117 return new DOMDocument ($ version , $ encoding );
134118 }
135-
136-
137- /**
138- * Validate an XML-string against a given schema.
139- *
140- * @param string $xml
141- * @param string $schemaFile
142- * @param int $options
143- *
144- * @throws \SimpleSAML\XML\Exception\SchemaViolationException when validation fails.
145- */
146- public static function schemaValidation (
147- string $ xml ,
148- string $ schemaFile ,
149- int $ options = self ::DEFAULT_OPTIONS ,
150- ): void {
151- if (!file_exists ($ schemaFile )) {
152- throw new IOException ('File not found. ' );
153- }
154-
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 ;
162- }
163-
164- throw new SchemaViolationException (sprintf (
165- "XML schema validation errors: \n - %s " ,
166- implode ("\n - " , array_unique ($ msgs )),
167- ));
168- }
169- }
170119}
0 commit comments