|
From: andi <bin...@li...> - 2001-09-16 20:39:50
|
andi Sun Sep 16 13:39:44 2001 EDT
Added files:
/r2/binarycloud/base/lib BCTagParser.php
Log:
Parser class that will provide parsing xhtml/xml files for bc:* tags. Very early experimetnal code
Index: r2/binarycloud/base/lib/BCTagParser.php
+++ r2/binarycloud/base/lib/BCTagParser.php
#!/usr/local/php4/bin/php -q
<?php
class BCTagParser {
/** Pool of all registered bc:foo tags and processor methods */
var $knownTags;
function BCTagParser($_file) {
$this->RegisterTag('BC:IMG', "ImageRequestBuilder::Process");
$this->RegisterTag('BC:JS', "JSRequestBuilder::Process");
$this->RegisterTag('BC:MODULE', "_ProcessModule");
$this->_Parse($_file);
}
function RegisterTag($_tag, $_processor) {
$this->knownTags[$_tag] = $_processor;
}
// method _StartElement(&$_parser, $_name, $_attrs) {{{
/**
* Method function triggered when an XML start element appears
* here we will parse tag's we recognize from the xml file
*
* @param $_parser ref the XML parser
* @param $_name string the tag found
* @param $_attrs array the attributes this tag carried
* @access private
* @author Andreas Adrehold, <a.a...@th...>
*/
function _StartElement(&$_parser, $_name, $_attrs) {
if (!isset($this->knownTags[$_name])) {
print "Unknown Tag $_name\n";
return;
}
print "Processing $_name using ".$this->knownTags[$_name]."\n";
var_dump($_attrs);
// call the processing function, return resulting php
// return $this->knownTags[$_name]($_attrs);
}
// }}}
// method _EndElement(&$_parser, $_name) {{{
function _EndElement(&$_parser, $_name) {
// nothing - for now
}
// }}}
// method _ProcessModule(&$_attrs) {{{
function _ProcessModule(&$_attrs) {
// wil call xml2php methods etc.
// retuns resulting php
var_dump($attrs);
}
// }}}
// method _Parse($_file) {{{
/**
* private method for parsing the XML file. actually creates XML
* parser and sets up element handlers
*
* @param $_file string the file to parse
*/
function _Parse($_file) {
printf("Parsing XML file: $_file...\n",$this);
if (empty($_file)) {
die("XML file to parse was not set");
}
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_object($xml_parser, &$this);
xml_set_element_handler($xml_parser, "_StartElement", "_EndElement");
if (!($fp = fopen($_file, "r"))) {
die("Could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d of $_file\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
}
// }}}
}
// }}}
// exectute the parser for demonstration
$xml =& new BCTagParser("data.html.xml");
/*
* Local Variables:
* mode: php
* tab-width: 4
* c-basic-offset: 4
* End:
*/
?>
|