Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

XML_Parser.php

Go to the documentation of this file.
00001 <?php 00002 /* vim: set expandtab tabstop=4 shiftwidth=4: */ 00003 // 00004 // Copyright (c) 2003 Laurent Bedubourg 00005 // 00006 // This library is free software; you can redistribute it and/or 00007 // modify it under the terms of the GNU Lesser General Public 00008 // License as published by the Free Software Foundation; either 00009 // version 2.1 of the License, or (at your option) any later version. 00010 // 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 // Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public 00017 // License along with this library; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 // 00020 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr> 00021 // 00022 00023 require_once PT_IP . "/Types/Errors.php"; 00024 00084 class PHPTAL_XML_Parser 00085 { 00086 var $_file = '#string'; 00087 var $_tags = array(); 00088 var $_parser; 00089 var $_error; 00090 var $_xmlErrors = array( 00091 XML_ERROR_NONE => "XML_ERROR_NONE", 00092 XML_ERROR_NO_MEMORY => "XML_ERROR_NO_MEMORY", 00093 XML_ERROR_SYNTAX => "XML_ERROR_SYNTAX", 00094 XML_ERROR_NO_ELEMENTS => "XML_ERROR_NO_ELEMENTS", 00095 XML_ERROR_INVALID_TOKEN => "XML_ERROR_INVALID_TOKEN", 00096 XML_ERROR_UNCLOSED_TOKEN => "XML_ERROR_UNCLOSED_TOKEN", 00097 XML_ERROR_PARTIAL_CHAR => "XML_ERROR_PARTIAL_CHAR", 00098 XML_ERROR_TAG_MISMATCH => "XML_ERROR_TAG_MISMATCH", 00099 XML_ERROR_DUPLICATE_ATTRIBUTE => "XML_ERROR_DUPLICATE_ATTRIBUTE", 00100 XML_ERROR_JUNK_AFTER_DOC_ELEMENT => "XML_ERROR_JUNK_AFTER_DOC_ELEMENT", 00101 XML_ERROR_PARAM_ENTITY_REF => "XML_ERROR_PARAM_ENTITY_REF", 00102 XML_ERROR_UNDEFINED_ENTITY => "XML_ERROR_UNDEFINED_ENTITY", 00103 XML_ERROR_RECURSIVE_ENTITY_REF => "XML_ERROR_RECURSIVE_ENTITY_REF", 00104 XML_ERROR_ASYNC_ENTITY => "XML_ERROR_ASYNC_ENTITY", 00105 XML_ERROR_BAD_CHAR_REF => "XML_ERROR_BAD_CHAR_REF", 00106 XML_ERROR_BINARY_ENTITY_REF => "XML_ERROR_BINARY_ENTITY_REF", 00107 XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF => "XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF", 00108 XML_ERROR_MISPLACED_XML_PI => "XML_ERROR_MISPLACED_XML_PI", 00109 XML_ERROR_UNKNOWN_ENCODING => "XML_ERROR_UNKNOWN_ENCODING", 00110 XML_ERROR_INCORRECT_ENCODING => "XML_ERROR_INCORRECT_ENCODING", 00111 XML_ERROR_UNCLOSED_CDATA_SECTION => "XML_ERROR_UNCLOSED_CDATA_SECTION", 00112 XML_ERROR_EXTERNAL_ENTITY_HANDLING => "XML_ERROR_EXTERNAL_ENTITY_HANDLING", 00113 ); 00114 00118 function PHPTAL_XML_Parser() 00119 { 00120 $this->__construct(); 00121 } 00122 00126 function __construct() 00127 { 00128 $this->_parser = xml_parser_create(); 00129 xml_set_object($this->_parser, $this); 00130 xml_set_element_handler($this->_parser, "_onElementStart", "_onElementClose"); 00131 xml_set_character_data_handler($this->_parser, "_onElementData"); 00132 xml_set_default_handler($this->_parser, "_onSpecific"); 00133 xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, 0); 00134 } 00135 00141 function _parse($data, $eof=true) 00142 { 00143 $data = str_replace('&','&amp;', $data); 00144 if (!xml_parse($this->_parser, $data)) { 00145 // PHPTAL errors first 00146 if (PEAR::isError($this->_error)) { 00147 return $this->_error; 00148 } 00149 // then look for parser errors 00150 $err = xml_get_error_code($this->_parser); 00151 return PEAR::raiseError($this->_xmlErrors[$err] 00152 .' in '.$this->_file 00153 .' around line '.$this->getLineNumber()); 00154 } 00155 if (PEAR::isError($this->_error)) { 00156 return $this->_error; 00157 } 00158 return true; 00159 } 00160 00161 function parseString($data) 00162 { 00163 return $this->_parse($data, true); 00164 } 00165 00166 function parseFile($path) 00167 { 00168 $this->_file = $path; 00169 $fp = @fopen($path, "r"); 00170 if (!$fp) { 00171 return PEAR::raiseError($php_errormsg); 00172 } 00173 while ($data = fread($fp, 1024)) { 00174 $err = $this->_parse($data, feof($fp)); 00175 if (PEAR::isError($err)) { 00176 fclose($fp); 00177 return $err; 00178 } 00179 } 00180 fclose($fp); 00181 } 00182 00183 function _onElementStart($parser, $tag, $attributes) 00184 { 00185 if (PEAR::isError($this->_error)) return; 00186 $this->_error = $this->onElementStart($tag, $attributes); 00187 } 00188 00189 function _onElementClose($parser, $tag) 00190 { 00191 if (PEAR::isError($this->_error)) return; 00192 $this->_error = $this->onElementClose($tag); 00193 } 00194 00195 function _onElementData($parser, $data) 00196 { 00197 if (PEAR::isError($this->_error)) return; 00198 $this->_error = $this->onElementData($data); 00199 } 00200 00201 function _onSpecific($parser, $data) 00202 { 00203 if (PEAR::isError($this->_error)) return; 00204 $this->_error = $this->onSpecific($data); 00205 } 00206 00212 function getLineNumber() 00213 { 00214 return xml_get_current_line_number($this->_parser); 00215 } 00216 00217 // 00218 // ABSTRACT METHODS 00219 // 00220 00227 function onElementStart($tag, $attributes){} 00228 00234 function onElementClose($tag){} 00235 00241 function onElementData($data){} 00242 00251 function onSpecific($data){} 00252 } 00253 00254 ?>

Generated on Tue Jun 29 23:40:07 2004 for Mediawiki by doxygen 1.3.7