AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / include / classes / Weblinks / WeblinksParser.class.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2009                                                                              */
6 /* Adaptive Technology Resource Centre / University of Toronto  */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
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 $
14
15 /**
16 * WeblinksParser
17 * Class for parsing XML language info and returning a Weblink Object
18 * @access       public
19 * @author       Harris Wong
20 */
21 class WeblinksParser {
22         //private
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
27         var $url; //url
28
29         //constructor
30         function WeblinksParser() {
31                 $this->parser = xml_parser_create(); 
32
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');
37         }
38
39
40         // public
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);              
46         }
47
48         // private
49         function startElement($parser, $name, $attributes) {
50                 //save attributes.
51                 switch($name) {
52                         case 'url':
53                                 $this->url = $attributes['href'];
54                                 break;
55                 }
56                 array_push($this->element_path, $name);
57    }
58
59         // private
60         /* called when an element ends */
61         /* removed the current element from the $path */
62         function endElement($parser, $name) {
63                 //check element path
64                 $current_pos = count($this->element_path) - 1;
65                 $last_element = $this->element_path[$current_pos - 1];
66
67                 switch($name) {
68                         case 'title':
69                                 $this->title = $this->character_data;
70                                 break;
71                 }
72
73                 //pop stack and reset character data, o/w it will stack up
74                 array_pop($this->element_path);
75                 $this->character_data = '';
76         }
77
78         // private      
79         function characterData($parser, $data){
80                 global $addslashes;
81                 if (trim($data)!=''){
82                         $this->character_data .= preg_replace('/[\t\0\x0B(\r\n)]*/', '', $data);
83 //                      $this->character_data .= trim($data);
84                 }
85         }
86
87         //public
88         function close(){
89                 //Free the XML parser
90                 xml_parser_free($this->parser);
91         }
92
93         //gets
94         function getTitle(){
95                 return $this->title;
96         }
97         function getUrl(){
98                 return urldecode($this->url);
99         }
100 }
101
102 ?>