1 <?php // rcs_id('$Id$');
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 (is_object($item) && 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));
96 elseif (is_array($item)) {
98 // Use XmlContent objects instead of arrays for collections of XmlElements.
99 trigger_error("Passing arrays to printXML() is deprecated: (" . AsXML($item, true) . ")",
101 foreach ($item as $x)
104 echo $this->_quote((string) $item);
111 foreach ($this->_content as $item) {
112 if (is_object($item)) {
113 if (method_exists($item, 'asXML'))
114 $xml .= $item->asXML();
115 elseif (method_exists($item, 'asString'))
116 $xml .= $this->_quote($item->asString());
118 $xml .= sprintf("==Object(%s)==", get_class($item));
120 elseif (is_array($item)) {
121 trigger_error("Passing arrays to ->asXML() is deprecated: (" . AsXML($item, true) . ")",
123 foreach ($item as $x)
124 $xml .= $this->asXML($x);
127 $xml .= $this->_quote((string) $item);
134 foreach ($this->_content as $item) {
135 if (is_object($item)) {
136 if (method_exists($item, 'asPDF'))
137 $pdf .= $item->asPDF();
138 elseif (method_exists($item, 'asString'))
139 $pdf .= $this->_quote($item->asString());
141 $pdf .= sprintf("==Object(%s)==", get_class($item));
144 $pdf .= $this->_quote((string) $item);
150 function __toString () {
151 return $this->asString();
154 function asString () {
156 foreach ($this->_content as $item) {
157 if (is_object($item)) {
158 if (method_exists($item, 'asString')) {
159 $string = $item->asString();
160 if (is_object($string)) {
161 ; // ignore error so far: ImageLink labels
163 $val .= $this->_quote($item->asString());
166 $val .= sprintf("==Object(%s)==", get_class($item));
170 $val .= (string) $item;
177 * See if element is empty.
179 * Empty means it has no content.
180 * @return bool True if empty.
182 function isEmpty () {
183 if (empty($this->_content))
185 foreach ($this->_content as $x) {
186 if (is_string($x) ? strlen($x) : !empty($x))
192 function _quote ($string) {
193 if (!$string) return $string;
194 if (check_php_version(4,1) and isset($GLOBALS['charset'])) {
195 return htmlspecialchars($string, ENT_COMPAT, $GLOBALS['charset']);
197 return htmlspecialchars($string);
205 * @param $tagname string Tag of html element.
207 class XmlElement extends XmlContent
209 function XmlElement ($tagname /* , $attr_or_content , ...*/) {
210 //FIXME: php5 incompatible
212 $this->_init(func_get_args());
215 function _init ($args) {
216 if (!is_array($args))
217 $args = func_get_args();
219 assert(count($args) >= 1);
220 //assert(is_string($args[0]));
221 $this->_tag = array_shift($args);
223 if ($args && is_array($args[0]))
224 $this->_attr = array_shift($args);
226 $this->_attr = array();
227 if ($args && $args[0] === false)
231 $this->setContent($args);
234 /** Methods only needed for XmlParser,
235 * to be fully compatible to perl Html::Element
237 // doesn't yet work with php5 as __destruct()
238 function _destruct () {
239 if ($this->hasChildren()) {
240 foreach ($this->getChildren() as $node) {
246 unset($this->_content);
249 function getChildren () {
250 return $this->_children;
253 function hasChildren () {
254 return !empty($this->_children);
256 /* End XmlParser Methods
263 function setAttr ($attr, $value = false) {
264 if (is_array($attr)) {
265 assert($value === false);
266 foreach ($attr as $a => $v) {
267 $this->_attr[strtolower($a)] = $v;
268 //$this->set($a, $v);
273 assert(is_string($attr));
275 if ($value === false) {
276 unset($this->_attr[$attr]);
281 $this->_attr[$attr] = (string) $value;
284 if ($attr == 'class')
285 unset($this->_classes);
288 function getAttr ($attr) {
289 if ($attr == 'class')
290 $this->_setClasses();
292 if (isset($this->_attr[strtolower($attr)]))
293 return $this->_attr[strtolower($attr)];
298 function _getClasses() {
299 if (!isset($this->_classes)) {
300 $this->_classes = array();
301 if (isset($this->_attr['class'])) {
302 $classes = explode(' ', (string) $this->_attr['class']);
303 foreach ($classes as $class) {
304 $class = trim($class);
306 $this->_classes[$class] = $class;
310 return $this->_classes;
313 function _setClasses() {
314 if (isset($this->_classes)) {
316 $this->_attr['class'] = join(' ', $this->_classes);
318 unset($this->_attr['class']);
323 * Manipulate the elements CSS class membership.
325 * This adds or remove an elements membership
326 * in a give CSS class.
328 * @param $class string
330 * @param $in_class bool
331 * If true (the default) the element is added to class $class.
332 * If false, the element is removed from the class.
334 function setInClass($class, $in_class=true) {
335 $this->_getClasses();
336 $class = trim($class);
338 $this->_classes[$class] = $class;
340 unset($this->_classes[$class]);
344 * Is element in a given (CSS) class?
346 * This checks for the presence of a particular class in the
347 * elements 'class' attribute.
349 * @param $class string The class to check for.
350 * @return bool True if the element is a member of $class.
352 function inClass($class) {
353 $this->_parseClasses();
354 return isset($this->_classes[trim($class)]);
357 function startTag() {
358 $start = "<" . $this->_tag;
359 $this->_setClasses();
360 foreach ($this->_attr as $attr => $val) {
366 $qval = str_replace("\"", '"', $this->_quote((string)$val));
367 $start .= " $attr=\"$qval\"";
373 function emptyTag() {
374 return substr($this->startTag(), 0, -1) . "/>";
379 return "</$this->_tag>";
383 function printXML () {
384 if ($this->isEmpty())
385 echo $this->emptyTag();
387 echo $this->startTag();
388 // FIXME: The next two lines could be removed for efficiency
389 if (!$this->hasInlineContent())
391 XmlContent::printXML();
392 echo "</$this->_tag>";
394 if (!$this->isInlineElement())
399 if ($this->isEmpty()) {
400 $xml = $this->emptyTag();
403 $xml = $this->startTag();
404 // FIXME: The next two lines could be removed for efficiency
405 if (!$this->hasInlineContent())
407 $xml .= XmlContent::asXML();
408 $xml .= "</$this->_tag>";
410 if (!$this->isInlineElement())
416 * Can this element have inline content?
418 * This is a hack, but is probably the best one can do without
419 * knowledge of the DTD...
421 function hasInlineContent () {
423 if (empty($this->_content))
425 if (is_object($this->_content[0]))
431 * Is this element part of inline content?
433 * This is a hack, but is probably the best one can do without
434 * knowledge of the DTD...
436 function isInlineElement () {
443 function RawXml ($xml_text) {
444 $this->_xml = $xml_text;
447 function printXML () {
452 function __toString () {
460 function asString () {
464 function isEmpty () {
465 return empty($this->_xml);
469 class FormattedText {
470 function FormattedText ($fs /* , ... */) {
472 $this->_init(func_get_args());
476 function _init ($args) {
477 $this->_fs = array_shift($args);
479 // PHP's sprintf doesn't support variable width specifiers,
480 // like sprintf("%*s", 10, "x"); --- so we won't either.
482 if (! preg_match_all('/(?<!%)%(\d+)\$/x', $this->_fs, $m)) {
483 $this->_args = $args;
486 // Format string has '%2$s' style argument reordering.
487 // PHP doesn't support this.
488 if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $this->_fs)) // $fmt
489 // literal variable name substitution only to keep locale
490 // strings uncluttered
491 trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
492 '%1\$s','%s'), E_USER_WARNING);
494 $this->_fs = preg_replace('/(?<!%)%\d+\$/x', '%', $this->_fs);
496 $this->_args = array();
497 foreach($m[1] as $argnum) {
498 if ($argnum < 1 || $argnum > count($args))
499 trigger_error(sprintf("%s: argument index out of range",
500 $argnum), E_USER_WARNING);
501 $this->_args[] = $args[$argnum - 1];
507 // Not all PHP's have vsprintf, so...
508 $args[] = XmlElement::_quote((string)$this->_fs);
509 foreach ($this->_args as $arg)
510 $args[] = AsXML($arg);
511 return call_user_func_array('sprintf', $args);
514 function printXML () {
515 // Not all PHP's have vsprintf, so...
516 $args[] = XmlElement::_quote((string)$this->_fs);
517 foreach ($this->_args as $arg)
518 $args[] = AsXML($arg);
519 call_user_func_array('printf', $args);
522 function asString() {
523 $args[] = $this->_fs;
524 foreach ($this->_args as $arg)
525 $args[] = AsString($arg);
526 return call_user_func_array('sprintf', $args);
530 function __toString () {
531 return $this->asString();
537 * Error[2048]: Non-static method XmlContent::_quote() should not be called statically
538 * Note: There's lot of room for performance increase if the right charset variant can
539 * be created on load-time.
541 function XmlContent_quote ($string) {
542 if (!$string) return $string;
543 if (check_php_version(4,1) and isset($GLOBALS['charset'])
544 and (!defined('IGNORE_CHARSET_NOT_SUPPORTED_WARNING') or !IGNORE_CHARSET_NOT_SUPPORTED_WARNING))
546 return htmlspecialchars($string, ENT_COMPAT, $GLOBALS['charset']);
548 return htmlspecialchars($string);
552 function PrintXML ($val /* , ... */ ) {
553 if (func_num_args() > 1) {
554 foreach (func_get_args() as $arg)
557 elseif (is_object($val)) {
558 if (method_exists($val, 'printXML'))
560 elseif (method_exists($val, 'asXML')) {
563 elseif (method_exists($val, 'asString'))
564 echo XmlContent_quote($val->asString());
566 printf("==Object(%s)==", get_class($val));
568 elseif (is_array($val)) {
570 // Use XmlContent objects instead of arrays for collections of XmlElements.
571 trigger_error("Passing arrays to PrintXML() is deprecated: (" . AsXML($val, true) . ")",
577 echo (string)XmlContent_quote((string)$val);
580 function AsXML ($val /* , ... */) {
583 if (func_num_args() > 1) {
585 foreach (func_get_args() as $arg)
589 elseif (is_object($val)) {
590 if (method_exists($val, 'asXML'))
591 return $val->asXML();
592 elseif (method_exists($val, 'asString'))
593 return XmlContent_quote($val->asString());
595 return sprintf("==Object(%s)==", get_class($val));
597 elseif (is_array($val)) {
599 // Use XmlContent objects instead of arrays for collections of XmlElements.
600 if (empty($nowarn)) {
602 trigger_error("Passing arrays to AsXML() is deprecated: (" . AsXML($val) . ")",
612 return XmlContent_quote((string)$val);
615 function AsString ($val) {
616 if (func_num_args() > 1) {
618 foreach (func_get_args() as $arg)
619 $str .= AsString($arg);
622 elseif (is_object($val)) {
623 if (method_exists($val, 'asString'))
624 return $val->asString();
626 return sprintf("==Object(%s)==", get_class($val));
628 elseif (is_array($val)) {
630 // Use XmlContent objects instead of arrays for collections of XmlElements.
631 trigger_error("Passing arrays to AsString() is deprecated", E_USER_NOTICE);
634 $str .= AsString($x);
638 return (string) $val;
641 function fmt ($fs /* , ... */) {
642 $s = new FormattedText(false);
644 $args = func_get_args();
645 $args[0] = _($args[0]);
650 // (c-file-style: "gnu")
655 // c-hanging-comment-ender-p: nil
656 // indent-tabs-mode: nil