|
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
|