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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 include_once( "lib/ezxml/classes/ezdomnode.php" );
00107
00108 class eZDOMDocument
00109 {
00110
00111
00112
00113 function eZDOMDocument( $name = "", $setParentNode = false )
00114 {
00115 $this->Name = $name;
00116 $this->setParentNode = $setParentNode;
00117 }
00118
00119
00120
00121
00122 function cleanup()
00123 {
00124 if ( $this->Root )
00125 eZDOMNode::cleanup( $this->Root );
00126 }
00127
00128
00129
00130
00131 function setName( $name )
00132 {
00133 $this->Name = $name;
00134 }
00135
00136
00137
00138
00139 function &root()
00140 {
00141 return $this->Root;
00142 }
00143
00144
00145
00146
00147
00148 function &get_root()
00149 {
00150 return $this->Root;
00151 }
00152
00153
00154
00155
00156
00157 function setRoot( &$node )
00158 {
00159 $this->appendChild( $node );
00160 }
00161
00162
00163
00164
00165
00166
00167 function appendChild( &$node )
00168 {
00169 if ( get_class( $node ) == "ezdomnode" )
00170 {
00171 if ( $this->setParentNode !== false )
00172 $this->updateParentNodeProperty( $node );
00173 $this->Root =& $node;
00174 }
00175 }
00176
00177
00178
00179
00180
00181
00182 function updateParentNodeProperty( &$node )
00183 {
00184 if ( $node->parentNode === false )
00185 $node->parentNode = null;
00186
00187 foreach( array_keys( $node->Children ) as $key )
00188 {
00189 $this->updateParentNodeProperty( $node->Children[$key] );
00190 }
00191 }
00192
00193
00194
00195
00196
00197 function &elementsByName( $name )
00198 {
00199 if ( !in_array( $name, array_keys( $this->NamedNodes ) ) )
00200 {
00201 $emptyArray = array();
00202 return $emptyArray;
00203 }
00204 return $this->NamedNodes[$name];
00205 }
00206
00207
00208
00209
00210 function get_elements_by_tagname( $name )
00211 {
00212 return $this->elementsByName( $name );
00213 }
00214
00215
00216
00217
00218
00219 function &elementsByNameNS( $name, $namespaceURI )
00220 {
00221 return $this->NamedNodesNS[$name][$namespaceURI];
00222 }
00223
00224
00225
00226
00227
00228 function namespaceByAlias( $alias )
00229 {
00230 if ( isset( $this->Namespaces[$alias] ) )
00231 {
00232 return $this->Namespaces[$alias];
00233 }
00234 else
00235 {
00236 return false;
00237 }
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 function createTextNode( $text )
00258 {
00259
00260
00261
00262
00263
00264
00265 $text = preg_replace('/[\x00-\x08\x0b-\x0c\x0e-\x1f]/', '', $text);
00266
00267 $node = new eZDOMNode();
00268 $node->setName( "#text" );
00269 $node->setContent( $text );
00270 $node->setType( 3 );
00271
00272 return $node;
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 function createCDATANode( $text )
00293 {
00294 $node = new eZDOMNode();
00295 $node->setName( "#cdata-section" );
00296 $node->setContent( $text );
00297 $node->setType( 4 );
00298
00299 return $node;
00300 }
00301
00302
00303
00304
00305
00306
00307
00308 function createElementNodeFromArray( $name, $array )
00309 {
00310 $node = new eZDOMNode();
00311 $node->setName( $name );
00312 $node->setType( 1 );
00313
00314 foreach ( $array as $arrayKey => $value )
00315 {
00316 if ( is_array( $value ) and
00317 count( $valueKeys = array_keys( $value ) ) > 0 )
00318 {
00319 if ( is_int( $valueKeys[0] ) )
00320 {
00321 foreach( $value as $child )
00322 {
00323 $node->appendChild( eZDOMDocument::createElementNodeFromArray( $arrayKey, $child ) );
00324 }
00325 }
00326 else
00327 {
00328 $node->appendChild( eZDOMDocument::createElementNodeFromArray( $arrayKey, $value ) );
00329 }
00330 }
00331 else
00332 {
00333 $node->appendAttribute( eZDomDocument::createAttributeNode( $arrayKey, $value ) );
00334 }
00335 }
00336
00337 return $node;
00338 }
00339
00340
00341
00342
00343
00344
00345
00346 function createArrayFromDOMNode( $domNode )
00347 {
00348 if ( !$domNode )
00349 {
00350 return null;
00351 }
00352
00353 $retArray = array();
00354 foreach ( $domNode->children() as $childNode )
00355 {
00356 if ( !isset( $retArray[$childNode->name()] ) )
00357 {
00358 $retArray[$childNode->name()] = array();
00359 }
00360
00361
00362
00363 if ( $childNode->hasChildren() )
00364 {
00365 $retArray[$childNode->name()][] = eZDOMDocument::createArrayFromDOMNode( $childNode );
00366 }
00367 else
00368 {
00369 $retArray[$childNode->name()] = eZDOMDocument::createArrayFromDOMNode( $childNode );
00370 }
00371 }
00372 foreach( $domNode->attributes() as $attributeNode )
00373 {
00374 $retArray[$attributeNode->name()] = $attributeNode->content();
00375 }
00376
00377 return $retArray;
00378 }
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 function createElementNode( $name, $attributes = array() )
00406 {
00407 $node = new eZDOMNode();
00408 $node->setName( $name );
00409 $node->setType( 1 );
00410 foreach ( $attributes as $attributeKey => $attributeValue )
00411 {
00412 $node->appendAttribute( eZDomDocument::createAttributeNode( $attributeKey, $attributeValue ) );
00413 }
00414
00415 return $node;
00416 }
00417
00418
00419
00420
00421 function create_element( $name, $attributes = array() )
00422 {
00423 return $this->createElementNode( $name, $attributes );
00424 }
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451 function createElementTextNode( $name, $text, $attributes = array() )
00452 {
00453 $node = eZDOMDocument::createElementNode( $name, $attributes );
00454 $textNode = eZDOMDocument::createTextNode( $text );
00455 $node->appendChild( $textNode );
00456
00457 return $node;
00458 }
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485 function createElementCDATANode( $name, $text, $attributes = array() )
00486 {
00487 $node = eZDOMDocument::createElementNode( $name, $attributes );
00488 $cdataNode = eZDOMDocument::createCDATANode( $text );
00489 $node->appendChild( $cdataNode );
00490
00491 return $node;
00492 }
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515 function createElementNodeNS( $uri, $name )
00516 {
00517 $node = new eZDOMNode();
00518 $node->setNamespaceURI( $uri );
00519 $node->setName( $name );
00520 $node->setType( 1 );
00521
00522 return $node;
00523 }
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 function createAttributeNode( $name, $content, $prefix = false )
00547 {
00548 $node = new eZDOMNode();
00549 $node->setName( $name );
00550 if ( $prefix )
00551 $node->setPrefix( $prefix );
00552 $node->setContent( $content );
00553 $node->setType( 2 );
00554
00555 return $node;
00556 }
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577 function createAttributeNamespaceDefNode( $prefix, $uri )
00578 {
00579 $node = new eZDOMNode();
00580 $node->setName( $prefix );
00581 $node->setPrefix( "xmlns" );
00582 $node->setContent( $uri );
00583 $node->setType( 2 );
00584
00585 return $node;
00586 }
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609 function createAttributeNodeNS( $uri, $name, $content )
00610 {
00611 $node = new eZDOMNode();
00612 $node->setName( $name );
00613
00614 $node->setNamespaceURI( $uri );
00615 $node->setContent( $content );
00616 $node->setType( 2 );
00617
00618 return $node;
00619 }
00620
00621
00622
00623
00624
00625
00626 function setStylesheet( $url )
00627 {
00628 if ( is_array( $url ) )
00629 $this->stylesheet = $url;
00630 if ( $url )
00631 $this->stylesheet = array( $url );
00632 }
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642 function setDocTypeDefinition( $url = false, $alias = false, $explict = false )
00643 {
00644 if ( $url or $alias )
00645 {
00646 $this->dtd['url'] = $url;
00647 $this->dtd['alias'] = $alias;
00648 $this->dtd['explict'] = $explict;
00649 }
00650 }
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668 function toString( $charset = true, $charsetConversion = true, $convertSpecialChars = true )
00669 {
00670 $charsetText = '';
00671 if ( $charset === true )
00672 $charset = 'UTF-8';
00673 if ( $charset !== false )
00674 $charsetText = " encoding=\"$charset\"";
00675 $text = "<?xml version=\"1.0\"$charsetText?>\n";
00676
00677 if ( get_class( $this->Root ) == "ezdomnode" )
00678 {
00679 if ( isset( $this->dtd ) )
00680 {
00681 $text .= '<!DOCTYPE ' . $this->Root->name();
00682 if ( $explict )
00683 $text .= ' SYSTEM ';
00684 else
00685 $text .= ' PUBLIC ';
00686 if ( $this->dtd['alias'] )
00687 $text .= '"' . $this->dtd['alias'] . '" "' . $this->dtd['url'] . '"';
00688 else
00689 $text .= '"' . $this->dtd['url'] . '"';
00690 $text .= ">\n";
00691 }
00692
00693 if ( isset( $this->stylesheet ) && count( $this->stylesheet ) )
00694 {
00695 foreach ( $this->stylesheet as $stylesheet )
00696 {
00697 $text .= '<?xml-stylesheet type="text/xsl" href="' . $stylesheet . '"?>' . "\n";
00698 }
00699 }
00700 $text .= $this->Root->toString( 0, $charset, $convertSpecialChars );
00701 }
00702
00703 if ( $charsetConversion )
00704 {
00705 include_once( 'lib/ezi18n/classes/eztextcodec.php' );
00706 $codec =& eZTextCodec::instance( false, $charset, false );
00707 if ( $codec )
00708 {
00709 $text = $codec->convertString( $text );
00710 }
00711 }
00712
00713 return $text;
00714 }
00715
00716
00717
00718
00719 function dump_mem( $charset = true, $conversion = true )
00720 {
00721 return $this->toString( $charset, $conversion );
00722 }
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732 function registerElement( &$node )
00733 {
00734 $this->NamedNodes[$node->name()][] =& $node;
00735
00736 if ( $node->namespaceURI() != "" )
00737 {
00738 $this->NamedNodesNS[$node->name()][$node->namespaceURI()][] =& $node;
00739 }
00740 }
00741
00742
00743
00744
00745
00746
00747 function registerNamespaceAlias( $alias, $namespace )
00748 {
00749 $this->Namespaces[$alias] =& $namespace;
00750 }
00751
00752
00753
00754
00755
00756
00757
00758 function &createElement( $name )
00759 {
00760 $node = new eZDOMNode();
00761 $node->setName( $name );
00762 $node->setType( 1 );
00763
00764 $this->registerElement( $node );
00765
00766 return $node;
00767 }
00768
00769
00770
00771 function createElementNS( $namespaceURI, $qualifiedName )
00772 {
00773 list( $prefix, $name ) = explode( ':', $qualifiedName );
00774
00775 $node = new eZDOMNode();
00776 $node->setName( $name );
00777 $node->setPrefix( $prefix );
00778 $node->setNamespaceURI( $namespaceURI );
00779 $node->setType( 1 );
00780 return node;
00781 }
00782
00783
00784
00785 function createAttributeNS( $namespaceURI, $qualifiedName )
00786 {
00787 list( $prefix, $name ) = explode( ':', $qualifiedName );
00788
00789 $attr = new eZDOMNode();
00790 $attr->setName( $name );
00791 $attr->setPrefix( $prefix );
00792 $attr->setNamespaceURI( $namespaceURI );
00793 $attr->setType( 2 );
00794 return $attr;
00795 }
00796
00797
00798
00799 function createAttribute( $name )
00800 {
00801 $attr = new eZDOMNode();
00802 $attr->setName( $name );
00803 $attr->setType( 2 );
00804 return $attr;
00805 }
00806
00807
00808
00809
00810 var $Name;
00811
00812
00813 var $Version;
00814
00815
00816 var $NamedNodes = array();
00817
00818
00819 var $NamedNodesNS = array();
00820
00821
00822 var $Namespaces = array();
00823
00824
00825 var $Root;
00826
00827
00828
00829 var $setParentNode = false;
00830 }
00831
00832 ?>