Typos fixed
[core.git] / inc / classes / main / parser / xml / class_XmlParser.php
1 <?php
2 /**
3  * A Xml Parser class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
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;
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Creates an instance of the class Parser and prepares it for usage
42          *
43          * @param       $templateInstance       A CompileableTemplate instance
44          * @return      $parserInstance         An instance of this parser
45          */
46         public final static function createXmlParser (CompileableTemplate $templateInstance) {
47                 // Get a new instance
48                 $parserInstance = new XmlParser();
49
50                 // Set the template instance
51                 $parserInstance->setTemplateInstance($templateInstance);
52
53                 // Return the prepared instance
54                 return $parserInstance;
55         }
56
57         /**
58          * Parses the given XML content
59          *
60          * @param       $content        Valid XML content
61          * @return      void
62          * @throws      XmlParserException      If an XML error was found
63          */
64         public function parseXmlContent ($content) {
65                 // Convert all to UTF8
66                 if (function_exists('recode')) {
67                         // Recode found, so use it
68                         $content = recode('html..utf8', $content);
69                 } else {
70                         // @TODO We need to find a fallback solution here
71                         $this->partialStub('Cannot find recode extension!');
72                 } // END - if
73
74                 // Get an XML parser
75                 $xmlParser = xml_parser_create();
76
77                 // Force case-folding to on
78                 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
79
80                 // Set UTF-8
81                 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
82
83                 // Get template instance
84                 $templateInstance = $this->getTemplateInstance();
85
86                 // Set object
87                 xml_set_object($xmlParser, $templateInstance);
88
89                 // Set handler call-backs
90                 xml_set_element_handler($xmlParser, 'startElement', 'endElement');
91                 xml_set_character_data_handler($xmlParser, 'characterHandler');
92
93                 // Now parse the XML tree
94                 if (!xml_parse($xmlParser, $content)) {
95                         // Error found in XML!
96                         //* DEBUG: */ die('<pre>'.htmlentities($content).'</pre>');
97                         throw new XmlParserException(array($this, $xmlParser), self::EXCEPTION_XML_PARSER_ERROR);
98                 } // END - if
99
100                 // Free the parser
101                 xml_parser_free($xmlParser);
102         }
103 }
104
105 // [EOF]
106 ?>