/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/XMLTagClass.inc
ViewVC logotype

Contents of /nfo/php/libs/com.newsblob.phphtmllib/XMLTagClass.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Sat Feb 22 20:55:24 2003 UTC (21 years, 4 months ago) by jonen
Branch: MAIN
Changes since 1.1: +62 -122 lines
+ updated whole lib to version 2.2.1 (new FormProcessing since 2.2.0!)

1 <?php
2
3 /**
4 * Holds the XMLTagClass
5 *
6 * $Id: XMLTagClass.inc,v 1.28 2003/02/20 23:23:43 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 function turns on the collapse flag
226 *
227 * @param boolean - the collapse flag
228 * @param boolean - the indent flag
229 * DEFAULT: TRUE;
230 */
231 function set_collapse($collapse=TRUE, $indent=TRUE) {
232 if ($collapse) {
233 $this->_flags |= _COLLAPSE;
234 } else {
235 $this->_flags &= ~_COLLAPSE;
236 }
237
238 $this->set_newline_after_opentag(FALSE);
239 $this->set_indent_flag($indent);
240 if ($indent) {
241 $this->set_newline_after_closetag(TRUE);
242 } else {
243 $this->set_newline_after_closetag(FALSE);
244 }
245 }
246
247 /**
248 * This function checks to see if
249 * there is only 1 content data, and
250 * its not an object, then it auto
251 * sets some of the indentation flags
252 *
253 */
254 function _prepare_flags() {
255 if ($this->_flags & _CONTENTREQUIRED) {
256 if ($this->count_content() == 1) {
257 if (!is_object($this->_content[0])) {
258 //ok looks like this object has only
259 //1 data for content and its a string.
260 if ( !strstr($this->_content[0], "\n") ) {
261 $this->_flags &= ~_NEWLINEAFTEROPENTAG;
262 }
263 }
264 }
265 }
266 }
267
268
269 /****************************************/
270 /* Some helper methods for rendering */
271 /* the output xml tree. */
272 /****************************************/
273
274 /**
275 * this function is responsible for
276 * rendering the open tag.
277 *
278 * @param int - the indent level
279 * @param boolean - do we add the finish / if we have no
280 * close tag and no content?
281 */
282 function _render_open_tag( $indent_level, $finish_slash=TRUE ) {
283 //get the indent level
284 $indent = $this->_render_indent( $indent_level );
285
286 //build the tag
287 if ($this->_flags & _ALWAYS_LOWERCASE) {
288 $this->_tag = strtolower($this->_tag);
289 } else if ($this->_flags & _ALWAYS_UPPERCASE) {
290 $this->_tag = strtoupper($this->_tag);
291 }
292 //save on mem
293 $xml = $indent . ($this->_tag_prefix ? $this->_tag_prefix : _TAG_PREFIX) . $this->_tag;
294
295 foreach( $this->_attributes as $name => $value) {
296 $xml .= $this->_build_attribute_string($name, $value);
297 }
298
299 if ( !($this->_flags & _CLOSETAGREQUIRED) && !($this->_flags & _NOFINISHSLASHXHTML)
300 && $finish_slash ) {
301 $xml .= " /".($this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX);
302 } else {
303 $xml .= ($this->_tag_postfix ? $this->_tag_postfix : _TAG_SUFFIX);
304 }
305
306 if ($this->_flags & _NEWLINEAFTEROPENTAG) {
307 $xml .= "\n";
308 }
309
310 return $xml;
311 }
312
313
314 /**
315 * this function is reponsible for
316 * rendering the pcdata, or content
317 * of the tag (if any)
318 *
319 * @param int - the indent level
320 */
321 function _render_content( $indent_level, $output_debug=0 ) {
322
323 //walk through the content
324 $xml = '';
325 for ($x=0; $x<=$this->_data_count-1; $x++) {
326 $item = &$this->_content[$x];
327 if (method_exists($item, "render")) {
328 if (($this->_flags & _COLLAPSE) && method_exists($item, "set_collapse")) {
329 $item->set_collapse(TRUE, FALSE);
330 }
331 if ($indent_level == INDENT_LEFT_JUSTIFY) {
332 $indent = INDENT_LEFT_JUSTIFY;
333 } else {
334 $indent = $indent_level + 1;
335 }
336 $xml .= $item->render($indent, $output_debug);
337 } else {
338 if ($this->_flags & _COLLAPSE) {
339 $xml .= $item;
340 } else {
341 if ($indent_level == INDENT_LEFT_JUSTIFY) {
342 $indent = INDENT_LEFT_JUSTIFY;
343 } else {
344 $indent = $indent_level + 1;
345 }
346 $indent = $this->_render_indent($indent + 1);
347 if ($this->_flags & _NEWLINEAFTEROPENTAG) {
348 $item = str_replace("\n", "\n" . $indent, $item);
349 $xml .= $indent . $item . "\n";
350 } else {
351 $item = str_replace("\n", "\n" . $indent, $item);
352 $xml .= $item;
353 }
354 }
355 }
356 }
357 if ($this->_flags & _CDATACONTENTWRAP) {
358 if ($this->_flags & _COLLAPSE) {
359 $xml = "<![CDATA[ ".$xml." ]]>";
360 } else {
361 $indent = $this->_render_indent($indent+1);
362 $indent1 = $this->_render_indent($indent+2);
363 $indent2 = $this->_render_indent($indent+3);
364 $xml = "\n".$indent1."<![CDATA[\n".$xml."\n".$indent1."]]>\n".$indent;
365 }
366
367 }
368 return $xml;
369 }
370
371
372 /**
373 * this function is reposnsible for
374 * rendering the closing tag (if any)
375 *
376 * @param int - the indent level
377 */
378 function _render_close_tag( $indent_level ) {
379 if (!($this->_flags & _CLOSETAGREQUIRED)) {
380 return '';
381 }
382
383 $indent = "";
384 if (($this->_flags & _INDENT) && ($this->_flags & _NEWLINEAFTEROPENTAG)) {
385 $indent = $this->_render_indent($indent_level);
386 }
387 $str = $indent ."</".$this->_tag.">";
388
389 if ($this->_flags & _NEWLINEAFTERCLOSETAG) {
390 $str .= "\n";
391 }
392
393 return $str;
394 }
395
396 /**
397 * this builds an attribute for an XML tag.
398 * XML attributes MUST have a name AND a
399 * value.
400 *
401 * @param string - $name attribute name
402 * @param mixed - $value attribute value
403 * @return the tag attribute name=value pair.
404 * to be added to the tag.
405 */
406 function _build_attribute_string($name, $value) {
407 if ( ((int)$name - 0) === $name) {
408 $returnval = " ".$value;
409 } else if ( $value === NULL ) {
410 $returnval = " ".$name;
411 } else {
412 $returnval= " ".$name."=\"".$value."\"";
413 }
414 return $returnval;
415 }
416 }
417 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed