1 |
<?php |
2 |
|
3 |
/** |
4 |
* Holds the XMLTagClass |
5 |
* |
6 |
* $Id: XMLTagClass.inc,v 1.37 2004/03/30 07:41:04 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 |
* {@source } |
61 |
* |
62 |
* @tutorial XMLTagClass.cls#example |
63 |
* |
64 |
* @param string - the tag name |
65 |
* @param array - the attributes array |
66 |
* can be in name => value |
67 |
* or just value |
68 |
* @param mixed - n items of content to add |
69 |
* |
70 |
*/ |
71 |
function XMLTagClass($name, $attributes=array() ) { |
72 |
$this->set_tag_name( $name ); |
73 |
$this->set_tag_attributes( $attributes ); |
74 |
|
75 |
$num_args = func_num_args(); |
76 |
for ($i=2;$i<$num_args;$i++) { |
77 |
$this->add(func_get_arg($i)); |
78 |
} |
79 |
|
80 |
$this->_set_flags(); |
81 |
} |
82 |
|
83 |
|
84 |
/** |
85 |
* This function is responsible |
86 |
* for rendering the tag and |
87 |
* its contents |
88 |
* |
89 |
* {@source } |
90 |
* |
91 |
* @param int - the current indentation |
92 |
* level for the tag |
93 |
*/ |
94 |
function render( $indent_level=0 ) { |
95 |
|
96 |
//try and guess the indentation flags |
97 |
//based on the data |
98 |
$this->_prepare_flags(); |
99 |
|
100 |
//return $xml; |
101 |
return $this->_render_open_tag( $indent_level ) . |
102 |
$this->_render_content( $indent_level ) . |
103 |
$this->_render_close_tag( $indent_level ); |
104 |
} |
105 |
|
106 |
|
107 |
/****************************************/ |
108 |
/* Some helper routines for building */ |
109 |
/* pieces of the xml tag, its */ |
110 |
/* attributes and PCDATA */ |
111 |
/****************************************/ |
112 |
|
113 |
/** |
114 |
* This method is used to set the bitmask |
115 |
* flags for this tag. It tells the |
116 |
* class how to render the tag. |
117 |
* |
118 |
* @access private |
119 |
* |
120 |
* NOTE: the child class can override this |
121 |
* to set the options |
122 |
*/ |
123 |
function _set_flags() { |
124 |
parent::_set_flags(); |
125 |
$this->_flags |= _NEWLINEAFTEROPENTAG | _NEWLINEAFTERCLOSETAG | |
126 |
_CONTENTREQUIRED | _CLOSETAGREQUIRED; |
127 |
} |
128 |
|
129 |
/** |
130 |
* This method sets the name of the tag |
131 |
* |
132 |
* {@source } |
133 |
* |
134 |
* @param string - the tag name |
135 |
*/ |
136 |
function set_tag_name( $name ) { |
137 |
$this->_tag = $name; |
138 |
} |
139 |
|
140 |
/** |
141 |
* This method gets the name of the tag |
142 |
* |
143 |
* {@source } |
144 |
* |
145 |
* @return string - the tag name |
146 |
*/ |
147 |
function get_tag_name() { |
148 |
return $this->_tag; |
149 |
} |
150 |
|
151 |
/** |
152 |
* This returns the tag declared for this class. |
153 |
* This should be used in favor of |
154 |
* accessing the $this->_tag directly. |
155 |
* |
156 |
* {@source } |
157 |
* |
158 |
* @return string - the _tag var for this class. |
159 |
*/ |
160 |
function get_tag() { |
161 |
//for compatibility only |
162 |
return $this->get_tag_name(); |
163 |
} |
164 |
|
165 |
/** |
166 |
* add a single attribute (name="value") |
167 |
* |
168 |
* {@source } |
169 |
* |
170 |
* @param string $name attribute name |
171 |
* @param mixed $value the value. |
172 |
* @return none |
173 |
*/ |
174 |
function set_tag_attribute( $name, $value=NULL ) { |
175 |
$this->_attributes[$name] = $value; |
176 |
} |
177 |
|
178 |
/** |
179 |
* add multiple attributes (name="value") |
180 |
* |
181 |
* {@source } |
182 |
* |
183 |
* @param array $attributes Associative array of name="value" pairs of |
184 |
* tag atributes. |
185 |
* ie array("border"=>"0", "class"=>"hover"); |
186 |
* @return none |
187 |
*/ |
188 |
function set_tag_attributes( $attributes=array() ) { |
189 |
$this->_attributes = array_merge($this->_attributes, $attributes); |
190 |
} |
191 |
|
192 |
/** |
193 |
* clear all attributes and start with new attributes |
194 |
* |
195 |
* {@source } |
196 |
* |
197 |
* @param array Associative array of name="value" pairs of |
198 |
* tag atributes. |
199 |
* ie array("border"=>"0", "class"=>"hover"); |
200 |
* @return none |
201 |
*/ |
202 |
function reset_attributes( $attributes=array() ) { |
203 |
$this->_attributes = array(); |
204 |
$this->set_tag_attributes( $attributes ); |
205 |
} |
206 |
|
207 |
/** |
208 |
* get the nth element from content array |
209 |
* |
210 |
* NOTE: |
211 |
* This has been made public in the Container |
212 |
* |
213 |
* {@source } |
214 |
* @access private |
215 |
* |
216 |
* @param int $cell the cell to get |
217 |
* @return mixed |
218 |
*/ |
219 |
function &_get_element( $cell ) { |
220 |
return $this->get_element($cell); |
221 |
} |
222 |
|
223 |
|
224 |
|
225 |
//**************************************************************** |
226 |
// Misc functions |
227 |
//**************************************************************** |
228 |
|
229 |
/** |
230 |
* set the newline_after_opentag flag |
231 |
* |
232 |
* {@source } |
233 |
* |
234 |
* @param boolean TRUE or FALSE |
235 |
* @return none |
236 |
*/ |
237 |
function set_newline_after_opentag( $flag ) { |
238 |
if ($flag) { |
239 |
$this->_flags |= _NEWLINEAFTEROPENTAG; |
240 |
} else{ |
241 |
$this->_flags &= ~_NEWLINEAFTEROPENTAG; |
242 |
} |
243 |
} |
244 |
|
245 |
/** |
246 |
* set the newline_after_content flag |
247 |
* |
248 |
* {@source } |
249 |
* |
250 |
* @param boolean TRUE or FALSE |
251 |
* @return none |
252 |
*/ |
253 |
function set_newline_after_closetag( $flag ) { |
254 |
if ($flag) { |
255 |
$this->_flags |= _NEWLINEAFTERCLOSETAG; |
256 |
} else{ |
257 |
$this->_flags &= ~_NEWLINEAFTERCLOSETAG; |
258 |
} |
259 |
} |
260 |
|
261 |
/** |
262 |
* This method turns on the automatic wrapping |
263 |
* of the tag's content inside the CDATA wrapper |
264 |
* for XML |
265 |
* |
266 |
* {@source } |
267 |
* @param boolean $flag TRUE or FALSE |
268 |
* @return none |
269 |
*/ |
270 |
function set_cdata_flag($flag) { |
271 |
if ($flag) { |
272 |
$this->_flags |= _CDATACONTENTWRAP; |
273 |
$this->set_collapse(TRUE); |
274 |
} else{ |
275 |
$this->_flags &= ~_CDATACONTENTWRAP; |
276 |
} |
277 |
} |
278 |
|
279 |
/** |
280 |
* This function turns on the collapse flag |
281 |
* |
282 |
* {@source } |
283 |
* |
284 |
* @param boolean - the collapse flag |
285 |
* @param boolean - the indent flag |
286 |
* DEFAULT: TRUE; |
287 |
* @return none |
288 |
*/ |
289 |
function set_collapse($collapse=TRUE, $indent=TRUE) { |
290 |
if ($collapse) { |
291 |
$this->_flags |= _COLLAPSE; |
292 |
} else { |
293 |
$this->_flags &= ~_COLLAPSE; |
294 |
} |
295 |
|
296 |
$this->set_newline_after_opentag(FALSE); |
297 |
$this->set_indent_flag($indent); |
298 |
if ($indent) { |
299 |
$this->set_newline_after_closetag(TRUE); |
300 |
} else { |
301 |
$this->set_newline_after_closetag(FALSE); |
302 |
} |
303 |
} |
304 |
|
305 |
/** |
306 |
* This function checks to see if |
307 |
* there is only 1 content data, and |
308 |
* its not an object, then it auto |
309 |
* sets some of the indentation flags |
310 |
* |
311 |
* {@source } |
312 |
* @access private |
313 |
* @return none |
314 |
*/ |
315 |
function _prepare_flags() { |
316 |
if ($this->_flags & _CONTENTREQUIRED) { |
317 |
if ($this->count_content() == 1) { |
318 |
if (!is_object($this->_content[0])) { |
319 |
//ok looks like this object has only |
320 |
//1 data for content and its a string. |
321 |
if ( !strstr($this->_content[0], "\n") ) { |
322 |
$this->_flags &= ~_NEWLINEAFTEROPENTAG; |
323 |
} |
324 |
} |
325 |
} else if ($this->count_content() == 0) { |
326 |
$this->_flags &= ~(_CONTENTREQUIRED | _CLOSETAGREQUIRED); |
327 |
} |
328 |
} |
329 |
} |
330 |
|
331 |
|
332 |
/****************************************/ |
333 |
/* Some helper methods for rendering */ |
334 |
/* the output xml tree. */ |
335 |
/****************************************/ |
336 |
|
337 |
/** |
338 |
* this function is responsible for |
339 |
* rendering the open tag. |
340 |
* |
341 |
* {@source } |
342 |
* @access private |
343 |
* |
344 |
* @param int - the indent level |
345 |
* @param boolean - do we add the finish / if we have no |
346 |
* close tag and no content? |
347 |
* @return string |
348 |
*/ |
349 |
function _render_open_tag( $indent_level, $finish_slash=TRUE ) { |
350 |
//get the indent level |
351 |
$indent = $this->_render_indent( $indent_level ); |
352 |
|
353 |
//build the tag |
354 |
if ($this->_flags & _ALWAYS_LOWERCASE) { |
355 |
$this->_tag = strtolower($this->_tag); |
356 |
} else if ($this->_flags & _ALWAYS_UPPERCASE) { |
357 |
$this->_tag = strtoupper($this->_tag); |
358 |
} |
359 |
//save on mem |
360 |
$xml = $indent . ((isset($this->_tag_prefix) && $this->_tag_prefix) ? $this->_tag_prefix : _TAG_PREFIX) . $this->_tag; |
361 |
|
362 |
foreach( $this->_attributes as $name => $value) { |
363 |
$xml .= $this->_build_attribute_string($name, $value); |
364 |
} |
365 |
|
366 |
if ( !($this->_flags & _CLOSETAGREQUIRED) && !($this->_flags & _NOFINISHSLASHXHTML) |
367 |
&& $finish_slash ) { |
368 |
$xml .= " /".(@$this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX); |
369 |
} else { |
370 |
$xml .= (@$this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX); |
371 |
} |
372 |
|
373 |
if ($this->_flags & _NEWLINEAFTEROPENTAG) { |
374 |
$xml .= "\n"; |
375 |
} |
376 |
|
377 |
return $xml; |
378 |
} |
379 |
|
380 |
|
381 |
/** |
382 |
* this function is reponsible for |
383 |
* rendering the pcdata, or content |
384 |
* of the tag (if any) |
385 |
* |
386 |
* {@source } |
387 |
* @access private |
388 |
* |
389 |
* @param int - the indent level |
390 |
* @return string |
391 |
*/ |
392 |
function _render_content( $indent_level, $output_debug=0 ) { |
393 |
|
394 |
//walk through the content |
395 |
$xml = ''; |
396 |
for ($x=0; $x<=$this->_data_count-1; $x++) { |
397 |
$item = &$this->_content[$x]; |
398 |
if (method_exists($item, "render")) { |
399 |
if (($this->_flags & _COLLAPSE) && method_exists($item, "set_collapse")) { |
400 |
$item->set_collapse(TRUE, FALSE); |
401 |
} |
402 |
if ($indent_level == INDENT_LEFT_JUSTIFY) { |
403 |
$indent = INDENT_LEFT_JUSTIFY; |
404 |
} else { |
405 |
$indent = $indent_level + 1; |
406 |
} |
407 |
$xml .= $item->render($indent, $output_debug); |
408 |
} else { |
409 |
if ($this->_flags & _COLLAPSE) { |
410 |
$xml .= $item; |
411 |
} else { |
412 |
if ($indent_level == INDENT_LEFT_JUSTIFY) { |
413 |
$indent = INDENT_LEFT_JUSTIFY; |
414 |
} else { |
415 |
$indent = $indent_level + 1; |
416 |
} |
417 |
$indent = $this->_render_indent($indent); |
418 |
if ($this->_flags & _NEWLINEAFTEROPENTAG) { |
419 |
$item = str_replace("\n", "\n" . $indent, $item); |
420 |
$xml .= $indent . $item . "\n"; |
421 |
} else { |
422 |
$item = str_replace("\n", "\n" . $indent, $item); |
423 |
$xml .= $item; |
424 |
} |
425 |
} |
426 |
} |
427 |
} |
428 |
if ($this->_flags & _CDATACONTENTWRAP) { |
429 |
if ($this->_flags & _COLLAPSE) { |
430 |
$xml = "<![CDATA[ ".$xml." ]]>"; |
431 |
} else { |
432 |
$indent = $this->_render_indent($indent+1); |
433 |
$indent1 = $this->_render_indent($indent+2); |
434 |
$indent2 = $this->_render_indent($indent+3); |
435 |
$xml = "\n".$indent1."<![CDATA[\n".$xml."\n".$indent1."]]>\n".$indent; |
436 |
} |
437 |
|
438 |
} |
439 |
return $xml; |
440 |
} |
441 |
|
442 |
|
443 |
/** |
444 |
* this function is reposnsible for |
445 |
* rendering the closing tag (if any) |
446 |
* |
447 |
* {@source } |
448 |
* @access private |
449 |
* |
450 |
* @param int - the indent level |
451 |
* @return string |
452 |
*/ |
453 |
function _render_close_tag( $indent_level ) { |
454 |
if (!($this->_flags & _CLOSETAGREQUIRED)) { |
455 |
return ''; |
456 |
} |
457 |
|
458 |
$indent = ""; |
459 |
if (($this->_flags & _INDENT) && ($this->_flags & _NEWLINEAFTEROPENTAG)) { |
460 |
$indent = $this->_render_indent($indent_level); |
461 |
} |
462 |
$str = $indent ."</".$this->_tag.">"; |
463 |
|
464 |
if ($this->_flags & _NEWLINEAFTERCLOSETAG) { |
465 |
$str .= "\n"; |
466 |
} |
467 |
|
468 |
return $str; |
469 |
} |
470 |
|
471 |
/** |
472 |
* this builds an attribute for an XML tag. |
473 |
* XML attributes MUST have a name AND a |
474 |
* value. |
475 |
* |
476 |
* {@source } |
477 |
* @access private |
478 |
* |
479 |
* @param string - $name attribute name |
480 |
* @param mixed - $value attribute value |
481 |
* @return the tag attribute name=value pair. |
482 |
* to be added to the tag. |
483 |
*/ |
484 |
function _build_attribute_string($name, $value) { |
485 |
return " ".$name."=\"".$value."\""; |
486 |
} |
487 |
} |
488 |
?> |