XMLStream.php
Go to the documentation of this file.
00001 <?PHP 00002 00003 # 00004 # FILE: SPT--XMLStream.php 00005 # 00006 # METHODS PROVIDED: 00007 # XMLStream($StartingIndentLevel = 0, $IndentSize = 4) 00008 # - constructor 00009 # AddElement($Name = NULL, $Content = NULL, $Attributes = NULL) 00010 # - add element (i.e. tag) to XML stream 00011 # OpenElement($Name, $Attributes = NULL) 00012 # - open new element in XML stream 00013 # CloseElement() 00014 # - close current element in XML stream 00015 # GetContent() 00016 # - return current stream content 00017 # ClearContent() 00018 # - clear current stream content 00019 # IndentLevel($NewIndentLevel = NULL) 00020 # - get/set current indent level 00021 # IndentSize($NewIndentSize = NULL) 00022 # - get/set current indent size 00023 # 00024 # AUTHOR: Edward Almasy 00025 # 00026 # Part of the Scout Portal Toolkit 00027 # Copyright 2003-2004 Internet Scout Project 00028 # http://scout.wisc.edu 00029 # 00030 00031 class XMLStream { 00032 00033 # ---- PUBLIC INTERFACE -------------------------------------------------- 00034 00035 # object constructor 00036 function XMLStream($StartingIndentLevel = 0, $IndentSize = 4) 00037 { 00038 # initialize stack used to track open elements 00039 $this->OpenTagStack = array(); 00040 00041 # initialize current stream content 00042 $this->Stream = "<?xml version=\"1.0\"?>\n"; 00043 00044 # initialize current indent level and size 00045 $this->CurrentIndentLevel = $StartingIndentLevel; 00046 $this->CurrentIndentSize = $IndentSize; 00047 } 00048 00049 # add element (i.e. tag) to XML stream 00050 function AddElement($Name = SCOUTXMLSTREAMNULLVALUE, $Content = SCOUTXMLSTREAMNULLVALUE, $Attributes = SCOUTXMLSTREAMNULLVALUE) 00051 { 00052 # if tag name supplied 00053 if ($Name !== SCOUTXMLSTREAMNULLVALUE) 00054 { 00055 # start out with appropriate indent 00056 $Tag = str_repeat(" ", ($this->CurrentIndentLevel * $this->CurrentIndentSize)); 00057 00058 # open begin tag 00059 $Tag .= "<".$Name; 00060 00061 # if attributes supplied 00062 if ($Attributes !== SCOUTXMLSTREAMNULLVALUE) 00063 { 00064 # add attributes 00065 foreach ($Attributes as $AttributeName => $AttributeValue) 00066 { 00067 $Tag .= " ".$AttributeName."=\"".$AttributeValue."\""; 00068 } 00069 } 00070 00071 # if content supplied or we are assuming tag content 00072 if ($Content !== SCOUTXMLSTREAMNULLVALUE) 00073 { 00074 # close begin tag 00075 $Tag .= ">"; 00076 00077 # add content 00078 if ($Content !== NULL) 00079 { 00080 $Tag .= htmlspecialchars($Content); 00081 } 00082 00083 # add end tag 00084 $Tag .= "</".$Name.">\n"; 00085 } 00086 else 00087 { 00088 # close begin tag 00089 $Tag .= ">\n"; 00090 00091 # increase indent level 00092 $this->CurrentIndentLevel++; 00093 00094 # add tag to open tag stack 00095 array_push($this->OpenTagStack, $Name); 00096 } 00097 } 00098 else 00099 { 00100 # decrease indent level 00101 if ($this->CurrentIndentLevel > 0) { $this->CurrentIndentLevel--; } 00102 00103 # pop last entry off of open tag stack 00104 $LastName = array_pop($this->OpenTagStack); 00105 00106 # start out with appropriate indent 00107 $Tag = str_repeat(" ", ($this->CurrentIndentLevel * $this->CurrentIndentSize)); 00108 00109 # add end tag to match last open tag 00110 $Tag .= "</".$LastName.">\n"; 00111 } 00112 00113 # add formatted tag to stream 00114 $this->Stream .= $Tag; 00115 } 00116 00117 # open new element in XML stream 00118 function OpenElement($Name, $Attributes = SCOUTXMLSTREAMNULLVALUE) 00119 { 00120 $this->AddElement($Name, SCOUTXMLSTREAMNULLVALUE, $Attributes); 00121 } 00122 00123 # close current element in XML stream 00124 function CloseElement() 00125 { 00126 $this->AddElement(); 00127 } 00128 00129 # return current stream content 00130 function GetContent() 00131 { 00132 return $this->Stream; 00133 } 00134 00135 # clear current stream content 00136 function ClearContent() 00137 { 00138 $this->Stream = ""; 00139 } 00140 00141 # get/set current indent level 00142 function IndentLevel($NewIndentLevel = NULL) 00143 { 00144 # if new indent level supplied 00145 if ($NewIndentLevel !== NULL) 00146 { 00147 # reset indent to requested level 00148 $this->CurrentIndentLevel = $NewIndentLevel; 00149 } 00150 00151 # return current indent level to caller 00152 return $this->CurrentIndentLevel; 00153 } 00154 00155 # get/set current indent size 00156 function IndentSize($NewIndentSize = NULL) 00157 { 00158 # if new indent size supplied 00159 if ($NewIndentSize !== NULL) 00160 { 00161 # reset indent to requested size 00162 $this->CurrentIndentSize = $NewIndentSize; 00163 } 00164 00165 # return current indent size to caller 00166 return $this->CurrentIndentSize; 00167 } 00168 00169 00170 # ---- PRIVATE INTERFACE ------------------------------------------------- 00171 00172 var $CurrentIndentLevel; 00173 var $CurrentIndentSize; 00174 var $OpenTagStack; 00175 var $Stream; 00176 } 00177 00178 define("SCOUTXMLSTREAMNULLVALUE", "X-SCOUT_XML_STREAM_NULL_VALUE-X"); 00179 00180 00181 ?>