cvs: pear-core /PEAR/PackageFile/Generator/PHP5 Common.php v1.php /PEAR/PackageFile/PHP5 package-1.0.xsd v1.php /PEAR/PackageFile/Parser/PHP5 v1.php
| From: | Greg Beaver | Date: | Wed, 01 Sep 2004 18:03:25 +0000 |
| Subject: | cvs: pear-core /PEAR/PackageFile/Generator/PHP5 Common.php v1.php /PEAR/PackageFile/PHP5 package-1.0.xsd v1.php /PEAR/PackageFile/Parser/PHP5 v1.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-23564@lists.php.net to get a copy of this message | ||
cellog Wed Sep 1 14:03:25 2004 EDT
Added files:
/pear-core/PEAR/PackageFile/PHP5 package-1.0.xsd
/pear-core/PEAR/PackageFile/Generator/PHP5 Common.php v1.php
Modified files:
/pear-core/PEAR/PackageFile/PHP5 v1.php
/pear-core/PEAR/PackageFile/Parser/PHP5 v1.php
Log:
completed package.xml v1.0 in PHP5:
- parsing
- generation (with some beautifying options)
- structural validation using xsd
- smart correction of minor validation errors (elements in wrong order, missing elements)
todo:
- conform with API for PHP4 version (get* and set* methods for package.xml portions)
http://cvs.php.net/diff.php/pear-core/PEAR/PackageFile/PHP5/v1.php?r1=1.1&r2=1.2&ty=u Index: pear-core/PEAR/PackageFile/PHP5/v1.php diff -u pear-core/PEAR/PackageFile/PHP5/v1.php:1.1 pear-core/PEAR/PackageFile/PHP5/v1.php:1.2 --- pear-core/PEAR/PackageFile/PHP5/v1.php:1.1 Sun Aug 29 18:33:14 2004 +++ pear-core/PEAR/PackageFile/PHP5/v1.php Wed Sep 1 14:03:25 2004 @@ -4,7 +4,7 @@ /** * @var DOMDocument */ - protected $dom; + public $dom; private $_schemaValidateWarnings = array(); /** * Never call this function directly - its only purpose @@ -18,8 +18,185 @@ public function validate() { + static $attempt = false; + $this->_schemaValidateWarnings = array(); set_error_handler(array($this, '_catchWarnings')); + $this->dom->schemaValidate(dirname(__FILE__) . DIRECTORY_SEPARATOR . + 'package-1.0.xsd'); restore_error_handler(); + $ret = false; + if (count($this->_schemaValidateWarnings)) { + if (!$attempt) { + $attempt = true; + $this->_salvageCrappySyntax(); + $ret = $this->validate(); + } + $attempt = false; + return $ret; + } + return true; + } + + public function getValidationWarnings() + { + return $this->_schemaValidateWarnings; + } + + /** + * for older, mal-formed package.xml, try to force it to be conformant first + */ + private function _salvageCrappySyntax() + { + $working = $this->dom->cloneNode(true); + $sx = simplexml_import_dom($working); + $rel = $this->dom->documentElement->getElementsByTagname('release')->item(0); + $fields = array_keys((array) $sx); + if (!in_array('name', $fields) || !in_array('summary', $fields) || + !in_array('description', $fields) || !in_array('maintainers', $fields) || + !in_array('release', $fields)) { + // if any of these are missing, it's hopeless + return; + } + $releasefields = array_keys((array) $sx->release); + if (!in_array('version', $releasefields) || !in_array('date', $releasefields) || + !in_array('state', $releasefields) || !in_array('notes', $releasefields) || + !in_array('filelist', $releasefields)) { + // if any of these are missing, it's hopeless + return; + } + if (!isset($sx->release->license) && isset($sx->license)) { + // fix those package.xml that only set global license + // by moving it into the release tag + $license = $this->dom->documentElement->removeChild( + $this->dom->documentElement->getElementsByTagname('license')->item(0)); + $this->dom->documentElement-> + getElementsByTagname('release')->item(0)-> + insertBefore( + $license, + $this->dom->documentElement->getElementsByTagname('state')->item(0)); + $this->_reorderBasic(); + return $this->_reorderRelease($sx); + } + $test = array('name', 'summary', 'description', '?license', 'maintainers', 'release','?changelog'); + $skip = false; + foreach ($test as $next) { + if (!$skip) { + $tag = array_shift($fields); + } + $skip = false; + if ($next{0} == '?') { + $opt = true; + $next = substr($next, 1); + } else { + $opt = false; + } + if ($next != $tag) { + if (!$opt) { + $this->_reorderBasic(); + } else { + // skip missing optional tags + $skip = true; + } + } + } + if (is_array($fields) && count($fields)) { + $this->_reorderBasic(); + } + $test = array('version', 'date', 'license', 'state', 'notes', '?warnings', '?provides', '?deps', '?configureoptions', 'filelist'); + $skip = false; + foreach ($test as $next) { + if (!$skip) { + $tag = array_shift($releasefields); + } + $skip = false; + if ($next{0} == '?') { + $opt = true; + $next = substr($next, 1); + } else { + $opt = false; + } + if ($next != $tag) { + if (!$opt) { + $this->_reorderRelease($sx); + } else { + // skip missing optional tags + $skip = true; + } + } + } + if (is_array($releasefields) && count($releasefields)) { + $this->_reorderRelease($sx, $rel, $this->dom->documentElement); + } + } + + private function _reorderBasic() + { + $package = $this->dom->createElement('package'); + $package->appendChild($this->dom->createTextNode("\n ")); + $package->appendChild($this->dom->documentElement-> + getElementsByTagname('name')->item(0)); + $package->appendChild($this->dom->createTextNode("\n ")); + $package->appendChild($this->dom->documentElement-> + getElementsByTagname('summary')->item(0)); + $package->appendChild($this->dom->createTextNode("\n ")); + $package->appendChild($this->dom->documentElement-> + getElementsByTagname('description')->item(0)); + $package->appendChild($this->dom->createTextNode("\n ")); + $package->appendChild($this->dom->documentElement-> + getElementsByTagname('maintainers')->item(0)); + $package->appendChild($this->dom->createTextNode("\n ")); + $package->appendChild($this->dom->documentElement-> + getElementsByTagname('release')->item(0)); + $package->appendChild($this->dom->createTextNode("\n")); + $this->dom->replaceChild($this->dom->documentElement, $package); + } + + private function _reorderRelease($sx, $rel, $par) + { + $release = $this->dom->createElement('release'); + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('version')->item(0)); + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('date')->item(0)); + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('license')->item(0)); + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('state')->item(0)); + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('notes')->item(0)); + if (isset($sx->release->warnings)) { + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('warnings')->item(0)); + } + if (isset($sx->release->provides)) { + foreach ($rel->getElementsByTagname('provides') as $provide) { + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($provide); + } + } + if (isset($sx->release->deps)) { + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('deps')->item(0)); + } + if (isset($sx->release->configureoptions)) { + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('configureoptions')->item(0)); + } + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('filelist')->item(0)); + $release->appendChild($this->dom->createTextNode("\n ")); + $par->replaceChild( + $release, + $rel); } public function fromDom($dom) @@ -35,6 +212,158 @@ */ public function fromV2($packagefile) { + } + + private function _getRole($node) + { + if ($node->hasAttribute('role')) { + return $node->getAttribute('role'); + } + $node = $node->parentNode; + while($node->nodeName == 'dir') { + if ($node->hasAttribute('role')) { + return $node->getAttribute('role'); + } + } + } + + private function _getFullPath($node) + { + $i = 0; + $path = $node->getAttribute('name'); + $node = $node->parentNode; + while($node->nodeName == 'dir' && $node->getAttribute('name') != '/') { + $path = $node->getAttribute('name') . '/' . $path; + $node = $node->parentNode; + } + return str_replace('//', '/', $path); + } + + private function _getParentFilelist($node) + { + while ($node->nodeName != 'filelist' && $node->nodeName != 'package') { + $node = $node->parentNode; + } + if ($node->nodeName == 'package') { + throw new Exception('node is not a child of filelist'); + } + return $node; + } + + function sortdomfiles($a, $b) + { + $z = strnatcmp($a->getAttribute('name'), $b->getAttribute('name')); + if ($z == 1) return -1; + if ($z == -1) return 1; + return 0; + } + + public function flattenFilelist($cmp = null, $depth = ' ') + { + $d = $this->dom; + if ($cmp === null) { + $cmp = $d->documentElement->getElementsByTagname('filelist')->item(0); + } + $sx = $d->createElement('filelist'); + $cmp->parentNode->replaceChild($sx, $cmp); + $sx->appendChild($d->createTextNode("\n$depth")); + $sx->appendChild($dir = $d->createElement('dir')); + $sx->appendChild($d->createTextNode("\n ")); + $dir->setAttribute('name', '/'); + $files = array(); + foreach ($cmp->getElementsByTagname('file') as $node) { + if ($this->_getParentFilelist($node) === $cmp) { + $el = $node->cloneNode(); + $el->setAttribute('role', $this->_getRole($node)); + $el->setAttribute('name', $this->_getFullPath($node)); + $files[] = $el; + } + } + usort($files, array($this, 'sortdomfiles')); + foreach ($files as $node) { + $dir->appendChild($d->createTextNode("\n$depth ")); + $dir->appendChild($node); + } + $dir->appendChild($d->createTextNode("\n ")); + return $files; + } + + public function recursiveFilelist($cmp = null, $depth = ' ') + { + $files = array_reverse($this->flattenFilelist()); + $d = $this->dom; + if ($cmp === null) { + $cmp = $d->documentElement->getElementsByTagname('filelist')->item(0); + } + $sx = $d->createElement('filelist'); + $cmp->parentNode->replaceChild($sx, $cmp); + $sx->appendChild($d->createTextNode("\n$depth")); + $sx->appendChild($dirnode = $d->createElement('dir')); + $dirnode->setAttribute('name', '/'); + $sx->appendChild($d->createTextNode("\n$depth")); + $dirs = $mainfiles = array(); + foreach ($files as $file) { + $name = explode('/', $file->getAttribute('name')); + $filename = array_pop($name); + $path = ''; + if (count($name)) { + foreach ($name as $i => $dir) { + if ($path != '') { + $path .= '/'; + } + $path .= $dir; + if (!isset($dirs[$path])) { + $dirs[$path] = $this->dom->createElement('dir'); + $dirs[$path]->setAttribute('name', $dir); + } + if ($i == count($name) - 1) { + $newfile = $file->cloneNode(); + $dirs[$path]->appendChild($this->dom->createTextNode("\n $depth" . + str_repeat(' ', count($name)))); + $dirs[$path]->appendChild($newfile); + $newfile->setAttribute('name', $filename); + } + } + } else { + $mainfiles[$file->getAttribute('name')] = $file->cloneNode(); + } + } + uksort($dirs, 'strnatcmp'); + $dirs = array_reverse($dirs); + $maindirs = array(); + foreach ($dirs as $path => $cdir) { + if (basename($path) != $path && isset($dirs[dirname($path)])) { + $cdir->appendChild($this->dom->createTextNode("\n $depth" . + str_repeat(' ', count(explode('/', dirname($path)))))); + $parent = $dirs[dirname($path)]; + $parent->insertBefore($cdir, $parent->firstChild); + $parent->insertBefore( + $this->dom->createTextNode("\n $depth" . + str_repeat(' ', count(explode('/', dirname($path))))), + $parent->firstChild); + $parent->insertBefore( + $co = $this->dom->createTextNode(' '), + $cdir->nextSibling); + $parent->insertBefore( + $this->dom->createComment($path), + $co->nextSibling); + } else { + $cdir->appendChild($this->dom->createTextNode("\n $depth")); + array_unshift($maindirs, array($path, $cdir)); + } + } + foreach ($maindirs as $cdir) { + $dirnode->appendChild($this->dom->createTextNode("\n $depth")); + $dirnode->appendChild($cdir[1]); + $dirnode->appendChild($this->dom->createTextNode(" ")); + $dirnode->appendChild($this->dom->createComment(" $cdir[0] ")); + } + uksort($mainfiles, 'strnatcmp'); + foreach ($mainfiles as $file) { + $dirnode->appendChild($this->dom->createTextNode("\n $depth")); + $dirnode->appendChild($file); + } + $dirnode->appendChild($this->dom->createTextNode("\n $depth")); } /** http://cvs.php.net/diff.php/pear-core/PEAR/PackageFile/Parser/PHP5/v1.php?r1=1.1&r2=1.2&ty=u Index: pear-core/PEAR/PackageFile/Parser/PHP5/v1.php diff -u pear-core/PEAR/PackageFile/Parser/PHP5/v1.php:1.1 pear-core/PEAR/PackageFile/Parser/PHP5/v1.php:1.2 --- pear-core/PEAR/PackageFile/Parser/PHP5/v1.php:1.1 Sun Aug 29 18:33:15 2004 +++ pear-core/PEAR/PackageFile/Parser/PHP5/v1.php Wed Sep 1 14:03:25 2004 @@ -8,10 +8,10 @@ * @param string contents of package.xml file, version 1.0 * @return PEAR_PackageFile_PHP5_v1 */ - public function parse($data) + static public function parse($data) { $dom = new DOMDocument; - if (!@$dom->loadXml()) { + if (!@$dom->loadXml($data)) { throw new Exception('(this will be PEAR_Exception) invalid package.xml, not xml'); } include_once 'PEAR/PackageFile/PHP5/v1.php'; @@ -20,4 +20,18 @@ return $p; } } +/* +leaving in because I might move things around +set_include_path('C:/devel/pear_with_channels'); +//$data = file_get_contents('C:/php4/pear_pkgs/Auth/package.xml'); +$data = file_get_contents('C:/devel/pear_with_channels/package-PEAR.xml'); +$a = PEAR_PackageFile_Parser_PHP5_v1::parse($data); +if (!$a->validate()) { + var_dump($a->getValidationWarnings()); +} else { + require_once 'PEAR/PackageFile/Generator/PHP5/v1.php'; + $b = new PEAR_PackageFile_Generator_PHP5_v1($a); + echo $b->toXml(); +} +*/ ?> http://cvs.php.net/co.php/pear-core/PEAR/PackageFile/PHP5/package-1.0.xsd?r=1.1&p=1 Index: pear-core/PEAR/PackageFile/PHP5/package-1.0.xsd +++ pear-core/PEAR/PackageFile/PHP5/package-1.0.xsd <?xml version="1.0"?> <!-- edited with XMLSPY v2004 rel. 4 U (http://www.xmlspy.com) by Greg Beaver (The Chiara String Quartet) --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="package"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="summary"/> <xs:element ref="description"/> <xs:element ref="license" minOccurs="0"/> <xs:element ref="maintainers"/> <xs:element ref="release"/> <xs:element ref="changelog" minOccurs="0"/> </xs:sequence> <xs:attribute name="type" default="empty"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="source"/> <xs:enumeration value="binary"/> <xs:enumeration value="empty"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="version" type="xs:string" use="required"/> </xs:complexType> </xs:element> <xs:element name="name" type="xs:string"/> <xs:element name="summary" type="xs:string"/> <xs:element name="description" type="xs:string"/> <xs:element name="license" type="xs:string"/> <xs:element name="maintainers"> <xs:complexType> <xs:sequence maxOccurs="unbounded"> <xs:element ref="maintainer"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="maintainer"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="user"/> <xs:element ref="role"/> <xs:element ref="name"/> <xs:element ref="email"/> </xs:choice> </xs:complexType> </xs:element> <xs:element name="user" type="xs:string"/> <xs:element name="role" type="xs:string"/> <xs:element name="email" type="xs:string"/> <xs:element name="changelog"> <xs:complexType> <xs:sequence minOccurs="0" maxOccurs="unbounded"> <xs:element name="release" type="changelogrelease"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="release"> <xs:complexType> <xs:sequence> <xs:element ref="version"/> <xs:element ref="date"/> <xs:element ref="license"/> <xs:element ref="state"/> <xs:element ref="notes"/> <xs:element ref="warnings" minOccurs="0"/> <xs:element ref="provides" minOccurs="0" maxOccurs="unbounded"/> <xs:element ref="deps" minOccurs="0"/> <xs:element ref="configureoptions" minOccurs="0"/> <xs:element ref="filelist"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="version" type="xs:string"/> <xs:element name="state" type="xs:string"/> <xs:element name="date" type="xs:string"/> <xs:element name="notes" type="xs:string"/> <xs:element name="provides"> <xs:complexType> <xs:attribute name="type" use="required"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="ext"/> <xs:enumeration value="prog"/> <xs:enumeration value="class"/> <xs:enumeration value="function"/> <xs:enumeration value="feature"/> <xs:enumeration value="api"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="extends" type="xs:string"/> </xs:complexType> </xs:element> <xs:element name="filelist"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="dir"/> <xs:element ref="file"/> <xs:element ref="libfile"/> </xs:choice> </xs:complexType> </xs:element> <xs:element name="dir"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="dir"/> <xs:element ref="file"/> <xs:element ref="libfile"/> </xs:choice> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="baseinstalldir" type="xs:string"/> <xs:attribute name="role"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="php"/> <xs:enumeration value="ext"/> <xs:enumeration value="test"/> <xs:enumeration value="doc"/> <xs:enumeration value="data"/> <xs:enumeration value="script"/> <xs:enumeration value="src"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element> <xs:element name="file"> <xs:complexType> <xs:sequence> <xs:element ref="replace" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="role" default="php"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="php"/> <xs:enumeration value="ext"/> <xs:enumeration value="test"/> <xs:enumeration value="doc"/> <xs:enumeration value="data"/> <xs:enumeration value="script"/> <xs:enumeration value="src"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="debug" default="na"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="na"/> <xs:enumeration value="on"/> <xs:enumeration value="off"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="threaded" default="na"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="na"/> <xs:enumeration value="on"/> <xs:enumeration value="off"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="format" type="xs:string"/> <xs:attribute name="baseinstalldir" type="xs:string"/> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="install-as" type="xs:string"/> <xs:attribute name="platform" type="xs:string"/> <xs:attribute name="md5sum" type="xs:string"/> </xs:complexType> </xs:element> <xs:element name="libfile"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="libname"/> <xs:element ref="sources"/> <xs:element ref="includes"/> <xs:element ref="libadd"/> </xs:choice> </xs:complexType> </xs:element> <xs:element name="libname" type="xs:string"/> <xs:element name="sources" type="xs:string"/> <xs:element name="includes" type="xs:string"/> <xs:element name="libadd" type="xs:string"/> <xs:element name="deps"> <xs:complexType> <xs:sequence minOccurs="0" maxOccurs="unbounded"> <xs:element ref="dep"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="dep"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="type" use="required"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="pkg"/> <xs:enumeration value="ext"/> <xs:enumeration value="php"/> <xs:enumeration value="prog"/> <xs:enumeration value="ldlib"/> <xs:enumeration value="ltlib"/> <xs:enumeration value="os"/> <xs:enumeration value="websrv"/> <xs:enumeration value="sapi"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="rel"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="has"/> <xs:enumeration value="eq"/> <xs:enumeration value="lt"/> <xs:enumeration value="le"/> <xs:enumeration value="gt"/> <xs:enumeration value="ge"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="version" type="xs:string"/> <xs:attribute name="optional" default="no"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="yes"/> <xs:enumeration value="no"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="replace"> <xs:complexType> <xs:attribute name="from" type="xs:string" use="required"/> <xs:attribute name="to" type="xs:string" use="required"/> <xs:attribute name="type" use="required"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="package-info"/> <xs:enumeration value="pear-config"/> <xs:enumeration value="php-const"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element> <xs:complexType name="changelogrelease"> <xs:sequence> <xs:element ref="version"/> <xs:element ref="date"/> <xs:element ref="license"/> <xs:element ref="state"/> <xs:element ref="notes"/> </xs:sequence> </xs:complexType> <xs:element name="warnings" type="xs:string"/> <xs:element name="configureoptions"> <xs:complexType> <xs:sequence> <xs:element ref="configureoption" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="configureoption"> <xs:complexType> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="default" type="xs:string"/> <xs:attribute name="prompt" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:schema> http://cvs.php.net/co.php/pear-core/PEAR/PackageFile/Generator/PHP5/Common.php?r=1.1&p=1 Index: pear-core/PEAR/PackageFile/Generator/PHP5/Common.php +++ pear-core/PEAR/PackageFile/Generator/PHP5/Common.php <?php class PEAR_PackageFile_Generator_PHP5_Common { protected $pf; protected $options = array( 'formatfilelist' => true, 'flatfilelist' => false, ); public function __construct(PEAR_PackageFile_PHP5_v1 $pf, $options = array()) { $this->pf = $pf; $this->options = array_merge($this->options, $options); } public function toXml() { if ($this->options['flatfilelist']) { $this->pf->flattenFilelist(); } else { $this->pf->recursiveFilelist(); } $this->beautify(); return $this->pf->dom->saveXml(); } public function beautify() { $result = $this->pf->dom->createElement('package'); $package = $this->pf->dom->replaceChild($result, $this->pf->dom->documentElement); $this->_recurbeautify($result, $package, ''); } private function _recurbeautify($result, $package, $depth) { $echo = $haschildnodes = false; if ($package->nodeName == 'filelist') { if ($this->options['formatfilelist']) { $result->parentNode->replaceChild($package, $result); return; } foreach ($package->childNodes as $node) { echo $node->nodeName . "\n"; } $echo = true; } foreach ($package->childNodes as $node) { if ($echo) echo $node->nodeName . "\n"; if ($node->nodeType == XML_COMMENT_NODE) { $result->appendChild($this->pf->dom->createTextNode("\n $depth")); $result->appendChild($node->cloneNode()); } if ($node->nodeType != XML_ELEMENT_NODE) { continue; } $haschildnodes = true; $result->appendChild($this->pf->dom->createTextNode("\n$depth ")); $newnode = $node->cloneNode(); $result->appendChild($newnode); $text = $this->_getElementText($node); foreach ($text as $i => $val) { if ($i == count($text) - 1) { if (is_string($val)) { if (count($text) > 1 || strpos($val, "\n")) { $val = trim($val) . "\n$depth "; } else { $val = trim($val); } } } if (!is_string($val)) { if ($val->nodeType == XML_ELEMENT_NODE) { $another = $val->cloneNode(); $newnode->appendChild($this->pf->dom->createTextNode("\n$depth ")); $newnode->appendChild($another); $this->_recurbeautify($another, $val, "$depth "); if ($i == count($text) - 1) { $newnode->appendChild($this->pf->dom->createTextNode("\n$depth ")); } } else { $result->appendChild($val); } } else { if (!trim($val)) { continue; } $newnode->appendChild($this->pf->dom->createTextNode($val)); } } } if (!$haschildnodes) { $res = $this->_getElementText($package); foreach ($res as $text) { if (is_string($text)) { $result->appendChild($this->pf->dom->createTextNode($text)); } else { $result->appendChild($text); } } } else { $result->appendChild($this->pf->dom->createTextNode("\n$depth")); } } private function _getElementText($node) { $text = array(); $index = 0; foreach ($node->childNodes as $cnode) { if ($cnode->nodeType == XML_TEXT_NODE) { if (!trim($cnode->nodeValue)) { continue; } if (!isset($text[$index])) { $text[$index] = ''; } $text[$index] .= $cnode->nodeValue; } elseif ($cnode->nodeType == XML_ENTITY_NODE || $cnode->nodeType == XML_CDATA_SECTION_NODE || $cnode->nodeType == XML_ENTITY_REF_NODE || $cnode->nodeType == XML_ELEMENT_NODE) { if (isset($text[$index])) { $index++; } $text[$index] = $cnode; } } return $text; } } ?> http://cvs.php.net/co.php/pear-core/PEAR/PackageFile/Generator/PHP5/v1.php?r=1.1&p=1 Index: pear-core/PEAR/PackageFile/Generator/PHP5/v1.php +++ pear-core/PEAR/PackageFile/Generator/PHP5/v1.php <?php require_once 'PEAR/PackageFile/Generator/PHP5/Common.php'; class PEAR_PackageFile_Generator_PHP5_v1 extends PEAR_PackageFile_Generator_PHP5_Common { public function toXml() { // re-format the dom stuff parent::toXml(); // do special formatting for short tags like name return $this->pf->dom->saveXml(); } } ?>
http://cvs.php.net/diff.php/pear-core/PEAR/PackageFile/PHP5/v1.php?r1=1.1&r2=1.2&ty=u Index: pear-core/PEAR/PackageFile/PHP5/v1.php diff -u pear-core/PEAR/PackageFile/PHP5/v1.php:1.1 pear-core/PEAR/PackageFile/PHP5/v1.php:1.2 --- pear-core/PEAR/PackageFile/PHP5/v1.php:1.1 Sun Aug 29 18:33:14 2004 +++ pear-core/PEAR/PackageFile/PHP5/v1.php Wed Sep 1 14:03:25 2004 @@ -4,7 +4,7 @@ /** * @var DOMDocument */ - protected $dom; + public $dom; private $_schemaValidateWarnings = array(); /** * Never call this function directly - its only purpose @@ -18,8 +18,185 @@ public function validate() { + static $attempt = false; + $this->_schemaValidateWarnings = array(); set_error_handler(array($this, '_catchWarnings')); + $this->dom->schemaValidate(dirname(__FILE__) . DIRECTORY_SEPARATOR . + 'package-1.0.xsd'); restore_error_handler(); + $ret = false; + if (count($this->_schemaValidateWarnings)) { + if (!$attempt) { + $attempt = true; + $this->_salvageCrappySyntax(); + $ret = $this->validate(); + } + $attempt = false; + return $ret; + } + return true; + } + + public function getValidationWarnings() + { + return $this->_schemaValidateWarnings; + } + + /** + * for older, mal-formed package.xml, try to force it to be conformant first + */ + private function _salvageCrappySyntax() + { + $working = $this->dom->cloneNode(true); + $sx = simplexml_import_dom($working); + $rel = $this->dom->documentElement->getElementsByTagname('release')->item(0); + $fields = array_keys((array) $sx); + if (!in_array('name', $fields) || !in_array('summary', $fields) || + !in_array('description', $fields) || !in_array('maintainers', $fields) || + !in_array('release', $fields)) { + // if any of these are missing, it's hopeless + return; + } + $releasefields = array_keys((array) $sx->release); + if (!in_array('version', $releasefields) || !in_array('date', $releasefields) || + !in_array('state', $releasefields) || !in_array('notes', $releasefields) || + !in_array('filelist', $releasefields)) { + // if any of these are missing, it's hopeless + return; + } + if (!isset($sx->release->license) && isset($sx->license)) { + // fix those package.xml that only set global license + // by moving it into the release tag + $license = $this->dom->documentElement->removeChild( + $this->dom->documentElement->getElementsByTagname('license')->item(0)); + $this->dom->documentElement-> + getElementsByTagname('release')->item(0)-> + insertBefore( + $license, + $this->dom->documentElement->getElementsByTagname('state')->item(0)); + $this->_reorderBasic(); + return $this->_reorderRelease($sx); + } + $test = array('name', 'summary', 'description', '?license', 'maintainers', 'release','?changelog'); + $skip = false; + foreach ($test as $next) { + if (!$skip) { + $tag = array_shift($fields); + } + $skip = false; + if ($next{0} == '?') { + $opt = true; + $next = substr($next, 1); + } else { + $opt = false; + } + if ($next != $tag) { + if (!$opt) { + $this->_reorderBasic(); + } else { + // skip missing optional tags + $skip = true; + } + } + } + if (is_array($fields) && count($fields)) { + $this->_reorderBasic(); + } + $test = array('version', 'date', 'license', 'state', 'notes', '?warnings', '?provides', '?deps', '?configureoptions', 'filelist'); + $skip = false; + foreach ($test as $next) { + if (!$skip) { + $tag = array_shift($releasefields); + } + $skip = false; + if ($next{0} == '?') { + $opt = true; + $next = substr($next, 1); + } else { + $opt = false; + } + if ($next != $tag) { + if (!$opt) { + $this->_reorderRelease($sx); + } else { + // skip missing optional tags + $skip = true; + } + } + } + if (is_array($releasefields) && count($releasefields)) { + $this->_reorderRelease($sx, $rel, $this->dom->documentElement); + } + } + + private function _reorderBasic() + { + $package = $this->dom->createElement('package'); + $package->appendChild($this->dom->createTextNode("\n ")); + $package->appendChild($this->dom->documentElement-> + getElementsByTagname('name')->item(0)); + $package->appendChild($this->dom->createTextNode("\n ")); + $package->appendChild($this->dom->documentElement-> + getElementsByTagname('summary')->item(0)); + $package->appendChild($this->dom->createTextNode("\n ")); + $package->appendChild($this->dom->documentElement-> + getElementsByTagname('description')->item(0)); + $package->appendChild($this->dom->createTextNode("\n ")); + $package->appendChild($this->dom->documentElement-> + getElementsByTagname('maintainers')->item(0)); + $package->appendChild($this->dom->createTextNode("\n ")); + $package->appendChild($this->dom->documentElement-> + getElementsByTagname('release')->item(0)); + $package->appendChild($this->dom->createTextNode("\n")); + $this->dom->replaceChild($this->dom->documentElement, $package); + } + + private function _reorderRelease($sx, $rel, $par) + { + $release = $this->dom->createElement('release'); + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('version')->item(0)); + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('date')->item(0)); + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('license')->item(0)); + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('state')->item(0)); + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('notes')->item(0)); + if (isset($sx->release->warnings)) { + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('warnings')->item(0)); + } + if (isset($sx->release->provides)) { + foreach ($rel->getElementsByTagname('provides') as $provide) { + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($provide); + } + } + if (isset($sx->release->deps)) { + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('deps')->item(0)); + } + if (isset($sx->release->configureoptions)) { + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('configureoptions')->item(0)); + } + $release->appendChild($this->dom->createTextNode("\n ")); + $release->appendChild($rel-> + getElementsByTagname('filelist')->item(0)); + $release->appendChild($this->dom->createTextNode("\n ")); + $par->replaceChild( + $release, + $rel); } public function fromDom($dom) @@ -35,6 +212,158 @@ */ public function fromV2($packagefile) { + } + + private function _getRole($node) + { + if ($node->hasAttribute('role')) { + return $node->getAttribute('role'); + } + $node = $node->parentNode; + while($node->nodeName == 'dir') { + if ($node->hasAttribute('role')) { + return $node->getAttribute('role'); + } + } + } + + private function _getFullPath($node) + { + $i = 0; + $path = $node->getAttribute('name'); + $node = $node->parentNode; + while($node->nodeName == 'dir' && $node->getAttribute('name') != '/') { + $path = $node->getAttribute('name') . '/' . $path; + $node = $node->parentNode; + } + return str_replace('//', '/', $path); + } + + private function _getParentFilelist($node) + { + while ($node->nodeName != 'filelist' && $node->nodeName != 'package') { + $node = $node->parentNode; + } + if ($node->nodeName == 'package') { + throw new Exception('node is not a child of filelist'); + } + return $node; + } + + function sortdomfiles($a, $b) + { + $z = strnatcmp($a->getAttribute('name'), $b->getAttribute('name')); + if ($z == 1) return -1; + if ($z == -1) return 1; + return 0; + } + + public function flattenFilelist($cmp = null, $depth = ' ') + { + $d = $this->dom; + if ($cmp === null) { + $cmp = $d->documentElement->getElementsByTagname('filelist')->item(0); + } + $sx = $d->createElement('filelist'); + $cmp->parentNode->replaceChild($sx, $cmp); + $sx->appendChild($d->createTextNode("\n$depth")); + $sx->appendChild($dir = $d->createElement('dir')); + $sx->appendChild($d->createTextNode("\n ")); + $dir->setAttribute('name', '/'); + $files = array(); + foreach ($cmp->getElementsByTagname('file') as $node) { + if ($this->_getParentFilelist($node) === $cmp) { + $el = $node->cloneNode(); + $el->setAttribute('role', $this->_getRole($node)); + $el->setAttribute('name', $this->_getFullPath($node)); + $files[] = $el; + } + } + usort($files, array($this, 'sortdomfiles')); + foreach ($files as $node) { + $dir->appendChild($d->createTextNode("\n$depth ")); + $dir->appendChild($node); + } + $dir->appendChild($d->createTextNode("\n ")); + return $files; + } + + public function recursiveFilelist($cmp = null, $depth = ' ') + { + $files = array_reverse($this->flattenFilelist()); + $d = $this->dom; + if ($cmp === null) { + $cmp = $d->documentElement->getElementsByTagname('filelist')->item(0); + } + $sx = $d->createElement('filelist'); + $cmp->parentNode->replaceChild($sx, $cmp); + $sx->appendChild($d->createTextNode("\n$depth")); + $sx->appendChild($dirnode = $d->createElement('dir')); + $dirnode->setAttribute('name', '/'); + $sx->appendChild($d->createTextNode("\n$depth")); + $dirs = $mainfiles = array(); + foreach ($files as $file) { + $name = explode('/', $file->getAttribute('name')); + $filename = array_pop($name); + $path = ''; + if (count($name)) { + foreach ($name as $i => $dir) { + if ($path != '') { + $path .= '/'; + } + $path .= $dir; + if (!isset($dirs[$path])) { + $dirs[$path] = $this->dom->createElement('dir'); + $dirs[$path]->setAttribute('name', $dir); + } + if ($i == count($name) - 1) { + $newfile = $file->cloneNode(); + $dirs[$path]->appendChild($this->dom->createTextNode("\n $depth" . + str_repeat(' ', count($name)))); + $dirs[$path]->appendChild($newfile); + $newfile->setAttribute('name', $filename); + } + } + } else { + $mainfiles[$file->getAttribute('name')] = $file->cloneNode(); + } + } + uksort($dirs, 'strnatcmp'); + $dirs = array_reverse($dirs); + $maindirs = array(); + foreach ($dirs as $path => $cdir) { + if (basename($path) != $path && isset($dirs[dirname($path)])) { + $cdir->appendChild($this->dom->createTextNode("\n $depth" . + str_repeat(' ', count(explode('/', dirname($path)))))); + $parent = $dirs[dirname($path)]; + $parent->insertBefore($cdir, $parent->firstChild); + $parent->insertBefore( + $this->dom->createTextNode("\n $depth" . + str_repeat(' ', count(explode('/', dirname($path))))), + $parent->firstChild); + $parent->insertBefore( + $co = $this->dom->createTextNode(' '), + $cdir->nextSibling); + $parent->insertBefore( + $this->dom->createComment($path), + $co->nextSibling); + } else { + $cdir->appendChild($this->dom->createTextNode("\n $depth")); + array_unshift($maindirs, array($path, $cdir)); + } + } + foreach ($maindirs as $cdir) { + $dirnode->appendChild($this->dom->createTextNode("\n $depth")); + $dirnode->appendChild($cdir[1]); + $dirnode->appendChild($this->dom->createTextNode(" ")); + $dirnode->appendChild($this->dom->createComment(" $cdir[0] ")); + } + uksort($mainfiles, 'strnatcmp'); + foreach ($mainfiles as $file) { + $dirnode->appendChild($this->dom->createTextNode("\n $depth")); + $dirnode->appendChild($file); + } + $dirnode->appendChild($this->dom->createTextNode("\n $depth")); } /** http://cvs.php.net/diff.php/pear-core/PEAR/PackageFile/Parser/PHP5/v1.php?r1=1.1&r2=1.2&ty=u Index: pear-core/PEAR/PackageFile/Parser/PHP5/v1.php diff -u pear-core/PEAR/PackageFile/Parser/PHP5/v1.php:1.1 pear-core/PEAR/PackageFile/Parser/PHP5/v1.php:1.2 --- pear-core/PEAR/PackageFile/Parser/PHP5/v1.php:1.1 Sun Aug 29 18:33:15 2004 +++ pear-core/PEAR/PackageFile/Parser/PHP5/v1.php Wed Sep 1 14:03:25 2004 @@ -8,10 +8,10 @@ * @param string contents of package.xml file, version 1.0 * @return PEAR_PackageFile_PHP5_v1 */ - public function parse($data) + static public function parse($data) { $dom = new DOMDocument; - if (!@$dom->loadXml()) { + if (!@$dom->loadXml($data)) { throw new Exception('(this will be PEAR_Exception) invalid package.xml, not xml'); } include_once 'PEAR/PackageFile/PHP5/v1.php'; @@ -20,4 +20,18 @@ return $p; } } +/* +leaving in because I might move things around +set_include_path('C:/devel/pear_with_channels'); +//$data = file_get_contents('C:/php4/pear_pkgs/Auth/package.xml'); +$data = file_get_contents('C:/devel/pear_with_channels/package-PEAR.xml'); +$a = PEAR_PackageFile_Parser_PHP5_v1::parse($data); +if (!$a->validate()) { + var_dump($a->getValidationWarnings()); +} else { + require_once 'PEAR/PackageFile/Generator/PHP5/v1.php'; + $b = new PEAR_PackageFile_Generator_PHP5_v1($a); + echo $b->toXml(); +} +*/ ?> http://cvs.php.net/co.php/pear-core/PEAR/PackageFile/PHP5/package-1.0.xsd?r=1.1&p=1 Index: pear-core/PEAR/PackageFile/PHP5/package-1.0.xsd +++ pear-core/PEAR/PackageFile/PHP5/package-1.0.xsd <?xml version="1.0"?> <!-- edited with XMLSPY v2004 rel. 4 U (http://www.xmlspy.com) by Greg Beaver (The Chiara String Quartet) --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="package"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="summary"/> <xs:element ref="description"/> <xs:element ref="license" minOccurs="0"/> <xs:element ref="maintainers"/> <xs:element ref="release"/> <xs:element ref="changelog" minOccurs="0"/> </xs:sequence> <xs:attribute name="type" default="empty"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="source"/> <xs:enumeration value="binary"/> <xs:enumeration value="empty"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="version" type="xs:string" use="required"/> </xs:complexType> </xs:element> <xs:element name="name" type="xs:string"/> <xs:element name="summary" type="xs:string"/> <xs:element name="description" type="xs:string"/> <xs:element name="license" type="xs:string"/> <xs:element name="maintainers"> <xs:complexType> <xs:sequence maxOccurs="unbounded"> <xs:element ref="maintainer"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="maintainer"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="user"/> <xs:element ref="role"/> <xs:element ref="name"/> <xs:element ref="email"/> </xs:choice> </xs:complexType> </xs:element> <xs:element name="user" type="xs:string"/> <xs:element name="role" type="xs:string"/> <xs:element name="email" type="xs:string"/> <xs:element name="changelog"> <xs:complexType> <xs:sequence minOccurs="0" maxOccurs="unbounded"> <xs:element name="release" type="changelogrelease"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="release"> <xs:complexType> <xs:sequence> <xs:element ref="version"/> <xs:element ref="date"/> <xs:element ref="license"/> <xs:element ref="state"/> <xs:element ref="notes"/> <xs:element ref="warnings" minOccurs="0"/> <xs:element ref="provides" minOccurs="0" maxOccurs="unbounded"/> <xs:element ref="deps" minOccurs="0"/> <xs:element ref="configureoptions" minOccurs="0"/> <xs:element ref="filelist"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="version" type="xs:string"/> <xs:element name="state" type="xs:string"/> <xs:element name="date" type="xs:string"/> <xs:element name="notes" type="xs:string"/> <xs:element name="provides"> <xs:complexType> <xs:attribute name="type" use="required"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="ext"/> <xs:enumeration value="prog"/> <xs:enumeration value="class"/> <xs:enumeration value="function"/> <xs:enumeration value="feature"/> <xs:enumeration value="api"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="extends" type="xs:string"/> </xs:complexType> </xs:element> <xs:element name="filelist"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="dir"/> <xs:element ref="file"/> <xs:element ref="libfile"/> </xs:choice> </xs:complexType> </xs:element> <xs:element name="dir"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="dir"/> <xs:element ref="file"/> <xs:element ref="libfile"/> </xs:choice> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="baseinstalldir" type="xs:string"/> <xs:attribute name="role"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="php"/> <xs:enumeration value="ext"/> <xs:enumeration value="test"/> <xs:enumeration value="doc"/> <xs:enumeration value="data"/> <xs:enumeration value="script"/> <xs:enumeration value="src"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element> <xs:element name="file"> <xs:complexType> <xs:sequence> <xs:element ref="replace" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="role" default="php"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="php"/> <xs:enumeration value="ext"/> <xs:enumeration value="test"/> <xs:enumeration value="doc"/> <xs:enumeration value="data"/> <xs:enumeration value="script"/> <xs:enumeration value="src"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="debug" default="na"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="na"/> <xs:enumeration value="on"/> <xs:enumeration value="off"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="threaded" default="na"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="na"/> <xs:enumeration value="on"/> <xs:enumeration value="off"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="format" type="xs:string"/> <xs:attribute name="baseinstalldir" type="xs:string"/> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="install-as" type="xs:string"/> <xs:attribute name="platform" type="xs:string"/> <xs:attribute name="md5sum" type="xs:string"/> </xs:complexType> </xs:element> <xs:element name="libfile"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element ref="libname"/> <xs:element ref="sources"/> <xs:element ref="includes"/> <xs:element ref="libadd"/> </xs:choice> </xs:complexType> </xs:element> <xs:element name="libname" type="xs:string"/> <xs:element name="sources" type="xs:string"/> <xs:element name="includes" type="xs:string"/> <xs:element name="libadd" type="xs:string"/> <xs:element name="deps"> <xs:complexType> <xs:sequence minOccurs="0" maxOccurs="unbounded"> <xs:element ref="dep"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="dep"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="type" use="required"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="pkg"/> <xs:enumeration value="ext"/> <xs:enumeration value="php"/> <xs:enumeration value="prog"/> <xs:enumeration value="ldlib"/> <xs:enumeration value="ltlib"/> <xs:enumeration value="os"/> <xs:enumeration value="websrv"/> <xs:enumeration value="sapi"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="rel"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="has"/> <xs:enumeration value="eq"/> <xs:enumeration value="lt"/> <xs:enumeration value="le"/> <xs:enumeration value="gt"/> <xs:enumeration value="ge"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="version" type="xs:string"/> <xs:attribute name="optional" default="no"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="yes"/> <xs:enumeration value="no"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="replace"> <xs:complexType> <xs:attribute name="from" type="xs:string" use="required"/> <xs:attribute name="to" type="xs:string" use="required"/> <xs:attribute name="type" use="required"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="package-info"/> <xs:enumeration value="pear-config"/> <xs:enumeration value="php-const"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element> <xs:complexType name="changelogrelease"> <xs:sequence> <xs:element ref="version"/> <xs:element ref="date"/> <xs:element ref="license"/> <xs:element ref="state"/> <xs:element ref="notes"/> </xs:sequence> </xs:complexType> <xs:element name="warnings" type="xs:string"/> <xs:element name="configureoptions"> <xs:complexType> <xs:sequence> <xs:element ref="configureoption" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="configureoption"> <xs:complexType> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="default" type="xs:string"/> <xs:attribute name="prompt" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:schema> http://cvs.php.net/co.php/pear-core/PEAR/PackageFile/Generator/PHP5/Common.php?r=1.1&p=1 Index: pear-core/PEAR/PackageFile/Generator/PHP5/Common.php +++ pear-core/PEAR/PackageFile/Generator/PHP5/Common.php <?php class PEAR_PackageFile_Generator_PHP5_Common { protected $pf; protected $options = array( 'formatfilelist' => true, 'flatfilelist' => false, ); public function __construct(PEAR_PackageFile_PHP5_v1 $pf, $options = array()) { $this->pf = $pf; $this->options = array_merge($this->options, $options); } public function toXml() { if ($this->options['flatfilelist']) { $this->pf->flattenFilelist(); } else { $this->pf->recursiveFilelist(); } $this->beautify(); return $this->pf->dom->saveXml(); } public function beautify() { $result = $this->pf->dom->createElement('package'); $package = $this->pf->dom->replaceChild($result, $this->pf->dom->documentElement); $this->_recurbeautify($result, $package, ''); } private function _recurbeautify($result, $package, $depth) { $echo = $haschildnodes = false; if ($package->nodeName == 'filelist') { if ($this->options['formatfilelist']) { $result->parentNode->replaceChild($package, $result); return; } foreach ($package->childNodes as $node) { echo $node->nodeName . "\n"; } $echo = true; } foreach ($package->childNodes as $node) { if ($echo) echo $node->nodeName . "\n"; if ($node->nodeType == XML_COMMENT_NODE) { $result->appendChild($this->pf->dom->createTextNode("\n $depth")); $result->appendChild($node->cloneNode()); } if ($node->nodeType != XML_ELEMENT_NODE) { continue; } $haschildnodes = true; $result->appendChild($this->pf->dom->createTextNode("\n$depth ")); $newnode = $node->cloneNode(); $result->appendChild($newnode); $text = $this->_getElementText($node); foreach ($text as $i => $val) { if ($i == count($text) - 1) { if (is_string($val)) { if (count($text) > 1 || strpos($val, "\n")) { $val = trim($val) . "\n$depth "; } else { $val = trim($val); } } } if (!is_string($val)) { if ($val->nodeType == XML_ELEMENT_NODE) { $another = $val->cloneNode(); $newnode->appendChild($this->pf->dom->createTextNode("\n$depth ")); $newnode->appendChild($another); $this->_recurbeautify($another, $val, "$depth "); if ($i == count($text) - 1) { $newnode->appendChild($this->pf->dom->createTextNode("\n$depth ")); } } else { $result->appendChild($val); } } else { if (!trim($val)) { continue; } $newnode->appendChild($this->pf->dom->createTextNode($val)); } } } if (!$haschildnodes) { $res = $this->_getElementText($package); foreach ($res as $text) { if (is_string($text)) { $result->appendChild($this->pf->dom->createTextNode($text)); } else { $result->appendChild($text); } } } else { $result->appendChild($this->pf->dom->createTextNode("\n$depth")); } } private function _getElementText($node) { $text = array(); $index = 0; foreach ($node->childNodes as $cnode) { if ($cnode->nodeType == XML_TEXT_NODE) { if (!trim($cnode->nodeValue)) { continue; } if (!isset($text[$index])) { $text[$index] = ''; } $text[$index] .= $cnode->nodeValue; } elseif ($cnode->nodeType == XML_ENTITY_NODE || $cnode->nodeType == XML_CDATA_SECTION_NODE || $cnode->nodeType == XML_ENTITY_REF_NODE || $cnode->nodeType == XML_ELEMENT_NODE) { if (isset($text[$index])) { $index++; } $text[$index] = $cnode; } } return $text; } } ?> http://cvs.php.net/co.php/pear-core/PEAR/PackageFile/Generator/PHP5/v1.php?r=1.1&p=1 Index: pear-core/PEAR/PackageFile/Generator/PHP5/v1.php +++ pear-core/PEAR/PackageFile/Generator/PHP5/v1.php <?php require_once 'PEAR/PackageFile/Generator/PHP5/Common.php'; class PEAR_PackageFile_Generator_PHP5_v1 extends PEAR_PackageFile_Generator_PHP5_Common { public function toXml() { // re-format the dom stuff parent::toXml(); // do special formatting for short tags like name return $this->pf->dom->saveXml(); } } ?>