11
11
12
12
namespace Symfony \Component \Translation \Loader ;
13
13
14
+ use DOMDocument ;
15
+ use InvalidArgumentException ;
14
16
use Symfony \Component \Config \Util \XmlUtils ;
15
17
use Symfony \Component \Translation \MessageCatalogue ;
16
18
use Symfony \Component \Translation \Exception \InvalidResourceException ;
@@ -41,46 +43,33 @@ public function load($resource, $locale, $domain = 'messages')
41
43
throw new NotFoundResourceException (sprintf ('File "%s" not found. ' , $ resource ));
42
44
}
43
45
44
- $ dom = $ this ->parseFile ($ resource );
45
- $ version = $ this ->getVersion ($ dom );
46
+ try {
47
+ $ dom = XmlUtils::loadFile ($ resource );
48
+ $ version = $ this ->getVersion ($ dom );
49
+
50
+ } catch (InvalidArgumentException $ e ) {
51
+ $ message = sprintf ('Unable to load "%s": %s ' , $ resource , $ e ->getMessage ());
52
+
53
+ throw new InvalidResourceException ($ message , $ e ->getCode (), $ e );
54
+ }
55
+
46
56
$ this ->validateSchema ($ dom , $ version ->getSchema ());
47
57
48
58
$ catalogue = new MessageCatalogue ($ locale );
49
59
$ version ->extract ($ dom , $ catalogue , $ domain );
60
+
50
61
$ catalogue ->addResource (new FileResource ($ resource ));
51
62
52
63
return $ catalogue ;
53
64
}
54
65
55
66
/**
56
- * Parses the given file into a DOMDocument
57
- *
58
- * @param string $file
59
- *
60
- * @throws \RuntimeException
61
- *
62
- * @return \DOMDocument
63
- *
64
- * @throws InvalidResourceException
65
- */
66
- private function parseFile ($ file )
67
- {
68
- try {
69
- $ dom = XmlUtils::loadFile ($ file );
70
- } catch (\InvalidArgumentException $ e ) {
71
- throw new InvalidResourceException (sprintf ('Unable to load "%s": %s ' , $ file , $ e ->getMessage ()), $ e ->getCode (), $ e );
72
- }
73
-
74
- return $ dom ;
75
- }
76
-
77
- /**
78
- * @param \DOMDocument $dom
67
+ * @param DOMDocument $dom
79
68
* @param string $schema source of the schema
80
69
*
81
70
* @throws InvalidResourceException
82
71
*/
83
- private function validateSchema (\ DOMDocument $ dom , $ schema )
72
+ private function validateSchema (DOMDocument $ dom , $ schema )
84
73
{
85
74
$ internalErrors = libxml_use_internal_errors (true );
86
75
@@ -124,21 +113,25 @@ private function getXmlErrors($internalErrors)
124
113
/**
125
114
* Detects xliff version from file
126
115
*
127
- * @param \ DOMDocument $dom
116
+ * @param DOMDocument $dom
128
117
*
129
- * @throws InvalidResourceException
118
+ * @throws InvalidArgumentException
130
119
*
131
120
* @return XliffVersion\AbstractXliffVersion
132
121
*/
133
- private function getVersion (\ DOMDocument $ dom )
122
+ private function getVersion (DOMDocument $ dom )
134
123
{
135
124
$ versionNumber = $ this ->getVersionNumber ($ dom );
136
125
137
- if ('1.2 ' === $ versionNumber ) {
138
- return new XliffVersion \XliffVersion12 ();
126
+ switch ($ versionNumber ) {
127
+ case '1.2 ' :
128
+ return new XliffVersion \XliffVersion12 ();
129
+
130
+ case '2.0 ' :
131
+ return new XliffVersion \XliffVersion20 ();
139
132
}
140
133
141
- throw new InvalidResourceException (sprintf (
134
+ throw new InvalidArgumentException (sprintf (
142
135
'No support implemented for loading XLIFF version "%s". ' ,
143
136
$ versionNumber
144
137
));
@@ -148,18 +141,32 @@ private function getVersion(\DOMDocument $dom)
148
141
* Gets xliff file version based on the root "version" attribute.
149
142
* Defaults to 1.2 for backwards compatibility
150
143
*
151
- * @param \DOMDocument $dom
144
+ * @param DOMDocument $dom
145
+ *
146
+ * @throws InvalidArgumentException
152
147
*
153
148
* @return string
154
149
*/
155
- private function getVersionNumber (\ DOMDocument $ dom )
150
+ private function getVersionNumber (DOMDocument $ dom )
156
151
{
157
152
/** @var \DOMNode $xliff */
158
153
foreach ($ dom ->getElementsByTagName ('xliff ' ) as $ xliff ) {
159
154
$ version = $ xliff ->attributes ->getNamedItem ('version ' );
160
155
if ($ version ) {
161
156
return $ version ->nodeValue ;
162
157
}
158
+
159
+ $ namespace = $ xliff ->attributes ->getNamedItem ('xmlns ' );
160
+ if ($ namespace ) {
161
+ if (substr_compare ('urn:oasis:names:tc:xliff:document: ' , $ namespace ->nodeValue , 0 , 34 ) !== 0 ) {
162
+ throw new InvalidArgumentException (sprintf (
163
+ 'Not a valid XLIFF namespace "%s" ' ,
164
+ $ namespace
165
+ ));
166
+ }
167
+
168
+ return substr ($ namespace , 34 );
169
+ }
163
170
}
164
171
165
172
// Falls back to v1.2
0 commit comments