Index: lib/xmlize.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/xmlize.php,v retrieving revision 1.8 diff -u -r1.8 xmlize.php --- lib/xmlize.php 26 May 2009 06:26:11 -0000 1.8 +++ lib/xmlize.php 27 Sep 2010 18:20:28 -0000 @@ -9,7 +9,8 @@ * on my part is to blame for the credit these people aren't receiving. * None of the code was copyrighted, though. * - * @package moodlecore + * @package core + * @subpackage lib * @author Hans Anderson * @version This is a stable release, 1.0. I don't foresee any changes, but you * might check {@link http://www.hansanderson.com/php/xml/} to see @@ -18,9 +19,32 @@ */ /** - * Create xml formatted output from an array. + * Exception thrown when there is an error parsing an XML file. * - * usage:
+ * @copyright 2010 The Open University + */ +class xml_format_exception extends moodle_exception { + /** @var string */ + public $errorstring; + public $line; + public $char; + function __construct($errorstring, $line, $char, $link = '') { + $this->errorstring = $errorstring; + $this->line = $line; + $this->char = $char; + + $a = new stdClass(); + $a->errorstring = $errorstring; + $a->errorline = $line; + $a->errorchar = $char; + parent::__construct('errorparsingxml', 'error', $link, $a); + } +} + +/** + * Create an array structure from an XML string. + * + * Usage:
* * $xml = xmlize($array); * @@ -38,33 +62,44 @@ * {@link http://bugs.php.net/bug.php?id=29711} * Ciao, Eloy :-) * - * - * @author Hans Anderson - * @param array $data The array to be converted - * @param int $WHITE If set to 1 allows the parser to skip "space" characters in xml document. Default is 1 + * @param string $data The XML source to parse. + * @param int $whitespace If set to 1 allows the parser to skip "space" characters in xml document. Default is 1 * @param string $encoding Specify an OUTPUT encoding. If not specified, it defaults to UTF-8. - * @return array + * @param bool $reporterrors if set to true, then a {@link xml_format_exception} + * exception will be thrown if the XML is not well-formed. Otherwise errors are ignored. + * @return array representation of the parsed XML. */ -function xmlize($data, $WHITE=1, $encoding='UTF-8') { +function xmlize($data, $whitespace = 1, $encoding = 'UTF-8', $reporterrors = false) { $data = trim($data); - $vals = $index = $array = array(); + $vals = array(); $parser = xml_parser_create($encoding); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); - xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE); - xml_parse_into_struct($parser, $data, $vals, $index); + xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $whitespace); + xml_parse_into_struct($parser, $data, $vals); + + // Error handling when the xml file is not well-formed + if ($reporterrors) { + $errorcode = xml_get_error_code($parser); + if ($errorcode) { + $exception = new xml_format_exception(xml_error_string($errorcode), + xml_get_current_line_number($parser), + xml_get_current_column_number($parser)); + xml_parser_free($parser); + throw $exception; + } + } xml_parser_free($parser); $i = 0; - if (empty($vals)) { // XML file is invalid or empty, return false return false; } + $array = array(); $tagname = $vals[$i]['tag']; - if ( isset ($vals[$i]['attributes'] ) ) - { + if (isset($vals[$i]['attributes'])) { $array[$tagname]['@'] = $vals[$i]['attributes']; } else { $array[$tagname]['@'] = array(); @@ -72,7 +107,6 @@ $array[$tagname]["#"] = xml_depth($vals, $i); - return $array; } @@ -192,5 +226,3 @@ return 1; } - -?> Index: lang/en/error.php =================================================================== RCS file: /cvsroot/moodle/moodle/lang/en/error.php,v retrieving revision 1.50 diff -u -r1.50 error.php --- lang/en/error.php 22 Sep 2010 08:22:58 -0000 1.50 +++ lang/en/error.php 27 Sep 2010 18:20:28 -0000 @@ -212,6 +212,7 @@ $string['errorcreatingrole'] = 'Error creating role'; $string['errorfetchingrssfeed'] = 'Error fetching RSS feed.'; $string['erroronline'] = 'Error on line {$a}'; +$string['errorparsingxml'] = 'Error parsing XML: {$a->errorstring} at line {$a->errorline}, char {$a->errorchar}'; $string['errorreadingfile'] = 'Error reading file "{$a}"'; $string['errorsavingrequest'] = 'An error occurred when trying to save your request.'; $string['errorsettinguserpref'] = 'Error setting user preference';