_options = $options; $this->_parser = $init_parser; } ///////////////////////////////////// // actual job is done here function filter($content, $object) { require_once('XML/HTMLSax3.php'); if(!$this->_parser) $this->_parser = new XML_HTMLSax3(); $this->_parser->set_object($this); $this->_parser->set_element_handler('_doOpenTag', '_doCloseTag'); $this->_parser->set_data_handler('_doData'); $this->_parser->set_pi_handler('_doPITag'); $this->_parser->set_jasp_handler('_doJASPTag'); $this->_parser->set_escape_handler('_doEscape'); $this->_out_data = ''; $this->_parser->parse($content); return $this->_out_data; } ///////////////////////////////////// // privates function _doOpenTag(&$Parser, $TagName, $Attr, $Empty) { if(!$Empty) $this->_push_tag($TagName, $Attr); $this->doOpenTag($Parser, $TagName, $Attr, $Empty); } function _doCloseTag(&$Parser, $TagName, $Empty) { $this->doCloseTag($Parser, $TagName, $Empty); if(!$Empty) $this->_tag_close($TagName); } function _doData(&$Parser, $Data) { $this->doData($Parser, $Data); } function _doPITag(&$Parser, $Target, $Data) { $this->doPITag($Parser, $Target, $Data); } function _doJASPTag(&$Parser, $Data) { $this->doJASPTag($Parser, $Data); } function _doEscape(&$Parser, $Data) { $this->doEscape($Parser, $Data); } // utils function _makeAttributeString($Attr) { $result = array(); foreach($Attr as $k => $v){ if($v === NULL) $result[] = $k; else $result[] = "$k=\"$v\""; } return implode(' ', $result); } function _dump($str) { $this->_out_data .= $str; } // utils - stack support function _push_tag($tag_name, $attr) { $tag_name = strtolower($tag_name); $entry = array( 'tag' => $tag_name, 'attr' => $attr, ); $this->_stack[$this->_stack_index] = $entry; $this->_stack_index++; } function _pop_tag() { $result = NULL; if($this->_stack_index > 0){ $this->_stack_index--; $result = $this->_stack[$this->_stack_index]; } return $result; } function _tag_nest_count($tag_name) { $tag_name = strtolower($tag_name); $result = 0; for($i = 0; $i < $this->_stack_index; $i++) if($this->_stack[$i]['tag'] == $tag_name) $result++; return $result; } function _tag_close($tag_name) { $tag_name = strtolower($tag_name); $count = $this->_tag_nest_count($tag_name); if($count > 0){ do { $this->_stack_index--; } while($this->_stack[$this->_stack_index]['tag'] != $tag_name); return true; } else return false; } // abstract options support function setOption($name, $value = 1) { $this->_options[$name] = $value; } function getOption($name) { return $this->_options[$name]; } function removeOption($name) { $result = NULL; if(isset($this->_options[$name])){ $result = $this->_options[$name]; unset($this->_options[$name]); } return $result; } // public overrides function doOpenTag(&$Parser, $TagName, $Attr, $Empty) { $attrib_string = $this->_makeAttributeString($Attr); $this->_dump("<$TagName" . ($attrib_string? " $attrib_string": '') . ($Empty? '/>': '>')); } function doCloseTag(&$Parser, $TagName, $Empty) { if(!$Empty) $this->_dump(""); } function doData(&$Parser, $Data) { $this->_dump($Data); } function doPITag(&$Parser, $Target, $Data) { $this->_dump(""); } function doJASPTag(&$Parser, $Data) { $this->_dump("<%$Data%>"); } function doEscape(&$Parser, $Data) { $this->_dump(""); } } ?>