5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 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 final static 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 $this->debugOuput('Empty content! Backtrace: <pre>');
69 debug_print_backtrace();
70 $this->debugOuput('</pre>');
72 } elseif (function_exists('recode')) {
73 // Recode found, so use it
74 $content = recode('html..utf8', $content);
76 // @TODO We need to find a fallback solution here
77 $this->partialStub('Cannot find recode extension!');
81 $xmlParser = xml_parser_create();
83 // Force case-folding to on
84 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
87 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
89 // Get instance (we need this :( )
90 $templateInstance = $this->getTemplateInstance();
93 xml_set_object($xmlParser, $templateInstance);
95 // Set handler call-backs
96 xml_set_element_handler($xmlParser, 'startElement', 'endElement');
97 xml_set_character_data_handler($xmlParser, 'characterHandler');
99 // Now parse the XML tree
100 if (!xml_parse($xmlParser, $content)) {
101 // Error found in XML!
102 //* DEBUG: */ die('<pre>'.htmlentities($content).'</pre>');
103 throw new XmlParserException(array($this, $xmlParser), self::EXCEPTION_XML_PARSER_ERROR);
107 xml_parser_free($xmlParser);