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
*
@@ -37,7 +40,8 @@ private function __construct()
37
40
*
38
41
* @return \DOMDocument
39
42
*
40
- * @throws \InvalidArgumentException When loading of XML file returns error
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
41
45
* @throws \RuntimeException When DOM extension is missing
42
46
*/
43
47
public static function parse ($ content , $ schemaOrCallable = null )
@@ -55,7 +59,7 @@ public static function parse($content, $schemaOrCallable = null)
55
59
if (!$ dom ->loadXML ($ content , LIBXML_NONET | (defined ('LIBXML_COMPACT ' ) ? LIBXML_COMPACT : 0 ))) {
56
60
libxml_disable_entity_loader ($ disableEntities );
57
61
58
- throw new \ InvalidArgumentException (implode ("\n" , static ::getXmlErrors ($ internalErrors )));
62
+ throw new XmlParsingException (implode ("\n" , static ::getXmlErrors ($ internalErrors )));
59
63
}
60
64
61
65
$ dom ->normalizeDocument ();
@@ -65,7 +69,7 @@ public static function parse($content, $schemaOrCallable = null)
65
69
66
70
foreach ($ dom ->childNodes as $ child ) {
67
71
if (XML_DOCUMENT_TYPE_NODE === $ child ->nodeType ) {
68
- throw new \ InvalidArgumentException ('Document types are not allowed. ' );
72
+ throw new XmlParsingException ('Document types are not allowed. ' );
69
73
}
70
74
}
71
75
@@ -86,15 +90,15 @@ public static function parse($content, $schemaOrCallable = null)
86
90
} else {
87
91
libxml_use_internal_errors ($ internalErrors );
88
92
89
- 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. ' );
90
94
}
91
95
92
96
if (!$ valid ) {
93
97
$ messages = static ::getXmlErrors ($ internalErrors );
94
98
if (empty ($ messages )) {
95
- $ messages = array ('The XML is not valid. ' );
99
+ throw new InvalidXmlException ('The XML is not valid. ' , 0 , $ e );
96
100
}
97
- throw new \ InvalidArgumentException (implode ("\n" , $ messages ), 0 , $ e );
101
+ throw new XmlParsingException (implode ("\n" , $ messages ), 0 , $ e );
98
102
}
99
103
}
100
104
@@ -113,6 +117,7 @@ public static function parse($content, $schemaOrCallable = null)
113
117
* @return \DOMDocument
114
118
*
115
119
* @throws \InvalidArgumentException When loading of XML file returns error
120
+ * @throws \Symfony\Component\Config\Util\Exception\XmlParsingException When XML parsing returns any errors.
116
121
* @throws \RuntimeException When DOM extension is missing
117
122
*/
118
123
public static function loadFile ($ file , $ schemaOrCallable = null )
@@ -124,11 +129,8 @@ public static function loadFile($file, $schemaOrCallable = null)
124
129
125
130
try {
126
131
return static ::parse ($ content , $ schemaOrCallable );
127
- } catch (\InvalidArgumentException $ e ) {
128
- if ('The XML is not valid. ' !== $ e ->getMessage ()) {
129
- throw $ e ;
130
- }
131
- throw new \InvalidArgumentException (sprintf ('The XML file "%s" is not valid. ' , $ file ), 0 , $ e ->getPrevious ());
132
+ } catch (InvalidXmlException $ e ) {
133
+ throw new XmlParsingException (sprintf ('The XML file "%s" is not valid. ' , $ file ), 0 , $ e ->getPrevious ());
132
134
}
133
135
}
134
136
0 commit comments