53 |
*/ |
*/ |
54 |
var $_attributes = array(); |
var $_attributes = array(); |
55 |
|
|
|
/** |
|
|
* flag to render close tag or not. |
|
|
* set to FALSE if no close tag is needed |
|
|
* ie <img src=hotchick.png> |
|
|
* @var boolean |
|
|
* @access private |
|
|
*/ |
|
|
var $_close_tag_required = TRUE; |
|
|
|
|
|
/** |
|
|
* Flag to render content or not. |
|
|
* set to FALSE to not render content. |
|
|
* ie <img src=hotchick.png> |
|
|
* @var boolean |
|
|
* @access private |
|
|
*/ |
|
|
var $_content_required = TRUE; |
|
|
|
|
|
/** |
|
|
* Flag to place a newline after open tag. |
|
|
* some tags are nice to have a \n after |
|
|
* to make reading of html easier. |
|
|
* ie <table> |
|
|
* @var boolean |
|
|
* @access public |
|
|
*/ |
|
|
var $newline_after_opentag = TRUE; |
|
|
|
|
|
/** |
|
|
* Flag to place a newline after close tag. |
|
|
* usefull for maintaining nice output inside |
|
|
* a table |
|
|
* @var boolean |
|
|
* @access public |
|
|
*/ |
|
|
var $newline_after_closetag = TRUE; |
|
|
|
|
|
/** |
|
|
* Flag to tell the renderer to NEVER |
|
|
* to render the tag name in lower case |
|
|
* no matter what |
|
|
* @var boolean |
|
|
* @access private |
|
|
*/ |
|
|
var $_always_upper_case = FALSE; |
|
|
|
|
|
/** |
|
|
* Flag to tell the renderer to NEVER |
|
|
* to render the tag name in upper case |
|
|
* no matter what |
|
|
* @var boolean |
|
|
* @access private |
|
|
*/ |
|
|
var $_always_lower_case = FALSE; |
|
|
|
|
|
/** |
|
|
* Automatically wrap ALL content |
|
|
* inside the <![CDATA[ CONTENT ]]> |
|
|
* tag |
|
|
* |
|
|
* @var boolean |
|
|
*/ |
|
|
var $_cdata_content_wrap = FALSE; |
|
|
|
|
|
|
|
|
/** |
|
|
* holds the tag prefix |
|
|
* |
|
|
*/ |
|
|
var $_tag_prefix = "<"; |
|
|
|
|
|
/** |
|
|
* holds the tag postfix |
|
|
* |
|
|
*/ |
|
|
var $_tag_postfix = ">"; |
|
|
|
|
|
|
|
|
|
|
56 |
|
|
57 |
/** |
/** |
58 |
* The constructor |
* The constructor |
59 |
|
* |
60 |
|
* {@source } |
61 |
|
* |
62 |
|
* @tutorial XMLTagClass.cls#example |
63 |
* |
* |
64 |
* @param string - the tag name |
* @param string - the tag name |
65 |
* @param array - the attributes array |
* @param array - the attributes array |
66 |
* can be in name => value |
* can be in name => value |
67 |
* or just value |
* or just value |
68 |
* @param mixed - n items of content to |
* @param mixed - n items of content to add |
|
* add |
|
69 |
* |
* |
70 |
*/ |
*/ |
71 |
function XMLTagClass($name, $attributes=array() ) { |
function XMLTagClass($name, $attributes=array() ) { |
75 |
$num_args = func_num_args(); |
$num_args = func_num_args(); |
76 |
for ($i=2;$i<$num_args;$i++) { |
for ($i=2;$i<$num_args;$i++) { |
77 |
$this->add(func_get_arg($i)); |
$this->add(func_get_arg($i)); |
78 |
} |
} |
79 |
|
|
80 |
|
$this->_set_flags(); |
81 |
} |
} |
82 |
|
|
83 |
|
|
85 |
* This function is responsible |
* This function is responsible |
86 |
* for rendering the tag and |
* for rendering the tag and |
87 |
* its contents |
* its contents |
88 |
|
* |
89 |
|
* {@source } |
90 |
* |
* |
91 |
* @param int - the current indentation |
* @param int - the current indentation |
92 |
* level for the tag |
* level for the tag |
97 |
//based on the data |
//based on the data |
98 |
$this->_prepare_flags(); |
$this->_prepare_flags(); |
99 |
|
|
100 |
//build the open tag and its |
//return $xml; |
101 |
//attributes |
return $this->_render_open_tag( $indent_level ) . |
102 |
$xml = $this->_render_open_tag( $indent_level ); |
$this->_render_content( $indent_level ) . |
103 |
|
$this->_render_close_tag( $indent_level ); |
|
//build the content or PCDATA |
|
|
$xml .= $this->_render_content( $indent_level ); |
|
|
|
|
|
//build the close tag |
|
|
//if required. |
|
|
$xml .= $this->_render_close_tag( $indent_level ); |
|
|
|
|
|
return $xml; |
|
104 |
} |
} |
105 |
|
|
106 |
|
|
110 |
/* attributes and PCDATA */ |
/* 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 |
* This method sets the name of the tag |
131 |
|
* |
132 |
|
* {@source } |
133 |
* |
* |
134 |
* @param string - the tag name |
* @param string - the tag name |
135 |
*/ |
*/ |
139 |
|
|
140 |
/** |
/** |
141 |
* This method gets the name of the tag |
* This method gets the name of the tag |
142 |
|
* |
143 |
|
* {@source } |
144 |
* |
* |
145 |
* @return string - the tag name |
* @return string - the tag name |
146 |
*/ |
*/ |
152 |
* This returns the tag declared for this class. |
* This returns the tag declared for this class. |
153 |
* This should be used in favor of |
* This should be used in favor of |
154 |
* accessing the $this->_tag directly. |
* accessing the $this->_tag directly. |
155 |
|
* |
156 |
|
* {@source } |
157 |
* |
* |
158 |
* @return string - the _tag var for this class. |
* @return string - the _tag var for this class. |
159 |
*/ |
*/ |
164 |
|
|
165 |
/** |
/** |
166 |
* add a single attribute (name="value") |
* add a single attribute (name="value") |
167 |
|
* |
168 |
|
* {@source } |
169 |
|
* |
170 |
* @param string $name attribute name |
* @param string $name attribute name |
171 |
* @param mixed $value the value. |
* @param mixed $value the value. |
172 |
* @access public |
* @return none |
173 |
*/ |
*/ |
174 |
function set_tag_attribute( $name, $value=NULL ) { |
function set_tag_attribute( $name, $value=NULL ) { |
175 |
$this->_attributes[$name] = $value; |
$this->_attributes[$name] = $value; |
177 |
|
|
178 |
/** |
/** |
179 |
* add multiple attributes (name="value") |
* add multiple attributes (name="value") |
180 |
|
* |
181 |
|
* {@source } |
182 |
|
* |
183 |
* @param array $attributes Associative array of name="value" pairs of |
* @param array $attributes Associative array of name="value" pairs of |
184 |
* tag atributes. |
* tag atributes. |
185 |
* ie array("border"=>"0", "class"=>"hover"); |
* ie array("border"=>"0", "class"=>"hover"); |
186 |
* @access public |
* @return none |
187 |
*/ |
*/ |
188 |
function set_tag_attributes( $attributes=array() ) { |
function set_tag_attributes( $attributes=array() ) { |
189 |
$this->_attributes = array_merge($this->_attributes, $attributes); |
$this->_attributes = array_merge($this->_attributes, $attributes); |
191 |
|
|
192 |
/** |
/** |
193 |
* clear all attributes and start with new attributes |
* clear all attributes and start with new attributes |
194 |
* @param array $attributes Associative array of name="value" pairs of |
* |
195 |
* tag atributes. |
* {@source } |
196 |
* ie array("border"=>"0", "class"=>"hover"); |
* |
197 |
* @access public |
* @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() ) { |
function reset_attributes( $attributes=array() ) { |
203 |
$this->_attributes = array(); |
$this->_attributes = array(); |
206 |
|
|
207 |
/** |
/** |
208 |
* get the nth element from content array |
* 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 |
* @param int $cell the cell to get |
217 |
* @return mixed |
* @return mixed |
218 |
*/ |
*/ |
219 |
function _get_element( $cell ) { |
function &_get_element( $cell ) { |
220 |
return $this->_content[$cell]; |
return $this->get_element($cell); |
221 |
} |
} |
222 |
|
|
223 |
|
|
228 |
|
|
229 |
/** |
/** |
230 |
* set the newline_after_opentag flag |
* set the newline_after_opentag flag |
231 |
* @param boolean $flag TRUE or FALSE |
* |
232 |
|
* {@source } |
233 |
|
* |
234 |
|
* @param boolean TRUE or FALSE |
235 |
|
* @return none |
236 |
*/ |
*/ |
237 |
function set_newline_after_opentag( $flag ) { |
function set_newline_after_opentag( $flag ) { |
238 |
$this->newline_after_opentag = $flag; |
if ($flag) { |
239 |
|
$this->_flags |= _NEWLINEAFTEROPENTAG; |
240 |
|
} else{ |
241 |
|
$this->_flags &= ~_NEWLINEAFTEROPENTAG; |
242 |
|
} |
243 |
} |
} |
244 |
|
|
245 |
/** |
/** |
246 |
* set the newline_after_content flag |
* set the newline_after_content flag |
247 |
* @param boolean $flag TRUE or FALSE |
* |
248 |
|
* {@source } |
249 |
|
* |
250 |
|
* @param boolean TRUE or FALSE |
251 |
|
* @return none |
252 |
*/ |
*/ |
253 |
function set_newline_after_closetag( $flag ) { |
function set_newline_after_closetag( $flag ) { |
254 |
$this->newline_after_closetag = $flag; |
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 |
* This function turns on the collapse flag |
281 |
|
* |
282 |
|
* {@source } |
283 |
* |
* |
284 |
* @param boolean - the collapse flag |
* @param boolean - the collapse flag |
285 |
* @param boolean - the indent flag |
* @param boolean - the indent flag |
286 |
* DEFAULT: TRUE; |
* DEFAULT: TRUE; |
287 |
|
* @return none |
288 |
*/ |
*/ |
289 |
function set_collapse($collapse=TRUE, $indent=TRUE) { |
function set_collapse($collapse=TRUE, $indent=TRUE) { |
290 |
$this->_collapse_flag = $collapse; |
if ($collapse) { |
291 |
|
$this->_flags |= _COLLAPSE; |
292 |
|
} else { |
293 |
|
$this->_flags &= ~_COLLAPSE; |
294 |
|
} |
295 |
|
|
296 |
$this->set_newline_after_opentag(FALSE); |
$this->set_newline_after_opentag(FALSE); |
297 |
$this->set_indent_flag($indent); |
$this->set_indent_flag($indent); |
299 |
$this->set_newline_after_closetag(TRUE); |
$this->set_newline_after_closetag(TRUE); |
300 |
} else { |
} else { |
301 |
$this->set_newline_after_closetag(FALSE); |
$this->set_newline_after_closetag(FALSE); |
302 |
} |
} |
303 |
} |
} |
304 |
|
|
305 |
/** |
/** |
307 |
* there is only 1 content data, and |
* there is only 1 content data, and |
308 |
* its not an object, then it auto |
* its not an object, then it auto |
309 |
* sets some of the indentation flags |
* sets some of the indentation flags |
310 |
* |
* |
311 |
|
* {@source } |
312 |
|
* @access private |
313 |
|
* @return none |
314 |
*/ |
*/ |
315 |
function _prepare_flags() { |
function _prepare_flags() { |
316 |
if ($this->_content_required) { |
if ($this->_flags & _CONTENTREQUIRED) { |
317 |
if ($this->count_content() == 1 || $this->count_content() == 0) { |
if ($this->count_content() == 1) { |
318 |
if (!is_object($this->_content[0])) { |
if (!is_object($this->_content[0])) { |
319 |
//ok looks like this object has only |
//ok looks like this object has only |
320 |
//1 data for content and its a string. |
//1 data for content and its a string. |
321 |
if ( !strstr($this->_content[0], "\n") ) { |
if ( !strstr($this->_content[0], "\n") ) { |
322 |
$this->newline_after_opentag = FALSE; |
$this->_flags &= ~_NEWLINEAFTEROPENTAG; |
323 |
} |
} |
324 |
} |
} |
325 |
} |
} else if ($this->count_content() == 0) { |
326 |
|
$this->_flags &= ~(_CONTENTREQUIRED | _CLOSETAGREQUIRED); |
327 |
|
} |
328 |
} |
} |
329 |
} |
} |
330 |
|
|
337 |
/** |
/** |
338 |
* this function is responsible for |
* this function is responsible for |
339 |
* rendering the open tag. |
* rendering the open tag. |
340 |
|
* |
341 |
|
* {@source } |
342 |
|
* @access private |
343 |
* |
* |
344 |
* @param int - the indent level |
* @param int - the indent level |
345 |
* @param boolean - do we add the finish / if we have no |
* @param boolean - do we add the finish / if we have no |
346 |
* close tag and no content? |
* close tag and no content? |
347 |
|
* @return string |
348 |
*/ |
*/ |
349 |
function _render_open_tag( $indent_level, $finish_slash=TRUE ) { |
function _render_open_tag( $indent_level, $finish_slash=TRUE ) { |
|
|
|
350 |
//get the indent level |
//get the indent level |
351 |
$indent = $this->_render_indent( $indent_level ); |
$indent = $this->_render_indent( $indent_level ); |
352 |
|
|
353 |
//build the tag |
//build the tag |
354 |
$tag = $this->_tag; |
if ($this->_flags & _ALWAYS_LOWERCASE) { |
355 |
if ($this->_always_lower_case) { |
$this->_tag = strtolower($this->_tag); |
356 |
$tag = strtolower($tag); |
} else if ($this->_flags & _ALWAYS_UPPERCASE) { |
357 |
} else if ($this->_always_upper_case) { |
$this->_tag = strtoupper($this->_tag); |
|
$tag = strtoupper($tag); |
|
358 |
} |
} |
359 |
$xml = $indent . $this->_tag_prefix . $tag; |
//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) { |
foreach( $this->_attributes as $name => $value) { |
363 |
$xml .= $this->_build_attribute_string($name, $value); |
$xml .= $this->_build_attribute_string($name, $value); |
364 |
} |
} |
365 |
|
|
366 |
if ( !$this->_close_tag_required && !$this->_no_finish_slash_xhtml |
if ( !($this->_flags & _CLOSETAGREQUIRED) && !($this->_flags & _NOFINISHSLASHXHTML) |
367 |
&& $finish_slash ) { |
&& $finish_slash ) { |
368 |
$xml .= " /".$this->_tag_postfix; |
$xml .= " /".(@$this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX); |
369 |
} else { |
} else { |
370 |
$xml .= $this->_tag_postfix; |
$xml .= (@$this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX); |
371 |
} |
} |
372 |
|
|
373 |
if ($this->newline_after_opentag) { |
if ($this->_flags & _NEWLINEAFTEROPENTAG) { |
374 |
$xml .= "\n"; |
$xml .= "\n"; |
375 |
} |
} |
376 |
|
|
377 |
return $xml; |
return $xml; |
378 |
} |
} |
379 |
|
|
383 |
* rendering the pcdata, or content |
* rendering the pcdata, or content |
384 |
* of the tag (if any) |
* of the tag (if any) |
385 |
* |
* |
386 |
|
* {@source } |
387 |
|
* @access private |
388 |
|
* |
389 |
* @param int - the indent level |
* @param int - the indent level |
390 |
|
* @return string |
391 |
*/ |
*/ |
392 |
function _render_content( $indent_level, $output_debug=0 ) { |
function _render_content( $indent_level, $output_debug=0 ) { |
393 |
|
|
394 |
//walk through the content |
//walk through the content |
395 |
$xml = ''; |
$xml = ''; |
396 |
foreach ($this->_content as $item) { |
for ($x=0; $x<=$this->_data_count-1; $x++) { |
397 |
|
$item = &$this->_content[$x]; |
398 |
if (method_exists($item, "render")) { |
if (method_exists($item, "render")) { |
399 |
if ($this->_collapse_flag && method_exists($item, "set_collapse")) { |
if (($this->_flags & _COLLAPSE) && method_exists($item, "set_collapse")) { |
400 |
$item->set_collapse(TRUE, FALSE); |
$item->set_collapse(TRUE, FALSE); |
401 |
} |
} |
402 |
if ($indent_level == INDENT_LEFT_JUSTIFY) { |
if ($indent_level == INDENT_LEFT_JUSTIFY) { |
406 |
} |
} |
407 |
$xml .= $item->render($indent, $output_debug); |
$xml .= $item->render($indent, $output_debug); |
408 |
} else { |
} else { |
409 |
if ($this->_collapse_flag) { |
if ($this->_flags & _COLLAPSE) { |
410 |
$xml .= $item; |
$xml .= $item; |
411 |
} else { |
} else { |
412 |
if ($indent_level == INDENT_LEFT_JUSTIFY) { |
if ($indent_level == INDENT_LEFT_JUSTIFY) { |
414 |
} else { |
} else { |
415 |
$indent = $indent_level + 1; |
$indent = $indent_level + 1; |
416 |
} |
} |
417 |
$indent = $this->_render_indent($indent + 1); |
$indent = $this->_render_indent($indent); |
418 |
if ($this->newline_after_opentag) { |
if ($this->_flags & _NEWLINEAFTEROPENTAG) { |
419 |
$item = str_replace("\n", "\n" . $indent, $item); |
$item = str_replace("\n", "\n" . $indent, $item); |
420 |
$xml .= $indent . $item . "\n"; |
$xml .= $indent . $item . "\n"; |
421 |
} else { |
} else { |
425 |
} |
} |
426 |
} |
} |
427 |
} |
} |
428 |
if ($this->_cdata_content_wrap) { |
if ($this->_flags & _CDATACONTENTWRAP) { |
429 |
if ($this->_collapse_flag) { |
if ($this->_flags & _COLLAPSE) { |
430 |
$xml = "<![CDATA[ ".$xml." ]]>"; |
$xml = "<![CDATA[ ".$xml." ]]>"; |
431 |
} else { |
} else { |
432 |
$indent = $this->_render_indent($indent+1); |
$indent = $this->_render_indent($indent+1); |
444 |
* this function is reposnsible for |
* this function is reposnsible for |
445 |
* rendering the closing tag (if any) |
* rendering the closing tag (if any) |
446 |
* |
* |
447 |
|
* {@source } |
448 |
|
* @access private |
449 |
|
* |
450 |
* @param int - the indent level |
* @param int - the indent level |
451 |
|
* @return string |
452 |
*/ |
*/ |
453 |
function _render_close_tag( $indent_level ) { |
function _render_close_tag( $indent_level ) { |
454 |
if (!$this->_close_tag_required) { |
if (!($this->_flags & _CLOSETAGREQUIRED)) { |
455 |
return ''; |
return ''; |
456 |
} |
} |
457 |
|
|
458 |
$indent = ""; |
$indent = ""; |
459 |
if ($this->indent_flag && $this->newline_after_opentag) { |
if (($this->_flags & _INDENT) && ($this->_flags & _NEWLINEAFTEROPENTAG)) { |
460 |
$indent = $this->_render_indent($indent_level); |
$indent = $this->_render_indent($indent_level); |
461 |
} |
} |
462 |
$str = $indent ."</".$this->_tag.">"; |
$str = $indent ."</".$this->_tag.">"; |
463 |
|
|
464 |
if ($this->newline_after_closetag) { |
if ($this->_flags & _NEWLINEAFTERCLOSETAG) { |
465 |
$str .= "\n"; |
$str .= "\n"; |
466 |
} |
} |
467 |
|
|
472 |
* this builds an attribute for an XML tag. |
* this builds an attribute for an XML tag. |
473 |
* XML attributes MUST have a name AND a |
* XML attributes MUST have a name AND a |
474 |
* value. |
* value. |
475 |
|
* |
476 |
|
* {@source } |
477 |
|
* @access private |
478 |
* |
* |
479 |
* @param string - $name attribute name |
* @param string - $name attribute name |
480 |
* @param mixed - $value attribute value |
* @param mixed - $value attribute value |
482 |
* to be added to the tag. |
* to be added to the tag. |
483 |
*/ |
*/ |
484 |
function _build_attribute_string($name, $value) { |
function _build_attribute_string($name, $value) { |
485 |
if ( ((int)$name - 0) === $name) { |
return " ".$name."=\"".$value."\""; |
|
$returnval = " ".$value; |
|
|
} else if ( $value === NULL ) { |
|
|
$returnval = " ".$name; |
|
|
} else { |
|
|
$returnval= " ".$name."=\"".$value."\""; |
|
|
} |
|
|
return $returnval; |
|
486 |
} |
} |
487 |
} |
} |
488 |
?> |
?> |