]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/XmlElement.php
Converted to use new PageList class.
[SourceForge/phpwiki.git] / lib / XmlElement.php
1 <?php rcs_id('$Id: XmlElement.php,v 1.3 2002-01-21 16:59:01 dairiki Exp $');
2 /*
3  * Code for writing XML.
4  */
5
6 /**
7  * An XML element.
8  */
9 class XmlElement
10 {
11     function XmlElement ($tagname /* , $attr_or_content , ...*/) {
12         $this->_tag = $tagname;
13         $this->_content = array();
14
15         if (func_num_args() > 1)
16             $this->_init(array_slice(func_get_args(), 1));
17         else 
18             $this->_attr = array();
19     }
20
21     function _init ($args) {
22         assert(is_array($args));
23         if (!$args)
24             return;
25
26         if (is_array($args[0]))
27             $this->_attr = array_shift($args);
28         else {
29             $this->_attr = array();
30             if (count($args) > 1 && ! $args[0])
31                 array_shift($args);
32         }
33
34         if (count($args) == 1 && is_array($args[0]))
35             $this->_content = $args[0];
36         else
37             $this->_content = $args;
38     }
39
40     function getTag () {
41         return $this->_tag;
42     }
43     
44     function setAttr ($attr, $value = false) {
45         if (is_array($attr)) {
46             assert($value === false);
47             foreach ($attr as $a => $v)
48                 $this->set($a, $v);
49             return;
50         }
51
52         assert(is_string($attr));
53         if ($value === false) {
54             unset($this->_attr[$attr]);
55         }
56         if (is_bool($value))
57             $value = $attr;
58         $this->_attr[$attr] = (string) $value;
59     }
60
61     function getAttr ($attr) {
62         if (isset($this->_attr[$attr]))
63             return $this->_attr[$attr];
64         else
65             return false;
66     }
67     
68     function pushContent ($args /*, ...*/) {
69         $c = &$this->_content;
70         if (func_num_args() != 1 || ! is_array($args))
71             $args = func_get_args();
72         array_splice($c, count($c), 0, $args);
73     }
74
75     function unshiftContent ($args /*, ...*/) {
76         $c = &$this->_content;
77         if (func_num_args() != 1 || ! is_array($args))
78             $args = func_get_args();
79         array_splice($c, 0, 0, $args);
80     }
81
82     function getContent () {
83         return $this->_content;
84     }
85
86     function _startTag() {
87         $start = "<" . $this->_tag;
88         foreach ($this->_attr as $attr => $val)
89             $start .= " $attr=\"" . $this->_quoteAttr($val) . '"';
90         $start .= ">";
91         return $start;
92     }
93
94     function _emptyTag() {
95         return substr($this->_startTag(), 0, -1) . "/>";
96     }
97     
98         
99     function printXML () {
100         
101         if ($this->isEmpty()) {
102             echo $this->_emptyTag();
103         }
104         else {
105             $sep = $this->hasInlineContent() ? "" : "\n";
106             echo $this->_startTag() . $sep;
107             foreach ($this->_content as $c) {
108                 PrintXML($c);
109                 echo $sep;
110             }
111             echo "</$this->_tag>";
112         }
113     }
114
115     function asXML () {
116         if ($this->isEmpty())
117             return $this->_emptyTag();
118
119         $sep = $this->hasInlineContent() ? "" : "\n";
120         $xml =  $this->_startTag() . $sep;
121         foreach ($this->_content as $c)
122             $xml .= AsXML($c) . $sep;
123         return $xml . "</$this->_tag>";
124     }
125
126     function asString () {
127         $str = '';
128         foreach ($this->_content as $c)
129             $val .= AsString($c);
130         return trim($str);
131     }
132
133     function isEmpty () {
134         return empty($this->_content);
135     }
136
137     function hasInlineContent () {
138         // FIXME: This is a hack.
139         if (empty($this->_content))
140             return false;
141         if (is_object($this->_content[0]))
142             return false;
143         return true;
144     }
145     
146     function _quote ($string) {
147         return str_replace('<', '&lt;',
148                            str_replace('>', '&gt;',
149                                        str_replace('&', '&amp;', $string)));
150     }
151
152     function _quoteAttr ($value) {
153         return str_replace('"', '&quot;', XmlElement::_quote($value));
154     }
155 };
156
157 class RawXml {
158     function RawXml ($xml_text) {
159         $this->_xml = $xml_text;
160     }
161
162     function printXML () {
163         echo $this->_xml;
164     }
165
166     function asXML () {
167         return $this->_xml;
168     }
169 }
170
171 class FormattedText {
172     function FormattedText ($fs /* , ... */) {
173         if ($fs !== false) {
174             $this->_init(func_get_args());
175         }
176     }
177
178     function _init ($args) {
179         $this->_fs = array_shift($args);
180
181         // PHP's sprintf doesn't support variable width specifiers,
182         // like sprintf("%*s", 10, "x"); --- so we won't either.
183
184         if (! preg_match_all('/(?<!%)%(\d+)\$/x', $this->_fs, $m)) {
185             $this->_args  = $args;
186         }
187         else {
188             // Format string has '%2$s' style argument reordering.
189             // PHP doesn't support this.
190             if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $fmt))
191                 // literal variable name substitution only to keep locale
192                 // strings uncluttered
193                 trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
194                                       '%1\$s','%s'), E_USER_WARNING);
195         
196             $this->_fs = preg_replace('/(?<!%)%\d+\$/x', '%', $this->_fs);
197
198             $this->_args = array();
199             foreach($m[1] as $argnum) {
200                 if ($argnum < 1 || $argnum > count($args))
201                     trigger_error(sprintf(_("%s: argument index out of range"), 
202                                           $argnum), E_USER_WARNING);
203                 $this->_args[] = $args[$argnum - 1];
204             }
205         }
206     }
207
208     function asXML () {
209         // Not all PHP's have vsprintf, so...
210         $args[] = XmlElement::_quote($this->_fs);
211         foreach ($this->_args as $arg)
212             $args[] = AsXML($arg);
213         return call_user_func_array('sprintf', $args);
214     }
215
216     function printXML () {
217         // Not all PHP's have vsprintf, so...
218         $args[] = XmlElement::_quote($this->_fs);
219         foreach ($this->_args as $arg)
220             $args[] = AsXML($arg);
221         call_user_func_array('printf', $args);
222     }
223
224     function asString() {
225         $args = $this->_args;
226         array_unshift($args, $this->_fs);
227         return call_user_func_array('sprintf', $args);
228     }
229 }
230
231 function PrintXML ($val) {
232     if (is_object($val)) {
233         if (method_exists($val, 'printxml'))
234             return $val->printXML();
235         elseif (method_exists($val, 'asxml')) {
236             echo $val->asXML();
237             return;
238         }
239         elseif (method_exists($val, 'asstring'))
240             $val = $val->asString();
241     }
242     elseif (is_array($val)) {
243         foreach ($val as $x)
244             PrintXML($x);
245     }
246         
247     echo (string)XmlElement::_quote($val);
248 }
249
250 function AsXML ($val) {
251     if (is_object($val)) {
252         if (method_exists($val, 'asxml'))
253             return $val->asXML();
254         elseif (method_exists($val, 'asstring'))
255             $val = $val->asString();
256     }
257     elseif (is_array($val)) {
258         $xml = '';
259         foreach ($val as $x)
260             $xml .= AsXML($x);
261         return $xml;
262     }
263     
264     return XmlElement::_quote((string)$val);
265 }
266
267 function AsString ($val) {
268     if (can($val, 'asString'))
269         return $val->asString();
270     elseif (is_array($val)) {
271         $str = '';
272         foreach ($val as $x)
273             $str .= AsString($x);
274         return $str;
275     }
276     return (string) $val;
277 }
278
279     
280 function fmt ($fs /* , ... */) {
281     $s = new FormattedText(false);
282
283     $args = func_get_args();
284     $args[0] = gettext($args[0]);
285     $s->_init($args);
286     return $s;
287 }
288     
289 // (c-file-style: "gnu")
290 // Local Variables:
291 // mode: php
292 // tab-width: 8
293 // c-basic-offset: 4
294 // c-hanging-comment-ender-p: nil
295 // indent-tabs-mode: nil
296 // End:   
297 ?>