From 865461d204c398383bf5f279570adb43e1b5408a Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 26 Aug 2012 18:47:56 +0200 Subject: [PATCH] standardized the way we handle XML errors --- .../Loader/XmlFileLoader.php | 21 ++++++++++++------- .../Routing/Loader/XmlFileLoader.php | 21 ++++++++++++------- .../Translation/Loader/XliffFileLoader.php | 17 ++++++++------- .../Mapping/Loader/XmlFileLoader.php | 17 ++++++++------- 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php index 12b59e5cbe..9521097fe0 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php @@ -211,14 +211,18 @@ class XmlFileLoader extends FileLoader */ private function parseFile($file) { + $internalErrors = libxml_use_internal_errors(true); + libxml_clear_errors(); + $dom = new \DOMDocument(); - libxml_use_internal_errors(true); - if (!$dom->load($file, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { - throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors())); - } $dom->validateOnParse = true; + if (!$dom->load($file, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { + throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors($internalErrors))); + } $dom->normalizeDocument(); - libxml_use_internal_errors(false); + + libxml_use_internal_errors($internalErrors); + $this->validate($dom, $file); return simplexml_import_dom($dom, 'Symfony\\Component\\DependencyInjection\\SimpleXMLElement'); @@ -360,12 +364,14 @@ EOF ; $current = libxml_use_internal_errors(true); + libxml_clear_errors(); + $valid = $dom->schemaValidateSource($source); foreach ($tmpfiles as $tmpfile) { @unlink($tmpfile); } if (!$valid) { - throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors())); + throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors($current))); } libxml_use_internal_errors($current); } @@ -406,7 +412,7 @@ EOF * * @return array */ - private function getXmlErrors() + private function getXmlErrors($internalErrors) { $errors = array(); foreach (libxml_get_errors() as $error) { @@ -421,6 +427,7 @@ EOF } libxml_clear_errors(); + libxml_use_internal_errors($internalErrors); return $errors; } diff --git a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php index 1203067d23..d7f1677b9f 100644 --- a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php @@ -150,14 +150,18 @@ class XmlFileLoader extends FileLoader */ protected function loadFile($file) { + $internalErrors = libxml_use_internal_errors(true); + libxml_clear_errors(); + $dom = new \DOMDocument(); - libxml_use_internal_errors(true); - if (!$dom->load($file, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { - throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors())); - } $dom->validateOnParse = true; + if (!$dom->load($file, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { + throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors($internalErrors))); + } $dom->normalizeDocument(); - libxml_use_internal_errors(false); + + libxml_use_internal_errors($internalErrors); + $this->validate($dom); return $dom; @@ -175,8 +179,10 @@ class XmlFileLoader extends FileLoader $location = __DIR__.'/schema/routing/routing-1.0.xsd'; $current = libxml_use_internal_errors(true); + libxml_clear_errors(); + if (!$dom->schemaValidate($location)) { - throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors())); + throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors($current))); } libxml_use_internal_errors($current); } @@ -186,7 +192,7 @@ class XmlFileLoader extends FileLoader * * @return array An array of libxml error strings */ - private function getXmlErrors() + private function getXmlErrors($internalErrors) { $errors = array(); foreach (libxml_get_errors() as $error) { @@ -201,6 +207,7 @@ class XmlFileLoader extends FileLoader } libxml_clear_errors(); + libxml_use_internal_errors($internalErrors); return $errors; } diff --git a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php index 4bd6b69a4a..86885eaf5a 100644 --- a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php @@ -55,10 +55,13 @@ class XliffFileLoader implements LoaderInterface */ private function parseFile($file) { + $internalErrors = libxml_use_internal_errors(true); + libxml_clear_errors(); + $dom = new \DOMDocument(); - $current = libxml_use_internal_errors(true); + $dom->validateOnParse = true; if (!@$dom->load($file, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { - throw new \RuntimeException(implode("\n", $this->getXmlErrors())); + throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors))); } $location = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd'; @@ -77,11 +80,11 @@ class XliffFileLoader implements LoaderInterface $source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source); if (!@$dom->schemaValidateSource($source)) { - throw new \RuntimeException(implode("\n", $this->getXmlErrors())); + throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors))); } - $dom->validateOnParse = true; $dom->normalizeDocument(); - libxml_use_internal_errors($current); + + libxml_use_internal_errors($internalErrors); return simplexml_import_dom($dom); } @@ -91,7 +94,7 @@ class XliffFileLoader implements LoaderInterface * * @return array An array of errors */ - private function getXmlErrors() + private function getXmlErrors($internalErrors) { $errors = array(); foreach (libxml_get_errors() as $error) { @@ -106,7 +109,7 @@ class XliffFileLoader implements LoaderInterface } libxml_clear_errors(); - libxml_use_internal_errors(false); + libxml_use_internal_errors($internalErrors); return $errors; } diff --git a/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php index d95d975377..d85e857681 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php @@ -180,22 +180,25 @@ class XmlFileLoader extends FileLoader */ protected function parseFile($file) { + $internalErrors = libxml_use_internal_errors(true); + libxml_clear_errors(); + $dom = new \DOMDocument(); - libxml_use_internal_errors(true); + $dom->validateOnParse = true; if (!$dom->load($file, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { - throw new MappingException(implode("\n", $this->getXmlErrors())); + throw new MappingException(implode("\n", $this->getXmlErrors($internalErrors))); } if (!$dom->schemaValidate(__DIR__.'/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd')) { - throw new MappingException(implode("\n", $this->getXmlErrors())); + throw new MappingException(implode("\n", $this->getXmlErrors($internalErrors))); } - $dom->validateOnParse = true; $dom->normalizeDocument(); - libxml_use_internal_errors(false); + + libxml_use_internal_errors($internalErrors); return simplexml_import_dom($dom); } - protected function getXmlErrors() + protected function getXmlErrors($internalErrors) { $errors = array(); foreach (libxml_get_errors() as $error) { @@ -210,7 +213,7 @@ class XmlFileLoader extends FileLoader } libxml_clear_errors(); - libxml_use_internal_errors(false); + libxml_use_internal_errors($internalErrors); return $errors; }