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 return htmlspecialchars($string, ENT_COMPAT, $GLOBALS['charset']);
201 * @param $tagname string Tag of html element.
203 class XmlElement extends XmlContent
205 function XmlElement ($tagname /* , $attr_or_content , ...*/) {
206 //FIXME: php5 incompatible
208 $this->_init(func_get_args());
211 function _init ($args) {
212 if (!is_array($args))
213 $args = func_get_args();
215 assert(count($args) >= 1);
216 //assert(is_string($args[0]));
217 $this->_tag = array_shift($args);
219 if ($args && is_array($args[0]))
220 $this->_attr = array_shift($args);
222 $this->_attr = array();
223 if ($args && $args[0] === false)
227 $this->setContent($args);
230 /** Methods only needed for XmlParser,
231 * to be fully compatible to perl Html::Element
233 // doesn't yet work with php5 as __destruct()
234 function _destruct () {
235 if ($this->hasChildren()) {
236 foreach ($this->getChildren() as $node) {
242 unset($this->_content);
245 function getChildren () {
246 return $this->_children;
249 function hasChildren () {
250 return !empty($this->_children);
252 /* End XmlParser Methods
259 function setAttr ($attr, $value = false) {
260 if (is_array($attr)) {
261 assert($value === false);
262 foreach ($attr as $a => $v) {
263 $this->_attr[strtolower($a)] = $v;
264 //$this->set($a, $v);
269 assert(is_string($attr));
271 if ($value === false) {
272 unset($this->_attr[$attr]);
277 $this->_attr[$attr] = (string) $value;
280 if ($attr == 'class')
281 unset($this->_classes);
284 function getAttr ($attr) {
285 if ($attr == 'class')
286 $this->_setClasses();
288 if (isset($this->_attr[strtolower($attr)]))
289 return $this->_attr[strtolower($attr)];
294 function _getClasses() {
295 if (!isset($this->_classes)) {
296 $this->_classes = array();
297 if (isset($this->_attr['class'])) {
298 $classes = explode(' ', (string) $this->_attr['class']);
299 foreach ($classes as $class) {
300 $class = trim($class);
302 $this->_classes[$class] = $class;
306 return $this->_classes;
309 function _setClasses() {
310 if (isset($this->_classes)) {
312 $this->_attr['class'] = join(' ', $this->_classes);
314 unset($this->_attr['class']);
319 * Manipulate the elements CSS class membership.
321 * This adds or remove an elements membership
322 * in a give CSS class.
324 * @param $class string
326 * @param $in_class bool
327 * If true (the default) the element is added to class $class.
328 * If false, the element is removed from the class.
330 function setInClass($class, $in_class=true) {
331 $this->_getClasses();
332 $class = trim($class);
334 $this->_classes[$class] = $class;
336 unset($this->_classes[$class]);
340 * Is element in a given (CSS) class?
342 * This checks for the presence of a particular class in the
343 * elements 'class' attribute.
345 * @param $class string The class to check for.
346 * @return bool True if the element is a member of $class.
348 function inClass($class) {
349 $this->_parseClasses();
350 return isset($this->_classes[trim($class)]);
353 function startTag() {
354 $start = "<" . $this->_tag;
355 $this->_setClasses();
356 foreach ($this->_attr as $attr => $val) {
362 $qval = str_replace("\"", '"', $this->_quote((string)$val));
363 $start .= " $attr=\"$qval\"";
369 function emptyTag() {
370 return substr($this->startTag(), 0, -1) . "/>";
375 return "</$this->_tag>";
379 function printXML () {
380 if ($this->isEmpty())
381 echo $this->emptyTag();
383 echo $this->startTag();
384 // FIXME: The next two lines could be removed for efficiency
385 if (!$this->hasInlineContent())
387 XmlContent::printXML();
388 echo "</$this->_tag>";
390 if (!$this->isInlineElement())
395 if ($this->isEmpty()) {
396 $xml = $this->emptyTag();
399 $xml = $this->startTag();
400 // FIXME: The next two lines could be removed for efficiency
401 if (!$this->hasInlineContent())
403 $xml .= XmlContent::asXML();
404 $xml .= "</$this->_tag>";
406 if (!$this->isInlineElement())
412 * Can this element have inline content?
414 * This is a hack, but is probably the best one can do without
415 * knowledge of the DTD...
417 function hasInlineContent () {
419 if (empty($this->_content))
421 if (is_object($this->_content[0]))
427 * Is this element part of inline content?
429 * This is a hack, but is probably the best one can do without
430 * knowledge of the DTD...
432 function isInlineElement () {
439 function RawXml ($xml_text) {
440 $this->_xml = $xml_text;
443 function printXML () {
448 function __toString () {
456 function asString () {
460 function isEmpty () {
461 return empty($this->_xml);
465 class FormattedText {
466 function FormattedText ($fs /* , ... */) {
468 $this->_init(func_get_args());
472 function _init ($args) {
473 $this->_fs = array_shift($args);
475 // PHP's sprintf doesn't support variable width specifiers,
476 // like sprintf("%*s", 10, "x"); --- so we won't either.
478 if (! preg_match_all('/(?<!%)%(\d+)\$/x', $this->_fs, $m)) {
479 $this->_args = $args;
482 // Format string has '%2$s' style argument reordering.
483 // PHP doesn't support this.
484 if (preg_match('/(?<!%)%[- ]?\d*[^- \d$]/x', $this->_fs)) // $fmt
485 // literal variable name substitution only to keep locale
486 // strings uncluttered
487 trigger_error(sprintf(_("Can't mix '%s' with '%s' type format strings"),
488 '%1\$s','%s'), E_USER_WARNING);
490 $this->_fs = preg_replace('/(?<!%)%\d+\$/x', '%', $this->_fs);
492 $this->_args = array();
493 foreach($m[1] as $argnum) {
494 if ($argnum < 1 || $argnum > count($args))
495 trigger_error(sprintf("%s: argument index out of range",
496 $argnum), E_USER_WARNING);
497 $this->_args[] = $args[$argnum - 1];
503 // Not all PHP's have vsprintf, so...
504 $args[] = XmlElement::_quote((string)$this->_fs);
505 foreach ($this->_args as $arg)
506 $args[] = AsXML($arg);
507 return call_user_func_array('sprintf', $args);
510 function printXML () {
511 // Not all PHP's have vsprintf, so...
512 $args[] = XmlElement::_quote((string)$this->_fs);
513 foreach ($this->_args as $arg)
514 $args[] = AsXML($arg);
515 call_user_func_array('printf', $args);
518 function asString() {
519 $args[] = $this->_fs;
520 foreach ($this->_args as $arg)
521 $args[] = AsString($arg);
522 return call_user_func_array('sprintf', $args);
526 function __toString () {
527 return $this->asString();
533 * Error[2048]: Non-static method XmlContent::_quote() should not be called statically
534 * Note: There's lot of room for performance increase if the right charset variant can
535 * be created on load-time.
537 function XmlContent_quote ($string) {
538 if (!$string) return $string;
539 if (isset($GLOBALS['charset'])
540 and (!defined('IGNORE_CHARSET_NOT_SUPPORTED_WARNING') or !IGNORE_CHARSET_NOT_SUPPORTED_WARNING))
542 return htmlspecialchars($string, ENT_COMPAT, $GLOBALS['charset']);
544 return htmlspecialchars($string);
548 function PrintXML ($val /* , ... */ ) {
549 if (func_num_args() > 1) {
550 foreach (func_get_args() as $arg)
553 elseif (is_object($val)) {
554 if (method_exists($val, 'printXML'))
556 elseif (method_exists($val, 'asXML')) {
559 elseif (method_exists($val, 'asString'))
560 echo XmlContent_quote($val->asString());
562 printf("==Object(%s)==", get_class($val));
564 elseif (is_array($val)) {
566 // Use XmlContent objects instead of arrays for collections of XmlElements.
567 trigger_error("Passing arrays to PrintXML() is deprecated: (" . AsXML($val, true) . ")",
573 echo (string)XmlContent_quote((string)$val);
576 function AsXML ($val /* , ... */) {
579 if (func_num_args() > 1) {
581 foreach (func_get_args() as $arg)
585 elseif (is_object($val)) {
586 if (method_exists($val, 'asXML'))
587 return $val->asXML();
588 elseif (method_exists($val, 'asString'))
589 return XmlContent_quote($val->asString());
591 return sprintf("==Object(%s)==", get_class($val));
593 elseif (is_array($val)) {
595 // Use XmlContent objects instead of arrays for collections of XmlElements.
596 if (empty($nowarn)) {
598 trigger_error("Passing arrays to AsXML() is deprecated: (" . AsXML($val) . ")",
608 return XmlContent_quote((string)$val);
611 function AsString ($val) {
612 if (func_num_args() > 1) {
614 foreach (func_get_args() as $arg)
615 $str .= AsString($arg);
618 elseif (is_object($val)) {
619 if (method_exists($val, 'asString'))
620 return $val->asString();
622 return sprintf("==Object(%s)==", get_class($val));
624 elseif (is_array($val)) {
626 // Use XmlContent objects instead of arrays for collections of XmlElements.
627 trigger_error("Passing arrays to AsString() is deprecated", E_USER_NOTICE);
630 $str .= AsString($x);
634 return (string) $val;
637 function fmt ($fs /* , ... */) {
638 $s = new FormattedText(false);
640 $args = func_get_args();
641 $args[0] = _($args[0]);
650 // c-hanging-comment-ender-p: nil
651 // indent-tabs-mode: nil