Location: PHPKode > scripts > Sax Filters > sax-filters/class_sax_filters.php
<?
// ##################################################################################
// Title                     : Sax Filters
// Version                   : 1.0
// Author                    : Luis Argerich (hide@address.com)
// Last modification date    : 05-20-2002
// Description               : A set of classes defining interfaces for generic SAX
//                             parsers as well as SAX filters.
// ##################################################################################
// History: 
// 05-15-2002                : First version of this class.
// ##################################################################################
// To-Dos:
// Improve the Expat SAX parser class to process PI's and other not-so-common
// XML nodes.
// ##################################################################################
// How to use it:
// Read the documentation in sax_filters.html
// ##################################################################################

// CHECK FOR DOUBLE DEFINITION HERE
if(defined("_class_sax_filters_is_included")) {
  // do nothing since the class is already included  
} else {
  define("_class_sax_filters_is_included",1); 


class AbstractStreamer {
  function stream($data) {}
}

class SimpleStreamer extends AbstractStreamer {
  function stream($data) {
      print($data);
  }
}

class AbstractSAXParser {
  var $listener;
  function AbstractSAXParser() {}
  function parserSetoption($opt,$val) {}
  function setListener($obj) {
    $this->listener=$obj;
  }
  function startElementHandler($parser,$name,$attribs) {
    $this->listener->startElementHandler($name,$attribs);
  }
  function endElementHandler($parser,$name) {
    $this->listener->endElementHandler($name);
  }
  function characterDataHandler($parser,$data) {
    $this->listener->characterDataHandler($data);
  }
  function parse() {}
}

class AbstractFilter {
  var $listener;

  function  setListener($obj) {
    $this->listener=$obj;
  }
  function startElementHandler($name,$attribs) {}
  function endElementHandler($name) {}
  function characterDataHandler($data) {}

}

class ExpatParser extends AbstractSAXParser {
  var $parser;
  var $filename;
  var $buffer;
  var $error_string;
  var $line;

  function ExpatParser($xmlfile) {
    $this->filename=$xmlfile;
    $this->parser=xml_parser_create();
    $this->buffer=4096;
    xml_set_object($this->parser,&$this);
    xml_set_element_handler($this->parser,"startElementHandler","endElementHandler");
    xml_set_character_data_handler($this->parser,"characterDataHandler");
  }

  function parserSetOption($opt,$val) {
    return xml_parser_set_option ( $this->parser, $opt, $val);
  }

  function parse() {
    if (!($fp = fopen($this->filename, "r"))) {
       return 0;
    }

    while ($data = fread($fp, $this->buffer)) {
      if (!xml_parse($this->parser, $data, feof($fp))) {
                    $this->error_string=xml_error_string(xml_get_error_code($xml_parser));
                    $this->line=xml_get_current_line_number($xml_parser);
                    die("Error: ".$this->error_string." on ".$this->line);
      }
    }
    xml_parser_free($this->parser);
    return 1;
  }
}



class FilterName extends AbstractFilter {
  var $flag=0;

  function startElementHandler($name,$attribs) {
    if(strtolower($name)=="name") {
      $this->flag=1;
    } else {
      $this->flag=0;
    }
    $this->listener->startElementHandler($name,$attribs);
  }

  function endElementHandler($name) {
    if(strtolower($name)=="name") {
      $this->flag=0;
    }
    $this->listener->endElementHandler($name);
  }

   function characterDataHandler($data) {
     if($this->flag) {
       $data=strtoupper($data);
     }
     $this->listener->characterDataHandler($data);
   }

}

class FilterNameBold extends AbstractFilter {
  var $flag=0;

  function startElementHandler($name,$attribs) {
    if(strtolower($name)=="name") {
      $this->flag=1;
    } else {
      $this->flag=0;
    }
    $this->listener->startElementHandler($name,$attribs);
    if($this->flag) {$this->listener->startElementHandler("b",array());}
  }

  function endElementHandler($name) {
    if(strtolower($name)=="name") {
      $this->flag=0;
      $this->listener->endElementHandler("b");
    }
    $this->listener->endElementHandler($name);

  }

   function characterDataHandler($data) {
     if($this->flag) {
       $data=strtoupper($data);
     }
     $this->listener->characterDataHandler($data);
   }

}

class FilterStreamer extends AbstractFilter {
   var $stream;

   function FilterStreamer($obj) {
     $this->stream=$obj;
   }

   function startElementHandler($name,$attribs) {
      $this->stream->stream("<$name");
      foreach($attribs as $key => $val) {
        $this->stream->stream(" $key='$val'");
      }
      $this->stream->stream(">");
   }

   function endElementHandler($name) {
      $this->stream->stream("</$name>");
   }

   function characterDataHandler($data) {
      $this->stream->stream("$data");
   }
}

class FilterOutput extends AbstractFilter {

   function startElementHandler($name,$attribs) {
     print("&lt;$name");
     foreach($attribs as $key => $val) {
       print(" $key='$val'");
     }
     print("&gt;<br />");
   }

   function endElementHandler($name) {
     print("&lt;/$name&gt;<br />");
   }

   function characterDataHandler($data) {
     print("$data");
   }

}


?>

<?
}
?>
         
Return current item: Sax Filters