1 |
jonen |
1.1 |
<?php |
2 |
|
|
|
3 |
|
|
/** |
4 |
|
|
* Holds the XMLTagClass |
5 |
|
|
* |
6 |
jonen |
1.4 |
* $Id: XMLTagClass.inc,v 1.37 2004/03/30 07:41:04 hemna Exp $ |
7 |
jonen |
1.1 |
* |
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 |
jonen |
1.4 |
* |
60 |
|
|
* {@source } |
61 |
|
|
* |
62 |
|
|
* @tutorial XMLTagClass.cls#example |
63 |
jonen |
1.1 |
* |
64 |
|
|
* @param string - the tag name |
65 |
|
|
* @param array - the attributes array |
66 |
|
|
* can be in name => value |
67 |
|
|
* or just value |
68 |
jonen |
1.4 |
* @param mixed - n items of content to add |
69 |
jonen |
1.1 |
* |
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 |
jonen |
1.2 |
} |
79 |
|
|
|
80 |
|
|
$this->_set_flags(); |
81 |
jonen |
1.1 |
} |
82 |
|
|
|
83 |
|
|
|
84 |
|
|
/** |
85 |
|
|
* This function is responsible |
86 |
|
|
* for rendering the tag and |
87 |
|
|
* its contents |
88 |
jonen |
1.4 |
* |
89 |
|
|
* {@source } |
90 |
jonen |
1.1 |
* |
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 |
jonen |
1.2 |
//return $xml; |
101 |
|
|
return $this->_render_open_tag( $indent_level ) . |
102 |
|
|
$this->_render_content( $indent_level ) . |
103 |
|
|
$this->_render_close_tag( $indent_level ); |
104 |
jonen |
1.1 |
} |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
/****************************************/ |
108 |
|
|
/* Some helper routines for building */ |
109 |
|
|
/* pieces of the xml tag, its */ |
110 |
|
|
/* attributes and PCDATA */ |
111 |
|
|
/****************************************/ |
112 |
|
|
|
113 |
jonen |
1.2 |
/** |
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 |
jonen |
1.4 |
* |
118 |
|
|
* @access private |
119 |
jonen |
1.2 |
* |
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 |
jonen |
1.1 |
|
129 |
|
|
/** |
130 |
|
|
* This method sets the name of the tag |
131 |
jonen |
1.4 |
* |
132 |
|
|
* {@source } |
133 |
jonen |
1.1 |
* |
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 |
jonen |
1.4 |
* |
143 |
|
|
* {@source } |
144 |
jonen |
1.1 |
* |
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 |
jonen |
1.4 |
* |
156 |
|
|
* {@source } |
157 |
jonen |
1.1 |
* |
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 |
jonen |
1.4 |
* |
168 |
|
|
* {@source } |
169 |
|
|
* |
170 |
jonen |
1.1 |
* @param string $name attribute name |
171 |
|
|
* @param mixed $value the value. |
172 |
jonen |
1.4 |
* @return none |
173 |
jonen |
1.1 |
*/ |
174 |
|
|
function set_tag_attribute( $name, $value=NULL ) { |
175 |
|
|
$this->_attributes[$name] = $value; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
/** |
179 |
|
|
* add multiple attributes (name="value") |
180 |
jonen |
1.4 |
* |
181 |
|
|
* {@source } |
182 |
|
|
* |
183 |
jonen |
1.1 |
* @param array $attributes Associative array of name="value" pairs of |
184 |
|
|
* tag atributes. |
185 |
|
|
* ie array("border"=>"0", "class"=>"hover"); |
186 |
jonen |
1.4 |
* @return none |
187 |
jonen |
1.1 |
*/ |
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 |
jonen |
1.4 |
* |
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 |
jonen |
1.1 |
*/ |
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 |
jonen |
1.4 |
* |
210 |
|
|
* NOTE: |
211 |
|
|
* This has been made public in the Container |
212 |
|
|
* |
213 |
|
|
* {@source } |
214 |
|
|
* @access private |
215 |
|
|
* |
216 |
jonen |
1.1 |
* @param int $cell the cell to get |
217 |
|
|
* @return mixed |
218 |
|
|
*/ |
219 |
jonen |
1.4 |
function &_get_element( $cell ) { |
220 |
|
|
return $this->get_element($cell); |
221 |
jonen |
1.1 |
} |
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
|
|
//**************************************************************** |
226 |
|
|
// Misc functions |
227 |
|
|
//**************************************************************** |
228 |
|
|
|
229 |
|
|
/** |
230 |
|
|
* set the newline_after_opentag flag |
231 |
jonen |
1.4 |
* |
232 |
|
|
* {@source } |
233 |
|
|
* |
234 |
|
|
* @param boolean TRUE or FALSE |
235 |
|
|
* @return none |
236 |
jonen |
1.1 |
*/ |
237 |
|
|
function set_newline_after_opentag( $flag ) { |
238 |
jonen |
1.2 |
if ($flag) { |
239 |
|
|
$this->_flags |= _NEWLINEAFTEROPENTAG; |
240 |
|
|
} else{ |
241 |
|
|
$this->_flags &= ~_NEWLINEAFTEROPENTAG; |
242 |
|
|
} |
243 |
jonen |
1.1 |
} |
244 |
|
|
|
245 |
|
|
/** |
246 |
|
|
* set the newline_after_content flag |
247 |
jonen |
1.4 |
* |
248 |
|
|
* {@source } |
249 |
|
|
* |
250 |
|
|
* @param boolean TRUE or FALSE |
251 |
|
|
* @return none |
252 |
jonen |
1.1 |
*/ |
253 |
|
|
function set_newline_after_closetag( $flag ) { |
254 |
jonen |
1.2 |
if ($flag) { |
255 |
|
|
$this->_flags |= _NEWLINEAFTERCLOSETAG; |
256 |
|
|
} else{ |
257 |
|
|
$this->_flags &= ~_NEWLINEAFTERCLOSETAG; |
258 |
|
|
} |
259 |
jonen |
1.1 |
} |
260 |
|
|
|
261 |
jonen |
1.3 |
/** |
262 |
|
|
* This method turns on the automatic wrapping |
263 |
|
|
* of the tag's content inside the CDATA wrapper |
264 |
|
|
* for XML |
265 |
|
|
* |
266 |
jonen |
1.4 |
* {@source } |
267 |
jonen |
1.3 |
* @param boolean $flag TRUE or FALSE |
268 |
jonen |
1.4 |
* @return none |
269 |
jonen |
1.3 |
*/ |
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 |
jonen |
1.1 |
/** |
280 |
|
|
* This function turns on the collapse flag |
281 |
jonen |
1.4 |
* |
282 |
|
|
* {@source } |
283 |
jonen |
1.1 |
* |
284 |
|
|
* @param boolean - the collapse flag |
285 |
|
|
* @param boolean - the indent flag |
286 |
|
|
* DEFAULT: TRUE; |
287 |
jonen |
1.4 |
* @return none |
288 |
jonen |
1.1 |
*/ |
289 |
|
|
function set_collapse($collapse=TRUE, $indent=TRUE) { |
290 |
jonen |
1.2 |
if ($collapse) { |
291 |
|
|
$this->_flags |= _COLLAPSE; |
292 |
|
|
} else { |
293 |
|
|
$this->_flags &= ~_COLLAPSE; |
294 |
|
|
} |
295 |
jonen |
1.1 |
|
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 |
jonen |
1.2 |
} |
303 |
jonen |
1.1 |
} |
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 |
jonen |
1.4 |
* |
311 |
|
|
* {@source } |
312 |
|
|
* @access private |
313 |
|
|
* @return none |
314 |
jonen |
1.1 |
*/ |
315 |
|
|
function _prepare_flags() { |
316 |
jonen |
1.2 |
if ($this->_flags & _CONTENTREQUIRED) { |
317 |
|
|
if ($this->count_content() == 1) { |
318 |
jonen |
1.1 |
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 |
jonen |
1.2 |
$this->_flags &= ~_NEWLINEAFTEROPENTAG; |
323 |
jonen |
1.1 |
} |
324 |
|
|
} |
325 |
jonen |
1.3 |
} else if ($this->count_content() == 0) { |
326 |
|
|
$this->_flags &= ~(_CONTENTREQUIRED | _CLOSETAGREQUIRED); |
327 |
|
|
} |
328 |
jonen |
1.1 |
} |
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 |
jonen |
1.4 |
* |
341 |
|
|
* {@source } |
342 |
|
|
* @access private |
343 |
jonen |
1.1 |
* |
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 |
jonen |
1.4 |
* @return string |
348 |
jonen |
1.1 |
*/ |
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 |
jonen |
1.2 |
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 |
jonen |
1.1 |
} |
359 |
jonen |
1.2 |
//save on mem |
360 |
jonen |
1.4 |
$xml = $indent . ((isset($this->_tag_prefix) && $this->_tag_prefix) ? $this->_tag_prefix : _TAG_PREFIX) . $this->_tag; |
361 |
jonen |
1.1 |
|
362 |
|
|
foreach( $this->_attributes as $name => $value) { |
363 |
|
|
$xml .= $this->_build_attribute_string($name, $value); |
364 |
|
|
} |
365 |
|
|
|
366 |
jonen |
1.2 |
if ( !($this->_flags & _CLOSETAGREQUIRED) && !($this->_flags & _NOFINISHSLASHXHTML) |
367 |
jonen |
1.1 |
&& $finish_slash ) { |
368 |
jonen |
1.3 |
$xml .= " /".(@$this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX); |
369 |
jonen |
1.1 |
} else { |
370 |
jonen |
1.3 |
$xml .= (@$this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX); |
371 |
jonen |
1.1 |
} |
372 |
|
|
|
373 |
jonen |
1.2 |
if ($this->_flags & _NEWLINEAFTEROPENTAG) { |
374 |
jonen |
1.1 |
$xml .= "\n"; |
375 |
|
|
} |
376 |
jonen |
1.2 |
|
377 |
jonen |
1.1 |
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 |
jonen |
1.4 |
* {@source } |
387 |
|
|
* @access private |
388 |
|
|
* |
389 |
jonen |
1.1 |
* @param int - the indent level |
390 |
jonen |
1.4 |
* @return string |
391 |
jonen |
1.1 |
*/ |
392 |
|
|
function _render_content( $indent_level, $output_debug=0 ) { |
393 |
|
|
|
394 |
|
|
//walk through the content |
395 |
jonen |
1.2 |
$xml = ''; |
396 |
|
|
for ($x=0; $x<=$this->_data_count-1; $x++) { |
397 |
|
|
$item = &$this->_content[$x]; |
398 |
jonen |
1.1 |
if (method_exists($item, "render")) { |
399 |
jonen |
1.2 |
if (($this->_flags & _COLLAPSE) && method_exists($item, "set_collapse")) { |
400 |
jonen |
1.1 |
$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 |
jonen |
1.2 |
if ($this->_flags & _COLLAPSE) { |
410 |
jonen |
1.1 |
$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 |
jonen |
1.3 |
$indent = $this->_render_indent($indent); |
418 |
jonen |
1.2 |
if ($this->_flags & _NEWLINEAFTEROPENTAG) { |
419 |
jonen |
1.1 |
$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 |
jonen |
1.2 |
if ($this->_flags & _CDATACONTENTWRAP) { |
429 |
|
|
if ($this->_flags & _COLLAPSE) { |
430 |
jonen |
1.1 |
$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 |
jonen |
1.4 |
* {@source } |
448 |
|
|
* @access private |
449 |
|
|
* |
450 |
jonen |
1.1 |
* @param int - the indent level |
451 |
jonen |
1.4 |
* @return string |
452 |
jonen |
1.1 |
*/ |
453 |
|
|
function _render_close_tag( $indent_level ) { |
454 |
jonen |
1.2 |
if (!($this->_flags & _CLOSETAGREQUIRED)) { |
455 |
jonen |
1.1 |
return ''; |
456 |
|
|
} |
457 |
|
|
|
458 |
|
|
$indent = ""; |
459 |
jonen |
1.2 |
if (($this->_flags & _INDENT) && ($this->_flags & _NEWLINEAFTEROPENTAG)) { |
460 |
jonen |
1.1 |
$indent = $this->_render_indent($indent_level); |
461 |
|
|
} |
462 |
|
|
$str = $indent ."</".$this->_tag.">"; |
463 |
|
|
|
464 |
jonen |
1.2 |
if ($this->_flags & _NEWLINEAFTERCLOSETAG) { |
465 |
jonen |
1.1 |
$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 |
jonen |
1.4 |
* |
476 |
|
|
* {@source } |
477 |
|
|
* @access private |
478 |
jonen |
1.1 |
* |
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 |
jonen |
1.3 |
return " ".$name."=\"".$value."\""; |
486 |
jonen |
1.1 |
} |
487 |
|
|
} |
488 |
|
|
?> |