1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
<?php
// $Id: saxparser.php,v 1.11.2.1 2005/06/29 17:03:34 mithyt2 Exp $
/*******************************************************************************
Location: <b>xml/SaxParser.class</b><br>
<br>
Provides basic functionality to read and parse XML documents. Subclasses
must implement all the their custom handlers by using add* function methods.
They may also use the handle*() methods to parse a specific XML begin and end
tags, but this is not recommended as it is more difficult.<br>
<br>
Copyright © 2001 eXtremePHP. All rights reserved.<br>
<br>
@author Ken Egervari<br>
*******************************************************************************/
class SaxParser
{
var $level;
var $parser;
var $isCaseFolding;
var $targetEncoding;
/* Custom Handler Variables */
var $tagHandlers = array();
/* Tag stack */
var $tags = array();
/* Xml Source Input */
var $xmlInput;
var $errors = array();
/****************************************************************************
Creates a SaxParser object using a FileInput to represent the stream
of XML data to parse. Use the static methods createFileInput or
createStringInput to construct xml input source objects to supply
to the constructor, or the implementor can construct them individually.
****************************************************************************/
function SaxParser(&$input)
{
$this->level = 0;
$this->parser = xml_parser_create('UTF-8');
xml_set_object($this->parser, $this);
$this->input =& $input;
$this->setCaseFolding(false);
$this->useUtfEncoding();
xml_set_element_handler($this->parser, 'handleBeginElement','handleEndElement');
xml_set_character_data_handler($this->parser, 'handleCharacterData');
xml_set_processing_instruction_handler($this->parser, 'handleProcessingInstruction');
xml_set_default_handler($this->parser, 'handleDefault');
xml_set_unparsed_entity_decl_handler($this->parser, 'handleUnparsedEntityDecl');
xml_set_notation_decl_handler($this->parser, 'handleNotationDecl');
xml_set_external_entity_ref_handler($this->parser, 'handleExternalEntityRef');
}
/*---------------------------------------------------------------------------
Property Methods
---------------------------------------------------------------------------*/
function getCurrentLevel()
{
return $this->level;
}
/****************************************************************************
* @param $isCaseFolding
* @returns void
****************************************************************************/
function setCaseFolding($isCaseFolding)
{
assert(is_bool($isCaseFolding));
$this->isCaseFolding = $isCaseFolding;
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, $this->isCaseFolding);
}
/****************************************************************************
* @returns void
****************************************************************************/
function useIsoEncoding()
{
$this->targetEncoding = 'ISO-8859-1';
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'ISO-8859-1');
}
/****************************************************************************
* @returns void
****************************************************************************/
function useAsciiEncoding()
{
$this->targetEncoding = 'US-ASCII';
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->targetEncoding);
}
/****************************************************************************
* @returns void
****************************************************************************/
function useUtfEncoding()
{
$this->targetEncoding = 'UTF-8';
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->targetEncoding);
}
/****************************************************************************
Returns the name of the xml tag being parsed
* @returns string
****************************************************************************/
function getCurrentTag()
{
return $this->tags[count($this->tags) - 1];
}
function getParentTag()
{
if (isset($this->tags[count($this->tags) - 2])) {
return $this->tags[count($this->tags) - 2];
}
return false;
}
/*---------------------------------------------------------------------------
Parser methods
---------------------------------------------------------------------------*/
/****************************************************************************
* @returns void
****************************************************************************/
function parse()
{
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'ISO-8859-1');
if (!is_resource($this->input)) {
if (!xml_parse($this->parser, $this->input)) {
$this->setErrors($this->getXmlError());
return false;
}
//if (!$fp = fopen($this->input, 'r')) {
// $this->setErrors('Could not open file: '.$this->input);
// return false;
//}
} else {
while ($data = fread($this->input, 1024)) {
$data=str_replace("é", "´", $data);
if (!xml_parse($this->parser, str_replace("'", "'", $data), feof($this->input))) {
$this->setErrors($this->getXmlError());
fclose($this->input);
return false;
}
}
fclose($this->input);
}
return true;
}
/****************************************************************************
* @returns void
****************************************************************************/
function free()
{
xml_parser_free($this->parser);
if (!method_exists($this, '__destruct')) {
unset($this);
}
else {
$this->__destruct();
}
}
/****************************************************************************
* @private
* @returns string
****************************************************************************/
function getXmlError()
{
return sprintf("XmlParse error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser));
}
/*---------------------------------------------------------------------------
Custom Handler Methods
---------------------------------------------------------------------------*/
/****************************************************************************
Adds a callback function to be called when a tag is encountered.<br>
Functions that are added must be of the form:<br>
<b>functionName( $attributes )</b>
* @param $tagName string. The name of the tag currently being parsed.
* @param $functionName string. The name of the function in XmlDocument's
subclass.
* @returns void
****************************************************************************/
function addTagHandler(&$tagHandler)
{
$name = $tagHandler->getName();
if (is_array($name)) {
foreach ($name as $n) {
$this->tagHandlers[$n] =& $tagHandler;
}
} else {
$this->tagHandlers[$name] =& $tagHandler;
}
}
/*---------------------------------------------------------------------------
Private Handler Methods
---------------------------------------------------------------------------*/
/****************************************************************************
Callback function that executes whenever a the start of a tag
occurs when being parsed.
* @param $parser int. The handle to the parser.
* @param $tagName string. The name of the tag currently being parsed.
* @param $attributesArray attay. The list of attributes associated with
the tag.
* @private
* @returns void
****************************************************************************/
function handleBeginElement($parser, $tagName, $attributesArray)
{
array_push($this->tags, $tagName);
$this->level++;
if (isset($this->tagHandlers[$tagName]) && is_subclass_of($this->tagHandlers[$tagName], 'xmltaghandler')) {
$this->tagHandlers[$tagName]->handleBeginElement($this, $attributesArray);
} else {
$this->handleBeginElementDefault($parser, $tagName, $attributesArray);
}
}
/****************************************************************************
Callback function that executes whenever the end of a tag
occurs when being parsed.
* @param $parser int. The handle to the parser.
* @param $tagName string. The name of the tag currently being parsed.
* @private
* @returns void
****************************************************************************/
function handleEndElement($parser, $tagName)
{
array_pop($this->tags);
if (isset($this->tagHandlers[$tagName]) && is_subclass_of($this->tagHandlers[$tagName], 'xmltaghandler')) {
$this->tagHandlers[$tagName]->handleEndElement($this);
} else {
$this->handleEndElementDefault($parser, $tagName);
}
$this->level--;
}
/****************************************************************************
Callback function that executes whenever character data is encountered
while being parsed.
* @param $parser int. The handle to the parser.
* @param $data string. Character data inside the tag
* @returns void
****************************************************************************/
function handleCharacterData($parser, $data)
{
$tagHandler =& $this->tagHandlers[$this->getCurrentTag()];
if (isset($tagHandler) && is_subclass_of($tagHandler, 'xmltaghandler')) {
$tagHandler->handleCharacterData($this, $data);
} else {
$this->handleCharacterDataDefault($parser, $data);
}
}
/****************************************************************************
* @param $parser int. The handle to the parser.
* @returns void
****************************************************************************/
function handleProcessingInstruction($parser, &$target, &$data)
{
// if($target == 'php') {
// eval($data);
// }
}
/****************************************************************************
* @param $parser int. The handle to the parser.
* @returns void
****************************************************************************/
function handleDefault($parser, $data)
{
}
/****************************************************************************
* @param $parser int. The handle to the parser.
* @returns void
****************************************************************************/
function handleUnparsedEntityDecl($parser, $entityName, $base, $systemId, $publicId, $notationName)
{
}
/****************************************************************************
* @param $parser int. The handle to the parser.
* @returns void
****************************************************************************/
function handleNotationDecl($parser, $notationName, $base, $systemId, $publicId)
{
}
/****************************************************************************
* @param $parser int. The handle to the parser.
* @returns void
****************************************************************************/
function handleExternalEntityRef($parser, $openEntityNames, $base, $systemId, $publicId)
{
}
/**
* The default tag handler method for a tag with no handler
*
* @abstract
*/
function handleBeginElementDefault($parser, $tagName, $attributesArray)
{
}
/**
* The default tag handler method for a tag with no handler
*
* @abstract
*/
function handleEndElementDefault($parser, $tagName)
{
}
/**
* The default tag handler method for a tag with no handler
*
* @abstract
*/
function handleCharacterDataDefault($parser, $data)
{
}
/**
* Sets error messages
*
* @param $error string an error message
*/
function setErrors($error)
{
$this->errors[] = trim($error);
}
/**
* Gets all the error messages
*
* @param $ashtml bool return as html?
* @return mixed
*/
function &getErrors($ashtml = true)
{
if (!$ashtml) {
return $this->errors;
} else {
$ret = '';
if (count($this->errors) > 0) {
foreach ($this->errors as $error) {
$ret .= $error.'<br />';
}
}
return $ret;
}
}
}
?> |
Partager