1111
1212namespace Symfony \Component \Translation \Loader ;
1313
14+ use DOMDocument ;
15+ use InvalidArgumentException ;
1416use Symfony \Component \Config \Util \XmlUtils ;
1517use Symfony \Component \Translation \MessageCatalogue ;
1618use Symfony \Component \Translation \Exception \InvalidResourceException ;
@@ -41,46 +43,33 @@ public function load($resource, $locale, $domain = 'messages')
4143 throw new NotFoundResourceException (sprintf ('File "%s" not found. ' , $ resource ));
4244 }
4345
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+
4656 $ this ->validateSchema ($ dom , $ version ->getSchema ());
4757
4858 $ catalogue = new MessageCatalogue ($ locale );
4959 $ version ->extract ($ dom , $ catalogue , $ domain );
60+
5061 $ catalogue ->addResource (new FileResource ($ resource ));
5162
5263 return $ catalogue ;
5364 }
5465
5566 /**
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
7968 * @param string $schema source of the schema
8069 *
8170 * @throws InvalidResourceException
8271 */
83- private function validateSchema (\ DOMDocument $ dom , $ schema )
72+ private function validateSchema (DOMDocument $ dom , $ schema )
8473 {
8574 $ internalErrors = libxml_use_internal_errors (true );
8675
@@ -124,21 +113,25 @@ private function getXmlErrors($internalErrors)
124113 /**
125114 * Detects xliff version from file
126115 *
127- * @param \ DOMDocument $dom
116+ * @param DOMDocument $dom
128117 *
129- * @throws InvalidResourceException
118+ * @throws InvalidArgumentException
130119 *
131120 * @return XliffVersion\AbstractXliffVersion
132121 */
133- private function getVersion (\ DOMDocument $ dom )
122+ private function getVersion (DOMDocument $ dom )
134123 {
135124 $ versionNumber = $ this ->getVersionNumber ($ dom );
136125
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 ();
139132 }
140133
141- throw new InvalidResourceException (sprintf (
134+ throw new InvalidArgumentException (sprintf (
142135 'No support implemented for loading XLIFF version "%s". ' ,
143136 $ versionNumber
144137 ));
@@ -148,18 +141,32 @@ private function getVersion(\DOMDocument $dom)
148141 * Gets xliff file version based on the root "version" attribute.
149142 * Defaults to 1.2 for backwards compatibility
150143 *
151- * @param \DOMDocument $dom
144+ * @param DOMDocument $dom
145+ *
146+ * @throws InvalidArgumentException
152147 *
153148 * @return string
154149 */
155- private function getVersionNumber (\ DOMDocument $ dom )
150+ private function getVersionNumber (DOMDocument $ dom )
156151 {
157152 /** @var \DOMNode $xliff */
158153 foreach ($ dom ->getElementsByTagName ('xliff ' ) as $ xliff ) {
159154 $ version = $ xliff ->attributes ->getNamedItem ('version ' );
160155 if ($ version ) {
161156 return $ version ->nodeValue ;
162157 }
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+ }
163170 }
164171
165172 // Falls back to v1.2
0 commit comments