*/ /** * patConfiguration writer for XML files * * used by the patConfiguration object to write XML config files * * $Id: XML.php,v 1.1 2004/02/29 16:56:46 schst Exp $ * * @package patConfiguration * @subpackage Writer * @author Stephan Schmidt */ class patConfiguration_Writer_XML extends patConfiguration_Writer { /** * table used for translation of xml special chars * @var array $xmlSpecialchars */ var $xmlSpecialchars = array( "&" => "&", "'" => "'", "\"" => """, "<" => "<", ">" => ">" ); /** * open tags * @var array $openTags */ var $openTags = array(); /** * create an xml representation of the current config * * @access private * @param array $config config to serialize * @param array $options options for the serialization * @return string $content xml representation */ function serializeConfig( $config, $options ) { if( !isset( $options["mode"] ) ) $options["mode"] = "plain"; $this->openTags = array(); ksort( $config ); reset( $config ); if( $options["mode"] == "pretty" ) $options["nl"] = "\n"; else $options["nl"] = ""; $options["depth"] = 0; $xml = "configObj->encoding}\"?>\n"; $xml .= "".$options["nl"]; // add comment in pretty mode if( $options["mode"] == "pretty" ) { $xml .= "\t\n"; } $options["depth"]++; while( list( $key, $value ) = each( $config ) ) { $path = explode( ".", $key ); switch( count( $path ) ) { case 0: patErrorManager::raiseWarning( PATCONFIGURATION_WARNING_CONFIGVALUE_WITHOUT_NAME, "configValue without name found!" ); default: $openNew = array(); $tag = array_pop( $path ); $start = max( count( $path ), count( $this->openTags ) ); for( $i=( $start-1 ); $i>=0; $i-- ) { if( !isset( $this->openTags[$i] ) || $path[$i] != $this->openTags[$i] ) { if( count( $this->openTags ) > 0 ) { array_pop( $this->openTags ); $options["depth"]--; if( $options["mode"] == "pretty" ) $xml .= str_repeat( "\t", $options["depth"] ); $xml .= "".$options["nl"]; } if( isset( $path[$i] ) ) { array_push( $openNew, $path[$i] ); } } } while( $path = array_pop( $openNew ) ) { array_push( $this->openTags, $path ); $xml .= str_repeat( "\t", $options["depth"] ); $xml .= "".$options["nl"]; $options["depth"]++; } $xml .= $this->createTag( $tag, $value, $options ); break; } } // close all open tags while( $open = array_pop( $this->openTags ) ) { $options["depth"]--; $xml .= str_repeat( "\t", $options["depth"] ); $xml .= "".$options["nl"]; } $xml .= ""; return $xml; } /** * create configValue tag * * @access private * @param string $name name attribute of the tag * @param mixed $value value of the tag * @return string $tag xml representation of the tag */ function createTag( $name, $value, $options ) { $atts = array(); if( $name !== NULL ) $atts["name"] = $name; if( is_bool( $value ) ) { $atts["type"] = "bool"; $value = $value ? "true" : "false"; } elseif( is_float( $value ) ) $atts["type"] = "float"; elseif( is_int( $value ) ) $atts["type"] = "int"; elseif( is_array( $value ) ) $atts["type"] = "array"; elseif( is_string( $value ) ) $atts["type"] = "string"; $tag = ""; if( $options["mode"] == "pretty" ) $tag .= str_repeat( "\t", $options["depth"] ); $tag .= " $val ) { if( is_int( $key ) ) $key = NULL; $tag .= $this->createTag( $key, $val, $options ); } $options["depth"]--; if( $options["mode"] == "pretty" ) $tag .= str_repeat( "\t", $options["depth"] ); } else $tag .= $this->replaceXMLSpecialchars( $value ); $tag .= "".$options["nl"]; } return $tag; } /** * replace XML special chars * * @param string $string string, where special chars should be replaced * @param array $table table used for replacing * @return string $string string with replaced chars */ function replaceXMLSpecialchars( $string, $table = array() ) { if( empty( $table ) ) $table = &$this->xmlSpecialchars; $string = strtr( $string, $table ); return $string; } } ?>