2 /****************************************************************/
4 /****************************************************************/
5 /* Copyright (c) 2002-2009 */
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /****************************************************************/
13 // $Id: WeblinksParser.class.php 8782 2009-09-04 17:33:07Z hwong $
17 * Class for parsing XML language info and returning a Weblink Object
21 class WeblinksParser {
23 var $parser; // the XML handler
24 var $character_data; // tmp variable for storing the data
25 var $element_path; // array of element paths (basically a stack)
26 var $title; //link's title
30 function WeblinksParser() {
31 $this->parser = xml_parser_create();
33 xml_set_object($this->parser, $this);
34 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); /* conform to W3C specs */
35 xml_set_element_handler($this->parser, 'startElement', 'endElement');
36 xml_set_character_data_handler($this->parser, 'characterData');
41 // @return true if parsed successfully, false otherwise
42 function parse($xml_data) {
43 $this->element_path = array();
44 $this->character_data = '';
45 xml_parse($this->parser, $xml_data, TRUE);
49 function startElement($parser, $name, $attributes) {
53 $this->url = $attributes['href'];
56 array_push($this->element_path, $name);
60 /* called when an element ends */
61 /* removed the current element from the $path */
62 function endElement($parser, $name) {
64 $current_pos = count($this->element_path) - 1;
65 $last_element = $this->element_path[$current_pos - 1];
69 $this->title = $this->character_data;
73 //pop stack and reset character data, o/w it will stack up
74 array_pop($this->element_path);
75 $this->character_data = '';
79 function characterData($parser, $data){
82 $this->character_data .= preg_replace('/[\t\0\x0B(\r\n)]*/', '', $data);
83 // $this->character_data .= trim($data);
90 xml_parser_free($this->parser);