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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Thu Jan 30 03:29:08 2003 UTC (21 years, 5 months ago) by jonen
Branch: MAIN
Branch point for: no_vendor_tag
Initial revision

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

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