]> git.mxchange.org Git - core.git/blob - inc/classes/main/parser/xml/class_XmlParser.php
e21044f7f2f1934660ba7a1cfb60b0ddc8da10af
[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                         $content = recode("html..utf8", $content);
68                 } else {
69                         // @TODO We need to find a fallback solution here
70                         $this->partialStub('Cannot find recode extension!');
71                 } // END - if
72
73                 // Get an XML parser
74                 $xmlParser = xml_parser_create();
75
76                 // Force case-folding to on
77                 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
78
79                 // Get template instance
80                 $templateInstance = $this->getTemplateInstance();
81
82                 // Set object
83                 xml_set_object($xmlParser, $templateInstance);
84
85                 // Set handler call-backs
86                 xml_set_element_handler($xmlParser, 'startElement', 'endElement');
87                 xml_set_character_data_handler($xmlParser, 'characterHandler');
88
89                 // Now parse the XML tree
90                 if (!xml_parse($xmlParser, $content)) {
91                         // Error found in XML!
92                         //* DEBUG: */ die('<pre>'.htmlentities($content).'</pre>');
93                         throw new XmlParserException(array($this, $xmlParser), self::EXCEPTION_XML_PARSER_ERROR);
94                 } // END - if
95
96                 // Free the parser
97                 xml_parser_free($xmlParser);
98         }
99 }
100
101 // [EOF]
102 ?>