3 * Code for writing XML.
5 * @author: Jeff Dairiki,
6 * Reini Urban (php5 tricks)
8 * WARNING: This module is very php5 sensitive.
9 * Fixed for 1.3.9, 1.3.11 and 1.3.13 (php-5.2).
10 * With allow_call_time_pass_reference clean fixes.
14 * A sequence of (zero or more) XmlElements (possibly interspersed with
15 * plain strings (CDATA).
19 function XmlContent( /* ... */)
21 $this->_content = array();
22 $this->_pushContent_array(func_get_args());
25 function pushContent($arg /*, ...*/)
27 if (func_num_args() > 1)
28 $this->_pushContent_array(func_get_args());
29 elseif (is_array($arg))
30 $this->_pushContent_array($arg); else
31 $this->_pushContent($arg);
34 function _pushContent_array($array)
36 foreach ($array as $item) {
38 $this->_pushContent_array($item);
40 $this->_pushContent($item);
44 function _pushContent($item)
46 if (is_object($item) && strtolower(get_class($item)) == 'xmlcontent')
47 array_splice($this->_content, count($this->_content), 0,
50 $this->_content[] = $item;
53 function unshiftContent($arg /*, ...*/)
55 if (func_num_args() > 1)
56 $this->_unshiftContent_array(func_get_args());
57 elseif (is_array($arg))
58 $this->_unshiftContent_array($arg); else
59 $this->_unshiftContent($arg);
62 function _unshiftContent_array($array)
64 foreach (array_reverse($array) as $item) {
66 $this->_unshiftContent_array($item);
68 $this->_unshiftContent($item);
72 function _unshiftContent($item)
74 if (strtolower(get_class($item)) == 'xmlcontent')
75 array_splice($this->_content, 0, 0, $item->_content);
77 array_unshift($this->_content, $item);
82 return $this->_content;
85 function setContent($arg /* , ... */)
87 $this->_content = array();
88 $this->_pushContent_array(func_get_args());
93 foreach ($this->_content as $item) {
94 if (is_object($item)) {
95 if (method_exists($item, 'printXML'))
97 elseif (method_exists($item, 'asXML'))
98 echo $item->asXML(); elseif (method_exists($item, 'asString'))
99 echo $this->_quote($item->asString()); else
100 printf("==Object(%s)==", get_class($item));
101 } elseif (is_array($item)) {
103 // Use XmlContent objects instead of arrays for collections of XmlElements.
104 trigger_error("Passing arrays to printXML() is deprecated: (" . AsXML($item, true) . ")",
106 foreach ($item as $x)
109 echo $this->_quote((string)$item);
117 foreach ($this->_content as $item) {
118 if (is_object($item)) {
119 if (method_exists($item, 'asXML'))
120 $xml .= $item->asXML();
121 elseif (method_exists($item, 'asString'))
122 $xml .= $this->_quote($item->asString()); else
123 $xml .= sprintf("==Object(%s)==", get_class($item));
124 } elseif (is_array($item)) {
125 trigger_error("Passing arrays to ->asXML() is deprecated: (" . AsXML($item, true) . ")",
127 foreach ($item as $x)
128 $xml .= $this->asXML($x);
130 $xml .= $this->_quote((string)$item);
138 foreach ($this->_content as $item) {
139 if (is_object($item)) {
140 if (method_exists($item, 'asPDF'))
141 $pdf .= $item->asPDF();
142 elseif (method_exists($item, 'asString'))
143 $pdf .= $this->_quote($item->asString()); else
144 $pdf .= sprintf("==Object(%s)==", get_class($item));
146 $pdf .= $this->_quote((string)$item);
152 function __toString()
154 return $this->asString();
160 foreach ($this->_content as $item) {
161 if (is_object($item)) {
162 if (method_exists($item, 'asString')) {
163 $string = $item->asString();
164 if (is_object($string)) {
165 ; // ignore error so far: ImageLink labels
167 $val .= $this->_quote($item->asString());
170 $val .= sprintf("==Object(%s)==", get_class($item));
173 $val .= (string)$item;
179 * See if element is empty.
181 * Empty means it has no content.
182 * @return bool True if empty.
186 if (empty($this->_content))
188 foreach ($this->_content as $x) {
189 if (is_string($x) ? strlen($x) : !empty($x))
195 function _quote($string)
197 if (!$string) return $string;
198 return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
205 * @param $tagname string Tag of html element.
207 class XmlElement extends XmlContent
209 function __construct($tagname /* , $attr_or_content , ...*/)
211 parent::__construct();
212 $this->_init(func_get_args());
215 function _init($args)
217 if (!is_array($args))
218 $args = func_get_args();
220 assert(count($args) >= 1);
221 //assert(is_string($args[0]));
222 $this->_tag = array_shift($args);
224 if ($args && is_array($args[0]))
225 $this->_attr = array_shift($args);
227 $this->_attr = array();
228 if ($args && $args[0] === false)
232 $this->setContent($args);
235 /** Methods only needed for XmlParser,
236 * to be fully compatible to perl Html::Element
238 // doesn't yet work with php5 as __destruct()
241 if ($this->hasChildren()) {
242 foreach ($this->getChildren() as $node) {
248 unset($this->_content);
251 function getChildren()
253 return $this->_children;
256 function hasChildren()
258 return !empty($this->_children);
261 /* End XmlParser Methods
269 function setAttr($attr, $value = false)
271 if (is_array($attr)) {
272 assert($value === false);
273 foreach ($attr as $a => $v) {
274 $this->_attr[strtolower($a)] = $v;
275 //$this->set($a, $v);
280 assert(is_string($attr));
282 if ($value === false) {
283 unset($this->_attr[$attr]);
287 $this->_attr[$attr] = (string)$value;
290 if ($attr == 'class')
291 unset($this->_classes);
294 function getAttr($attr)
296 if ($attr == 'class')
297 $this->_setClasses();
299 if (isset($this->_attr[strtolower($attr)]))
300 return $this->_attr[strtolower($attr)];
305 function _getClasses()
307 if (!isset($this->_classes)) {
308 $this->_classes = array();
309 if (isset($this->_attr['class'])) {
310 $classes = explode(' ', (string)$this->_attr['class']);
311 foreach ($classes as $class) {
312 $class = trim($class);
314 $this->_classes[$class] = $class;
318 return $this->_classes;
321 function _setClasses()
323 if (isset($this->_classes)) {
325 $this->_attr['class'] = join(' ', $this->_classes);
327 unset($this->_attr['class']);
332 * Manipulate the elements CSS class membership.
334 * This adds or remove an elements membership
335 * in a give CSS class.
337 * @param $class string
339 * @param $in_class bool
340 * If true (the default) the element is added to class $class.
341 * If false, the element is removed from the class.
343 function setInClass($class, $in_class = true)
345 $this->_getClasses();
346 $class = trim($class);
348 $this->_classes[$class] = $class;
350 unset($this->_classes[$class]);
354 * Is element in a given (CSS) class?
356 * This checks for the presence of a particular class in the
357 * elements 'class' attribute.
359 * @param $class string The class to check for.
360 * @return bool True if the element is a member of $class.
362 function inClass($class)
364 $this->_parseClasses();
365 return isset($this->_classes[trim($class)]);
370 $start = "<" . $this->_tag;
371 $this->_setClasses();
372 foreach ($this->_attr as $attr => $val) {
378 $qval = str_replace("\"", '"', $this->_quote((string)$val));
379 $start .= " $attr=\"$qval\"";
387 return substr($this->startTag(), 0, -1) . "/>";
392 return "</$this->_tag>";
397 if ($this->isEmpty())
398 echo $this->emptyTag();
400 echo $this->startTag();
401 // FIXME: The next two lines could be removed for efficiency
402 if (!$this->hasInlineContent())
404 XmlContent::printXML();
405 echo "</$this->_tag>";
407 if (!$this->isInlineElement())
413 if ($this->isEmpty()) {
414 $xml = $this->emptyTag();
416 $xml = $this->startTag();
417 // FIXME: The next two lines could be removed for efficiency
418 if (!$this->hasInlineContent())
420 $xml .= XmlContent::asXML();
421 $xml .= "</$this->_tag>";
423 if (!$this->isInlineElement())
429 * Can this element have inline content?
431 * This is a hack, but is probably the best one can do without
432 * knowledge of the DTD...
434 function hasInlineContent()
437 if (empty($this->_content))
439 if (is_object($this->_content[0]))
445 * Is this element part of inline content?
447 * This is a hack, but is probably the best one can do without
448 * knowledge of the DTD...
450 function isInlineElement()
459 function RawXml($xml_text)
461 $this->_xml = $xml_text;
470 function __toString()
487 return empty($this->_xml);
493 function FormattedText($fs /* , ... */)
496 $this->_init(func_get_args());
500 function _init($args)
502 $this->_fs = array_shift($args);
504 // PHP's sprintf doesn't support variable width specifiers,
505 // like sprintf("%*s", 10, "x"); --- so we won't either.
507 if (!preg_match_all('/(?<!%)%(\d+)\$/x', $this->_fs, $m)) {
508 $this->_args = $args;
510 // Format string has '%2$s' style argument reordering.
511 // PHP doesn't support this.
512 if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $this->_fs)) // $fmt
513 // literal variable name substitution only to keep locale
514 // strings uncluttered
515 trigger_error(sprintf(_("Can't mix ā%sā with ā%sā type format strings"),
516 '%1\$s', '%s'), E_USER_WARNING);
518 $this->_fs = preg_replace('/(?<!%)%\d+\$/x', '%', $this->_fs);
520 $this->_args = array();
521 foreach ($m[1] as $argnum) {
522 if ($argnum < 1 || $argnum > count($args))
523 trigger_error(sprintf("%s: argument index out of range",
524 $argnum), E_USER_WARNING);
525 $this->_args[] = $args[$argnum - 1];
532 // Not all PHP's have vsprintf, so...
533 $args[] = XmlElement::_quote((string)$this->_fs);
534 foreach ($this->_args as $arg)
535 $args[] = AsXML($arg);
536 return call_user_func_array('sprintf', $args);
541 // Not all PHP's have vsprintf, so...
542 $args[] = XmlElement::_quote((string)$this->_fs);
543 foreach ($this->_args as $arg)
544 $args[] = AsXML($arg);
545 call_user_func_array('printf', $args);
550 $args[] = $this->_fs;
551 foreach ($this->_args as $arg)
552 $args[] = AsString($arg);
553 return call_user_func_array('sprintf', $args);
557 function __toString()
559 return $this->asString();
565 * Error[2048]: Non-static method XmlContent::_quote() should not be called statically
566 * Note: There's lot of room for performance increase if the right charset variant can
567 * be created on load-time.
569 function XmlContent_quote($string)
571 if (!$string) return $string;
572 return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
575 function PrintXML($val /* , ... */)
577 if (func_num_args() > 1) {
578 foreach (func_get_args() as $arg)
580 } elseif (is_object($val)) {
581 if (method_exists($val, 'printXML'))
583 elseif (method_exists($val, 'asXML')) {
585 } elseif (method_exists($val, 'asString'))
586 echo XmlContent_quote($val->asString()); else
587 printf("==Object(%s)==", get_class($val));
588 } elseif (is_array($val)) {
590 // Use XmlContent objects instead of arrays for collections of XmlElements.
591 trigger_error("Passing arrays to PrintXML() is deprecated: (" . AsXML($val, true) . ")",
596 echo (string)XmlContent_quote((string)$val);
599 function AsXML($val /* , ... */)
603 if (func_num_args() > 1) {
605 foreach (func_get_args() as $arg)
608 } elseif (is_object($val)) {
609 if (method_exists($val, 'asXML'))
610 return $val->asXML();
611 elseif (method_exists($val, 'asString'))
612 return XmlContent_quote($val->asString()); else
613 return sprintf("==Object(%s)==", get_class($val));
614 } elseif (is_array($val)) {
616 // Use XmlContent objects instead of arrays for collections of XmlElements.
617 if (empty($nowarn)) {
619 trigger_error("Passing arrays to AsXML() is deprecated: (" . AsXML($val) . ")",
628 return XmlContent_quote((string)$val);
631 function AsString($val)
633 if (func_num_args() > 1) {
635 foreach (func_get_args() as $arg)
636 $str .= AsString($arg);
638 } elseif (is_object($val)) {
639 if (method_exists($val, 'asString'))
640 return $val->asString();
642 return sprintf("==Object(%s)==", get_class($val));
643 } elseif (is_array($val)) {
645 // Use XmlContent objects instead of arrays for collections of XmlElements.
646 trigger_error("Passing arrays to AsString() is deprecated", E_USER_NOTICE);
649 $str .= AsString($x);
656 function fmt($fs /* , ... */)
658 $s = new FormattedText(false);
660 $args = func_get_args();
661 $args[0] = _($args[0]);
670 // c-hanging-comment-ender-p: nil
671 // indent-tabs-mode: nil