1 <?php rcs_id('$Id: XmlElement.php,v 1.2 2002-01-21 06:55:47 dairiki Exp $');
3 * Code for writing XML.
11 function XmlElement ($tagname /* , $attr_or_content , ...*/) {
12 $this->_tag = $tagname;
13 $this->_content = array();
15 if (func_num_args() > 1)
16 $this->_init(array_slice(func_get_args(), 1));
18 $this->_attr = array();
21 function _init ($args) {
22 assert(is_array($args));
26 if (is_array($args[0]))
27 $this->_attr = array_shift($args);
29 $this->_attr = array();
30 if (count($args) > 1 && ! $args[0])
34 if (count($args) == 1 && is_array($args[0]))
35 $this->_content = $args[0];
37 $this->_content = $args;
44 function setAttr ($attr, $value = false) {
45 if (is_array($attr)) {
46 assert($value === false);
47 foreach ($attr as $a => $v)
52 assert(is_string($attr));
53 if ($value === false) {
54 unset($this->_attr[$attr]);
58 $this->_attr[$attr] = (string) $value;
61 function getAttr ($attr) {
62 if (isset($this->_attr[$attr]))
63 return $this->_attr[$attr];
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);
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);
82 function getContent () {
83 return $this->_content;
86 function _startTag () {
88 foreach ($this->_attr as $attr => $val)
89 $tag .= " $attr=\"" . $this->_quoteAttr($val) . '"';
93 function printXML () {
94 if (!$this->_content) {
95 echo "<" . $this->_startTag() . "/>";
98 echo "<" . $this->_startTag() . ">";
99 foreach ($this->_content as $c)
101 echo "</$this->_tag>";
106 if (!$this->_content) {
107 return "<" . $this->_startTag() . "/>";
110 $xml = "<" . $this->_startTag() . ">";
111 foreach ($this->_content as $c)
113 $xml .= "</$this->_tag>";
117 function asString () {
119 foreach ($this->_content as $c)
120 $val .= AsString($c);
124 function _quote ($string) {
125 return str_replace('<', '<',
126 str_replace('>', '>',
127 str_replace('&', '&', $string)));
130 function _quoteAttr ($value) {
131 return str_replace('"', '"', XmlElement::_quote($value));
136 function RawXml ($xml_text) {
137 $this->_xml = $xml_text;
140 function printXML () {
149 class FormattedText {
150 function FormattedText ($fs /* , ... */) {
152 $this->_init(func_get_args());
156 function _init ($args) {
157 $this->_fs = array_shift($args);
159 // PHP's sprintf doesn't support variable width specifiers,
160 // like sprintf("%*s", 10, "x"); --- so we won't either.
162 if (! preg_match_all('/(?<!%)%(\d+)\$/x', $this->_fs, $m)) {
163 $this->_args = $args;
166 // Format string has '%2$s' style argument reordering.
167 // PHP doesn't support this.
168 if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $fmt))
169 // literal variable name substitution only to keep locale
170 // strings uncluttered
171 trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
172 '%1\$s','%s'), E_USER_WARNING);
174 $this->_fs = preg_replace('/(?<!%)%\d+\$/x', '%', $this->_fs);
176 $this->_args = array();
177 foreach($m[1] as $argnum) {
178 if ($argnum < 1 || $argnum > count($args))
179 trigger_error(sprintf(_("%s: argument index out of range"),
180 $argnum), E_USER_WARNING);
181 $this->_args[] = $args[$argnum - 1];
187 // Not all PHP's have vsprintf, so...
188 $args[] = XmlElement::_quote($this->_fs);
189 foreach ($this->_args as $arg)
190 $args[] = AsXML($arg);
191 return call_user_func_array('sprintf', $args);
194 function printXML () {
195 // Not all PHP's have vsprintf, so...
196 $args[] = XmlElement::_quote($this->_fs);
197 foreach ($this->_args as $arg)
198 $args[] = AsXML($arg);
199 call_user_func_array('printf', $args);
202 function asString() {
203 $args = $this->_args;
204 array_unshift($args, $this->_fs);
205 return call_user_func_array('sprintf', $args);
209 function PrintXML ($val) {
210 if (is_object($val)) {
211 if (method_exists($val, 'printxml'))
212 return $val->printXML();
213 elseif (method_exists($val, 'asxml')) {
217 elseif (method_exists($val, 'asstring'))
218 $val = $val->asString();
220 elseif (is_array($val)) {
225 echo (string)XmlElement::_quote($val);
228 function AsXML ($val) {
229 if (is_object($val)) {
230 if (method_exists($val, 'asxml'))
231 return $val->asXML();
232 elseif (method_exists($val, 'asstring'))
233 $val = $val->asString();
235 elseif (is_array($val)) {
242 return XmlElement::_quote((string)$val);
245 function AsString ($val) {
246 if (can($val, 'asString'))
247 return $val->asString();
248 elseif (is_array($val)) {
251 $str .= AsString($x);
254 return (string) $val;
258 function fmt ($fs /* , ... */) {
259 $s = new FormattedText(false);
261 $args = func_get_args();
262 $args[0] = gettext($args[0]);
267 // (c-file-style: "gnu")
272 // c-hanging-comment-ender-p: nil
273 // indent-tabs-mode: nil