1 |
<?php |
2 |
|
3 |
/** |
4 |
* Holds the XMLTagClass |
5 |
* |
6 |
* $Id: XMLTagClass.inc,v 1.33 2003/07/31 23:06:53 hemna Exp $ |
7 |
* |
8 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
9 |
* @package phpHtmlLib |
10 |
* |
11 |
* @copyright LGPL - See LICENCE |
12 |
* |
13 |
*/ |
14 |
|
15 |
/** |
16 |
* make sure we include the parent class |
17 |
*/ |
18 |
require_once( $phphtmllib."/ContainerClass.inc" ); |
19 |
|
20 |
|
21 |
/** |
22 |
* |
23 |
* This class is used for building and rendering |
24 |
* an XML tag. |
25 |
* |
26 |
* This class is the base class for the |
27 |
* HTMLTagClass. |
28 |
* |
29 |
* This is part of the phphtmllib libraries |
30 |
* released under the LGPL license. |
31 |
* |
32 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
33 |
* @package phpHtmlLib |
34 |
*/ |
35 |
class XMLTagClass extends Container { |
36 |
|
37 |
|
38 |
/** |
39 |
* Tag definition for class. |
40 |
* subclass defines this value. |
41 |
* ie var $tag = "<TABLE>"; |
42 |
* @var string |
43 |
* @access private |
44 |
*/ |
45 |
var $_tag = ""; |
46 |
|
47 |
/** |
48 |
* Tag attributes as an array |
49 |
* of name value pairs. |
50 |
* ie $attributes["cellspacing"]=2; |
51 |
* @var array |
52 |
* @access private |
53 |
*/ |
54 |
var $_attributes = array(); |
55 |
|
56 |
|
57 |
/** |
58 |
* The constructor |
59 |
* |
60 |
* @param string - the tag name |
61 |
* @param array - the attributes array |
62 |
* can be in name => value |
63 |
* or just value |
64 |
* @param mixed - n items of content to |
65 |
* add |
66 |
* |
67 |
*/ |
68 |
function XMLTagClass($name, $attributes=array() ) { |
69 |
$this->set_tag_name( $name ); |
70 |
$this->set_tag_attributes( $attributes ); |
71 |
|
72 |
$num_args = func_num_args(); |
73 |
for ($i=2;$i<$num_args;$i++) { |
74 |
$this->add(func_get_arg($i)); |
75 |
} |
76 |
|
77 |
$this->_set_flags(); |
78 |
} |
79 |
|
80 |
|
81 |
/** |
82 |
* This function is responsible |
83 |
* for rendering the tag and |
84 |
* its contents |
85 |
* |
86 |
* @param int - the current indentation |
87 |
* level for the tag |
88 |
*/ |
89 |
function render( $indent_level=0 ) { |
90 |
|
91 |
//try and guess the indentation flags |
92 |
//based on the data |
93 |
$this->_prepare_flags(); |
94 |
|
95 |
//return $xml; |
96 |
return $this->_render_open_tag( $indent_level ) . |
97 |
$this->_render_content( $indent_level ) . |
98 |
$this->_render_close_tag( $indent_level ); |
99 |
} |
100 |
|
101 |
|
102 |
/****************************************/ |
103 |
/* Some helper routines for building */ |
104 |
/* pieces of the xml tag, its */ |
105 |
/* attributes and PCDATA */ |
106 |
/****************************************/ |
107 |
|
108 |
/** |
109 |
* This method is used to set the bitmask |
110 |
* flags for this tag. It tells the |
111 |
* class how to render the tag. |
112 |
* |
113 |
* NOTE: the child class can override this |
114 |
* to set the options |
115 |
*/ |
116 |
function _set_flags() { |
117 |
parent::_set_flags(); |
118 |
$this->_flags |= _NEWLINEAFTEROPENTAG | _NEWLINEAFTERCLOSETAG | |
119 |
_CONTENTREQUIRED | _CLOSETAGREQUIRED; |
120 |
} |
121 |
|
122 |
/** |
123 |
* This method sets the name of the tag |
124 |
* |
125 |
* @param string - the tag name |
126 |
*/ |
127 |
function set_tag_name( $name ) { |
128 |
$this->_tag = $name; |
129 |
} |
130 |
|
131 |
/** |
132 |
* This method gets the name of the tag |
133 |
* |
134 |
* @return string - the tag name |
135 |
*/ |
136 |
function get_tag_name() { |
137 |
return $this->_tag; |
138 |
} |
139 |
|
140 |
/** |
141 |
* This returns the tag declared for this class. |
142 |
* This should be used in favor of |
143 |
* accessing the $this->_tag directly. |
144 |
* |
145 |
* @return string - the _tag var for this class. |
146 |
*/ |
147 |
function get_tag() { |
148 |
//for compatibility only |
149 |
return $this->get_tag_name(); |
150 |
} |
151 |
|
152 |
/** |
153 |
* add a single attribute (name="value") |
154 |
* @param string $name attribute name |
155 |
* @param mixed $value the value. |
156 |
* @access public |
157 |
*/ |
158 |
function set_tag_attribute( $name, $value=NULL ) { |
159 |
$this->_attributes[$name] = $value; |
160 |
} |
161 |
|
162 |
/** |
163 |
* add multiple attributes (name="value") |
164 |
* @param array $attributes Associative array of name="value" pairs of |
165 |
* tag atributes. |
166 |
* ie array("border"=>"0", "class"=>"hover"); |
167 |
* @access public |
168 |
*/ |
169 |
function set_tag_attributes( $attributes=array() ) { |
170 |
$this->_attributes = array_merge($this->_attributes, $attributes); |
171 |
} |
172 |
|
173 |
/** |
174 |
* clear all attributes and start with new attributes |
175 |
* @param array $attributes Associative array of name="value" pairs of |
176 |
* tag atributes. |
177 |
* ie array("border"=>"0", "class"=>"hover"); |
178 |
* @access public |
179 |
*/ |
180 |
function reset_attributes( $attributes=array() ) { |
181 |
$this->_attributes = array(); |
182 |
$this->set_tag_attributes( $attributes ); |
183 |
} |
184 |
|
185 |
/** |
186 |
* get the nth element from content array |
187 |
* @param int $cell the cell to get |
188 |
* @return mixed |
189 |
*/ |
190 |
function _get_element( $cell ) { |
191 |
return $this->_content[$cell]; |
192 |
} |
193 |
|
194 |
|
195 |
|
196 |
//**************************************************************** |
197 |
// Misc functions |
198 |
//**************************************************************** |
199 |
|
200 |
/** |
201 |
* set the newline_after_opentag flag |
202 |
* @param boolean $flag TRUE or FALSE |
203 |
*/ |
204 |
function set_newline_after_opentag( $flag ) { |
205 |
if ($flag) { |
206 |
$this->_flags |= _NEWLINEAFTEROPENTAG; |
207 |
} else{ |
208 |
$this->_flags &= ~_NEWLINEAFTEROPENTAG; |
209 |
} |
210 |
} |
211 |
|
212 |
/** |
213 |
* set the newline_after_content flag |
214 |
* @param boolean $flag TRUE or FALSE |
215 |
*/ |
216 |
function set_newline_after_closetag( $flag ) { |
217 |
if ($flag) { |
218 |
$this->_flags |= _NEWLINEAFTERCLOSETAG; |
219 |
} else{ |
220 |
$this->_flags &= ~_NEWLINEAFTERCLOSETAG; |
221 |
} |
222 |
} |
223 |
|
224 |
/** |
225 |
* This method turns on the automatic wrapping |
226 |
* of the tag's content inside the CDATA wrapper |
227 |
* for XML |
228 |
* |
229 |
* @param boolean $flag TRUE or FALSE |
230 |
*/ |
231 |
function set_cdata_flag($flag) { |
232 |
if ($flag) { |
233 |
$this->_flags |= _CDATACONTENTWRAP; |
234 |
$this->set_collapse(TRUE); |
235 |
} else{ |
236 |
$this->_flags &= ~_CDATACONTENTWRAP; |
237 |
} |
238 |
} |
239 |
|
240 |
/** |
241 |
* This function turns on the collapse flag |
242 |
* |
243 |
* @param boolean - the collapse flag |
244 |
* @param boolean - the indent flag |
245 |
* DEFAULT: TRUE; |
246 |
*/ |
247 |
function set_collapse($collapse=TRUE, $indent=TRUE) { |
248 |
if ($collapse) { |
249 |
$this->_flags |= _COLLAPSE; |
250 |
} else { |
251 |
$this->_flags &= ~_COLLAPSE; |
252 |
} |
253 |
|
254 |
$this->set_newline_after_opentag(FALSE); |
255 |
$this->set_indent_flag($indent); |
256 |
if ($indent) { |
257 |
$this->set_newline_after_closetag(TRUE); |
258 |
} else { |
259 |
$this->set_newline_after_closetag(FALSE); |
260 |
} |
261 |
} |
262 |
|
263 |
/** |
264 |
* This function checks to see if |
265 |
* there is only 1 content data, and |
266 |
* its not an object, then it auto |
267 |
* sets some of the indentation flags |
268 |
* |
269 |
*/ |
270 |
function _prepare_flags() { |
271 |
if ($this->_flags & _CONTENTREQUIRED) { |
272 |
if ($this->count_content() == 1) { |
273 |
if (!is_object($this->_content[0])) { |
274 |
//ok looks like this object has only |
275 |
//1 data for content and its a string. |
276 |
if ( !strstr($this->_content[0], "\n") ) { |
277 |
$this->_flags &= ~_NEWLINEAFTEROPENTAG; |
278 |
} |
279 |
} |
280 |
} else if ($this->count_content() == 0) { |
281 |
$this->_flags &= ~(_CONTENTREQUIRED | _CLOSETAGREQUIRED); |
282 |
} |
283 |
} |
284 |
} |
285 |
|
286 |
|
287 |
/****************************************/ |
288 |
/* Some helper methods for rendering */ |
289 |
/* the output xml tree. */ |
290 |
/****************************************/ |
291 |
|
292 |
/** |
293 |
* this function is responsible for |
294 |
* rendering the open tag. |
295 |
* |
296 |
* @param int - the indent level |
297 |
* @param boolean - do we add the finish / if we have no |
298 |
* close tag and no content? |
299 |
*/ |
300 |
function _render_open_tag( $indent_level, $finish_slash=TRUE ) { |
301 |
//get the indent level |
302 |
$indent = $this->_render_indent( $indent_level ); |
303 |
|
304 |
//build the tag |
305 |
if ($this->_flags & _ALWAYS_LOWERCASE) { |
306 |
$this->_tag = strtolower($this->_tag); |
307 |
} else if ($this->_flags & _ALWAYS_UPPERCASE) { |
308 |
$this->_tag = strtoupper($this->_tag); |
309 |
} |
310 |
//save on mem |
311 |
$xml = $indent . (@$this->_tag_prefix ? $this->_tag_prefix : _TAG_PREFIX) . $this->_tag; |
312 |
|
313 |
foreach( $this->_attributes as $name => $value) { |
314 |
$xml .= $this->_build_attribute_string($name, $value); |
315 |
} |
316 |
|
317 |
if ( !($this->_flags & _CLOSETAGREQUIRED) && !($this->_flags & _NOFINISHSLASHXHTML) |
318 |
&& $finish_slash ) { |
319 |
$xml .= " /".(@$this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX); |
320 |
} else { |
321 |
$xml .= (@$this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX); |
322 |
} |
323 |
|
324 |
if ($this->_flags & _NEWLINEAFTEROPENTAG) { |
325 |
$xml .= "\n"; |
326 |
} |
327 |
|
328 |
return $xml; |
329 |
} |
330 |
|
331 |
|
332 |
/** |
333 |
* this function is reponsible for |
334 |
* rendering the pcdata, or content |
335 |
* of the tag (if any) |
336 |
* |
337 |
* @param int - the indent level |
338 |
*/ |
339 |
function _render_content( $indent_level, $output_debug=0 ) { |
340 |
|
341 |
//walk through the content |
342 |
$xml = ''; |
343 |
for ($x=0; $x<=$this->_data_count-1; $x++) { |
344 |
$item = &$this->_content[$x]; |
345 |
if (method_exists($item, "render")) { |
346 |
if (($this->_flags & _COLLAPSE) && method_exists($item, "set_collapse")) { |
347 |
$item->set_collapse(TRUE, FALSE); |
348 |
} |
349 |
if ($indent_level == INDENT_LEFT_JUSTIFY) { |
350 |
$indent = INDENT_LEFT_JUSTIFY; |
351 |
} else { |
352 |
$indent = $indent_level + 1; |
353 |
} |
354 |
$xml .= $item->render($indent, $output_debug); |
355 |
} else { |
356 |
if ($this->_flags & _COLLAPSE) { |
357 |
$xml .= $item; |
358 |
} else { |
359 |
if ($indent_level == INDENT_LEFT_JUSTIFY) { |
360 |
$indent = INDENT_LEFT_JUSTIFY; |
361 |
} else { |
362 |
$indent = $indent_level + 1; |
363 |
} |
364 |
$indent = $this->_render_indent($indent); |
365 |
if ($this->_flags & _NEWLINEAFTEROPENTAG) { |
366 |
$item = str_replace("\n", "\n" . $indent, $item); |
367 |
$xml .= $indent . $item . "\n"; |
368 |
} else { |
369 |
$item = str_replace("\n", "\n" . $indent, $item); |
370 |
$xml .= $item; |
371 |
} |
372 |
} |
373 |
} |
374 |
} |
375 |
if ($this->_flags & _CDATACONTENTWRAP) { |
376 |
if ($this->_flags & _COLLAPSE) { |
377 |
$xml = "<![CDATA[ ".$xml." ]]>"; |
378 |
} else { |
379 |
$indent = $this->_render_indent($indent+1); |
380 |
$indent1 = $this->_render_indent($indent+2); |
381 |
$indent2 = $this->_render_indent($indent+3); |
382 |
$xml = "\n".$indent1."<![CDATA[\n".$xml."\n".$indent1."]]>\n".$indent; |
383 |
} |
384 |
|
385 |
} |
386 |
return $xml; |
387 |
} |
388 |
|
389 |
|
390 |
/** |
391 |
* this function is reposnsible for |
392 |
* rendering the closing tag (if any) |
393 |
* |
394 |
* @param int - the indent level |
395 |
*/ |
396 |
function _render_close_tag( $indent_level ) { |
397 |
if (!($this->_flags & _CLOSETAGREQUIRED)) { |
398 |
return ''; |
399 |
} |
400 |
|
401 |
$indent = ""; |
402 |
if (($this->_flags & _INDENT) && ($this->_flags & _NEWLINEAFTEROPENTAG)) { |
403 |
$indent = $this->_render_indent($indent_level); |
404 |
} |
405 |
$str = $indent ."</".$this->_tag.">"; |
406 |
|
407 |
if ($this->_flags & _NEWLINEAFTERCLOSETAG) { |
408 |
$str .= "\n"; |
409 |
} |
410 |
|
411 |
return $str; |
412 |
} |
413 |
|
414 |
/** |
415 |
* this builds an attribute for an XML tag. |
416 |
* XML attributes MUST have a name AND a |
417 |
* value. |
418 |
* |
419 |
* @param string - $name attribute name |
420 |
* @param mixed - $value attribute value |
421 |
* @return the tag attribute name=value pair. |
422 |
* to be added to the tag. |
423 |
*/ |
424 |
function _build_attribute_string($name, $value) { |
425 |
return " ".$name."=\"".$value."\""; |
426 |
} |
427 |
} |
428 |
?> |