1 <?php rcs_id('$Id: XmlElement.php,v 1.40 2007-01-13 23:28:56 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 and 1.3.11.
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 () {
435 function isEmpty () {
436 return empty($this->_xml);
440 class FormattedText {
441 function FormattedText ($fs /* , ... */) {
443 $this->_init(func_get_args());
447 function _init ($args) {
448 $this->_fs = array_shift($args);
450 // PHP's sprintf doesn't support variable width specifiers,
451 // like sprintf("%*s", 10, "x"); --- so we won't either.
453 if (! preg_match_all('/(?<!%)%(\d+)\$/x', $this->_fs, $m)) {
454 $this->_args = $args;
457 // Format string has '%2$s' style argument reordering.
458 // PHP doesn't support this.
459 if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $this->_fs)) // $fmt
460 // literal variable name substitution only to keep locale
461 // strings uncluttered
462 trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
463 '%1\$s','%s'), E_USER_WARNING);
465 $this->_fs = preg_replace('/(?<!%)%\d+\$/x', '%', $this->_fs);
467 $this->_args = array();
468 foreach($m[1] as $argnum) {
469 if ($argnum < 1 || $argnum > count($args))
470 trigger_error(sprintf("%s: argument index out of range",
471 $argnum), E_USER_WARNING);
472 $this->_args[] = $args[$argnum - 1];
478 // Not all PHP's have vsprintf, so...
479 $args[] = XmlElement::_quote((string)$this->_fs);
480 foreach ($this->_args as $arg)
481 $args[] = AsXML($arg);
482 return call_user_func_array('sprintf', $args);
485 function printXML () {
486 // Not all PHP's have vsprintf, so...
487 $args[] = XmlElement::_quote((string)$this->_fs);
488 foreach ($this->_args as $arg)
489 $args[] = AsXML($arg);
490 call_user_func_array('printf', $args);
493 function asString() {
494 $args[] = $this->_fs;
495 foreach ($this->_args as $arg)
496 $args[] = AsString($arg);
497 return call_user_func_array('sprintf', $args);
503 * Error[2048]: Non-static method XmlContent::_quote() should not be called statically
504 * Note: There's lot of room for performance increase if the right charset variant can
505 * be created on load-time.
507 function XmlContent_quote ($string) {
508 if (!$string) return $string;
509 if (check_php_version(4,1) and isset($GLOBALS['charset'])
510 and (!defined('IGNORE_CHARSET_NOT_SUPPORTED_WARNING') or !IGNORE_CHARSET_NOT_SUPPORTED_WARNING))
512 return htmlspecialchars($string, ENT_COMPAT, $GLOBALS['charset']);
514 return htmlspecialchars($string);
518 function PrintXML ($val /* , ... */ ) {
519 if (func_num_args() > 1) {
520 foreach (func_get_args() as $arg)
523 elseif (is_object($val)) {
524 if (method_exists($val, 'printXML'))
526 elseif (method_exists($val, 'asXML')) {
529 elseif (method_exists($val, 'asString'))
530 echo XmlContent_quote($val->asString());
532 printf("==Object(%s)==", get_class($val));
534 elseif (is_array($val)) {
536 // Use XmlContent objects instead of arrays for collections of XmlElements.
537 trigger_error("Passing arrays to PrintXML() is deprecated: (" . AsXML($val, true) . ")",
543 echo (string)XmlContent_quote((string)$val);
546 function AsXML ($val /* , ... */) {
549 if (func_num_args() > 1) {
551 foreach (func_get_args() as $arg)
555 elseif (is_object($val)) {
556 if (method_exists($val, 'asXML'))
557 return $val->asXML();
558 elseif (method_exists($val, 'asString'))
559 return XmlContent_quote($val->asString());
561 return sprintf("==Object(%s)==", get_class($val));
563 elseif (is_array($val)) {
565 // Use XmlContent objects instead of arrays for collections of XmlElements.
566 if (empty($nowarn)) {
568 trigger_error("Passing arrays to AsXML() is deprecated: (" . AsXML($val) . ")",
578 return XmlContent_quote((string)$val);
581 function AsString ($val) {
582 if (func_num_args() > 1) {
584 foreach (func_get_args() as $arg)
585 $str .= AsString($arg);
588 elseif (is_object($val)) {
589 if (method_exists($val, 'asString'))
590 return $val->asString();
592 return sprintf("==Object(%s)==", get_class($val));
594 elseif (is_array($val)) {
596 // Use XmlContent objects instead of arrays for collections of XmlElements.
597 trigger_error("Passing arrays to AsString() is deprecated", E_USER_NOTICE);
600 $str .= AsString($x);
604 return (string) $val;
608 function fmt ($fs /* , ... */) {
609 $s = new FormattedText(false);
611 $args = func_get_args();
612 $args[0] = _($args[0]);
617 // $Log: not supported by cvs2svn $
618 // Revision 1.39 2006/08/15 13:36:23 rurban
619 // support iso-8859-2
621 // Revision 1.38 2005/10/10 19:36:09 rurban
624 // Revision 1.37 2005/01/25 07:04:27 rurban
625 // case-sensitive for php5
627 // Revision 1.36 2004/12/06 19:49:56 rurban
628 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
629 // renamed delete_page to purge_page.
630 // enable action=edit&version=-1 to force creation of a new version.
631 // added BABYCART_PATH config
632 // fixed magiqc in adodb.inc.php
633 // and some more docs
635 // Revision 1.35 2004/11/21 11:59:18 rurban
636 // remove final \n to be ob_cache independent
638 // Revision 1.34 2004/10/12 13:13:19 rurban
639 // php5 compatibility (5.0.1 ok)
641 // Revision 1.33 2004/07/02 09:55:58 rurban
642 // more stability fixes: new DISABLE_GETIMAGESIZE if your php crashes when loading LinkIcons: failing getimagesize in old phps; blockparser stabilized
644 // Revision 1.32 2004/06/20 15:30:05 rurban
645 // get_class case-sensitivity issues
647 // Revision 1.31 2004/06/20 14:42:54 rurban
648 // various php5 fixes (still broken at blockparser)
651 // (c-file-style: "gnu")
656 // c-hanging-comment-ender-p: nil
657 // indent-tabs-mode: nil