5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class XmlParser extends BaseParser implements Parseable {
25 // Exception constants
26 const EXCEPTION_XML_PARSER_ERROR = 0x1e0;
27 const EXCEPTION_XML_NODE_UNKNOWN = 0x1e1;
28 const EXCEPTION_XML_NODE_MISMATCH = 0x1e2;
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
41 * Creates an instance of the class Parser and prepares it for usage
43 * @param $templateInstance A CompileableTemplate instance
44 * @return $parserInstance An instance of this parser
46 public static final function createXmlParser (CompileableTemplate $templateInstance) {
48 $parserInstance = new XmlParser();
50 // Set the template instance
51 $parserInstance->setTemplateInstance($templateInstance);
53 // Return the prepared instance
54 return $parserInstance;
58 * Parses the given XML content
60 * @param $content Valid XML content
62 * @throws XmlParserException If an XML error was found
64 public function parseXmlContent ($content) {
65 // Convert all to UTF8
66 if (empty($content)) {
68 self::createDebugInstance(__CLASS__)->debugOutput('Empty content! Backtrace: <pre>');
69 debug_print_backtrace();
70 self::createDebugInstance(__CLASS__)->debugOutput('</pre>');
72 } elseif (function_exists('recode')) {
73 // Recode found, so use it
74 $content = recode('html..utf8', $content);
75 } elseif (function_exists('mb_convert_encoding')) {
76 // Use mb_convert_encoding()
77 $content = mb_convert_encoding($content, 'UTF-8', 'auto');
79 // @TODO We need to find a fallback solution here
80 $this->partialStub('Cannot find recode/mbstring extension!');
84 $xmlParser = xml_parser_create();
86 // Force case-folding to on
87 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
90 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
92 // Get instance (we need this :( )
93 $templateInstance = $this->getTemplateInstance();
96 xml_set_object($xmlParser, $templateInstance);
98 // Set handler call-backs
99 xml_set_element_handler($xmlParser, 'startElement', 'endElement');
100 xml_set_character_data_handler($xmlParser, 'characterHandler');
102 // Now parse the XML tree
103 if (!xml_parse($xmlParser, $content)) {
104 // Error found in XML!
105 //* DEBUG: */ die(__METHOD__ . ':<pre>'.htmlentities($content).'</pre>');
106 throw new XmlParserException(array($this, $xmlParser), self::EXCEPTION_XML_PARSER_ERROR);
110 xml_parser_free($xmlParser);