Location: PHPKode > scripts > PHP Lenex 2 > php-lenex-2/Lenex.reader.php
<?

require_once("LenexClasses/LenexDocument.class.php");

class LenexDummy {
}

class LenexReader {
	
	var $filename;
	var $parser;
	var $error;
	var $result=NULL;

	var $objectStack=NULL;
	var $level;
	
	var $collectionTags = array(
		"MEETS","SESSIONS","EVENTS","CLUBS","ATHLETES","RESULTS","SPLITS","RELAYPOSITIONS",
		"RECORDLISTS","TIMESTANDARDLISTS","TIMESTANDARDS","RELAYS","RECORDS","AGEGROUPS",
		"OFFICIALS",
	);
	
	function _startElement($parser, $name, $attrs) {
		
		if (in_array($name,$this->collectionTags)) {
			/* collection tags generate no objects */
			return;
		}
		
		if ($this->level>-1) $currentObj =& $this->objectStack[$this->level]; else $currentObj = NULL;
		$newObj = NULL;
		
		switch ($name) {
			case "SPLASH" : 
			case "LENEX" : 
			{
				$this->result =& LenexDocument::fromSAX($attrs);
				$newObj =& $this->result;
				break;
			}
			case "CONSTRUCTOR" : 
			case "CONTACT" : 
			case "MEET" :
			case "AGEDATE" :
			case "POINTTABLE" :
			case "POOL" :
			case "QUALIFY" :
			case "SESSION" :
			case "EVENT" :
			case "SWIMSTYLE" :
			case "FEE" :
			case "CLUB" :
			case "AGEGROUP" :
			case "ATHLETE" :
			case "RESULT" :
			case "SPLIT" :
			case "RELAY" :
			case "RELAYPOSITION" :
			case "TIMESTANDARDLIST" :
			case "TIMESTANDARD" :
			case "RECORDLIST" :
			case "RECORD" :
			case "MEETINFO" :
			case "OFFICIAL" :
			{
				eval("\$newObj =& Lenex".$name."::fromSAX(\$attrs);");
				$newObj->setParent($currentObj);
				break;
			}
			default : {
				trigger_error("$name not supported");
				$newObj =& new LenexDummy();
				break;
			}
		}
		
		$this->level++;
		$this->objectStack[$this->level] =& $newObj;
	}
		
	function _endElement($parser, $name) {

		if (in_array($name,$this->collectionTags)) {
			/* collection tags generate no objects */
			return;
		}

		$this->level--;
	}
		
	function _characterdata($parser, $data) {
	}

    function LenexReader($filename) {
        $this->filename=$filename;
        $this->parser = xml_parser_create();
        xml_set_object($this->parser, $this);
        xml_set_element_handler($this->parser, "_startElement", "_endElement");
        xml_set_character_data_handler($this->parser, "_characterdata");
    }

    function parse() {
	    $this->objectStack=array();
	    $this->level=-1;
	    $this->error = "";
	    
	    $fp=false;

		if (strrchr($this->filename,".")==".lef") {
			$fp = fopen($this->filename,"rb");
			$read_function = "fread";
		} else if (strrchr($this->filename,".")==".lxf") {
			if (function_exists("zip_open")) {
				if (($zip = zip_open($this->filename)) && ($zip_entry = zip_read($zip)) && (zip_entry_open($zip,$zip_entry,"rb"))) {
					$fp=$zip_entry;
					$read_function = "zip_entry_read";
				}
			} else if (class_exists("dUnzip2")) { //http://www.phpclasses.org/browse/package/2495.html
				$zip =& new dUnzip2($this->filename);
				$list = @$zip->getList();
				if (is_array($list)) {
					$lef = current($list);
					if (strrchr($lef["file_name"],".")==".lef") {
						$tmpfname = tempnam ("/tmp", "lenex.temp"); 
						$zip->unzip($lef["file_name"],$tmpfname);
						$fp = fopen($tmpfname,"rb");
						$read_function = "fread";
					} else {
					    trigger_error("zipped file is not .lef");
					    return;
					}
				} else {
				    trigger_error("reading $filename failed");
				    return;
				}
			} else {
			    trigger_error("no zip functions are available");
			    return;
			}
	    } else {
		    trigger_error("unknown extension in ".$this->filename);
		    return;
	    }
	    
	    if (!$fp) {
		    trigger_error("cannot read from ".$this->filename);
		    return;
	    }

        while (true) {
	        $txt = $read_function($fp,1024);
	        echo $txt;
	        $eof = ($txt===FALSE) || (strlen($txt)==0);
	        if (!xml_parse($this->parser, $txt, $eof)) {
			        $this->error = sprintf("XML error: %s on line %d",
			                    xml_error_string(xml_get_error_code($this->parser)),
			                    xml_get_current_line_number($this->parser));
					break;
			}
			if ($eof) break;
		}
	}
}

?>
Return current item: PHP Lenex 2