|
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:
*/
?>
|
|
From: andi <bin...@li...> - 2001-09-21 15:34:42
|
andi Fri Sep 21 08:34:35 2001 EDT
Modified files:
/r2/binarycloud/base/lib BCTagParser.php
Log:
Basically functional now. The slot-in functions that actually compile the tags to php or img or whatever do not work. Grepin the <bc:*> tags and works. Extending the tags with custom tags works.
Index: r2/binarycloud/base/lib/BCTagParser.php
diff -u r2/binarycloud/base/lib/BCTagParser.php:1.1 r2/binarycloud/base/lib/BCTagParser.php:1.2
--- r2/binarycloud/base/lib/BCTagParser.php:1.1 Sun Sep 16 13:39:44 2001
+++ r2/binarycloud/base/lib/BCTagParser.php Fri Sep 21 08:34:35 2001
@@ -1,102 +1,177 @@
#!/usr/local/php4/bin/php -q
<?php
+
+// these are the actual compiling methods.
+// They are registered at setup they must
+// return a sting with the compiled data,
+// or null in case of a compile error
+
+function BCCompileModuleTag($_tags) {
+ // translating xml to php by using XMLUtils
+ // this seems not to work for tag attributes
+
+ $trees = XMLUtils::XMLStr2XML($_tags);
+ $xmlTree=$trees[0];
+ $phpTree = XMLUtils::XML2PHP($xmlTree);
+ $txtTree = XMLUtils::PHP2PHPArr($phpTree,false);
+ var_dump($phpTree);
+ return "Module Compiler output";
+}
+
+function BCCompileImgTag($_tag) {
+ return "<img src=\"example_compliler_output.gif\">";
+}
+?>
+<?php
+// here we go
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);
+ /** Pool of all registered bc:foo tags and processor methods */
+ var $knownTags = array(); // list of kown tags and their compiler methods
+ var $infile = null; // source file
+ var $outfile = null; // file to write back
+
+ function BCTagParser($_infile, $_outfile) {
+ $this->infile = $_infile;
+ $this->outfile = $_outfile;
}
+ function Compile() {
+ $this->_CompileFile();
+ }
+
function RegisterTag($_tag, $_processor) {
- $this->knownTags[$_tag] = $_processor;
+ $this->knownTags[strtoupper($_tag)] = $_processor;
+ }
+
+ function _CompileFile() {
+
+ /* read file from disk */
+ $source = $this->_Readfile($this->infile);
+
+ /* pull out the bc code blocks that have children | not */
+ preg_match_all("!<(bc:.*?)>.*?</bc:.*?>|<bc:.*?/>!si", $source, $match);
+ $bcBlocks =& $match[0];
+
+ /* assemble interal array with all bc: tags */
+ $tagData = array();
+ for ($i = 0; $i < count($bcBlocks); ++$i) {
+ /* look it tag is registered and assign name and compiler function */
+ foreach($this->knownTags as $name => $val) {
+ if (stristr($bcBlocks[$i], $name)) {
+ $tagData[$i]['name'] = $name;
+ $tagData[$i]['handler'] = $val;
+ $tagData[$i]['data'] = $bcBlocks[$i];
+ break;
+ } else {
+ $tagData[$i]['name'] = 'UNKNOWN';
+ $tagData[$i]['handler'] = 'UNKNOWN';
+ $tagData[$i]['data'] = 'UNKNOWN';
+ }
+ }
+ }
+ var_dump($tagData);
+ /* found at least one block, loop through and call compiler */
+ for ($i = 0; $i < count($tagData); $i++) {
+ echo "Compiling...\n";
+ $source = preg_replace('!'.preg_quote($tagData[$i]['data'],'!').'!is', $this->_CompileTag($tagData[$i]), $source);
+ }
+
+ /* write the new file to disk */
+ $this->_Writefile($this->outfile, $source);
+
}
+
- // method _StartElement(&$_parser, $_name, $_attrs) {{{
+ // method _CompileTag($_data) {{{
/**
- * Method function triggered when an XML start element appears
- * here we will parse tag's we recognize from the xml file
+ * This calls the xml parser providing the tag to parse. And retuns the
+ * compiled tag.
*
- * @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...>
+ * @param $_tag string the xml tag to parse
+ * @access private
*/
+ function _CompileTag($_data) {
- 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);
+ $tName =& $_data['name'];
+ $tData =& $_data['data'];
+ $tHandler =& $_data['handler'];
+
+ if ($tName == 'UNKNOWN') {
+ echo " +Unknown tag: $tName... skipped\n";
+ return "<!-- tag compiler unknown tag: $tName -->\n";
+ }
+
+ echo " +Processing $tName using $tHandler... ";
+
+ $ret = $tHandler($tData);
+
+ if ($ret !== null) {
+ echo "done\n";
+ return $ret;
+ } else {
+ echo "compile error\n";
+ return "<!-- tag compiler error while compiling $_name -->\n";
+ }
}
// }}}
- // method _EndElement(&$_parser, $_name) {{{
-
- function _EndElement(&$_parser, $_name) {
- // nothing - for now
+ // method _Readfile($_filename) {{{
+
+ /**
+ * private method for reading a file into an internal buffer
+ *
+ * @param $_filename string the file to read
+ * @return string the contents of the file
+ */
+ function _Readfile($_filename) {
+ if (!($fd = fopen($_filename, 'r'))) {
+ die("problem reading '$_filename.'");
+ return false;
+ }
+ flock($fd, LOCK_SH);
+ $contents = fread($fd, filesize($_filename));
+ fclose($fd);
+ return $contents;
}
// }}}
- // 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
+ * private method for writing a file from an internal buffer.
+ * Method stolen from smarty ;-)
*
- * @param $_file string the file to parse
+ * @param $_filename string the file to write
+ * @param $_contents string the data to write
+ * @return bool
*/
+ function _Writefile($_filename, $_contents) {
- 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)));
- }
+ if (!($fd = fopen($_filename, 'w'))) {
+ die ("Problem writing '$_filename.'");
+ return false;
}
- xml_parser_free($xml_parser);
+ if (strtoupper(substr(PHP_OS,0,3)) == 'WIN' || (flock($fd, LOCK_EX)) ) {
+ fwrite($fd, $_contents);
+ fclose($fd);
+ chmod($_filename, 0644);
+ }
+ return true;
}
- // }}}
-
}
// }}}
-// exectute the parser for demonstration
-$xml =& new BCTagParser("data.html.xml");
+?>
+<?php
+// runngin the parser for demonstration
+include_once('./XMLUtils.php');
+
+$xml =& new BCTagParser("data.html.xml", "data.html");
+$xml->RegisterTag('BC:MODULE', "BCCompileModuleTag");
+$xml->RegisterTag('BC:IMG', "BCCompileImgTag");
+$xml->Compile();
+
/*
* Local Variables:
* mode: php
|
|
From: andi <bin...@li...> - 2001-09-21 18:17:34
|
andi Fri Sep 21 11:17:24 2001 EDT
Modified files:
/r2/binarycloud/base/lib BCTagParser.php
Log:
Cleared up and documented the parser.
Index: r2/binarycloud/base/lib/BCTagParser.php
diff -u r2/binarycloud/base/lib/BCTagParser.php:1.2 r2/binarycloud/base/lib/BCTagParser.php:1.3
--- r2/binarycloud/base/lib/BCTagParser.php:1.2 Fri Sep 21 08:34:35 2001
+++ r2/binarycloud/base/lib/BCTagParser.php Fri Sep 21 11:17:24 2001
@@ -1,49 +1,98 @@
-#!/usr/local/php4/bin/php -q
<?php
+// Header {{{
+/*
+ * -File $Id: BCTagParser.php,v 1.3 2001/09/21 18:17:24 andi Exp $
+ * -License LGPL (http://www.gnu.org/copyleft/lesser.html)
+ * -Copyright 2001, Thyrell
+ * -Authors Andreas Aderhold, <a.a...@th...>
+ */
-// these are the actual compiling methods.
-// They are registered at setup they must
-// return a sting with the compiled data,
-// or null in case of a compile error
-
-function BCCompileModuleTag($_tags) {
- // translating xml to php by using XMLUtils
- // this seems not to work for tag attributes
+$PACKAGE='binarycloud.lib';
- $trees = XMLUtils::XMLStr2XML($_tags);
- $xmlTree=$trees[0];
- $phpTree = XMLUtils::XML2PHP($xmlTree);
- $txtTree = XMLUtils::PHP2PHPArr($phpTree,false);
- var_dump($phpTree);
- return "Module Compiler output";
-}
+// }}}
+// {{{ BCTagParser
+/**
+ * BCTagParser class.
+ *
+ *
+ * @author Andreas Aderhold, a.a...@th...
+ * @version $Id: BCTagParser.php,v 1.3 2001/09/21 18:17:24 andi Exp $
+ */
-function BCCompileImgTag($_tag) {
- return "<img src=\"example_compliler_output.gif\">";
-}
-?>
-<?php
-// here we go
class BCTagParser {
/** Pool of all registered bc:foo tags and processor methods */
- var $knownTags = array(); // list of kown tags and their compiler methods
- var $infile = null; // source file
- var $outfile = null; // file to write back
-
+ var $knownTags = array();
+
+ /** source and destination files */
+ var $infile = null;
+ var $outfile = null;
+
+ // BCTagParser($_infile, $_outfile) {{{
+ /**
+ * Constructor. Set in und out file
+ *
+ * @param string filename of the sourcefile
+ * @param string filename of destination file
+ * @access public
+ * @author Andreas Aderhold, <a.a...@th...>
+ */
+
function BCTagParser($_infile, $_outfile) {
$this->infile = $_infile;
$this->outfile = $_outfile;
}
+
+ // }}}
+ // Compile() {{{
+ /**
+ * Compile. Assesor that starts the compiling process.
+ *
+ * @access public
+ * @author Andreas Aderhold, <a.a...@th...>
+ */
function Compile() {
$this->_CompileFile();
}
+ // }}}
+ // RegisterTag($_tag, $_processor) {{{
+ /**
+ * Register tag method. Registers a bc-style tag and the relating
+ * compiler function.
+ *
+ * i.e. $this->RegisterTag('BC:MODULE','BCCompileModuleTag');
+ *
+ * @access public
+ * @author Andreas Aderhold, <a.a...@th...>
+ */
+
function RegisterTag($_tag, $_processor) {
$this->knownTags[strtoupper($_tag)] = $_processor;
}
+ // }}}
+ // _CompileFile() {{{
+ /**
+ * Compiles file. Runs over the infile and looks for bc-style tags.
+ * Reads this tags to a buffer and builds an array with tag data
+ * and tag metadata (name, compiler handler, tagdata). Passes the
+ * tagdata to _CompileTag and replaces the tag with the compiled
+ * string in the outbuffer. Writes the outfile. The tagmeta array
+ * is in this form
+ *
+ * array(
+ * 'name' => 'BC:SOMETHING',
+ * 'handler' => 'BCCompileSomethingTag',
+ * 'data' => complete xml tag data
+ * );
+ *
+ * @return bool true/false upon success or failure
+ * @access public
+ * @author Andreas Aderhold, <a.a...@th...>
+ */
+
function _CompileFile() {
/* read file from disk */
@@ -70,7 +119,7 @@
}
}
}
- var_dump($tagData);
+
/* found at least one block, loop through and call compiler */
for ($i = 0; $i < count($tagData); $i++) {
echo "Compiling...\n";
@@ -78,19 +127,19 @@
}
/* write the new file to disk */
- $this->_Writefile($this->outfile, $source);
-
+ $success = $this->_Writefile($this->outfile, $source);
+ return $success;
}
-
- // method _CompileTag($_data) {{{
+ // }}}
+ // _CompileTag($_data) {{{
/**
- * This calls the xml parser providing the tag to parse. And retuns the
- * compiled tag.
+ * This calls the compiler function. And retuns the compiled tag.
*
- * @param $_tag string the xml tag to parse
+ * @param string $_tag the xml tag data to parse
* @access private
*/
+
function _CompileTag($_data) {
$tName =& $_data['name'];
@@ -116,14 +165,17 @@
}
// }}}
- // method _Readfile($_filename) {{{
-
+ // _Readfile($_filename) {{{
/**
- * private method for reading a file into an internal buffer
+ * Private method for reading a file into a buffer
*
- * @param $_filename string the file to read
- * @return string the contents of the file
+ * @param string $_filename the file to read
+ * @return string the contents of the file
+ * @access private
+ * @author Monte Ohrt <mo...@is...>
+ * @author Andrei Zmievski <an...@ph...>
*/
+
function _Readfile($_filename) {
if (!($fd = fopen($_filename, 'r'))) {
die("problem reading '$_filename.'");
@@ -136,16 +188,18 @@
}
// }}}
- // {{{
-
+ // _Writefile($_filename, $_contents) {{{
/**
* private method for writing a file from an internal buffer.
- * Method stolen from smarty ;-)
*
- * @param $_filename string the file to write
- * @param $_contents string the data to write
+ * @param string $_filename the filename to write
+ * @param string $_contents the data to write
* @return bool
+ * @access private
+ * @author Monte Ohrt <mo...@is...>
+ * @author Andrei Zmievski <an...@ph...>
*/
+
function _Writefile($_filename, $_contents) {
if (!($fd = fopen($_filename, 'w'))) {
@@ -160,17 +214,32 @@
}
return true;
}
+
+ // }}}
}
// }}}
-?>
-<?php
-// runngin the parser for demonstration
-include_once('./XMLUtils.php');
+
+
+// these are the actual compiling methods.
+// They are registered at setup they must
+// return a sting with the compiled data,
+// or null in case of a compile error
+// might be useful to swap them to another file
+
+function BCCompileModuleTag($_tags) {
+ // translating xml to php by using XMLUtils
+ // this seems not to work for tag attributes
-$xml =& new BCTagParser("data.html.xml", "data.html");
-$xml->RegisterTag('BC:MODULE', "BCCompileModuleTag");
-$xml->RegisterTag('BC:IMG', "BCCompileImgTag");
-$xml->Compile();
+ $trees = XMLUtils::XMLStr2XML($_tags);
+ $xmlTree = $trees[0];
+ $phpTree = XMLUtils::XML2PHP($xmlTree);
+ $txtTree = XMLUtils::PHP2PHPArr($phpTree,false);
+ return $txtTree;
+}
+
+function BCCompileImgTag($_tag) {
+ return "<img src=\"example_compliler_output.gif\">";
+}
/*
* Local Variables:
|
|
From: andi <bin...@li...> - 2001-09-26 10:57:23
|
andi Wed Sep 26 03:57:17 2001 EDT
Removed files:
/r2/binarycloud/base/lib BCTagParser.php
Log:
Don't need this
|