|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php // $Id: _parse_proppatch.php,v 1.2 2010/12/14 17:36:02 moodlerobot Exp $ 00002 /* 00003 +----------------------------------------------------------------------+ 00004 | Copyright (c) 2002-2007 Christian Stocker, Hartmut Holzgraefe | 00005 | All rights reserved | 00006 | | 00007 | Redistribution and use in source and binary forms, with or without | 00008 | modification, are permitted provided that the following conditions | 00009 | are met: | 00010 | | 00011 | 1. Redistributions of source code must retain the above copyright | 00012 | notice, this list of conditions and the following disclaimer. | 00013 | 2. Redistributions in binary form must reproduce the above copyright | 00014 | notice, this list of conditions and the following disclaimer in | 00015 | the documentation and/or other materials provided with the | 00016 | distribution. | 00017 | 3. The names of the authors may not be used to endorse or promote | 00018 | products derived from this software without specific prior | 00019 | written permission. | 00020 | | 00021 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 00022 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 00023 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 00024 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 00025 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 00026 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 00027 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 00028 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 00029 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 00030 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | 00031 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 00032 | POSSIBILITY OF SUCH DAMAGE. | 00033 +----------------------------------------------------------------------+ 00034 */ 00035 00036 00044 class _parse_proppatch 00045 { 00052 var $success; 00053 00060 var $props; 00061 00068 var $depth; 00069 00076 var $mode; 00077 00084 var $current; 00085 00092 function _parse_proppatch($path) 00093 { 00094 $this->success = true; 00095 00096 $this->depth = 0; 00097 $this->props = array(); 00098 $had_input = false; 00099 00100 $f_in = fopen($path, "r"); 00101 if (!$f_in) { 00102 $this->success = false; 00103 return; 00104 } 00105 00106 $xml_parser = xml_parser_create_ns("UTF-8", " "); 00107 00108 xml_set_element_handler($xml_parser, 00109 array(&$this, "_startElement"), 00110 array(&$this, "_endElement")); 00111 00112 xml_set_character_data_handler($xml_parser, 00113 array(&$this, "_data")); 00114 00115 xml_parser_set_option($xml_parser, 00116 XML_OPTION_CASE_FOLDING, false); 00117 00118 while($this->success && !feof($f_in)) { 00119 $line = fgets($f_in); 00120 if (is_string($line)) { 00121 $had_input = true; 00122 $this->success &= xml_parse($xml_parser, $line, false); 00123 } 00124 } 00125 00126 if($had_input) { 00127 $this->success &= xml_parse($xml_parser, "", true); 00128 } 00129 00130 xml_parser_free($xml_parser); 00131 00132 fclose($f_in); 00133 } 00134 00144 function _startElement($parser, $name, $attrs) 00145 { 00146 if (strstr($name, " ")) { 00147 list($ns, $tag) = explode(" ", $name); 00148 if ($ns == "") 00149 $this->success = false; 00150 } else { 00151 $ns = ""; 00152 $tag = $name; 00153 } 00154 00155 if ($this->depth == 1) { 00156 $this->mode = $tag; 00157 } 00158 00159 if ($this->depth == 3) { 00160 $prop = array("name" => $tag); 00161 $this->current = array("name" => $tag, "ns" => $ns, "status"=> 200); 00162 if ($this->mode == "set") { 00163 $this->current["val"] = ""; // default set val 00164 } 00165 } 00166 00167 if ($this->depth >= 4) { 00168 $this->current["val"] .= "<$tag"; 00169 if (isset($attr)) { 00170 foreach ($attr as $key => $val) { 00171 $this->current["val"] .= ' '.$key.'="'.str_replace('"','"', $val).'"'; 00172 } 00173 } 00174 $this->current["val"] .= ">"; 00175 } 00176 00177 00178 00179 $this->depth++; 00180 } 00181 00190 function _endElement($parser, $name) 00191 { 00192 if (strstr($name, " ")) { 00193 list($ns, $tag) = explode(" ", $name); 00194 if ($ns == "") 00195 $this->success = false; 00196 } else { 00197 $ns = ""; 00198 $tag = $name; 00199 } 00200 00201 $this->depth--; 00202 00203 if ($this->depth >= 4) { 00204 $this->current["val"] .= "</$tag>"; 00205 } 00206 00207 if ($this->depth == 3) { 00208 if (isset($this->current)) { 00209 $this->props[] = $this->current; 00210 unset($this->current); 00211 } 00212 } 00213 } 00214 00223 function _data($parser, $data) 00224 { 00225 if (isset($this->current)) { 00226 $this->current["val"] .= $data; 00227 } 00228 } 00229 } 00230 00231 /* 00232 * Local variables: 00233 * tab-width: 4 00234 * c-basic-offset: 4 00235 * indent-tabs-mode:nil 00236 * End: 00237 */