1 <?php rcs_id('$Id: XmlElement.php,v 1.42 2007-01-20 11:41:30 rurban Exp $');
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 (/* ... */) {
20 $this->_content = array();
21 $this->_pushContent_array(func_get_args());
24 function pushContent ($arg /*, ...*/) {
25 if (func_num_args() > 1)
26 $this->_pushContent_array(func_get_args());
27 elseif (is_array($arg))
28 $this->_pushContent_array($arg);
30 $this->_pushContent($arg);
33 function _pushContent_array ($array) {
34 foreach ($array as $item) {
36 $this->_pushContent_array($item);
38 $this->_pushContent($item);
42 function _pushContent ($item) {
43 if (strtolower(get_class($item)) == 'xmlcontent')
44 array_splice($this->_content, count($this->_content), 0,
47 $this->_content[] = $item;
50 function unshiftContent ($arg /*, ...*/) {
51 if (func_num_args() > 1)
52 $this->_unshiftContent_array(func_get_args());
53 elseif (is_array($arg))
54 $this->_unshiftContent_array($arg);
56 $this->_unshiftContent($arg);
59 function _unshiftContent_array ($array) {
60 foreach (array_reverse($array) as $item) {
62 $this->_unshiftContent_array($item);
64 $this->_unshiftContent($item);
68 function _unshiftContent ($item) {
69 if (strtolower(get_class($item)) == 'xmlcontent')
70 array_splice($this->_content, 0, 0, $item->_content);
72 array_unshift($this->_content, $item);
75 function getContent () {
76 return $this->_content;
79 function setContent ($arg /* , ... */) {
80 $this->_content = array();
81 $this->_pushContent_array(func_get_args());
84 function printXML () {
85 foreach ($this->_content as $item) {
86 if (is_object($item)) {
87 if (method_exists($item, 'printXML'))
89 elseif (method_exists($item, 'asXML'))
91 elseif (method_exists($item, 'asString'))
92 echo $this->_quote($item->asString());
94 printf("==Object(%s)==", get_class($item));
97 echo $this->_quote((string) $item);
103 foreach ($this->_content as $item) {
104 if (is_object($item)) {
105 if (method_exists($item, 'asXML'))
106 $xml .= $item->asXML();
107 elseif (method_exists($item, 'asString'))
108 $xml .= $this->_quote($item->asString());
110 $xml .= sprintf("==Object(%s)==", get_class($item));
113 $xml .= $this->_quote((string) $item);
120 foreach ($this->_content as $item) {
121 if (is_object($item)) {
122 if (method_exists($item, 'asPDF'))
123 $pdf .= $item->asPDF();
124 elseif (method_exists($item, 'asString'))
125 $pdf .= $this->_quote($item->asString());
127 $pdf .= sprintf("==Object(%s)==", get_class($item));
130 $pdf .= $this->_quote((string) $item);
136 function __toString () {
137 return $this->asString();
140 function asString () {
142 foreach ($this->_content as $item) {
143 if (is_object($item)) {
144 if (method_exists($item, 'asString')) {
145 $val .= (string) $item->asString();
147 $val .= sprintf("==Object(%s)==", get_class($item));
151 $val .= (string) $item;
158 * See if element is empty.
160 * Empty means it has no content.
161 * @return bool True if empty.
163 function isEmpty () {
164 if (empty($this->_content))
166 foreach ($this->_content as $x) {
167 if (is_string($x) ? strlen($x) : !empty($x))
173 function _quote ($string) {
174 if (!$string) return $string;
175 if (check_php_version(4,1) and isset($GLOBALS['charset']))
176 return htmlspecialchars($string, ENT_COMPAT, $GLOBALS['charset']);
178 return htmlspecialchars($string);
185 * @param $tagname string Tag of html element.
187 class XmlElement extends XmlContent
189 function XmlElement ($tagname /* , $attr_or_content , ...*/) {
190 //FIXME: php5 incompatible
192 $this->_init(func_get_args());
195 function _init ($args) {
196 if (!is_array($args))
197 $args = func_get_args();
199 assert(count($args) >= 1);
200 //assert(is_string($args[0]));
201 $this->_tag = array_shift($args);
203 if ($args && is_array($args[0]))
204 $this->_attr = array_shift($args);
206 $this->_attr = array();
207 if ($args && $args[0] === false)
211 $this->setContent($args);
214 /** Methods only needed for XmlParser,
215 * to be fully compatible to perl Html::Element
217 // doesn't yet work with php5 as __destruct()
218 function _destruct () {
219 if ($this->hasChildren()) {
220 foreach ($this->getChildren() as $node) {
226 unset($this->_content);
229 function getChildren () {
230 return $this->_children;
233 function hasChildren () {
234 return !empty($this->_children);
236 /* End XmlParser Methods
243 function setAttr ($attr, $value = false) {
244 if (is_array($attr)) {
245 assert($value === false);
246 foreach ($attr as $a => $v) {
247 $this->_attr[strtolower($a)] = $v;
248 //$this->set($a, $v);
253 assert(is_string($attr));
255 if ($value === false) {
256 unset($this->_attr[$attr]);
261 $this->_attr[$attr] = (string) $value;
264 if ($attr == 'class')
265 unset($this->_classes);
268 function getAttr ($attr) {
269 if ($attr == 'class')
270 $this->_setClasses();
272 if (isset($this->_attr[strtolower($attr)]))
273 return $this->_attr[strtolower($attr)];
278 function _getClasses() {
279 if (!isset($this->_classes)) {
280 $this->_classes = array();
281 if (isset($this->_attr['class'])) {
282 $classes = explode(' ', (string) $this->_attr['class']);
283 foreach ($classes as $class) {
284 $class = trim($class);
286 $this->_classes[$class] = $class;
290 return $this->_classes;
293 function _setClasses() {
294 if (isset($this->_classes)) {
296 $this->_attr['class'] = join(' ', $this->_classes);
298 unset($this->_attr['class']);
303 * Manipulate the elements CSS class membership.
305 * This adds or remove an elements membership
306 * in a give CSS class.
308 * @param $class string
310 * @param $in_class bool
311 * If true (the default) the element is added to class $class.
312 * If false, the element is removed from the class.
314 function setInClass($class, $in_class=true) {
315 $this->_getClasses();
316 $class = trim($class);
318 $this->_classes[$class] = $class;
320 unset($this->_classes[$class]);
324 * Is element in a given (CSS) class?
326 * This checks for the presence of a particular class in the
327 * elements 'class' attribute.
329 * @param $class string The class to check for.
330 * @return bool True if the element is a member of $class.
332 function inClass($class) {
333 $this->_parseClasses();
334 return isset($this->_classes[trim($class)]);
337 function startTag() {
338 $start = "<" . $this->_tag;
339 $this->_setClasses();
340 foreach ($this->_attr as $attr => $val) {
346 $qval = str_replace("\"", '"', $this->_quote((string)$val));
347 $start .= " $attr=\"$qval\"";
353 function emptyTag() {
354 return substr($this->startTag(), 0, -1) . "/>";
359 return "</$this->_tag>";
363 function printXML () {
364 if ($this->isEmpty())
365 echo $this->emptyTag();
367 echo $this->startTag();
368 // FIXME: The next two lines could be removed for efficiency
369 if (!$this->hasInlineContent())
371 XmlContent::printXML();
372 echo "</$this->_tag>";
374 if (!$this->isInlineElement())
379 if ($this->isEmpty()) {
380 $xml = $this->emptyTag();
383 $xml = $this->startTag();
384 // FIXME: The next two lines could be removed for efficiency
385 if (!$this->hasInlineContent())
387 $xml .= XmlContent::asXML();
388 $xml .= "</$this->_tag>";
390 if (!$this->isInlineElement())
396 * Can this element have inline content?
398 * This is a hack, but is probably the best one can do without
399 * knowledge of the DTD...
401 function hasInlineContent () {
403 if (empty($this->_content))
405 if (is_object($this->_content[0]))
411 * Is this element part of inline content?
413 * This is a hack, but is probably the best one can do without
414 * knowledge of the DTD...
416 function isInlineElement () {
423 function RawXml ($xml_text) {
424 $this->_xml = $xml_text;
427 function printXML () {
432 function __toString () {
440 function isEmpty () {
441 return empty($this->_xml);
445 class FormattedText {
446 function FormattedText ($fs /* , ... */) {
448 $this->_init(func_get_args());
452 function _init ($args) {
453 $this->_fs = array_shift($args);
455 // PHP's sprintf doesn't support variable width specifiers,
456 // like sprintf("%*s", 10, "x"); --- so we won't either.
458 if (! preg_match_all('/(?<!%)%(\d+)\$/x', $this->_fs, $m)) {
459 $this->_args = $args;
462 // Format string has '%2$s' style argument reordering.
463 // PHP doesn't support this.
464 if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $this->_fs)) // $fmt
465 // literal variable name substitution only to keep locale
466 // strings uncluttered
467 trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
468 '%1\$s','%s'), E_USER_WARNING);
470 $this->_fs = preg_replace('/(?<!%)%\d+\$/x', '%', $this->_fs);
472 $this->_args = array();
473 foreach($m[1] as $argnum) {
474 if ($argnum < 1 || $argnum > count($args))
475 trigger_error(sprintf("%s: argument index out of range",
476 $argnum), E_USER_WARNING);
477 $this->_args[] = $args[$argnum - 1];
483 // Not all PHP's have vsprintf, so...
484 $args[] = XmlElement::_quote((string)$this->_fs);
485 foreach ($this->_args as $arg)
486 $args[] = AsXML($arg);
487 return call_user_func_array('sprintf', $args);
490 function printXML () {
491 // Not all PHP's have vsprintf, so...
492 $args[] = XmlElement::_quote((string)$this->_fs);
493 foreach ($this->_args as $arg)
494 $args[] = AsXML($arg);
495 call_user_func_array('printf', $args);
498 function asString() {
499 $args[] = $this->_fs;
500 foreach ($this->_args as $arg)
501 $args[] = AsString($arg);
502 return call_user_func_array('sprintf', $args);
506 function __toString () {
507 return $this->asString();
513 * Error[2048]: Non-static method XmlContent::_quote() should not be called statically
514 * Note: There's lot of room for performance increase if the right charset variant can
515 * be created on load-time.
517 function XmlContent_quote ($string) {
518 if (!$string) return $string;
519 if (check_php_version(4,1) and isset($GLOBALS['charset'])
520 and (!defined('IGNORE_CHARSET_NOT_SUPPORTED_WARNING') or !IGNORE_CHARSET_NOT_SUPPORTED_WARNING))
522 return htmlspecialchars($string, ENT_COMPAT, $GLOBALS['charset']);
524 return htmlspecialchars($string);
528 function PrintXML ($val /* , ... */ ) {
529 if (func_num_args() > 1) {
530 foreach (func_get_args() as $arg)
533 elseif (is_object($val)) {
534 if (method_exists($val, 'printXML'))
536 elseif (method_exists($val, 'asXML')) {
539 elseif (method_exists($val, 'asString'))
540 echo XmlContent_quote($val->asString());
542 printf("==Object(%s)==", get_class($val));
544 elseif (is_array($val)) {
546 // Use XmlContent objects instead of arrays for collections of XmlElements.
547 trigger_error("Passing arrays to PrintXML() is deprecated: (" . AsXML($val, true) . ")",
553 echo (string)XmlContent_quote((string)$val);
556 function AsXML ($val /* , ... */) {
559 if (func_num_args() > 1) {
561 foreach (func_get_args() as $arg)
565 elseif (is_object($val)) {
566 if (method_exists($val, 'asXML'))
567 return $val->asXML();
568 elseif (method_exists($val, 'asString'))
569 return XmlContent_quote($val->asString());
571 return sprintf("==Object(%s)==", get_class($val));
573 elseif (is_array($val)) {
575 // Use XmlContent objects instead of arrays for collections of XmlElements.
576 if (empty($nowarn)) {
578 trigger_error("Passing arrays to AsXML() is deprecated: (" . AsXML($val) . ")",
588 return XmlContent_quote((string)$val);
591 function AsString ($val) {
592 if (func_num_args() > 1) {
594 foreach (func_get_args() as $arg)
595 $str .= AsString($arg);
598 elseif (is_object($val)) {
599 if (method_exists($val, 'asString'))
600 return $val->asString();
602 return sprintf("==Object(%s)==", get_class($val));
604 elseif (is_array($val)) {
606 // Use XmlContent objects instead of arrays for collections of XmlElements.
607 trigger_error("Passing arrays to AsString() is deprecated", E_USER_NOTICE);
610 $str .= AsString($x);
614 return (string) $val;
618 function fmt ($fs /* , ... */) {
619 $s = new FormattedText(false);
621 $args = func_get_args();
622 $args[0] = _($args[0]);
627 // $Log: not supported by cvs2svn $
628 // Revision 1.41 2007/01/13 23:34:49 rurban
629 // More php-5.2.0 fixes __toString
631 // Revision 1.40 2007/01/13 23:28:56 rurban
632 // php-5.2.0 fix for string fallback: Provide the magic method __toString
634 // Revision 1.39 2006/08/15 13:36:23 rurban
635 // support iso-8859-2
637 // Revision 1.38 2005/10/10 19:36:09 rurban
640 // Revision 1.37 2005/01/25 07:04:27 rurban
641 // case-sensitive for php5
643 // Revision 1.36 2004/12/06 19:49:56 rurban
644 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
645 // renamed delete_page to purge_page.
646 // enable action=edit&version=-1 to force creation of a new version.
647 // added BABYCART_PATH config
648 // fixed magiqc in adodb.inc.php
649 // and some more docs
651 // Revision 1.35 2004/11/21 11:59:18 rurban
652 // remove final \n to be ob_cache independent
654 // Revision 1.34 2004/10/12 13:13:19 rurban
655 // php5 compatibility (5.0.1 ok)
657 // Revision 1.33 2004/07/02 09:55:58 rurban
658 // more stability fixes: new DISABLE_GETIMAGESIZE if your php crashes when loading LinkIcons: failing getimagesize in old phps; blockparser stabilized
660 // Revision 1.32 2004/06/20 15:30:05 rurban
661 // get_class case-sensitivity issues
663 // Revision 1.31 2004/06/20 14:42:54 rurban
664 // various php5 fixes (still broken at blockparser)
667 // (c-file-style: "gnu")
672 // c-hanging-comment-ender-p: nil
673 // indent-tabs-mode: nil