CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

XMLStream.php
Go to the documentation of this file.
1 <?PHP
2
3 #
4 # FILE: SPT--XMLStream.php
5 #
6 # METHODS PROVIDED:
7 # XMLStream($StartingIndentLevel = 0, $IndentSize = 4)
8 # - constructor
9 # AddElement($Name = NULL, $Content = NULL, $Attributes = NULL)
10 # - add element (i.e. tag) to XML stream
11 # OpenElement($Name, $Attributes = NULL)
12 # - open new element in XML stream
13 # CloseElement()
14 # - close current element in XML stream
15 # GetContent()
16 # - return current stream content
17 # ClearContent()
18 # - clear current stream content
19 # IndentLevel($NewIndentLevel = NULL)
20 # - get/set current indent level
21 # IndentSize($NewIndentSize = NULL)
22 # - get/set current indent size
23 #
24 # AUTHOR: Edward Almasy
25 #
26 # Part of the Scout Portal Toolkit
27 # Copyright 2003-2004 Internet Scout Project
28 # http://scout.wisc.edu
29 #
30
31 class XMLStream {
32
33 # ---- PUBLIC INTERFACE --------------------------------------------------
34
35 # object constructor
36 function XMLStream($StartingIndentLevel = 0, $IndentSize = 4)
37 {
38 # initialize stack used to track open elements
39 $this->OpenTagStack = array();
40
41 # initialize current stream content
42 $this->Stream = "<?xml version=\"1.0\"?>\n";
43
44 # initialize current indent level and size
45 $this->CurrentIndentLevel = $StartingIndentLevel;
46 $this->CurrentIndentSize = $IndentSize;
47 }
48
49 # add element (i.e. tag) to XML stream
50 function AddElement($Name = SCOUTXMLSTREAMNULLVALUE, $Content = SCOUTXMLSTREAMNULLVALUE, $Attributes = SCOUTXMLSTREAMNULLVALUE)
51 {
52 # if tag name supplied
53 if ($Name !== SCOUTXMLSTREAMNULLVALUE)
54 {
55 # start out with appropriate indent
56 $Tag = str_repeat(" ", ($this->CurrentIndentLevel * $this->CurrentIndentSize));
57
58 # open begin tag
59 $Tag .= "<".$Name;
60
61 # if attributes supplied
62 if ($Attributes !== SCOUTXMLSTREAMNULLVALUE)
63 {
64 # add attributes
65 foreach ($Attributes as $AttributeName => $AttributeValue)
66 {
67 $Tag .= " ".$AttributeName."=\"".$AttributeValue."\"";
68 }
69 }
70
71 # if content supplied or we are assuming tag content
72 if ($Content !== SCOUTXMLSTREAMNULLVALUE)
73 {
74 # close begin tag
75 $Tag .= ">";
76
77 # add content
78 if ($Content !== NULL)
79 {
80 $Tag .= htmlspecialchars($Content);
81 }
82
83 # add end tag
84 $Tag .= "</".$Name.">\n";
85 }
86 else
87 {
88 # close begin tag
89 $Tag .= ">\n";
90
91 # increase indent level
92 $this->CurrentIndentLevel++;
93
94 # add tag to open tag stack
95 array_push($this->OpenTagStack, $Name);
96 }
97 }
98 else
99 {
100 # decrease indent level
101 if ($this->CurrentIndentLevel > 0) { $this->CurrentIndentLevel--; }
102
103 # pop last entry off of open tag stack
104 $LastName = array_pop($this->OpenTagStack);
105
106 # start out with appropriate indent
107 $Tag = str_repeat(" ", ($this->CurrentIndentLevel * $this->CurrentIndentSize));
108
109 # add end tag to match last open tag
110 $Tag .= "</".$LastName.">\n";
111 }
112
113 # add formatted tag to stream
114 $this->Stream .= $Tag;
115 }
116
117 # open new element in XML stream
118 function OpenElement($Name, $Attributes = SCOUTXMLSTREAMNULLVALUE)
119 {
120 $this->AddElement($Name, SCOUTXMLSTREAMNULLVALUE, $Attributes);
121 }
122
123 # close current element in XML stream
124 function CloseElement()
125 {
126 $this->AddElement();
127 }
128
129 # return current stream content
130 function GetContent()
131 {
132 return $this->Stream;
133 }
134
135 # clear current stream content
136 function ClearContent()
137 {
138 $this->Stream = "";
139 }
140
141 # get/set current indent level
142 function IndentLevel($NewIndentLevel = NULL)
143 {
144 # if new indent level supplied
145 if ($NewIndentLevel !== NULL)
146 {
147 # reset indent to requested level
148 $this->CurrentIndentLevel = $NewIndentLevel;
149 }
150
151 # return current indent level to caller
152 return $this->CurrentIndentLevel;
153 }
154
155 # get/set current indent size
156 function IndentSize($NewIndentSize = NULL)
157 {
158 # if new indent size supplied
159 if ($NewIndentSize !== NULL)
160 {
161 # reset indent to requested size
162 $this->CurrentIndentSize = $NewIndentSize;
163 }
164
165 # return current indent size to caller
166 return $this->CurrentIndentSize;
167 }
168
169
170 # ---- PRIVATE INTERFACE -------------------------------------------------
171
172 var $CurrentIndentLevel;
173 var $CurrentIndentSize;
174 var $OpenTagStack;
175 var $Stream;
176 }
177
178 define("SCOUTXMLSTREAMNULLVALUE", "X-SCOUT_XML_STREAM_NULL_VALUE-X");
179
180
181 ?>