]> git.mxchange.org Git - core.git/blob - inc/classes/main/parser/xml/class_XmlParser.php
Rewrote core:
[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 - 2012 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 static final 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 (empty($content)) {
67                         // No empty content!
68                         self::createDebugInstance(__CLASS__)->debugOutput('Empty content! Backtrace: <pre>');
69                         debug_print_backtrace();
70                         self::createDebugInstance(__CLASS__)->debugOutput('</pre>');
71                         die();
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');
78                 } else {
79                         // @TODO We need to find a fallback solution here
80                         $this->partialStub('Cannot find recode/mbstring extension!');
81                 } // END - if
82
83                 // Get an XML parser
84                 $xmlParser = xml_parser_create();
85
86                 // Force case-folding to on
87                 xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, true);
88
89                 // Set UTF-8
90                 xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
91
92                 // Get instance (we need this :( )
93                 $templateInstance = $this->getTemplateInstance();
94
95                 // Set object
96                 xml_set_object($xmlParser, $templateInstance);
97
98                 // Set handler call-backs
99                 xml_set_element_handler($xmlParser, 'startElement', 'endElement');
100                 xml_set_character_data_handler($xmlParser, 'characterHandler');
101
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);
107                 } // END - if
108
109                 // Free the parser
110                 xml_parser_free($xmlParser);
111         }
112 }
113
114 // [EOF]
115 ?>