11
11
12
12
namespace Symfony \Component \Config \Util ;
13
13
14
+ use Symfony \Component \Config \Util \Exception \InvalidXmlException ;
15
+ use Symfony \Component \Config \Util \Exception \XmlParsingException ;
16
+
14
17
/**
15
18
* XMLUtils is a bunch of utility methods to XML operations.
16
19
*
17
20
* This class contains static methods only and is not meant to be instantiated.
18
21
*
19
22
* @author Fabien Potencier <[email protected] >
20
23
* @author Martin Hasoň <[email protected] >
24
+ * @author Ole Rößner <[email protected] >
21
25
*/
22
26
class XmlUtils
23
27
{
@@ -29,27 +33,23 @@ private function __construct()
29
33
}
30
34
31
35
/**
32
- * Loads an XML file .
36
+ * Parses an XML string .
33
37
*
34
- * @param string $file An XML file path
38
+ * @param string $content An XML string
35
39
* @param string|callable|null $schemaOrCallable An XSD schema file path, a callable, or null to disable validation
36
40
*
37
41
* @return \DOMDocument
38
42
*
39
- * @throws \InvalidArgumentException When loading of XML file returns error
40
- * @throws \RuntimeException When DOM extension is missing
43
+ * @throws \Symfony\Component\Config\Util\Exception\XmlParsingException When parsing of XML file returns error
44
+ * @throws \Symfony\Component\Config\Util\Exception\InvalidXmlException When parsing of XML with schema or callable produces any errors unrelated to the XML parsing itself
45
+ * @throws \RuntimeException When DOM extension is missing
41
46
*/
42
- public static function loadFile ( $ file , $ schemaOrCallable = null )
47
+ public static function parse ( $ content , $ schemaOrCallable = null )
43
48
{
44
49
if (!extension_loaded ('dom ' )) {
45
50
throw new \RuntimeException ('Extension DOM is required. ' );
46
51
}
47
52
48
- $ content = @file_get_contents ($ file );
49
- if ('' === trim ($ content )) {
50
- throw new \InvalidArgumentException (sprintf ('File %s does not contain valid XML, it is empty. ' , $ file ));
51
- }
52
-
53
53
$ internalErrors = libxml_use_internal_errors (true );
54
54
$ disableEntities = libxml_disable_entity_loader (true );
55
55
libxml_clear_errors ();
@@ -59,7 +59,7 @@ public static function loadFile($file, $schemaOrCallable = null)
59
59
if (!$ dom ->loadXML ($ content , LIBXML_NONET | (defined ('LIBXML_COMPACT ' ) ? LIBXML_COMPACT : 0 ))) {
60
60
libxml_disable_entity_loader ($ disableEntities );
61
61
62
- throw new \ InvalidArgumentException (implode ("\n" , static ::getXmlErrors ($ internalErrors )));
62
+ throw new XmlParsingException (implode ("\n" , static ::getXmlErrors ($ internalErrors )));
63
63
}
64
64
65
65
$ dom ->normalizeDocument ();
@@ -69,7 +69,7 @@ public static function loadFile($file, $schemaOrCallable = null)
69
69
70
70
foreach ($ dom ->childNodes as $ child ) {
71
71
if (XML_DOCUMENT_TYPE_NODE === $ child ->nodeType ) {
72
- throw new \ InvalidArgumentException ('Document types are not allowed. ' );
72
+ throw new XmlParsingException ('Document types are not allowed. ' );
73
73
}
74
74
}
75
75
@@ -90,15 +90,15 @@ public static function loadFile($file, $schemaOrCallable = null)
90
90
} else {
91
91
libxml_use_internal_errors ($ internalErrors );
92
92
93
- throw new \ InvalidArgumentException ('The schemaOrCallable argument has to be a valid path to XSD file or callable. ' );
93
+ throw new XmlParsingException ('The schemaOrCallable argument has to be a valid path to XSD file or callable. ' );
94
94
}
95
95
96
96
if (!$ valid ) {
97
97
$ messages = static ::getXmlErrors ($ internalErrors );
98
98
if (empty ($ messages )) {
99
- $ messages = array ( sprintf ( 'The XML file "%s" is not valid. ' , $ file ) );
99
+ throw new InvalidXmlException ( 'The XML is not valid. ' , 0 , $ e );
100
100
}
101
- throw new \ InvalidArgumentException (implode ("\n" , $ messages ), 0 , $ e );
101
+ throw new XmlParsingException (implode ("\n" , $ messages ), 0 , $ e );
102
102
}
103
103
}
104
104
@@ -108,6 +108,32 @@ public static function loadFile($file, $schemaOrCallable = null)
108
108
return $ dom ;
109
109
}
110
110
111
+ /**
112
+ * Loads an XML file.
113
+ *
114
+ * @param string $file An XML file path
115
+ * @param string|callable|null $schemaOrCallable An XSD schema file path, a callable, or null to disable validation
116
+ *
117
+ * @return \DOMDocument
118
+ *
119
+ * @throws \InvalidArgumentException When loading of XML file returns error
120
+ * @throws \Symfony\Component\Config\Util\Exception\XmlParsingException when XML parsing returns any errors
121
+ * @throws \RuntimeException When DOM extension is missing
122
+ */
123
+ public static function loadFile ($ file , $ schemaOrCallable = null )
124
+ {
125
+ $ content = @file_get_contents ($ file );
126
+ if ('' === trim ($ content )) {
127
+ throw new \InvalidArgumentException (sprintf ('File %s does not contain valid XML, it is empty. ' , $ file ));
128
+ }
129
+
130
+ try {
131
+ return static ::parse ($ content , $ schemaOrCallable );
132
+ } catch (InvalidXmlException $ e ) {
133
+ throw new XmlParsingException (sprintf ('The XML file "%s" is not valid. ' , $ file ), 0 , $ e ->getPrevious ());
134
+ }
135
+ }
136
+
111
137
/**
112
138
* Converts a \DomElement object to a PHP array.
113
139
*
0 commit comments