00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 App::import('Core', 'Set');
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class XmlNode extends Object {
00038
00039
00040
00041
00042
00043
00044 var $name = null;
00045
00046
00047
00048
00049
00050
00051 var $namespace = null;
00052
00053
00054
00055
00056
00057
00058 var $namespaces = array();
00059
00060
00061
00062
00063
00064
00065 var $value;
00066
00067
00068
00069
00070
00071
00072 var $attributes = array();
00073
00074
00075
00076
00077
00078
00079 var $children = array();
00080
00081
00082
00083
00084
00085
00086 var $__parent = null;
00087
00088
00089
00090
00091
00092
00093
00094
00095 function __construct($name = null, $value = null, $namespace = null) {
00096 if (strpos($name, ':') !== false) {
00097 list($prefix, $name) = explode(':', $name);
00098 if (!$namespace) {
00099 $namespace = $prefix;
00100 }
00101 }
00102 $this->name = $name;
00103 if ($namespace) {
00104 $this->namespace = $namespace;
00105 }
00106
00107 if (is_array($value) || is_object($value)) {
00108 $this->normalize($value);
00109 } elseif (!empty($value) || $value === 0 || $value === '0') {
00110 $this->createTextNode($value);
00111 }
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121 function addNamespace($prefix, $url) {
00122 if ($ns = Xml::addGlobalNs($prefix, $url)) {
00123 $this->namespaces = array_merge($this->namespaces, $ns);
00124 return true;
00125 }
00126 return false;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135 function removeNamespace($prefix) {
00136 if (Xml::removeGlobalNs($prefix)) {
00137 return true;
00138 }
00139 return false;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149 function &createNode($name = null, $value = null, $namespace = false) {
00150 $node =& new XmlNode($name, $value, $namespace);
00151 $node->setParent($this);
00152 return $node;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 function &createElement($name = null, $value = null, $attributes = array(), $namespace = false) {
00164 $element =& new XmlElement($name, $value, $attributes, $namespace);
00165 $element->setParent($this);
00166 return $element;
00167 }
00168
00169
00170
00171
00172
00173
00174 function &createTextNode($value = null) {
00175 $node = new XmlTextNode($value);
00176 $node->setParent($this);
00177 return $node;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186 function normalize($object, $keyName = null, $options = array()) {
00187 if (is_a($object, 'XmlNode')) {
00188 return $object;
00189 }
00190 $name = null;
00191 $options += array('format' => 'attributes');
00192
00193 if ($keyName !== null && !is_numeric($keyName)) {
00194 $name = $keyName;
00195 } elseif (!empty($object->_name_)) {
00196 $name = $object->_name_;
00197 } elseif (isset($object->name)) {
00198 $name = $object->name;
00199 } elseif ($options['format'] == 'attributes') {
00200 $name = get_class($object);
00201 }
00202
00203 $tagOpts = $this->__tagOptions($name);
00204
00205 if ($tagOpts === false) {
00206 return;
00207 }
00208
00209 if (isset($tagOpts['name'])) {
00210 $name = $tagOpts['name'];
00211 } elseif ($name != strtolower($name)) {
00212 $name = Inflector::slug(Inflector::underscore($name));
00213 }
00214
00215 if (!empty($name)) {
00216 $node =& $this->createElement($name);
00217 } else {
00218 $node =& $this;
00219 }
00220
00221 $namespace = array();
00222 $attributes = array();
00223 $children = array();
00224 $chldObjs = array();
00225
00226 if (is_object($object)) {
00227 $chldObjs = get_object_vars($object);
00228 } elseif (is_array($object)) {
00229 $chldObjs = $object;
00230 } elseif (!empty($object) || $object === 0) {
00231 $node->createTextNode($object);
00232 }
00233 $attr = array();
00234
00235 if (isset($tagOpts['attributes'])) {
00236 $attr = $tagOpts['attributes'];
00237 }
00238 if (isset($tagOpts['value']) && isset($chldObjs[$tagOpts['value']])) {
00239 $node->createTextNode($chldObjs[$tagOpts['value']]);
00240 unset($chldObjs[$tagOpts['value']]);
00241 }
00242
00243 $n = $name;
00244 if (!empty($chldObjs['_name_'])) {
00245 $n = null;
00246 unset($chldObjs['_name_']);
00247 }
00248 $c = 0;
00249
00250 foreach ($chldObjs as $key => $val) {
00251 if (in_array($key, $attr) && !is_object($val) && !is_array($val)) {
00252 $attributes[$key] = $val;
00253 } else {
00254 if (!isset($tagOpts['children']) || $tagOpts['children'] === array() || (is_array($tagOpts['children']) && in_array($key, $tagOpts['children']))) {
00255 if (!is_numeric($key)) {
00256 $n = $key;
00257 }
00258 if (is_array($val)) {
00259 foreach ($val as $n2 => $obj2) {
00260 if (is_numeric($n2)) {
00261 $n2 = $n;
00262 }
00263 $node->normalize($obj2, $n2, $options);
00264 }
00265 } else {
00266 if (is_object($val)) {
00267
00268 $node->normalize($val, $n, $options);
00269 } elseif ($options['format'] == 'tags' && $this->__tagOptions($key) !== false) {
00270 $tmp =& $node->createElement($key);
00271 if (!empty($val) || $val === 0) {
00272 $tmp->createTextNode($val);
00273 }
00274 } elseif ($options['format'] == 'attributes') {
00275 $node->addAttribute($key, $val);
00276 }
00277 }
00278 }
00279 }
00280 $c++;
00281 }
00282 if (!empty($name)) {
00283 return $node;
00284 }
00285 return $children;
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295 function __tagOptions($name, $option = null) {
00296 if (isset($this->__tags[$name])) {
00297 $tagOpts = $this->__tags[$name];
00298 } elseif (isset($this->__tags[strtolower($name)])) {
00299 $tagOpts = $this->__tags[strtolower($name)];
00300 } else {
00301 return null;
00302 }
00303 if ($tagOpts === false) {
00304 return false;
00305 }
00306 if (empty($option)) {
00307 return $tagOpts;
00308 }
00309 if (isset($tagOpts[$option])) {
00310 return $tagOpts[$option];
00311 }
00312 return null;
00313 }
00314
00315
00316
00317
00318
00319 function name() {
00320 if (!empty($this->namespace)) {
00321 $_this =& XmlManager::getInstance();
00322 if (!isset($_this->options['verifyNs']) || !$_this->options['verifyNs'] || in_array($this->namespace, array_keys($_this->namespaces))) {
00323 return $this->namespace . ':' . $this->name;
00324 }
00325 }
00326 return $this->name;
00327 }
00328
00329
00330
00331
00332
00333 function setParent(&$parent) {
00334 if (strtolower(get_class($this)) == 'xml') {
00335 return;
00336 }
00337 if (isset($this->__parent) && is_object($this->__parent)) {
00338 if ($this->__parent->compare($parent)) {
00339 return;
00340 }
00341 foreach ($this->__parent->children as $i => $child) {
00342 if ($this->compare($child)) {
00343 array_splice($this->__parent->children, $i, 1);
00344 break;
00345 }
00346 }
00347 }
00348 if ($parent == null) {
00349 unset($this->__parent);
00350 } else {
00351 $parent->children[] =& $this;
00352 $this->__parent =& $parent;
00353 }
00354 }
00355
00356
00357
00358
00359
00360
00361 function cloneNode() {
00362 return clone($this);
00363 }
00364
00365
00366
00367
00368
00369
00370
00371 function compare($node) {
00372 $keys = array(get_object_vars($this), get_object_vars($node));
00373 return ($keys[0] === $keys[1]);
00374 }
00375
00376
00377
00378
00379
00380
00381
00382
00383 function &append(&$child, $options = array()) {
00384 if (empty($child)) {
00385 $return = false;
00386 return $return;
00387 }
00388
00389 if (is_object($child)) {
00390 if ($this->compare($child)) {
00391 trigger_error('Cannot append a node to itself.');
00392 $return = false;
00393 return $return;
00394 }
00395 } else if (is_array($child)) {
00396 $child = Set::map($child);
00397 if (is_array($child)) {
00398 if (!is_a(current($child), 'XmlNode')) {
00399 foreach ($child as $i => $childNode) {
00400 $child[$i] = $this->normalize($childNode, null, $options);
00401 }
00402 } else {
00403 foreach ($child as $childNode) {
00404 $this->append($childNode, $options);
00405 }
00406 }
00407 return $child;
00408 }
00409 } else {
00410 $attributes = array();
00411 if (func_num_args() >= 2) {
00412 $attributes = func_get_arg(1);
00413 }
00414 $child =& $this->createNode($child, null, $attributes);
00415 }
00416
00417 $child = $this->normalize($child, null, $options);
00418
00419 if (empty($child->namespace) && !empty($this->namespace)) {
00420 $child->namespace = $this->namespace;
00421 }
00422
00423 if (is_a($child, 'XmlNode')) {
00424 $child->setParent($this);
00425 }
00426
00427 return $child;
00428 }
00429
00430
00431
00432
00433
00434
00435 function &first() {
00436 if (isset($this->children[0])) {
00437 return $this->children[0];
00438 } else {
00439 $return = null;
00440 return $return;
00441 }
00442 }
00443
00444
00445
00446
00447
00448
00449 function &last() {
00450 if (count($this->children) > 0) {
00451 return $this->children[count($this->children) - 1];
00452 } else {
00453 $return = null;
00454 return $return;
00455 }
00456 }
00457
00458
00459
00460
00461
00462
00463
00464 function &child($id) {
00465 $null = null;
00466
00467 if (is_int($id)) {
00468 if (isset($this->children[$id])) {
00469 return $this->children[$id];
00470 } else {
00471 return null;
00472 }
00473 } elseif (is_string($id)) {
00474 for ($i = 0; $i < count($this->children); $i++) {
00475 if ($this->children[$i]->name == $id) {
00476 return $this->children[$i];
00477 }
00478 }
00479 }
00480 return $null;
00481 }
00482
00483
00484
00485
00486
00487
00488
00489 function children($name) {
00490 $nodes = array();
00491 $count = count($this->children);
00492 for ($i = 0; $i < $count; $i++) {
00493 if ($this->children[$i]->name == $name) {
00494 $nodes[] =& $this->children[$i];
00495 }
00496 }
00497 return $nodes;
00498 }
00499
00500
00501
00502
00503
00504
00505 function &nextSibling() {
00506 $null = null;
00507 $count = count($this->__parent->children);
00508 for ($i = 0; $i < $count; $i++) {
00509 if ($this->__parent->children[$i] == $this) {
00510 if ($i >= $count - 1 || !isset($this->__parent->children[$i + 1])) {
00511 return $null;
00512 }
00513 return $this->__parent->children[$i + 1];
00514 }
00515 }
00516 return $null;
00517 }
00518
00519
00520
00521
00522
00523
00524 function &previousSibling() {
00525 $null = null;
00526 $count = count($this->__parent->children);
00527 for ($i = 0; $i < $count; $i++) {
00528 if ($this->__parent->children[$i] == $this) {
00529 if ($i == 0 || !isset($this->__parent->children[$i - 1])) {
00530 return $null;
00531 }
00532 return $this->__parent->children[$i - 1];
00533 }
00534 }
00535 return $null;
00536 }
00537
00538
00539
00540
00541
00542
00543 function &parent() {
00544 return $this->__parent;
00545 }
00546
00547
00548
00549
00550
00551
00552 function &document() {
00553 $document =& $this;
00554 while (true) {
00555 if (get_class($document) == 'Xml' || $document == null) {
00556 break;
00557 }
00558 $document =& $document->parent();
00559 }
00560 return $document;
00561 }
00562
00563
00564
00565
00566
00567
00568 function hasChildren() {
00569 if (is_array($this->children) && count($this->children) > 0) {
00570 return true;
00571 }
00572 return false;
00573 }
00574
00575
00576
00577
00578
00579
00580 function toString($options = array(), $depth = 0) {
00581 if (is_int($options)) {
00582 $depth = $options;
00583 $options = array();
00584 }
00585 $defaults = array('cdata' => true, 'whitespace' => false, 'convertEntities' => false, 'showEmpty' => true, 'leaveOpen' => false);
00586 $options = array_merge($defaults, Xml::options(), $options);
00587 $tag = !(strpos($this->name, '#') === 0);
00588 $d = '';
00589
00590 if ($tag) {
00591 if ($options['whitespace']) {
00592 $d .= str_repeat("\t", $depth);
00593 }
00594
00595 $d .= '<' . $this->name();
00596 if (count($this->namespaces) > 0) {
00597 foreach ($this->namespaces as $key => $val) {
00598 $val = str_replace('"', '\"', $val);
00599 $d .= ' xmlns:' . $key . '="' . $val . '"';
00600 }
00601 }
00602
00603 $parent =& $this->parent();
00604 if ($parent->name === '#document' && count($parent->namespaces) > 0) {
00605 foreach ($parent->namespaces as $key => $val) {
00606 $val = str_replace('"', '\"', $val);
00607 $d .= ' xmlns:' . $key . '="' . $val . '"';
00608 }
00609 }
00610
00611 if (is_array($this->attributes) && count($this->attributes) > 0) {
00612 foreach ($this->attributes as $key => $val) {
00613 if (is_bool($val) && $val === false) {
00614 $val = 0;
00615 }
00616 $d .= ' ' . $key . '="' . htmlspecialchars($val, ENT_QUOTES, Configure::read('App.encoding')) . '"';
00617 }
00618 }
00619 }
00620
00621 if (!$this->hasChildren() && empty($this->value) && $this->value !== 0 && $tag) {
00622 if (!$options['leaveOpen']) {
00623 $d .= ' />';
00624 }
00625 if ($options['whitespace']) {
00626 $d .= "\n";
00627 }
00628 } elseif ($tag || $this->hasChildren()) {
00629 if ($tag) {
00630 $d .= '>';
00631 }
00632 if ($this->hasChildren()) {
00633 if ($options['whitespace']) {
00634 $d .= "\n";
00635 }
00636 $count = count($this->children);
00637 $cDepth = $depth + 1;
00638 for ($i = 0; $i < $count; $i++) {
00639 $d .= $this->children[$i]->toString($options, $cDepth);
00640 }
00641 if ($tag) {
00642 if ($options['whitespace'] && $tag) {
00643 $d .= str_repeat("\t", $depth);
00644 }
00645 if (!$options['leaveOpen']) {
00646 $d .= '</' . $this->name() . '>';
00647 }
00648 if ($options['whitespace']) {
00649 $d .= "\n";
00650 }
00651 }
00652 }
00653 }
00654 return $d;
00655 }
00656
00657
00658
00659
00660
00661
00662
00663 function toArray($camelize = true) {
00664 $out = $this->attributes;
00665 $multi = null;
00666
00667 foreach ($this->children as $child) {
00668 $key = $camelize ? Inflector::camelize($child->name) : $child->name;
00669
00670 if (is_a($child, 'XmlTextNode')) {
00671 $out['value'] = $child->value;
00672 continue;
00673 } elseif (isset($child->children[0]) && is_a($child->children[0], 'XmlTextNode')) {
00674 $value = $child->children[0]->value;
00675
00676 if ($child->attributes) {
00677 $value = array_merge(array('value' => $value), $child->attributes);
00678 }
00679
00680 if (isset($out[$child->name]) || isset($multi[$key])) {
00681 if (!isset($multi[$key])) {
00682 $multi[$key] = array($out[$child->name]);
00683 unset($out[$child->name]);
00684 }
00685 $multi[$key][] = $value;
00686 } else {
00687 $out[$child->name] = $value;
00688 }
00689 continue;
00690 } elseif (count($child->children) === 0 && $child->value == '') {
00691 $value = $child->attributes;
00692
00693 if (isset($out[$child->name]) || isset($multi[$key])) {
00694 if (!isset($multi[$key])) {
00695 $multi[$key] = array($out[$child->name]);
00696 unset($out[$child->name]);
00697 }
00698 $multi[$key][] = $value;
00699 } else {
00700 $out[$key] = $value;
00701 }
00702 continue;
00703 } else {
00704 $value = $child->toArray($camelize);
00705 }
00706
00707 if (!isset($out[$key])) {
00708 $out[$key] = $value;
00709 } else {
00710 if (!is_array($out[$key]) || !isset($out[$key][0])) {
00711 $out[$key] = array($out[$key]);
00712 }
00713 $out[$key][] = $value;
00714 }
00715 }
00716
00717 if (isset($multi)) {
00718 $out = array_merge($out, $multi);
00719 }
00720 return $out;
00721 }
00722
00723
00724
00725
00726
00727
00728 function __toString() {
00729 return $this->toString();
00730 }
00731
00732
00733
00734
00735
00736
00737
00738 function __killParent($recursive = true) {
00739 unset($this->__parent, $this->_log);
00740 if ($recursive && $this->hasChildren()) {
00741 for ($i = 0; $i < count($this->children); $i++) {
00742 $this->children[$i]->__killParent(true);
00743 }
00744 }
00745 }
00746 }
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757 class Xml extends XmlNode {
00758
00759
00760
00761
00762
00763
00764
00765 var $__parser;
00766
00767
00768
00769
00770
00771
00772 var $__file;
00773
00774
00775
00776
00777
00778
00779 var $__rawData = null;
00780
00781
00782
00783
00784
00785
00786
00787 var $__header = null;
00788
00789
00790
00791
00792
00793
00794
00795
00796 var $__tags = array();
00797
00798
00799
00800
00801
00802
00803
00804 var $version = '1.0';
00805
00806
00807
00808
00809
00810
00811
00812 var $encoding = 'UTF-8';
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833 function __construct($input = null, $options = array()) {
00834 $defaults = array(
00835 'root' => '#document', 'tags' => array(), 'namespaces' => array(),
00836 'version' => '1.0', 'encoding' => 'UTF-8', 'format' => 'attributes'
00837 );
00838 $options = array_merge($defaults, Xml::options(), $options);
00839
00840 foreach (array('version', 'encoding', 'namespaces') as $key) {
00841 $this->{$key} = $options[$key];
00842 }
00843 $this->__tags = $options['tags'];
00844 parent::__construct('#document');
00845
00846 if ($options['root'] !== '#document') {
00847 $Root = $this->createNode($options['root']);
00848 } else {
00849 $Root =& $this;
00850 }
00851
00852 if (!empty($input)) {
00853 if (is_string($input)) {
00854 $Root->load($input);
00855 } elseif (is_array($input) || is_object($input)) {
00856 $Root->append($input, $options);
00857 }
00858 }
00859
00860
00861
00862 }
00863
00864
00865
00866
00867
00868
00869
00870 function load($input) {
00871 if (!is_string($input)) {
00872 return false;
00873 }
00874 $this->__rawData = null;
00875 $this->__header = null;
00876
00877 if (strstr($input, "<")) {
00878 $this->__rawData = $input;
00879 } elseif (strpos($input, 'http:
00880 App::import('Core', 'HttpSocket');
00881 $socket = new HttpSocket();
00882 $this->__rawData = $socket->get($input);
00883 } elseif (file_exists($input)) {
00884 $this->__rawData = file_get_contents($input);
00885 } else {
00886 trigger_error('XML cannot be read');
00887 return false;
00888 }
00889 return $this->parse();
00890 }
00891
00892
00893
00894
00895
00896
00897
00898
00899 function parse() {
00900 $this->__initParser();
00901 $this->__rawData = trim($this->__rawData);
00902 $this->__header = trim(str_replace(
00903 a('<' . '?', '?' . '>'),
00904 a('', ''),
00905 substr($this->__rawData, 0, strpos($this->__rawData, '?' . '>'))
00906 ));
00907
00908 xml_parse_into_struct($this->__parser, $this->__rawData, $vals);
00909 $xml =& $this;
00910 $count = count($vals);
00911
00912 for ($i = 0; $i < $count; $i++) {
00913 $data = $vals[$i];
00914 $data += array('tag' => null, 'value' => null, 'attributes' => array());
00915
00916 switch ($data['type']) {
00917 case "open" :
00918 $xml =& $xml->createElement($data['tag'], $data['value'], $data['attributes']);
00919 break;
00920 case "close" :
00921 $xml =& $xml->parent();
00922 break;
00923 case "complete" :
00924 $xml->createElement($data['tag'], $data['value'], $data['attributes']);
00925 break;
00926 case 'cdata':
00927 $xml->createTextNode($data['value']);
00928 break;
00929 }
00930 }
00931 return true;
00932 }
00933
00934
00935
00936
00937
00938
00939 function __initParser() {
00940 if (empty($this->__parser)) {
00941 $this->__parser = xml_parser_create();
00942 xml_set_object($this->__parser, $this);
00943 xml_parser_set_option($this->__parser, XML_OPTION_CASE_FOLDING, 0);
00944 xml_parser_set_option($this->__parser, XML_OPTION_SKIP_WHITE, 1);
00945 }
00946 }
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957 function compose($options = array()) {
00958 return $this->toString($options);
00959 }
00960
00961
00962
00963
00964
00965
00966
00967
00968 function error($msg, $code = 0, $line = 0) {
00969 if (Configure::read('debug')) {
00970 echo $msg . " " . $code . " " . $line;
00971 }
00972 }
00973
00974
00975
00976
00977
00978
00979
00980 function getError($code) {
00981 $r = @xml_error_string($code);
00982 return $r;
00983 }
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993 function &next() {
00994 $return = null;
00995 return $return;
00996 }
00997
00998
00999
01000
01001
01002
01003 function &previous() {
01004 $return = null;
01005 return $return;
01006 }
01007
01008
01009
01010
01011
01012
01013 function &parent() {
01014 $return = null;
01015 return $return;
01016 }
01017
01018
01019
01020
01021
01022
01023
01024 function addNamespace($prefix, $url) {
01025 if ($count = count($this->children)) {
01026 for ($i = 0; $i < $count; $i++) {
01027 $this->children[$i]->addNamespace($prefix, $url);
01028 }
01029 return true;
01030 }
01031 return parent::addNamespace($prefix, $url);
01032 }
01033
01034
01035
01036
01037
01038
01039 function removeNamespace($prefix) {
01040 if ($count = count($this->children)) {
01041 for ($i = 0; $i < $count; $i++) {
01042 $this->children[$i]->removeNamespace($prefix);
01043 }
01044 return true;
01045 }
01046 return parent::removeNamespace($prefix);
01047 }
01048
01049
01050
01051
01052
01053
01054 function toString($options = array()) {
01055 if (is_bool($options)) {
01056 $options = array('header' => $options);
01057 }
01058
01059 $defaults = array('header' => false, 'encoding' => $this->encoding);
01060 $options = array_merge($defaults, Xml::options(), $options);
01061 $data = parent::toString($options, 0);
01062
01063 if ($options['header']) {
01064 if (!empty($this->__header)) {
01065 return $this->header($this->__header) . "\n" . $data;
01066 }
01067 return $this->header() . "\n" . $data;
01068 }
01069
01070 return $data;
01071 }
01072
01073
01074
01075
01076
01077
01078 function header($attrib = array()) {
01079 $header = 'xml';
01080 if (is_string($attrib)) {
01081 $header = $attrib;
01082 } else {
01083
01084 $attrib = array_merge(array('version' => $this->version, 'encoding' => $this->encoding), $attrib);
01085 foreach ($attrib as $key=>$val) {
01086 $header .= ' ' . $key . '="' . $val . '"';
01087 }
01088 }
01089 return '<' . '?' . $header . ' ?' . '>';
01090 }
01091
01092
01093
01094
01095
01096
01097 function __destruct() {
01098 if (is_resource($this->__parser)) {
01099 xml_parser_free($this->__parser);
01100 }
01101 }
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112 function addGlobalNs($name, $url = null) {
01113 $_this =& XmlManager::getInstance();
01114 if ($ns = Xml::resolveNamespace($name, $url)) {
01115 $_this->namespaces = array_merge($_this->namespaces, $ns);
01116 return $ns;
01117 }
01118 return false;
01119 }
01120
01121
01122
01123
01124
01125
01126
01127 function resolveNamespace($name, $url) {
01128 $_this =& XmlManager::getInstance();
01129 if ($url == null && isset($_this->defaultNamespaceMap[$name])) {
01130 $url = $_this->defaultNamespaceMap[$name];
01131 } elseif ($url == null) {
01132 return false;
01133 }
01134
01135 if (!strpos($url, ':
01136 $_url = $_this->defaultNamespaceMap[$name];
01137 $name = $url;
01138 $url = $_url;
01139 }
01140 return array($name => $url);
01141 }
01142
01143
01144
01145
01146
01147
01148 function addGlobalNamespace($name, $url = null) {
01149 return Xml::addGlobalNs($name, $url);
01150 }
01151
01152
01153
01154
01155
01156
01157
01158 function removeGlobalNs($name) {
01159 $_this =& XmlManager::getInstance();
01160 if (isset($_this->namespaces[$name])) {
01161 unset($_this->namespaces[$name]);
01162 unset($this->namespaces[$name]);
01163 return true;
01164 } elseif (in_array($name, $_this->namespaces)) {
01165 $keys = array_keys($_this->namespaces);
01166 $count = count($keys);
01167 for ($i = 0; $i < $count; $i++) {
01168 if ($_this->namespaces[$keys[$i]] == $name) {
01169 unset($_this->namespaces[$keys[$i]]);
01170 unset($this->namespaces[$keys[$i]]);
01171 return true;
01172 }
01173 }
01174 }
01175 return false;
01176 }
01177
01178
01179
01180
01181
01182
01183 function removeGlobalNamespace($name) {
01184 return Xml::removeGlobalNs($name);
01185 }
01186
01187
01188
01189
01190
01191
01192
01193
01194 function options($options = array()) {
01195 $_this =& XmlManager::getInstance();
01196 $_this->options = array_merge($_this->options, $options);
01197 return $_this->options;
01198 }
01199 }
01200
01201
01202
01203
01204 class XmlElement extends XmlNode {
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214 function __construct($name = null, $value = null, $attributes = array(), $namespace = false) {
01215 parent::__construct($name, $value, $namespace);
01216 $this->addAttribute($attributes);
01217 }
01218
01219
01220
01221
01222
01223 function attributes() {
01224 return $this->attributes;
01225 }
01226
01227
01228
01229
01230
01231
01232
01233 function addAttribute($name, $val = null) {
01234 if (is_object($name)) {
01235 $name = get_object_vars($name);
01236 }
01237 if (is_array($name)) {
01238 foreach ($name as $key => $val) {
01239 $this->addAttribute($key, $val);
01240 }
01241 return true;
01242 }
01243 if (is_numeric($name)) {
01244 $name = $val;
01245 $val = null;
01246 }
01247 if (!empty($name)) {
01248 if (strpos($name, 'xmlns') === 0) {
01249 if ($name == 'xmlns') {
01250 $this->namespace = $val;
01251 } else {
01252 list($pre, $prefix) = explode(':', $name);
01253 $this->addNamespace($prefix, $val);
01254 return true;
01255 }
01256 }
01257 $this->attributes[$name] = $val;
01258 return true;
01259 }
01260 return false;
01261 }
01262
01263
01264
01265
01266
01267
01268 function removeAttribute($attr) {
01269 if (array_key_exists($attr, $this->attributes)) {
01270 unset($this->attributes[$attr]);
01271 return true;
01272 }
01273 return false;
01274 }
01275 }
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286 class XmlTextNode extends XmlNode {
01287
01288
01289
01290
01291
01292 var $name = '#text';
01293
01294
01295
01296
01297
01298 var $value = null;
01299
01300
01301
01302
01303
01304
01305 function __construct($value = null) {
01306 $this->value = $value;
01307 }
01308
01309
01310
01311
01312
01313 function hasChildren() {
01314 return false;
01315 }
01316
01317
01318
01319
01320
01321
01322 function append() {
01323 return false;
01324 }
01325
01326
01327
01328
01329
01330
01331 function toString($options = array(), $depth = 0) {
01332 if (is_int($options)) {
01333 $depth = $options;
01334 $options = array();
01335 }
01336
01337 $defaults = array('cdata' => true, 'whitespace' => false, 'convertEntities' => false);
01338 $options = array_merge($defaults, Xml::options(), $options);
01339 $val = $this->value;
01340
01341 if ($options['convertEntities'] && function_exists('mb_convert_encoding')) {
01342 $val = mb_convert_encoding($val,'UTF-8', 'HTML-ENTITIES');
01343 }
01344
01345 if ($options['cdata'] === true && !is_numeric($val)) {
01346 $val = '<![CDATA[' . $val . ']]>';
01347 }
01348
01349 if ($options['whitespace']) {
01350 return str_repeat("\t", $depth) . $val . "\n";
01351 }
01352 return $val;
01353 }
01354 }
01355
01356
01357
01358
01359
01360
01361 class XmlManager {
01362
01363
01364
01365
01366
01367
01368
01369 var $namespaces = array();
01370
01371
01372
01373
01374
01375
01376 var $options = array();
01377
01378
01379
01380
01381
01382
01383 var $defaultNamespaceMap = array(
01384 'dc' => 'http:
01385 'dct' => 'http:
01386 'g' => 'http:
01387 'rc' => 'http:
01388 'wf' => 'http:
01389 'fb' => 'http:
01390 'lj' => 'http:
01391 'itunes' => 'http:
01392 'xhtml' => 'http:
01393 'atom' => 'http:
01394 );
01395
01396
01397
01398
01399
01400
01401 function &getInstance() {
01402 static $instance = array();
01403
01404 if (!$instance) {
01405 $instance[0] =& new XmlManager();
01406 }
01407 return $instance[0];
01408 }
01409 }
01410 ?>