Login   Register  
PHP Classes
elePHPant
Icontem

File: expatXBEL.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Bas Burger  >  expatXBEL  >  expatXBEL.php  >  Download  
File: expatXBEL.php
Role: ???
Content type: text/plain
Description: class file.
Class: expatXBEL
A simple expat based XBEL-file parser
Author: By
Last change:
Date: 12 years ago
Size: 8,961 bytes
 

Contents

Class file image Download
<?php

/*
* expatXBEL.php - expat based Xbel parser. Copyright (C) 2002  Bas Burger - Amsterdam.
*
* This program is free software; you can redistribute it and/or 
* modify it under the terms of the GNU General Public License 
* as published by the Free Software Foundation; either version 2 
* of the License, or (at your option) any later version. 
* 
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
* GNU General Public License for more details. 
* 
* You should have received a copy of the GNU General Public License 
* along with this program; if not, write to the Free Software 
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
* 
* Bas Burger [yaa_@_euronet_dot_nl] 
* http://tke.ath.cx
*
********************************************************
Use it as you please but mail me if you like it.
********************************************************	 


main input:   $xbel = new expatXBEL($xbelfile);

output:
			 				$xbel->Top				(string)top anchor holder
							$xbel->Content		(string)Content list | delimited
							$xbel->Output			(string)Output main list

	 
example calling script:
-----------------------------------------------
<?php

require 'expatXBEL.php';
	 
$xbelfile = 'example-xbel.xml';
$xbel = new expatXBEL($xbelfile);
	 
echo "
	 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
	 <html>
	 <head>
	 <title>XBEL Script</title>
	 <style>
	 body ,td {
	   scrollbar-3d-light-color : #FFFFFF;
	   scrollbar-arrow-color : #7B5584;
	   scrollbar-track-color : #DEDFBD;
	   scrollbar-base-color : #DEDFBD;
	   scrollbar-dark-shadow-color : #C0C0C0;
	   scrollbar-face-color : #C6BA9C;
	   scrollbar-highlight-color : #FFFFFF;
	   scrollbar-shadow-color : #050363;
	   background : #C6BA9C;
	 }
	 A {
   	 font:.8em Verdana, Arial, Helvetica, sans-serif;
  	 text-decoration: none;
	 }
	 A:LINK {
   	 color : #000000;
	 }
	 A:VISITED {
	   color : #000000;
	 }
	 A:ACTIVE {
     color : #000000;
	 }
	 A:HOVER {
     color : #FFF78C;
	 }
	 A:FOCUS {
     color : #000000;
	 }
	 h1 {
   		font:1.8em Verdana, Arial, Helvetica, sans-serif;
	 }
	 h2 {
  	 font:1.7em Verdana, Arial, Helvetica, sans-serif;
	 }
	 h3 {
  	 font:1.6em Verdana, Arial, Helvetica, sans-serif;
	 }
	 h4 {
   	 font:1.5em Verdana, Arial, Helvetica, sans-serif;
	 }
   h5 {
     font:1.4em Verdana, Arial, Helvetica, sans-serif;
   }
   h6 {
     font:1.3em Verdana, Arial, Helvetica, sans-serif;
   }
   h7 {
     font:1.2em Verdana, Arial, Helvetica, sans-serif;
	 }
	 #desc {
	 	padding: 0px 0px 10px 0px;
	  font-family : Arial, Helvetica, sans-serif;
	 }
	 #cont0 {
	 }
	 #cont1 {
	 }
	 #cont2 {
	 }
	 #cont3 {
	 }
	 #cont4 {
	 }
	 #cont5 {
	 }
	 #cont6 {
	 }
	 #cont7 {
	 }
	 #0 {
     background-color : #B5B6B5;
	 }
	 #1 {
     background-color : #C6BA9C;
	 }
	 #2 {
     background-color : #B5B6B5;
	 }
	 #3 {
     background-color : #C6BA9C;
	 }
	 #4 {
     background-color : #B5B6B5;
	 }
	 #5 {
     background-color : #B5B6B5;
	 }
	 #6 {
     background-color : #B5B6B5;
	 }
	 #7 {
     background-color : #B5B6B5;
	 }
	 </style>
	 </head>
";

echo "
	 <body>
";

// minimal error reporting from expat.
if(!$xbel->Error) {			 
	 // top anchor holder, you can place this also at
	 // bottom when the content is placed under the list
  echo "
	 $xbel->Top
	 
	 <table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"800\">
	 <tr>
	 	 <td align=\"center\">
		 <table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">
			<tr>
  ";



	// "$xbel->Content" is your content list
	// it is a single string with | concatenated
	// you can place it either above or under your
	// content list
	$acounter = 1;
	
	$tok = strtok($xbel->Content,"|");
	while ($tok) {
    if (preg_match ("/\bcont2\b/i", $tok)) {
		  echo '<td width=\"33%\">' . $tok . '</td>';
		  if ($acounter == 3) {
			  echo '</tr><tr>';
			  $acounter = 1;
			}
			else {
			  $acounter++;
			}
		}
    $tok = strtok("|");
  }

	// $xbel->Output is the main xbel list
	// just echo it to the stdout.
  echo "
			</tr>
		  </table>
		 </td>
	 </tr>
	 <tr>
		  <td align=\"center\">
			  $xbel->Output
			</td>
	 </tr>
	 </table>
  ";
}
else {
	die('Error in instantiating expatXBEL class: ' . $xbel->Error);
}

echo "	 
	 </body>
	 </html>
";
	 
?>
-----------------------------------------------
*/

