Mercurial > judyates
diff e2gallerypro/php/xmlparse.v4.php @ 3:3f6b44aa6b35 judyates
[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author | rlm |
---|---|
date | Mon, 22 Feb 2010 08:02:39 -0500 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/e2gallerypro/php/xmlparse.v4.php Mon Feb 22 08:02:39 2010 -0500 1.3 @@ -0,0 +1,435 @@ 1.4 +<?php 1.5 +/** 1.6 + This program is free software; you can redistribute it and/or modify 1.7 + it under the terms of the GNU General Public License as published by 1.8 + the Free Software Foundation; either version 2 of the License, or 1.9 + (at your option) any later version. 1.10 + 1.11 + This program is distributed in the hope that it will be useful, 1.12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 1.13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.14 + GNU General Public License for more details. 1.15 + 1.16 + For Support, please visit http://www.criticaldevelopment.net/xml/ 1.17 +*/ 1.18 + 1.19 +/** 1.20 + * XML Parser Class (php4) 1.21 + * 1.22 + * Parses an XML document into an object structure much like the SimpleXML extension. 1.23 + * 1.24 + * @author Adam A. Flynn <adamaflynn@criticaldevelopment.net> 1.25 + * @copyright Copyright (c) 2005-2007, Adam A. Flynn 1.26 + * 1.27 + * @version 1.3.0 1.28 + */ 1.29 +class XMLParser 1.30 +{ 1.31 + /** 1.32 + * The XML parser 1.33 + * 1.34 + * @var resource 1.35 + */ 1.36 + var $parser; 1.37 + 1.38 + /** 1.39 + * The XML document 1.40 + * 1.41 + * @var string 1.42 + */ 1.43 + var $xml; 1.44 + 1.45 + /** 1.46 + * Document tag 1.47 + * 1.48 + * @var object 1.49 + */ 1.50 + var $document; 1.51 + 1.52 + /** 1.53 + * Current object depth 1.54 + * 1.55 + * @var array 1.56 + */ 1.57 + var $stack; 1.58 + /** 1.59 + * Whether or not to replace dashes and colons in tag 1.60 + * names with underscores. 1.61 + * 1.62 + * @var bool 1.63 + */ 1.64 + var $cleanTagNames; 1.65 + 1.66 + 1.67 + /** 1.68 + * Constructor. Loads XML document. 1.69 + * 1.70 + * @param string $xml The string of the XML document 1.71 + * @return XMLParser 1.72 + */ 1.73 + function XMLParser($xml = '', $cleanTagNames = true) 1.74 + { 1.75 + //Load XML document 1.76 + $this->xml = $xml; 1.77 + 1.78 + // Set stack to an array 1.79 + $this->stack = array(); 1.80 + 1.81 + //Set whether or not to clean tag names 1.82 + $this->cleanTagNames = $cleanTagNames; 1.83 + } 1.84 + 1.85 + /** 1.86 + * Initiates and runs PHP's XML parser 1.87 + */ 1.88 + function Parse() 1.89 + { 1.90 + //Create the parser resource 1.91 + $this->parser = xml_parser_create(); 1.92 + 1.93 + //Set the handlers 1.94 + xml_set_object($this->parser, $this); 1.95 + xml_set_element_handler($this->parser, 'StartElement', 'EndElement'); 1.96 + xml_set_character_data_handler($this->parser, 'CharacterData'); 1.97 + 1.98 + //Error handling 1.99 + if (!xml_parse($this->parser, $this->xml)) 1.100 + $this->HandleError(xml_get_error_code($this->parser), xml_get_current_line_number($this->parser), xml_get_current_column_number($this->parser)); 1.101 + 1.102 + //Free the parser 1.103 + xml_parser_free($this->parser); 1.104 + } 1.105 + 1.106 + /** 1.107 + * Handles an XML parsing error 1.108 + * 1.109 + * @param int $code XML Error Code 1.110 + * @param int $line Line on which the error happened 1.111 + * @param int $col Column on which the error happened 1.112 + */ 1.113 + function HandleError($code, $line, $col) 1.114 + { 1.115 + trigger_error('XML Parsing Error at '.$line.':'.$col.'. Error '.$code.': '.xml_error_string($code)); 1.116 + } 1.117 + 1.118 + 1.119 + /** 1.120 + * Gets the XML output of the PHP structure within $this->document 1.121 + * 1.122 + * @return string 1.123 + */ 1.124 + function GenerateXML() 1.125 + { 1.126 + return $this->document->GetXML(); 1.127 + } 1.128 + 1.129 + /** 1.130 + * Gets the reference to the current direct parent 1.131 + * 1.132 + * @return object 1.133 + */ 1.134 + function GetStackLocation() 1.135 + { 1.136 + $return = ''; 1.137 + 1.138 + foreach($this->stack as $stack) 1.139 + $return .= $stack.'->'; 1.140 + 1.141 + return rtrim($return, '->'); 1.142 + } 1.143 + 1.144 + /** 1.145 + * Handler function for the start of a tag 1.146 + * 1.147 + * @param resource $parser 1.148 + * @param string $name 1.149 + * @param array $attrs 1.150 + */ 1.151 + function StartElement($parser, $name, $attrs = array()) 1.152 + { 1.153 + //Make the name of the tag lower case 1.154 + $name = strtolower($name); 1.155 + 1.156 + //Check to see if tag is root-level 1.157 + if (count($this->stack) == 0) 1.158 + { 1.159 + //If so, set the document as the current tag 1.160 + $this->document = new XMLTag($name, $attrs); 1.161 + 1.162 + //And start out the stack with the document tag 1.163 + $this->stack = array('document'); 1.164 + } 1.165 + //If it isn't root level, use the stack to find the parent 1.166 + else 1.167 + { 1.168 + //Get the name which points to the current direct parent, relative to $this 1.169 + $parent = $this->GetStackLocation(); 1.170 + 1.171 + //Add the child 1.172 + eval('$this->'.$parent.'->AddChild($name, $attrs, '.count($this->stack).', $this->cleanTagNames);'); 1.173 + 1.174 + //If the cleanTagName feature is on, replace colons and dashes with underscores 1.175 + if($this->cleanTagNames) 1.176 + $name = str_replace(array(':', '-'), '_', $name); 1.177 + 1.178 + 1.179 + //Update the stack 1.180 + eval('$this->stack[] = $name.\'[\'.(count($this->'.$parent.'->'.$name.') - 1).\']\';'); 1.181 + } 1.182 + } 1.183 + 1.184 + /** 1.185 + * Handler function for the end of a tag 1.186 + * 1.187 + * @param resource $parser 1.188 + * @param string $name 1.189 + */ 1.190 + function EndElement($parser, $name) 1.191 + { 1.192 + //Update stack by removing the end value from it as the parent 1.193 + array_pop($this->stack); 1.194 + } 1.195 + 1.196 + /** 1.197 + * Handler function for the character data within a tag 1.198 + * 1.199 + * @param resource $parser 1.200 + * @param string $data 1.201 + */ 1.202 + function CharacterData($parser, $data) 1.203 + { 1.204 + //Get the reference to the current parent object 1.205 + $tag = $this->GetStackLocation(); 1.206 + 1.207 + //Assign data to it 1.208 + eval('$this->'.$tag.'->tagData .= trim($data);'); 1.209 + } 1.210 +} 1.211 + 1.212 + 1.213 +/** 1.214 + * XML Tag Object (php4) 1.215 + * 1.216 + * This object stores all of the direct children of itself in the $children array. They are also stored by 1.217 + * type as arrays. So, if, for example, this tag had 2 <font> tags as children, there would be a class member 1.218 + * called $font created as an array. $font[0] would be the first font tag, and $font[1] would be the second. 1.219 + * 1.220 + * To loop through all of the direct children of this object, the $children member should be used. 1.221 + * 1.222 + * To loop through all of the direct children of a specific tag for this object, it is probably easier 1.223 + * to use the arrays of the specific tag names, as explained above. 1.224 + * 1.225 + * @author Adam A. Flynn <adamaflynn@criticaldevelopment.net> 1.226 + * @copyright Copyright (c) 2005-2007, Adam A. Flynn 1.227 + * 1.228 + * @version 1.3.0 1.229 + */ 1.230 +class XMLTag 1.231 +{ 1.232 + /** 1.233 + * Array with the attributes of this XML tag 1.234 + * 1.235 + * @var array 1.236 + */ 1.237 + var $tagAttrs; 1.238 + 1.239 + /** 1.240 + * The name of the tag 1.241 + * 1.242 + * @var string 1.243 + */ 1.244 + var $tagName; 1.245 + 1.246 + /** 1.247 + * The data the tag contains 1.248 + * 1.249 + * So, if the tag doesn't contain child tags, and just contains a string, it would go here 1.250 + * 1.251 + * @var string 1.252 + */ 1.253 + var $tagData; 1.254 + 1.255 + /** 1.256 + * Array of references to the objects of all direct children of this XML object 1.257 + * 1.258 + * @var array 1.259 + */ 1.260 + var $tagChildren; 1.261 + 1.262 + /** 1.263 + * The number of parents this XML object has (number of levels from this tag to the root tag) 1.264 + * 1.265 + * Used presently only to set the number of tabs when outputting XML 1.266 + * 1.267 + * @var int 1.268 + */ 1.269 + var $tagParents; 1.270 + 1.271 + /** 1.272 + * Constructor, sets up all the default values 1.273 + * 1.274 + * @param string $name 1.275 + * @param array $attrs 1.276 + * @param int $parents 1.277 + * @return XMLTag 1.278 + */ 1.279 + function XMLTag($name, $attrs = array(), $parents = 0) 1.280 + { 1.281 + //Make the keys of the attr array lower case, and store the value 1.282 + $this->tagAttrs = array_change_key_case($attrs, CASE_LOWER); 1.283 + 1.284 + //Make the name lower case and store the value 1.285 + $this->tagName = strtolower($name); 1.286 + 1.287 + //Set the number of parents 1.288 + $this->tagParents = $parents; 1.289 + 1.290 + //Set the types for children and data 1.291 + $this->tagChildren = array(); 1.292 + $this->tagData = ''; 1.293 + } 1.294 + 1.295 + /** 1.296 + * Adds a direct child to this object 1.297 + * 1.298 + * @param string $name 1.299 + * @param array $attrs 1.300 + * @param int $parents 1.301 + * @param bool $cleanTagName 1.302 + */ 1.303 + function AddChild($name, $attrs, $parents, $cleanTagName = true) 1.304 + { 1.305 + //If the tag is a reserved name, output an error 1.306 + if(in_array($name, array('tagChildren', 'tagAttrs', 'tagParents', 'tagData', 'tagName'))) 1.307 + { 1.308 + trigger_error('You have used a reserved name as the name of an XML tag. Please consult the documentation (http://www.criticaldevelopment.net/xml/) and rename the tag named "'.$name.'" to something other than a reserved name.', E_USER_ERROR); 1.309 + 1.310 + return; 1.311 + } 1.312 + 1.313 + //Create the child object itself 1.314 + $child = new XMLTag($name, $attrs, $parents); 1.315 + 1.316 + //If the cleanTagName feature is on, replace colons and dashes with underscores 1.317 + if($cleanTagName) 1.318 + $name = str_replace(array(':', '-'), '_', $name); 1.319 + 1.320 + //Toss up a notice if someone's trying to to use a colon or dash in a tag name 1.321 + elseif(strstr($name, ':') || strstr($name, '-')) 1.322 + trigger_error('Your tag named "'.$name.'" contains either a dash or a colon. Neither of these characters are friendly with PHP variable names, and, as such, they cannot be accessed and will cause the parser to not work. You must enable the cleanTagName feature (pass true as the second argument of the XMLParser constructor). For more details, see http://www.criticaldevelopment.net/xml/', E_USER_ERROR); 1.323 + 1.324 + //If there is no array already set for the tag name being added, 1.325 + //create an empty array for it 1.326 + if(!isset($this->$name)) 1.327 + $this->$name = array(); 1.328 + 1.329 + //Add the reference of it to the end of an array member named for the tag's name 1.330 + $this->{$name}[] =& $child; 1.331 + 1.332 + //Add the reference to the children array member 1.333 + $this->tagChildren[] =& $child; 1.334 + } 1.335 + 1.336 + /** 1.337 + * Returns the string of the XML document which would be generated from this object 1.338 + * 1.339 + * This function works recursively, so it gets the XML of itself and all of its children, which 1.340 + * in turn gets the XML of all their children, which in turn gets the XML of all thier children, 1.341 + * and so on. So, if you call GetXML from the document root object, it will return a string for 1.342 + * the XML of the entire document. 1.343 + * 1.344 + * This function does not, however, return a DTD or an XML version/encoding tag. That should be 1.345 + * handled by XMLParser::GetXML() 1.346 + * 1.347 + * @return string 1.348 + */ 1.349 + function GetXML() 1.350 + { 1.351 + //Start a new line, indent by the number indicated in $this->parents, add a <, and add the name of the tag 1.352 + $out = "\n".str_repeat("\t", $this->tagParents).'<'.$this->tagName; 1.353 + 1.354 + //For each attribute, add attr="value" 1.355 + foreach($this->tagAttrs as $attr => $value) 1.356 + $out .= ' '.$attr.'="'.$value.'"'; 1.357 + 1.358 + //If there are no children and it contains no data, end it off with a /> 1.359 + if(empty($this->tagChildren) && empty($this->tagData)) 1.360 + $out .= " />"; 1.361 + 1.362 + //Otherwise... 1.363 + else 1.364 + { 1.365 + //If there are children 1.366 + if(!empty($this->tagChildren)) 1.367 + { 1.368 + //Close off the start tag 1.369 + $out .= '>'; 1.370 + 1.371 + //For each child, call the GetXML function (this will ensure that all children are added recursively) 1.372 + foreach($this->tagChildren as $child) 1.373 + { 1.374 + if(is_object($child)) 1.375 + $out .= $child->GetXML(); 1.376 + } 1.377 + 1.378 + //Add the newline and indentation to go along with the close tag 1.379 + $out .= "\n".str_repeat("\t", $this->tagParents); 1.380 + } 1.381 + 1.382 + //If there is data, close off the start tag and add the data 1.383 + elseif(!empty($this->tagData)) 1.384 + $out .= '>'.$this->tagData; 1.385 + 1.386 + //Add the end tag 1.387 + $out .= '</'.$this->tagName.'>'; 1.388 + } 1.389 + 1.390 + //Return the final output 1.391 + return $out; 1.392 + } 1.393 + 1.394 + /** 1.395 + * Deletes this tag's child with a name of $childName and an index 1.396 + * of $childIndex 1.397 + * 1.398 + * @param string $childName 1.399 + * @param int $childIndex 1.400 + */ 1.401 + function Delete($childName, $childIndex = 0) 1.402 + { 1.403 + //Delete all of the children of that child 1.404 + $this->{$childName}[$childIndex]->DeleteChildren(); 1.405 + 1.406 + //Destroy the child's value 1.407 + $this->{$childName}[$childIndex] = null; 1.408 + 1.409 + //Remove the child's name from the named array 1.410 + unset($this->{$childName}[$childIndex]); 1.411 + 1.412 + //Loop through the tagChildren array and remove any null 1.413 + //values left behind from the above operation 1.414 + for($x = 0; $x < count($this->tagChildren); $x ++) 1.415 + { 1.416 + if(is_null($this->tagChildren[$x])) 1.417 + unset($this->tagChildren[$x]); 1.418 + } 1.419 + } 1.420 + 1.421 + /** 1.422 + * Removes all of the children of this tag in both name and value 1.423 + */ 1.424 + function DeleteChildren() 1.425 + { 1.426 + //Loop through all child tags 1.427 + for($x = 0; $x < count($this->tagChildren); $x ++) 1.428 + { 1.429 + //Do this recursively 1.430 + $this->tagChildren[$x]->DeleteChildren(); 1.431 + 1.432 + //Delete the name and value 1.433 + $this->tagChildren[$x] = null; 1.434 + unset($this->tagChildren[$x]); 1.435 + } 1.436 + } 1.437 +} 1.438 +?> 1.439 \ No newline at end of file