Changeset 23600 for branches/version-2_13-dev/data/module/XML/Util.php
- Timestamp:
- 2014/08/22 18:31:39 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_13-dev/data/module/XML/Util.php
r21713 r23600 7 7 * 8 8 * XML Utilities package 9 * 9 * 10 10 * PHP versions 4 and 5 11 11 * … … 44 44 * @copyright 2003-2008 Stephan Schmidt <[email protected]> 45 45 * @license http://opensource.org/licenses/bsd-license New BSD License 46 * @version CVS: $Id : Util.php,v 1.38 2008/11/13 00:03:38 ashnazg Exp$46 * @version CVS: $Id$ 47 47 * @link http://pear.php.net/package/XML_Util 48 48 */ … … 120 120 * @copyright 2003-2008 Stephan Schmidt <[email protected]> 121 121 * @license http://opensource.org/licenses/bsd-license New BSD License 122 * @version Release: 1.2. 1122 * @version Release: 1.2.3 123 123 * @link http://pear.php.net/package/XML_Util 124 124 */ … … 162 162 * </code> 163 163 * 164 * @param string $string string where XML special chars 164 * @param string $string string where XML special chars 165 165 * should be replaced 166 * @param int $replaceEntities setting for entities in attribute values 167 * (one of XML_UTIL_ENTITIES_XML, 168 * XML_UTIL_ENTITIES_XML_REQUIRED, 166 * @param int $replaceEntities setting for entities in attribute values 167 * (one of XML_UTIL_ENTITIES_XML, 168 * XML_UTIL_ENTITIES_XML_REQUIRED, 169 169 * XML_UTIL_ENTITIES_HTML) 170 170 * @param string $encoding encoding value (if any)... … … 228 228 * </code> 229 229 * 230 * @param string $string string where XML special chars 230 * @param string $string string where XML special chars 231 231 * should be replaced 232 * @param int $replaceEntities setting for entities in attribute values 233 * (one of XML_UTIL_ENTITIES_XML, 234 * XML_UTIL_ENTITIES_XML_REQUIRED, 232 * @param int $replaceEntities setting for entities in attribute values 233 * (one of XML_UTIL_ENTITIES_XML, 234 * XML_UTIL_ENTITIES_XML_REQUIRED, 235 235 * XML_UTIL_ENTITIES_HTML) 236 236 * @param string $encoding encoding value (if any)... … … 287 287 * @uses attributesToString() to serialize the attributes of the XML declaration 288 288 */ 289 function getXMLDeclaration($version = '1.0', $encoding = null, 289 function getXMLDeclaration($version = '1.0', $encoding = null, 290 290 $standalone = null) 291 291 { … … 302 302 } 303 303 304 return sprintf('<?xml%s?>', 304 return sprintf('<?xml%s?>', 305 305 XML_Util::attributesToString($attributes, false)); 306 306 } … … 317 317 * 318 318 * @param string $root name of the root tag 319 * @param string $uri uri of the doctype definition 319 * @param string $uri uri of the doctype definition 320 320 * (or array with uri and public id) 321 321 * @param string $internalDtd internal dtd entries … … 359 359 * 360 360 * @param array $attributes attribute array 361 * @param bool|array $sort sort attribute list alphabetically, 362 * may also be an assoc array containing 363 * the keys 'sort', 'multiline', 'indent', 361 * @param bool|array $sort sort attribute list alphabetically, 362 * may also be an assoc array containing 363 * the keys 'sort', 'multiline', 'indent', 364 364 * 'linebreak' and 'entities' 365 * @param bool $multiline use linebreaks, if more than 365 * @param bool $multiline use linebreaks, if more than 366 366 * one attribute is given 367 * @param string $indent string used for indentation of 367 * @param string $indent string used for indentation of 368 368 * multiline attributes 369 * @param string $linebreak string used for linebreaks of 369 * @param string $linebreak string used for linebreaks of 370 370 * multiline attributes 371 * @param int $entities setting for entities in attribute values 372 * (one of XML_UTIL_ENTITIES_NONE, 373 * XML_UTIL_ENTITIES_XML, 374 * XML_UTIL_ENTITIES_XML_REQUIRED, 371 * @param int $entities setting for entities in attribute values 372 * (one of XML_UTIL_ENTITIES_NONE, 373 * XML_UTIL_ENTITIES_XML, 374 * XML_UTIL_ENTITIES_XML_REQUIRED, 375 375 * XML_UTIL_ENTITIES_HTML) 376 376 * … … 381 381 * @todo allow sort also to be an options array 382 382 */ 383 function attributesToString($attributes, $sort = true, $multiline = false, 383 function attributesToString($attributes, $sort = true, $multiline = false, 384 384 $indent = ' ', $linebreak = "\n", $entities = XML_UTIL_ENTITIES_XML) 385 385 { … … 452 452 * in the IF branch 453 453 */ 454 function collapseEmptyTags($xml, $mode = XML_UTIL_COLLAPSE_ALL) 454 function collapseEmptyTags($xml, $mode = XML_UTIL_COLLAPSE_ALL) 455 455 { 456 456 if ($mode == XML_UTIL_COLLAPSE_XHTML_ONLY) { … … 475 475 * 476 476 * // create an XML tag: 477 * $tag = XML_Util::createTag('myNs:myTag', 478 * array('foo' => 'bar'), 479 * 'This is inside the tag', 477 * $tag = XML_Util::createTag('myNs:myTag', 478 * array('foo' => 'bar'), 479 * 'This is inside the tag', 480 480 * 'http://www.w3c.org/myNs#'); 481 481 * </code> … … 485 485 * @param mixed $content the content 486 486 * @param string $namespaceUri URI of the namespace 487 * @param int $replaceEntities whether to replace XML special chars in 488 * content, embedd it in a CData section 487 * @param int $replaceEntities whether to replace XML special chars in 488 * content, embedd it in a CData section 489 489 * or none of both 490 * @param bool $multiline whether to create a multiline tag where 490 * @param bool $multiline whether to create a multiline tag where 491 491 * each attribute gets written to a single line 492 * @param string $indent string used to indent attributes 493 * (_auto indents attributes so they start 492 * @param string $indent string used to indent attributes 493 * (_auto indents attributes so they start 494 494 * at the same column) 495 495 * @param string $linebreak string used for linebreaks … … 502 502 * @uses createTagFromArray() to create the tag 503 503 */ 504 function createTag($qname, $attributes = array(), $content = null, 505 $namespaceUri = null, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, 506 $multiline = false, $indent = '_auto', $linebreak = "\n", 504 function createTag($qname, $attributes = array(), $content = null, 505 $namespaceUri = null, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, 506 $multiline = false, $indent = '_auto', $linebreak = "\n", 507 507 $sortAttributes = true) 508 508 { … … 522 522 } 523 523 524 return XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, 524 return XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, 525 525 $indent, $linebreak, $sortAttributes); 526 526 } … … 532 532 * array( 533 533 * // qualified name of the tag 534 * 'qname' => $qname 534 * 'qname' => $qname 535 535 * 536 536 * // namespace prefix (optional, if qname is specified or no namespace) 537 * 'namespace' => $namespace 537 * 'namespace' => $namespace 538 538 * 539 539 * // local part of the tagname (optional, if qname is specified) 540 * 'localpart' => $localpart, 540 * 'localpart' => $localpart, 541 541 * 542 542 * // array containing all attributes (optional) 543 * 'attributes' => array(), 543 * 'attributes' => array(), 544 544 * 545 545 * // tag content (optional) 546 * 'content' => $content, 546 * 'content' => $content, 547 547 * 548 548 * // namespaceUri for the given namespace (optional) 549 * 'namespaceUri' => $namespaceUri 549 * 'namespaceUri' => $namespaceUri 550 550 * ) 551 551 * </pre> … … 565 565 * 566 566 * @param array $tag tag definition 567 * @param int $replaceEntities whether to replace XML special chars in 568 * content, embedd it in a CData section 567 * @param int $replaceEntities whether to replace XML special chars in 568 * content, embedd it in a CData section 569 569 * or none of both 570 * @param bool $multiline whether to create a multiline tag where each 570 * @param bool $multiline whether to create a multiline tag where each 571 571 * attribute gets written to a single line 572 * @param string $indent string used to indent attributes 573 * (_auto indents attributes so they start 572 * @param string $indent string used to indent attributes 573 * (_auto indents attributes so they start 574 574 * at the same column) 575 575 * @param string $linebreak string used for linebreaks … … 586 586 */ 587 587 function createTagFromArray($tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, 588 $multiline = false, $indent = '_auto', $linebreak = "\n", 588 $multiline = false, $indent = '_auto', $linebreak = "\n", 589 589 $sortAttributes = true) 590 590 { … … 596 596 if (!isset($tag['qname']) && !isset($tag['localPart'])) { 597 597 return XML_Util::raiseError('You must either supply a qualified name ' 598 . '(qname) or local tag name (localPart).', 598 . '(qname) or local tag name (localPart).', 599 599 XML_UTIL_ERROR_NO_TAG_NAME); 600 600 } … … 650 650 651 651 // create attribute list 652 $attList = XML_Util::attributesToString($tag['attributes'], 653 $sortAttributes, $multiline, $indent, $linebreak , $replaceEntities);652 $attList = XML_Util::attributesToString($tag['attributes'], 653 $sortAttributes, $multiline, $indent, $linebreak); 654 654 if (!isset($tag['content']) || (string)$tag['content'] == '') { 655 655 $tag = sprintf('<%s%s />', $tag['qname'], $attList); … … 662 662 break; 663 663 default: 664 $tag['content'] = XML_Util::replaceEntities($tag['content'], 664 $tag['content'] = XML_Util::replaceEntities($tag['content'], 665 665 $replaceEntities); 666 666 break; … … 679 679 * 680 680 * // create an XML start element: 681 * $tag = XML_Util::createStartElement('myNs:myTag', 681 * $tag = XML_Util::createStartElement('myNs:myTag', 682 682 * array('foo' => 'bar') ,'http://www.w3c.org/myNs#'); 683 683 * </code> … … 686 686 * @param array $attributes array containg attributes 687 687 * @param string $namespaceUri URI of the namespace 688 * @param bool $multiline whether to create a multiline tag where each 688 * @param bool $multiline whether to create a multiline tag where each 689 689 * attribute gets written to a single line 690 690 * @param string $indent string used to indent attributes (_auto indents … … 699 699 */ 700 700 function createStartElement($qname, $attributes = array(), $namespaceUri = null, 701 $multiline = false, $indent = '_auto', $linebreak = "\n", 701 $multiline = false, $indent = '_auto', $linebreak = "\n", 702 702 $sortAttributes = true) 703 703 { … … 729 729 730 730 // create attribute list 731 $attList = XML_Util::attributesToString($attributes, $sortAttributes, 731 $attList = XML_Util::attributesToString($attributes, $sortAttributes, 732 732 $multiline, $indent, $linebreak); 733 733 $element = sprintf('<%s%s>', $qname, $attList); … … 798 798 function createCDataSection($data) 799 799 { 800 return sprintf('<![CDATA[%s]]>', 800 return sprintf('<![CDATA[%s]]>', 801 801 preg_replace('/\]\]>/', ']]]]><![CDATA[>', strval($data))); 802 802 … … 872 872 { 873 873 // check for invalid chars 874 if (!preg_match('/^[[:alpha:]_] $/', $string{0})) {874 if (!preg_match('/^[[:alpha:]_]\\z/', $string{0})) { 875 875 return XML_Util::raiseError('XML names may only start with letter ' 876 876 . 'or underscore', XML_UTIL_ERROR_INVALID_START); … … 878 878 879 879 // check for invalid chars 880 if (!preg_match('/^([[:alpha:]_]([[:alnum:]\-\.]*)?:)?[[:alpha:]_]([[:alnum:]\_\-\.]+)? $/',880 if (!preg_match('/^([[:alpha:]_]([[:alnum:]\-\.]*)?:)?[[:alpha:]_]([[:alnum:]\_\-\.]+)?\\z/', 881 881 $string) 882 882 ) { 883 883 return XML_Util::raiseError('XML names may only contain alphanumeric ' 884 . 'chars, period, hyphen, colon and underscores', 884 . 'chars, period, hyphen, colon and underscores', 885 885 XML_UTIL_ERROR_INVALID_CHARS); 886 886 }
Note: See TracChangeset
for help on using the changeset viewer.