class expatXBEL {

	// Public output containers.
	var $Top;
	var $Content;
	var $Output;
	var $Error;

	// Private properties
	var $xml_parser;
	var $_xbelfile;
	var $current_element;
	var $parent_element;
	var $parse_level = 1;
	var $foldercount = 0;
	
	function startElement($parser, $name, $attrs = array()){
		$this->parent_element = $this->current_element;
		$this->current_element = $name;
		switch($name) {
		case "XBEL":
			$this->Output .= '<table width="100%" id="' . $attrs['ID'] . '"><tr><td>';
			$this->Top .= '<a name="top"></a>';
			break;
		case "TITLE":
		  if ("BOOKMARK" != $this->parent_element) {
			  $this->Output .= '<a href="#top" name="' . $this->foldercount . '"><h'. $this->parse_level .'>';
				$this->Content .= '<span id="cont'. $this->parse_level .'"><a href="#' . $this->foldercount .'"><h'. $this->parse_level .'>'; 
			}
			break;
		case "DESC":
		  if ("BOOKMARK" != $this->parent_element) {
			    $this->Output .= '<div id="desc">';
			}
			break;
		case "INFO":
			break;
		case "METADATA":
			break;
		case "FOLDER":
		  $this->Output .= '<div id="' . $this->parse_level . '"><a name="' . $this->foldercount . '"></a><ul id="' . $attrs['ID'] . '">';
			$this->parse_level++;
			$this->foldercount++;
			break;
		case "SEPERATOR":
		  $this->Output .= '<br />';
			break;
		case "BOOKMARK":
		  $this->Output .= '<li><a id="' . $attrs['ID'] . '" href="' . $attrs['HREF'] . '" target="_blank">';
			break;
		case "ALIAS":
		  break;
		}
	}
	function characterData($parser, $data){
		switch($this->current_element){
			case "XBEL":
			  $this->Output .= trim($data);
				break;
			case "TITLE":
			  if ("BOOKMARK" != $this->parent_element) {
			    $this->Content .= trim($data);
				}
			  $this->Output .= trim($data);
				break;
			case "DESC":
			  if ("BOOKMARK" != $this->parent_element) {
			    $this->Output .= trim($data);
			  }
				break;
			case "INFO":
			  break;
			case "METADATA":
			  break;
			case "FOLDER":
			  $this->Output .= trim($data);
			  break;
			case "SEPERATOR":
			  break;
			case "BOOKMARK":
			  $this->Output .= trim($data);
			  break;
			case "ALIAS":
			  break;
		}
	}
	
  function endElement($parser, $name) {
	  switch($name){
		  case "ALIAS":
			  break;
		  case "BOOKMARK":
			  $this->Output .= '</a></li>';
			  break;
		  case "SEPERATOR":
			  $this->Output .= '<hr />';
			  break;
		  case "FOLDER":
			  $this->Output .= '</ul></div>';
				$this->parse_level--;
			  break;
		  case "METADATA":
			  break;
			case "INFO":
			  break;
		  case "DESC":
			  if ("BOOKMARK" != $this->parent_element) {
			    $this->Output .= '</div>';
			  }		  
			  break;
		  case "TITLE":
				if ("BOOKMARK" != $this->parent_element) {
			    $this->Output .= '</h'. $this->parse_level .'></a>';
			    $this->Content .= '</h'. $this->parse_level .'></a></span>|';
				}
				break;
		  case "XBEL":
			  $this->Output .= '</td></tr></table>';
				break;
	  }
		$this->current_element = $this->parent_element;
  }
		
		
	// Constructor. Pass it a filename.	
	function expatXBEL($filename) {
		if($this->_xbelfile = @file($filename, 1)){
			$this->xml_parser = xml_parser_create();
			xml_set_object($this->xml_parser, &$this);
			xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, true);
			xml_parser_set_option($this->xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
			xml_set_element_handler($this->xml_parser, "startElement", "endElement");
			xml_set_character_data_handler($this->xml_parser, "characterData");
			return $this->parse();
		} else {
			$this->Error = 'Could not fetch file';
			return false;
		}
	}

	function parse() {
		if(!count($this->_xbelfile)){
			$this->Error = "Empty or missing data.";
			return false;
		} else {
			while(list($line_num, $line) = each($this->_xbelfile)) {
				if(!xml_parse($this->xml_parser, ereg_replace('&', '&amp;', $line))) {
					$this->Error = "on line:" . $line_num ." ". xml_error_string(xml_get_error_code($this->xml_parser));
					return false;
				}
			}
			xml_parser_free($this->xml_parser);
			unset($this->_xbelfile);
		}
		return true;
	}
}

?>