Subversion Repositories Applications.gtt

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
Line 2... Line 2...
2
/**
2
/**
3
 * package.xml generation class, package.xml version 2.0
3
 * package.xml generation class, package.xml version 2.0
4
 *
4
 *
5
 * PHP versions 4 and 5
5
 * PHP versions 4 and 5
6
 *
6
 *
7
 * LICENSE: This source file is subject to version 3.0 of the PHP license
-
 
8
 * that is available through the world-wide-web at the following URI:
-
 
9
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
-
 
10
 * the PHP License and are unable to obtain it through the web, please
-
 
11
 * send a note to license@php.net so we can mail you a copy immediately.
-
 
12
 *
-
 
13
 * @category   pear
7
 * @category   pear
14
 * @package    PEAR
8
 * @package    PEAR
15
 * @author     Greg Beaver <cellog@php.net>
9
 * @author     Greg Beaver <cellog@php.net>
16
 * @author     Stephan Schmidt (original XML_Serializer code)
10
 * @author     Stephan Schmidt (original XML_Serializer code)
17
 * @copyright  1997-2006 The PHP Group
11
 * @copyright  1997-2009 The Authors
18
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
12
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
19
 * @version    CVS: $Id: v2.php,v 1.35 2006/03/25 21:09:08 cellog Exp $
-
 
20
 * @link       http://pear.php.net/package/PEAR
13
 * @link       http://pear.php.net/package/PEAR
21
 * @since      File available since Release 1.4.0a1
14
 * @since      File available since Release 1.4.0a1
22
 */
15
 */
23
/**
16
/**
24
 * file/dir manipulation routines
17
 * file/dir manipulation routines
25
 */
18
 */
26
require_once 'System.php';
19
require_once 'System.php';
-
 
20
require_once 'XML/Util.php';
-
 
21
 
27
/**
22
/**
28
 * This class converts a PEAR_PackageFile_v2 object into any output format.
23
 * This class converts a PEAR_PackageFile_v2 object into any output format.
29
 *
24
 *
30
 * Supported output formats include array, XML string (using S. Schmidt's
25
 * Supported output formats include array, XML string (using S. Schmidt's
31
 * XML_Serializer, slightly customized)
26
 * XML_Serializer, slightly customized)
32
 * @category   pear
27
 * @category   pear
33
 * @package    PEAR
28
 * @package    PEAR
34
 * @author     Greg Beaver <cellog@php.net>
29
 * @author     Greg Beaver <cellog@php.net>
35
 * @author     Stephan Schmidt (original XML_Serializer code)
30
 * @author     Stephan Schmidt (original XML_Serializer code)
36
 * @copyright  1997-2006 The PHP Group
31
 * @copyright  1997-2009 The Authors
37
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
32
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
38
 * @version    Release: 1.5.1
33
 * @version    Release: 1.10.1
39
 * @link       http://pear.php.net/package/PEAR
34
 * @link       http://pear.php.net/package/PEAR
40
 * @since      Class available since Release 1.4.0a1
35
 * @since      Class available since Release 1.4.0a1
41
 */
36
 */
42
class PEAR_PackageFile_Generator_v2
37
class PEAR_PackageFile_Generator_v2
43
{
38
{
Line 45... Line 40...
45
    * default options for the serialization
40
    * default options for the serialization
46
    * @access private
41
    * @access private
47
    * @var array $_defaultOptions
42
    * @var array $_defaultOptions
48
    */
43
    */
49
    var $_defaultOptions = array(
44
    var $_defaultOptions = array(
50
                         'indent'             => ' ',                    // string used for indentation
45
        'indent'             => ' ',                    // string used for indentation
51
                         'linebreak'          => "\n",                  // string used for newlines
46
        'linebreak'          => "\n",                  // string used for newlines
52
                         'typeHints'          => false,                 // automatically add type hin attributes
47
        'typeHints'          => false,                 // automatically add type hin attributes
53
                         'addDecl'            => true,                 // add an XML declaration
48
        'addDecl'            => true,                 // add an XML declaration
54
                         'defaultTagName'     => 'XML_Serializer_Tag',  // tag used for indexed arrays or invalid names
49
        'defaultTagName'     => 'XML_Serializer_Tag',  // tag used for indexed arrays or invalid names
55
                         'classAsTagName'     => false,                 // use classname for objects in indexed arrays
50
        'classAsTagName'     => false,                 // use classname for objects in indexed arrays
56
                         'keyAttribute'       => '_originalKey',        // attribute where original key is stored
51
        'keyAttribute'       => '_originalKey',        // attribute where original key is stored
57
                         'typeAttribute'      => '_type',               // attribute for type (only if typeHints => true)
52
        'typeAttribute'      => '_type',               // attribute for type (only if typeHints => true)
58
                         'classAttribute'     => '_class',              // attribute for class of objects (only if typeHints => true)
53
        'classAttribute'     => '_class',              // attribute for class of objects (only if typeHints => true)
59
                         'scalarAsAttributes' => false,                 // scalar values (strings, ints,..) will be serialized as attribute
54
        'scalarAsAttributes' => false,                 // scalar values (strings, ints,..) will be serialized as attribute
60
                         'prependAttributes'  => '',                    // prepend string for attributes
55
        'prependAttributes'  => '',                    // prepend string for attributes
61
                         'indentAttributes'   => false,                 // indent the attributes, if set to '_auto', it will indent attributes so they all start at the same column
56
        'indentAttributes'   => false,                 // indent the attributes, if set to '_auto', it will indent attributes so they all start at the same column
62
                         'mode'               => 'simplexml',             // use 'simplexml' to use parent name as tagname if transforming an indexed array
57
        'mode'               => 'simplexml',             // use 'simplexml' to use parent name as tagname if transforming an indexed array
63
                         'addDoctype'         => false,                 // add a doctype declaration
58
        'addDoctype'         => false,                 // add a doctype declaration
64
                         'doctype'            => null,                  // supply a string or an array with id and uri ({@see PEAR_PackageFile_Generator_v2_PEAR_PackageFile_Generator_v2_XML_Util::getDoctypeDeclaration()}
59
        'doctype'            => null,                  // supply a string or an array with id and uri ({@see XML_Util::getDoctypeDeclaration()}
65
                         'rootName'           => 'package',                  // name of the root tag
60
        'rootName'           => 'package',                  // name of the root tag
66
                         'rootAttributes'     => array(
61
        'rootAttributes'     => array(
67
                             'version' => '2.0',
62
            'version' => '2.0',
68
                             'xmlns' => 'http://pear.php.net/dtd/package-2.0',
63
            'xmlns' => 'http://pear.php.net/dtd/package-2.0',
69
                             'xmlns:tasks' => 'http://pear.php.net/dtd/tasks-1.0',
64
            'xmlns:tasks' => 'http://pear.php.net/dtd/tasks-1.0',
70
                             'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
65
            'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
71
                             'xsi:schemaLocation' => 'http://pear.php.net/dtd/tasks-1.0
66
            'xsi:schemaLocation' => 'http://pear.php.net/dtd/tasks-1.0
72
http://pear.php.net/dtd/tasks-1.0.xsd
67
http://pear.php.net/dtd/tasks-1.0.xsd
73
http://pear.php.net/dtd/package-2.0
68
http://pear.php.net/dtd/package-2.0
74
http://pear.php.net/dtd/package-2.0.xsd',
69
http://pear.php.net/dtd/package-2.0.xsd',
75
                         ),               // attributes of the root tag
70
        ),               // attributes of the root tag
76
                         'attributesArray'    => 'attribs',                  // all values in this key will be treated as attributes
71
        'attributesArray'    => 'attribs',                  // all values in this key will be treated as attributes
77
                         'contentName'        => '_content',                   // this value will be used directly as content, instead of creating a new tag, may only be used in conjuction with attributesArray
72
        'contentName'        => '_content',                   // this value will be used directly as content, instead of creating a new tag, may only be used in conjunction with attributesArray
78
                         'beautifyFilelist'   => false,
73
        'beautifyFilelist'   => false,
79
                         'encoding' => 'UTF-8',
74
        'encoding' => 'UTF-8',
80
                        );
75
    );
Line 81... Line 76...
81
 
76
 
82
   /**
77
   /**
83
    * options for the serialization
78
    * options for the serialization
84
    * @access private
79
    * @access private
Line 102... Line 97...
102
     */
97
     */
103
    var $_packagefile;
98
    var $_packagefile;
104
    /**
99
    /**
105
     * @param PEAR_PackageFile_v2
100
     * @param PEAR_PackageFile_v2
106
     */
101
     */
107
    function PEAR_PackageFile_Generator_v2(&$packagefile)
102
    function __construct(&$packagefile)
108
    {
103
    {
109
        $this->_packagefile = &$packagefile;
104
        $this->_packagefile = &$packagefile;
-
 
105
        if (isset($this->_packagefile->encoding)) {
-
 
106
            $this->_defaultOptions['encoding'] = $this->_packagefile->encoding;
-
 
107
        }
110
    }
108
    }
Line 111... Line 109...
111
 
109
 
112
    /**
110
    /**
113
     * @return string
111
     * @return string
114
     */
112
     */
115
    function getPackagerVersion()
113
    function getPackagerVersion()
116
    {
114
    {
117
        return '1.5.1';
115
        return '1.10.1';
Line 118... Line 116...
118
    }
116
    }
119
 
117
 
120
    /**
118
    /**
Line 142... Line 140...
142
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: "' .
140
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: "' .
143
                basename($pf1->getPackageFile()) .
141
                basename($pf1->getPackageFile()) .
144
                '" is not equivalent to "' . basename($this->_packagefile->getPackageFile())
142
                '" is not equivalent to "' . basename($this->_packagefile->getPackageFile())
145
                . '"');
143
                . '"');
146
        }
144
        }
-
 
145
 
147
        if ($where === null) {
146
        if ($where === null) {
148
            if (!($where = System::mktemp(array('-d')))) {
147
            if (!($where = System::mktemp(array('-d')))) {
149
                return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: mktemp failed');
148
                return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: mktemp failed');
150
            }
149
            }
151
        } elseif (!@System::mkDir(array('-p', $where))) {
150
        } elseif (!@System::mkDir(array('-p', $where))) {
152
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: "' . $where . '" could' .
151
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: "' . $where . '" could' .
153
                ' not be created');
152
                ' not be created');
154
        }
153
        }
-
 
154
 
155
        if (file_exists($where . DIRECTORY_SEPARATOR . 'package.xml') &&
155
        $file = $where . DIRECTORY_SEPARATOR . 'package.xml';
156
              !is_file($where . DIRECTORY_SEPARATOR . 'package.xml')) {
156
        if (file_exists($file) && !is_file($file)) {
157
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: unable to save package.xml as' .
157
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: unable to save package.xml as' .
158
                ' "' . $where . DIRECTORY_SEPARATOR . 'package.xml"');
158
                ' "' . $file  .'"');
159
        }
159
        }
-
 
160
 
160
        if (!$this->_packagefile->validate(PEAR_VALIDATE_PACKAGING)) {
161
        if (!$this->_packagefile->validate(PEAR_VALIDATE_PACKAGING)) {
161
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: invalid package.xml');
162
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: invalid package.xml');
162
        }
163
        }
-
 
164
 
163
        $ext = $compress ? '.tgz' : '.tar';
165
        $ext = $compress ? '.tgz' : '.tar';
164
        $pkgver = $this->_packagefile->getPackage() . '-' . $this->_packagefile->getVersion();
166
        $pkgver = $this->_packagefile->getPackage() . '-' . $this->_packagefile->getVersion();
165
        $dest_package = getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext;
167
        $dest_package = getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext;
166
        if (file_exists(getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext) &&
168
        if (file_exists($dest_package) && !is_file($dest_package)) {
167
              !is_file(getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext)) {
-
 
168
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: cannot create tgz file "' .
169
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: cannot create tgz file "' .
169
                getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext . '"');
170
                $dest_package . '"');
170
        }
171
        }
-
 
172
 
171
        if ($pkgfile = $this->_packagefile->getPackageFile()) {
173
        $pkgfile = $this->_packagefile->getPackageFile();
172
            $pkgdir = dirname(realpath($pkgfile));
-
 
173
            $pkgfile = basename($pkgfile);
-
 
174
        } else {
174
        if (!$pkgfile) {
175
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: package file object must ' .
175
            return PEAR::raiseError('PEAR_Packagefile_v2::toTgz: package file object must ' .
176
                'be created from a real file');
176
                'be created from a real file');
177
        }
177
        }
-
 
178
 
-
 
179
        $pkgdir  = dirname(realpath($pkgfile));
-
 
180
        $pkgfile = basename($pkgfile);
-
 
181
 
178
        // {{{ Create the package file list
182
        // {{{ Create the package file list
179
        $filelist = array();
183
        $filelist = array();
180
        $i = 0;
184
        $i = 0;
181
        $this->_packagefile->flattenFilelist();
185
        $this->_packagefile->flattenFilelist();
182
        $contents = $this->_packagefile->getContents();
186
        $contents = $this->_packagefile->getContents();
183
        if (isset($contents['bundledpackage'])) { // bundles of packages
187
        if (isset($contents['bundledpackage'])) { // bundles of packages
184
            $contents = $contents['bundledpackage'];
188
            $contents = $contents['bundledpackage'];
185
            if (!isset($contents[0])) {
189
            if (!isset($contents[0])) {
186
                $contents = array($contents);
190
                $contents = array($contents);
187
            }
191
            }
-
 
192
 
188
            $packageDir = $where;
193
            $packageDir = $where;
189
            foreach ($contents as $i => $package) {
194
            foreach ($contents as $i => $package) {
190
                $fname = $package;
195
                $fname = $package;
191
                $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
196
                $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
192
                if (!file_exists($file)) {
197
                if (!file_exists($file)) {
193
                    return $packager->raiseError("File does not exist: $fname");
198
                    return $packager->raiseError("File does not exist: $fname");
194
                }
199
                }
-
 
200
 
195
                $tfile = $packageDir . DIRECTORY_SEPARATOR . $fname;
201
                $tfile = $packageDir . DIRECTORY_SEPARATOR . $fname;
196
                System::mkdir(array('-p', dirname($tfile)));
202
                System::mkdir(array('-p', dirname($tfile)));
197
                copy($file, $tfile);
203
                copy($file, $tfile);
198
                $filelist[$i++] = $tfile;
204
                $filelist[$i++] = $tfile;
199
                $packager->log(2, "Adding package $fname");
205
                $packager->log(2, "Adding package $fname");
Line 201... Line 207...
201
        } else { // normal packages
207
        } else { // normal packages
202
            $contents = $contents['dir']['file'];
208
            $contents = $contents['dir']['file'];
203
            if (!isset($contents[0])) {
209
            if (!isset($contents[0])) {
204
                $contents = array($contents);
210
                $contents = array($contents);
205
            }
211
            }
206
    
212
 
207
            $packageDir = $where;
213
            $packageDir = $where;
208
            foreach ($contents as $i => $file) {
214
            foreach ($contents as $i => $file) {
209
                $fname = $file['attribs']['name'];
215
                $fname = $file['attribs']['name'];
210
                $atts = $file['attribs'];
216
                $atts = $file['attribs'];
211
                $orig = $file;
217
                $orig = $file;
212
                $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
218
                $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
213
                if (!file_exists($file)) {
219
                if (!file_exists($file)) {
214
                    return $packager->raiseError("File does not exist: $fname");
220
                    return $packager->raiseError("File does not exist: $fname");
215
                } else {
221
                }
-
 
222
 
-
 
223
                $origperms = fileperms($file);
216
                    $tfile = $packageDir . DIRECTORY_SEPARATOR . $fname;
224
                $tfile = $packageDir . DIRECTORY_SEPARATOR . $fname;
217
                    unset($orig['attribs']);
225
                unset($orig['attribs']);
218
                    if (count($orig)) { // file with tasks
226
                if (count($orig)) { // file with tasks
219
                        // run any package-time tasks
227
                    // run any package-time tasks
220
                        $contents = file_get_contents($file);
228
                    $contents = file_get_contents($file);
221
                        foreach ($orig as $tag => $raw) {
229
                    foreach ($orig as $tag => $raw) {
-
 
230
                        $tag = str_replace(
222
                            $tag = str_replace($this->_packagefile->getTasksNs() . ':', '', $tag);
231
                            array($this->_packagefile->getTasksNs() . ':', '-'),
-
 
232
                            array('', '_'), $tag);
223
                            $task = "PEAR_Task_$tag";
233
                        $task = "PEAR_Task_$tag";
224
                            $task = &new $task($this->_packagefile->_config,
234
                        $task = new $task($this->_packagefile->_config,
225
                                $this->_packagefile->_logger,
235
                            $this->_packagefile->_logger,
226
                                PEAR_TASK_PACKAGE);
236
                            PEAR_TASK_PACKAGE);
227
                            $task->init($raw, $atts, null);
237
                        $task->init($raw, $atts, null);
228
                            $res = $task->startSession($this->_packagefile, $contents, $tfile);
238
                        $res = $task->startSession($this->_packagefile, $contents, $tfile);
229
                            if (!$res) {
239
                        if (!$res) {
230
                                continue; // skip this task
240
                            continue; // skip this task
231
                            }
-
 
232
                            if (PEAR::isError($res)) {
-
 
233
                                return $res;
-
 
234
                            }
-
 
235
                            $contents = $res; // save changes
-
 
236
                            System::mkdir(array('-p', dirname($tfile)));
-
 
237
                            $wp = fopen($tfile, "wb");
-
 
238
                            fwrite($wp, $contents);
-
 
239
                            fclose($wp);
-
 
240
                        }
241
                        }
-
 
242
 
-
 
243
                        if (PEAR::isError($res)) {
-
 
244
                            return $res;
241
                    }
245
                        }
-
 
246
 
242
                    if (!file_exists($tfile)) {
247
                        $contents = $res; // save changes
243
                        System::mkdir(array('-p', dirname($tfile)));
248
                        System::mkdir(array('-p', dirname($tfile)));
244
                        copy($file, $tfile);
249
                        $wp = fopen($tfile, "wb");
-
 
250
                        fwrite($wp, $contents);
-
 
251
                        fclose($wp);
245
                    }
252
                    }
246
                    $filelist[$i++] = $tfile;
-
 
247
                    $this->_packagefile->setFileAttribute($fname, 'md5sum', md5_file($tfile), $i - 1);
-
 
248
                    $packager->log(2, "Adding file $fname");
-
 
249
                }
253
                }
-
 
254
 
-
 
255
                if (!file_exists($tfile)) {
-
 
256
                    System::mkdir(array('-p', dirname($tfile)));
-
 
257
                    copy($file, $tfile);
-
 
258
                }
-
 
259
 
-
 
260
                chmod($tfile, $origperms);
-
 
261
                $filelist[$i++] = $tfile;
-
 
262
                $this->_packagefile->setFileAttribute($fname, 'md5sum', md5_file($tfile), $i - 1);
-
 
263
                $packager->log(2, "Adding file $fname");
250
            }
264
            }
251
        }
265
        }
252
            // }}}
266
            // }}}
253
        if ($pf1 !== null) {
-
 
254
            $name = 'package2.xml';
-
 
255
        } else {
-
 
-
 
267
 
256
            $name = 'package.xml';
268
        $name       = $pf1 !== null ? 'package2.xml' : 'package.xml';
257
        }
-
 
258
        $packagexml = $this->toPackageFile($where, PEAR_VALIDATE_PACKAGING, $name);
269
        $packagexml = $this->toPackageFile($where, PEAR_VALIDATE_PACKAGING, $name);
259
        if ($packagexml) {
270
        if ($packagexml) {
260
            $tar =& new Archive_Tar($dest_package, $compress);
271
            $tar = new Archive_Tar($dest_package, $compress);
261
            $tar->setErrorHandling(PEAR_ERROR_RETURN); // XXX Don't print errors
272
            $tar->setErrorHandling(PEAR_ERROR_RETURN); // XXX Don't print errors
262
            // ----- Creates with the package.xml file
273
            // ----- Creates with the package.xml file
263
            $ok = $tar->createModify(array($packagexml), '', $where);
274
            $ok = $tar->createModify(array($packagexml), '', $where);
264
            if (PEAR::isError($ok)) {
275
            if (PEAR::isError($ok)) {
265
                return $packager->raiseError($ok);
276
                return $packager->raiseError($ok);
266
            } elseif (!$ok) {
277
            } elseif (!$ok) {
267
                return $packager->raiseError('PEAR_Packagefile_v2::toTgz(): adding ' . $name .
278
                return $packager->raiseError('PEAR_Packagefile_v2::toTgz(): adding ' . $name .
268
                    ' failed');
279
                    ' failed');
269
            }
280
            }
-
 
281
 
270
            // ----- Add the content of the package
282
            // ----- Add the content of the package
271
            if (!$tar->addModify($filelist, $pkgver, $where)) {
283
            if (!$tar->addModify($filelist, $pkgver, $where)) {
272
                return $packager->raiseError(
284
                return $packager->raiseError(
273
                    'PEAR_Packagefile_v2::toTgz(): tarball creation failed');
285
                    'PEAR_Packagefile_v2::toTgz(): tarball creation failed');
274
            }
286
            }
-
 
287
 
275
            // add the package.xml version 1.0
288
            // add the package.xml version 1.0
276
            if ($pf1 !== null) {
289
            if ($pf1 !== null) {
277
                $pfgen = &$pf1->getDefaultGenerator();
290
                $pfgen = &$pf1->getDefaultGenerator();
278
                $packagexml1 = $pfgen->toPackageFile($where, PEAR_VALIDATE_PACKAGING,
291
                $packagexml1 = $pfgen->toPackageFile($where, PEAR_VALIDATE_PACKAGING, 'package.xml', true);
279
                    'package.xml', true);
-
 
280
                if (!$tar->addModify(array($packagexml1), '', $where)) {
292
                if (!$tar->addModify(array($packagexml1), '', $where)) {
281
                    return $packager->raiseError(
293
                    return $packager->raiseError(
282
                        'PEAR_Packagefile_v2::toTgz(): adding package.xml failed');
294
                        'PEAR_Packagefile_v2::toTgz(): adding package.xml failed');
283
                }
295
                }
284
            }
296
            }
-
 
297
 
285
            return $dest_package;
298
            return $dest_package;
286
        }
299
        }
287
    }
300
    }
Line 288... Line 301...
288
 
301
 
289
    function toPackageFile($where = null, $state = PEAR_VALIDATE_NORMAL, $name = 'package.xml')
302
    function toPackageFile($where = null, $state = PEAR_VALIDATE_NORMAL, $name = 'package.xml')
290
    {
303
    {
291
        if (!$this->_packagefile->validate($state)) {
304
        if (!$this->_packagefile->validate($state)) {
292
            return PEAR::raiseError('PEAR_Packagefile_v2::toPackageFile: invalid package.xml',
305
            return PEAR::raiseError('PEAR_Packagefile_v2::toPackageFile: invalid package.xml',
293
                null, null, null, $this->_packagefile->getValidationWarnings());
306
                null, null, null, $this->_packagefile->getValidationWarnings());
-
 
307
        }
294
        }
308
 
295
        if ($where === null) {
309
        if ($where === null) {
296
            if (!($where = System::mktemp(array('-d')))) {
310
            if (!($where = System::mktemp(array('-d')))) {
297
                return PEAR::raiseError('PEAR_Packagefile_v2::toPackageFile: mktemp failed');
311
                return PEAR::raiseError('PEAR_Packagefile_v2::toPackageFile: mktemp failed');
298
            }
312
            }
299
        } elseif (!@System::mkDir(array('-p', $where))) {
313
        } elseif (!@System::mkDir(array('-p', $where))) {
300
            return PEAR::raiseError('PEAR_Packagefile_v2::toPackageFile: "' . $where . '" could' .
314
            return PEAR::raiseError('PEAR_Packagefile_v2::toPackageFile: "' . $where . '" could' .
301
                ' not be created');
315
                ' not be created');
-
 
316
        }
302
        }
317
 
303
        $newpkgfile = $where . DIRECTORY_SEPARATOR . $name;
318
        $newpkgfile = $where . DIRECTORY_SEPARATOR . $name;
304
        $np = @fopen($newpkgfile, 'wb');
319
        $np = @fopen($newpkgfile, 'wb');
305
        if (!$np) {
320
        if (!$np) {
306
            return PEAR::raiseError('PEAR_Packagefile_v2::toPackageFile: unable to save ' .
321
            return PEAR::raiseError('PEAR_Packagefile_v2::toPackageFile: unable to save ' .
Line 327... Line 342...
327
        $this->_packagefile->setDate(date('Y-m-d'));
342
        $this->_packagefile->setDate(date('Y-m-d'));
328
        $this->_packagefile->setTime(date('H:i:s'));
343
        $this->_packagefile->setTime(date('H:i:s'));
329
        if (!$this->_packagefile->validate($state)) {
344
        if (!$this->_packagefile->validate($state)) {
330
            return false;
345
            return false;
331
        }
346
        }
-
 
347
 
332
        if (is_array($options)) {
348
        if (is_array($options)) {
333
            $this->options = array_merge($this->_defaultOptions, $options);
349
            $this->options = array_merge($this->_defaultOptions, $options);
334
        } else {
350
        } else {
335
            $this->options = $this->_defaultOptions;
351
            $this->options = $this->_defaultOptions;
336
        }
352
        }
-
 
353
 
337
        $arr = $this->_packagefile->getArray();
354
        $arr = $this->_packagefile->getArray();
338
        if (isset($arr['filelist'])) {
355
        if (isset($arr['filelist'])) {
339
            unset($arr['filelist']);
356
            unset($arr['filelist']);
340
        }
357
        }
-
 
358
 
341
        if (isset($arr['_lastversion'])) {
359
        if (isset($arr['_lastversion'])) {
342
            unset($arr['_lastversion']);
360
            unset($arr['_lastversion']);
343
        }
361
        }
-
 
362
 
-
 
363
        // Fix the notes a little bit
-
 
364
        if (isset($arr['notes'])) {
-
 
365
            // This trims out the indenting, needs fixing
-
 
366
            $arr['notes'] = "\n" . trim($arr['notes']) . "\n";
-
 
367
        }
-
 
368
 
-
 
369
        if (isset($arr['changelog']) && !empty($arr['changelog'])) {
-
 
370
            // Fix for inconsistency how the array is filled depending on the changelog release amount
-
 
371
            if (!isset($arr['changelog']['release'][0])) {
-
 
372
                $release = $arr['changelog']['release'];
-
 
373
                unset($arr['changelog']['release']);
-
 
374
 
-
 
375
                $arr['changelog']['release']    = array();
-
 
376
                $arr['changelog']['release'][0] = $release;
-
 
377
            }
-
 
378
 
-
 
379
            foreach (array_keys($arr['changelog']['release']) as $key) {
-
 
380
                $c =& $arr['changelog']['release'][$key];
-
 
381
                if (isset($c['notes'])) {
-
 
382
                    // This trims out the indenting, needs fixing
-
 
383
                    $c['notes'] = "\n" . trim($c['notes']) . "\n";
-
 
384
                }
-
 
385
            }
-
 
386
        }
-
 
387
 
344
        if ($state ^ PEAR_VALIDATE_PACKAGING && !isset($arr['bundle'])) {
388
        if ($state ^ PEAR_VALIDATE_PACKAGING && !isset($arr['bundle'])) {
345
            $use = $this->_recursiveXmlFilelist($arr['contents']['dir']['file']);
389
            $use = $this->_recursiveXmlFilelist($arr['contents']['dir']['file']);
346
            unset($arr['contents']['dir']['file']);
390
            unset($arr['contents']['dir']['file']);
347
            if (isset($use['dir'])) {
391
            if (isset($use['dir'])) {
348
                $arr['contents']['dir']['dir'] = $use['dir'];
392
                $arr['contents']['dir']['dir'] = $use['dir'];
Line 350... Line 394...
350
            if (isset($use['file'])) {
394
            if (isset($use['file'])) {
351
                $arr['contents']['dir']['file'] = $use['file'];
395
                $arr['contents']['dir']['file'] = $use['file'];
352
            }
396
            }
353
            $this->options['beautifyFilelist'] = true;
397
            $this->options['beautifyFilelist'] = true;
354
        }
398
        }
-
 
399
 
355
        $arr['attribs']['packagerversion'] = '1.5.1';
400
        $arr['attribs']['packagerversion'] = '1.10.1';
356
        if ($this->serialize($arr, $options)) {
401
        if ($this->serialize($arr, $options)) {
357
            return $this->_serializedData . "\n";
402
            return $this->_serializedData . "\n";
358
        }
403
        }
-
 
404
 
359
        return false;
405
        return false;
360
    }
406
    }
Line 361... Line 407...
361
 
407
 
Line 475... Line 521...
475
    */
521
    */
476
    function setOption($name, $value)
522
    function setOption($name, $value)
477
    {
523
    {
478
        $this->options[$name] = $value;
524
        $this->options[$name] = $value;
479
    }
525
    }
480
    
526
 
481
   /**
527
   /**
482
    * sets several options at once
528
    * sets several options at once
483
    *
529
    *
484
    * You can use this method if you do not want to set all options in the constructor
530
    * You can use this method if you do not want to set all options in the constructor
485
    *
531
    *
Line 507... Line 553...
507
            if (isset($options['overrideOptions']) && $options['overrideOptions'] == true) {
553
            if (isset($options['overrideOptions']) && $options['overrideOptions'] == true) {
508
                $this->options = array_merge($this->_defaultOptions, $options);
554
                $this->options = array_merge($this->_defaultOptions, $options);
509
            } else {
555
            } else {
510
                $this->options = array_merge($this->options, $options);
556
                $this->options = array_merge($this->options, $options);
511
            }
557
            }
512
        }
-
 
513
        else {
558
        } else {
514
            $optionsBak = null;
559
            $optionsBak = null;
515
        }
560
        }
516
        
561
 
517
        //  start depth is zero
562
        //  start depth is zero
518
        $this->_tagDepth = 0;
563
        $this->_tagDepth = 0;
519
 
-
 
520
        $this->_serializedData = '';
564
        $this->_serializedData = '';
521
        // serialize an array
565
        // serialize an array
522
        if (is_array($data)) {
566
        if (is_array($data)) {
523
            if (isset($this->options['rootName'])) {
-
 
524
                $tagName = $this->options['rootName'];
567
            $tagName = isset($this->options['rootName']) ? $this->options['rootName'] : 'array';
525
            } else {
-
 
526
                $tagName = 'array';
-
 
527
            }
-
 
528
 
-
 
529
            $this->_serializedData .= $this->_serializeArray($data, $tagName, $this->options['rootAttributes']);
568
            $this->_serializedData .= $this->_serializeArray($data, $tagName, $this->options['rootAttributes']);
530
        }
569
        }
531
        
570
 
532
        // add doctype declaration
571
        // add doctype declaration
533
        if ($this->options['addDoctype'] === true) {
572
        if ($this->options['addDoctype'] === true) {
534
            $this->_serializedData = PEAR_PackageFile_Generator_v2_XML_Util::getDoctypeDeclaration($tagName, $this->options['doctype'])
573
            $this->_serializedData = XML_Util::getDoctypeDeclaration($tagName, $this->options['doctype'])
535
                                   . $this->options['linebreak']
574
                                   . $this->options['linebreak']
536
                                   . $this->_serializedData;
575
                                   . $this->_serializedData;
537
        }
576
        }
Line 538... Line 577...
538
 
577
 
539
        //  build xml declaration
578
        //  build xml declaration
540
        if ($this->options['addDecl']) {
579
        if ($this->options['addDecl']) {
541
            $atts = array();
-
 
542
            if (isset($this->options['encoding']) ) {
580
            $atts = array();
543
                $encoding = $this->options['encoding'];
-
 
544
            } else {
-
 
545
                $encoding = null;
-
 
546
            }
581
            $encoding = isset($this->options['encoding']) ? $this->options['encoding'] : null;
547
            $this->_serializedData = PEAR_PackageFile_Generator_v2_XML_Util::getXMLDeclaration('1.0', $encoding)
582
            $this->_serializedData = XML_Util::getXMLDeclaration('1.0', $encoding)
548
                                   . $this->options['linebreak']
583
                                   . $this->options['linebreak']
549
                                   . $this->_serializedData;
584
                                   . $this->_serializedData;
550
        }
585
        }
551
        
586
 
552
        
587
 
553
		if ($optionsBak !== null) {
588
        if ($optionsBak !== null) {
554
			$this->options = $optionsBak;
589
            $this->options = $optionsBak;
555
		}
590
        }
556
		
591
 
557
        return  true;
592
        return  true;
Line 558... Line 593...
558
    }
593
    }
559
 
594
 
Line 563... Line 598...
563
    * @access public
598
    * @access public
564
    * @return string serialized XML
599
    * @return string serialized XML
565
    */
600
    */
566
    function getSerializedData()
601
    function getSerializedData()
567
    {
602
    {
568
        if ($this->_serializedData == null ) {
603
        if ($this->_serializedData === null) {
569
            return  $this->raiseError('No serialized data available. Use XML_Serializer::serialize() first.', XML_SERIALIZER_ERROR_NO_SERIALIZATION);
604
            return  $this->raiseError('No serialized data available. Use XML_Serializer::serialize() first.', XML_SERIALIZER_ERROR_NO_SERIALIZATION);
570
        }
605
        }
571
        return $this->_serializedData;
606
        return $this->_serializedData;
572
    }
607
    }
573
    
608
 
574
   /**
609
   /**
575
    * serialize any value
610
    * serialize any value
576
    *
611
    *
577
    * This method checks for the type of the value and calls the appropriate method
612
    * This method checks for the type of the value and calls the appropriate method
578
    *
613
    *
Line 596... Line 631...
596
                        );
631
                        );
597
            $xml = $this->_createXMLTag($tag);
632
            $xml = $this->_createXMLTag($tag);
598
        }
633
        }
599
        return $xml;
634
        return $xml;
600
    }
635
    }
601
    
636
 
602
   /**
637
   /**
603
    * serialize an array
638
    * serialize an array
604
    *
639
    *
605
    * @access   private
640
    * @access   private
606
    * @param    array   $array       array to serialize
641
    * @param    array   $array       array to serialize
607
    * @param    string  $tagName     name of the root tag
642
    * @param    string  $tagName     name of the root tag
608
    * @param    array   $attributes  attributes for the root tag
643
    * @param    array   $attributes  attributes for the root tag
609
    * @return   string  $string      serialized data
644
    * @return   string  $string      serialized data
610
    * @uses     PEAR_PackageFile_Generator_v2_XML_Util::isValidName() to check, whether key has to be substituted
645
    * @uses     XML_Util::isValidName() to check, whether key has to be substituted
611
    */
646
    */
612
    function _serializeArray(&$array, $tagName = null, $attributes = array())
647
    function _serializeArray(&$array, $tagName = null, $attributes = array())
613
    {
648
    {
614
        $_content = null;
649
        $_content = null;
615
        
650
 
616
        /**
651
        /**
617
         * check for special attributes
652
         * check for special attributes
618
         */
653
         */
619
        if ($this->options['attributesArray'] !== null) {
654
        if ($this->options['attributesArray'] !== null) {
620
            if (isset($array[$this->options['attributesArray']])) {
655
            if (isset($array[$this->options['attributesArray']])) {
Line 674... Line 709...
674
                            unset($this->_curdir);
709
                            unset($this->_curdir);
675
                        } else {
710
                        } else {
676
                            $this->_curdir = $savedir;
711
                            $this->_curdir = $savedir;
677
                        }
712
                        }
678
                    }
713
                    }
679
                    
714
 
680
                    $string .= $this->options['linebreak'];
715
                    $string .= $this->options['linebreak'];
681
        			//	do indentation
716
                    // do indentation
682
                    if ($this->options['indent']!==null && $this->_tagDepth>0) {
717
                    if ($this->options['indent'] !== null && $this->_tagDepth > 0) {
683
                        $string .= str_repeat($this->options['indent'], $this->_tagDepth);
718
                        $string .= str_repeat($this->options['indent'], $this->_tagDepth);
684
                    }
719
                    }
685
                }
720
                }
686
                return rtrim($string);
721
                return rtrim($string);
687
            }
722
            }
688
        }
723
        }
689
        
724
 
690
		if ($this->options['scalarAsAttributes'] === true) {
725
        if ($this->options['scalarAsAttributes'] === true) {
691
	        foreach ($array as $key => $value) {
726
            foreach ($array as $key => $value) {
692
				if (is_scalar($value) && (PEAR_PackageFile_Generator_v2_XML_Util::isValidName($key) === true)) {
727
                if (is_scalar($value) && (XML_Util::isValidName($key) === true)) {
693
					unset($array[$key]);
728
                    unset($array[$key]);
694
					$attributes[$this->options['prependAttributes'].$key] = $value;
729
                    $attributes[$this->options['prependAttributes'].$key] = $value;
695
				}
730
                }
696
			}
731
            }
697
		}
732
        }
Line 698... Line 733...
698
 
733
 
699
        // check for empty array => create empty tag
734
        // check for empty array => create empty tag
700
        if (empty($array)) {
735
        if (empty($array)) {
701
            $tag = array(
736
            $tag = array(
Line 706... Line 741...
706
 
741
 
707
        } else {
742
        } else {
708
            $this->_tagDepth++;
743
            $this->_tagDepth++;
709
            $tmp = $this->options['linebreak'];
744
            $tmp = $this->options['linebreak'];
710
            foreach ($array as $key => $value) {
745
            foreach ($array as $key => $value) {
711
    			//	do indentation
746
                // do indentation
712
                if ($this->options['indent']!==null && $this->_tagDepth>0) {
747
                if ($this->options['indent'] !== null && $this->_tagDepth > 0) {
713
                    $tmp .= str_repeat($this->options['indent'], $this->_tagDepth);
748
                    $tmp .= str_repeat($this->options['indent'], $this->_tagDepth);
714
                }
749
                }
715
    
750
 
716
    			//	copy key
751
                // copy key
717
    			$origKey	=	$key;
752
                $origKey = $key;
718
    			//	key cannot be used as tagname => use default tag
753
                // key cannot be used as tagname => use default tag
719
                $valid = PEAR_PackageFile_Generator_v2_XML_Util::isValidName($key);
754
                $valid = XML_Util::isValidName($key);
720
    	        if (PEAR::isError($valid)) {
755
                if (PEAR::isError($valid)) {
721
    	            if ($this->options['classAsTagName'] && is_object($value)) {
756
                    if ($this->options['classAsTagName'] && is_object($value)) {
722
    	                $key = get_class($value);
757
                        $key = get_class($value);
723
    	            } else {
758
                    } else {
724
            	        $key = $this->options['defaultTagName'];
759
                        $key = $this->options['defaultTagName'];
725
    	            }
760
                    }
726
           	 	}
761
                }
727
                $atts = array();
762
                $atts = array();
728
                if ($this->options['typeHints'] === true) {
763
                if ($this->options['typeHints'] === true) {
729
                    $atts[$this->options['typeAttribute']] = gettype($value);
764
                    $atts[$this->options['typeAttribute']] = gettype($value);
730
    				if ($key !== $origKey) {
765
                    if ($key !== $origKey) {
731
    					$atts[$this->options['keyAttribute']] = (string)$origKey;
766
                        $atts[$this->options['keyAttribute']] = (string)$origKey;
732
    				}
767
                    }
733
    
768
 
734
                }
769
                }
735
                if ($this->options['beautifyFilelist'] && $key == 'dir') {
770
                if ($this->options['beautifyFilelist'] && $key == 'dir') {
736
                    if (!isset($this->_curdir)) {
771
                    if (!isset($this->_curdir)) {
737
                        $this->_curdir = '';
772
                        $this->_curdir = '';
Line 764... Line 799...
764
                        }
799
                        }
765
                    }
800
                    }
766
                }
801
                }
767
                $tmp .= $this->options['linebreak'];
802
                $tmp .= $this->options['linebreak'];
768
            }
803
            }
769
            
804
 
770
            $this->_tagDepth--;
805
            $this->_tagDepth--;
771
            if ($this->options['indent']!==null && $this->_tagDepth>0) {
806
            if ($this->options['indent']!==null && $this->_tagDepth>0) {
772
                $tmp .= str_repeat($this->options['indent'], $this->_tagDepth);
807
                $tmp .= str_repeat($this->options['indent'], $this->_tagDepth);
773
            }
808
            }
774
    
809
 
775
    		if (trim($tmp) === '') {
810
            if (trim($tmp) === '') {
776
    			$tmp = null;
811
                $tmp = null;
777
    		}
812
            }
778
    		
813
 
779
            $tag = array(
814
            $tag = array(
780
                            'qname'      => $tagName,
815
                'qname'      => $tagName,
781
                            'content'    => $tmp,
816
                'content'    => $tmp,
782
                            'attributes' => $attributes
817
                'attributes' => $attributes
783
                        );
818
            );
784
        }
819
        }
785
        if ($this->options['typeHints'] === true) {
820
        if ($this->options['typeHints'] === true) {
786
            if (!isset($tag['attributes'][$this->options['typeAttribute']])) {
821
            if (!isset($tag['attributes'][$this->options['typeAttribute']])) {
787
                $tag['attributes'][$this->options['typeAttribute']] = 'array';
822
                $tag['attributes'][$this->options['typeAttribute']] = 'array';
788
            }
823
            }
789
        }
824
        }
Line 790... Line 825...
790
 
825
 
791
        $string = $this->_createXMLTag($tag, false);
826
        $string = $this->_createXMLTag($tag, false);
792
        return $string;
827
        return $string;
793
    }
828
    }
794
  
829
 
795
   /**
830
   /**
796
    * create a tag from an array
831
    * create a tag from an array
797
    * this method awaits an array in the following format
832
    * this method awaits an array in the following format
798
    * array(
833
    * array(
Line 806... Line 841...
806
    * @access   private
841
    * @access   private
807
    * @param    array   $tag tag definition
842
    * @param    array   $tag tag definition
808
    * @param    boolean $replaceEntities whether to replace XML entities in content or not
843
    * @param    boolean $replaceEntities whether to replace XML entities in content or not
809
    * @return   string  $string XML tag
844
    * @return   string  $string XML tag
810
    */
845
    */
811
    function _createXMLTag( $tag, $replaceEntities = true )
846
    function _createXMLTag($tag, $replaceEntities = true)
812
    {
847
    {
813
        if ($this->options['indentAttributes'] !== false) {
848
        if ($this->options['indentAttributes'] !== false) {
814
            $multiline = true;
849
            $multiline = true;
815
            $indent    = str_repeat($this->options['indent'], $this->_tagDepth);
850
            $indent    = str_repeat($this->options['indent'], $this->_tagDepth);
Line 819... Line 854...
819
 
854
 
820
            } else {
855
            } else {
821
                $indent .= $this->options['indentAttributes'];
856
                $indent .= $this->options['indentAttributes'];
822
            }
857
            }
823
        } else {
858
        } else {
824
            $multiline = false;
-
 
825
            $indent    = false;
859
            $indent = $multiline = false;
826
        }
860
        }
827
    
861
 
828
        if (is_array($tag['content'])) {
862
        if (is_array($tag['content'])) {
829
            if (empty($tag['content'])) {
863
            if (empty($tag['content'])) {
830
                $tag['content'] =   '';
864
                $tag['content'] = '';
831
            }
865
            }
832
        } elseif(is_scalar($tag['content']) && (string)$tag['content'] == '') {
866
        } elseif(is_scalar($tag['content']) && (string)$tag['content'] == '') {
833
            $tag['content'] =   '';
867
            $tag['content'] = '';
834
        }
868
        }
835
    
869
 
836
        if (is_scalar($tag['content']) || is_null($tag['content'])) {
870
        if (is_scalar($tag['content']) || is_null($tag['content'])) {
837
            if ($this->options['encoding'] == 'UTF-8' &&
-
 
838
                  version_compare(phpversion(), '5.0.0', 'lt')) {
-
 
839
                $encoding = PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_UTF8_XML;
-
 
840
            } else {
871
            if ($replaceEntities === true) {
841
                $encoding = PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML;
872
                $replaceEntities = XML_UTIL_ENTITIES_XML;
-
 
873
            }
842
            }
874
 
843
            $tag = PEAR_PackageFile_Generator_v2_XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options['linebreak'], $encoding);
875
            $tag = XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options['linebreak']);
844
        } elseif (is_array($tag['content'])) {
876
        } elseif (is_array($tag['content'])) {
845
            $tag    =   $this->_serializeArray($tag['content'], $tag['qname'], $tag['attributes']);
877
            $tag = $this->_serializeArray($tag['content'], $tag['qname'], $tag['attributes']);
846
        } elseif (is_object($tag['content'])) {
878
        } elseif (is_object($tag['content'])) {
847
            $tag    =   $this->_serializeObject($tag['content'], $tag['qname'], $tag['attributes']);
879
            $tag = $this->_serializeObject($tag['content'], $tag['qname'], $tag['attributes']);
848
        } elseif (is_resource($tag['content'])) {
880
        } elseif (is_resource($tag['content'])) {
849
            settype($tag['content'], 'string');
-
 
850
            $tag    =   PEAR_PackageFile_Generator_v2_XML_Util::createTagFromArray($tag, $replaceEntities);
-
 
851
        }
-
 
852
        return  $tag;
-
 
853
    }
-
 
854
}
-
 
855
 
-
 
856
// well, it's one way to do things without extra deps ...
-
 
857
/* vim: set expandtab tabstop=4 shiftwidth=4: */
-
 
858
// +----------------------------------------------------------------------+
-
 
859
// | PHP Version 4                                                        |
-
 
860
// +----------------------------------------------------------------------+
-
 
861
// | Copyright (c) 1997-2002 The PHP Group                                |
-
 
862
// +----------------------------------------------------------------------+
-
 
863
// | This source file is subject to version 2.0 of the PHP license,       |
-
 
864
// | that is bundled with this package in the file LICENSE, and is        |
-
 
865
// | available at through the world-wide-web at                           |
-
 
866
// | http://www.php.net/license/2_02.txt.                                 |
-
 
867
// | If you did not receive a copy of the PHP license and are unable to   |
-
 
868
// | obtain it through the world-wide-web, please send a note to          |
-
 
869
// | license@php.net so we can mail you a copy immediately.               |
-
 
870
// +----------------------------------------------------------------------+
-
 
871
// | Authors: Stephan Schmidt <schst@php-tools.net>                       |
-
 
872
// +----------------------------------------------------------------------+
-
 
873
//
-
 
874
//    $Id: v2.php,v 1.35 2006/03/25 21:09:08 cellog Exp $
-
 
875
 
-
 
876
/**
-
 
877
 * error code for invalid chars in XML name
-
 
878
 */
-
 
879
define("PEAR_PackageFile_Generator_v2_XML_Util_ERROR_INVALID_CHARS", 51);
-
 
880
 
-
 
881
/**
-
 
882
 * error code for invalid chars in XML name
-
 
883
 */
-
 
884
define("PEAR_PackageFile_Generator_v2_XML_Util_ERROR_INVALID_START", 52);
-
 
885
 
-
 
886
/**
-
 
887
 * error code for non-scalar tag content
-
 
888
 */
-
 
889
define("PEAR_PackageFile_Generator_v2_XML_Util_ERROR_NON_SCALAR_CONTENT", 60);
-
 
890
    
-
 
891
/**
-
 
892
 * error code for missing tag name
-
 
893
 */
-
 
894
define("PEAR_PackageFile_Generator_v2_XML_Util_ERROR_NO_TAG_NAME", 61);
-
 
895
    
-
 
896
/**
-
 
897
 * replace XML entities
-
 
898
 */
-
 
899
define("PEAR_PackageFile_Generator_v2_XML_Util_REPLACE_ENTITIES", 1);
-
 
900
 
-
 
901
/**
-
 
902
 * embedd content in a CData Section
-
 
903
 */
-
 
904
define("PEAR_PackageFile_Generator_v2_XML_Util_CDATA_SECTION", 2);
-
 
905
 
-
 
906
/**
-
 
907
 * do not replace entitites
-
 
908
 */
-
 
909
define("PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_NONE", 0);
-
 
910
 
-
 
911
/**
-
 
912
 * replace all XML entitites
-
 
913
 * This setting will replace <, >, ", ' and &
-
 
914
 */
-
 
915
define("PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML", 1);
-
 
916
 
-
 
917
/**
-
 
918
 * replace only required XML entitites
-
 
919
 * This setting will replace <, " and &
-
 
920
 */
-
 
921
define("PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML_REQUIRED", 2);
-
 
922
 
-
 
923
/**
-
 
924
 * replace HTML entitites
-
 
925
 * @link    http://www.php.net/htmlentities
-
 
926
 */
-
 
927
define("PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_HTML", 3);
-
 
928
 
-
 
929
/**
-
 
930
 * replace all XML entitites, and encode from ISO-8859-1 to UTF-8
-
 
931
 * This setting will replace <, >, ", ' and &
-
 
932
 */
-
 
933
define("PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_UTF8_XML", 4);
-
 
934
 
-
 
935
/**
-
 
936
 * utility class for working with XML documents
-
 
937
 * 
-
 
938
 * customized version of XML_Util 0.6.0
-
 
939
 *
-
 
940
 * @category XML
-
 
941
 * @package  PEAR
-
 
942
 * @version  0.6.0
-
 
943
 * @author   Stephan Schmidt <schst@php.net>
-
 
944
 * @author   Gregory Beaver <cellog@php.net>
-
 
945
 */
-
 
946
class PEAR_PackageFile_Generator_v2_XML_Util {
-
 
947
 
-
 
948
   /**
-
 
949
    * return API version
-
 
950
    *
-
 
951
    * @access   public
-
 
952
    * @static
-
 
953
    * @return   string  $version API version
-
 
954
    */
-
 
955
    function apiVersion()
-
 
956
    {
-
 
957
        return "0.6";
-
 
958
    }
-
 
959
 
-
 
960
   /**
-
 
961
    * replace XML entities
-
 
962
    *
-
 
963
    * With the optional second parameter, you may select, which
-
 
964
    * entities should be replaced.
-
 
965
    *
-
 
966
    * <code>
-
 
967
    * require_once 'XML/Util.php';
-
 
968
    * 
-
 
969
    * // replace XML entites:
-
 
970
    * $string = PEAR_PackageFile_Generator_v2_XML_Util::replaceEntities("This string contains < & >.");
-
 
971
    * </code>
-
 
972
    *
-
 
973
    * @access   public
-
 
974
    * @static
-
 
975
    * @param    string  string where XML special chars should be replaced
-
 
976
    * @param    integer setting for entities in attribute values (one of PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML, PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML_REQUIRED, PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_HTML)
-
 
977
    * @return   string  string with replaced chars
-
 
978
    */
-
 
979
    function replaceEntities($string, $replaceEntities = PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML)
-
 
980
    {
-
 
981
        switch ($replaceEntities) {
-
 
982
            case PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_UTF8_XML:
-
 
983
                return strtr(utf8_encode($string),array(
-
 
984
                                          '&'  => '&amp;',
-
 
985
                                          '>'  => '&gt;',
-
 
986
                                          '<'  => '&lt;',
-
 
987
                                          '"'  => '&quot;',
-
 
988
                                          '\'' => '&apos;' ));
-
 
989
                break;
-
 
990
            case PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML:
-
 
991
                return strtr($string,array(
-
 
992
                                          '&'  => '&amp;',
-
 
993
                                          '>'  => '&gt;',
-
 
994
                                          '<'  => '&lt;',
-
 
995
                                          '"'  => '&quot;',
-
 
996
                                          '\'' => '&apos;' ));
-
 
997
                break;
-
 
998
            case PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML_REQUIRED:
-
 
999
                return strtr($string,array(
-
 
1000
                                          '&'  => '&amp;',
-
 
1001
                                          '<'  => '&lt;',
-
 
1002
                                          '"'  => '&quot;' ));
-
 
1003
                break;
-
 
1004
            case PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_HTML:
-
 
1005
                return htmlspecialchars($string);
-
 
1006
                break;
-
 
1007
        }
-
 
1008
        return $string;
-
 
1009
    }
-
 
1010
 
-
 
1011
   /**
-
 
1012
    * build an xml declaration
-
 
1013
    *
-
 
1014
    * <code>
-
 
1015
    * require_once 'XML/Util.php';
-
 
1016
    * 
-
 
1017
    * // get an XML declaration:
-
 
1018
    * $xmlDecl = PEAR_PackageFile_Generator_v2_XML_Util::getXMLDeclaration("1.0", "UTF-8", true);
-
 
1019
    * </code>
-
 
1020
    *
-
 
1021
    * @access   public
-
 
1022
    * @static
-
 
1023
    * @param    string  $version     xml version
-
 
1024
    * @param    string  $encoding    character encoding
-
 
1025
    * @param    boolean $standAlone  document is standalone (or not)
-
 
1026
    * @return   string  $decl xml declaration
-
 
1027
    * @uses     PEAR_PackageFile_Generator_v2_XML_Util::attributesToString() to serialize the attributes of the XML declaration
-
 
1028
    */
-
 
1029
    function getXMLDeclaration($version = "1.0", $encoding = null, $standalone = null)
-
 
1030
    {
-
 
1031
        $attributes = array(
-
 
1032
                            "version" => $version,
-
 
1033
                           );
-
 
1034
        // add encoding
-
 
1035
        if ($encoding !== null) {
-
 
1036
            $attributes["encoding"] = $encoding;
-
 
1037
        }
-
 
1038
        // add standalone, if specified
-
 
1039
        if ($standalone !== null) {
-
 
1040
            $attributes["standalone"] = $standalone ? "yes" : "no";
-
 
1041
        }
-
 
1042
        
-
 
1043
        return sprintf("<?xml%s?>", PEAR_PackageFile_Generator_v2_XML_Util::attributesToString($attributes, false));
-
 
1044
    }
-
 
1045
 
-
 
1046
   /**
-
 
1047
    * build a document type declaration
-
 
1048
    *
-
 
1049
    * <code>
-
 
1050
    * require_once 'XML/Util.php';
-
 
1051
    * 
-
 
1052
    * // get a doctype declaration:
-
 
1053
    * $xmlDecl = PEAR_PackageFile_Generator_v2_XML_Util::getDocTypeDeclaration("rootTag","myDocType.dtd");
-
 
1054
    * </code>
-
 
1055
    *
-
 
1056
    * @access   public
-
 
1057
    * @static
-
 
1058
    * @param    string  $root         name of the root tag
-
 
1059
    * @param    string  $uri          uri of the doctype definition (or array with uri and public id)
-
 
1060
    * @param    string  $internalDtd  internal dtd entries   
-
 
1061
    * @return   string  $decl         doctype declaration
-
 
1062
    * @since    0.2
-
 
1063
    */
-
 
1064
    function getDocTypeDeclaration($root, $uri = null, $internalDtd = null)
-
 
1065
    {
-
 
1066
        if (is_array($uri)) {
-
 
1067
            $ref = sprintf( ' PUBLIC "%s" "%s"', $uri["id"], $uri["uri"] );
-
 
1068
        } elseif (!empty($uri)) {
-
 
1069
            $ref = sprintf( ' SYSTEM "%s"', $uri );
-
 
1070
        } else {
-
 
1071
            $ref = "";
-
 
1072
        }
-
 
1073
 
-
 
1074
        if (empty($internalDtd)) {
-
 
1075
            return sprintf("<!DOCTYPE %s%s>", $root, $ref);
-
 
1076
        } else {
-
 
1077
            return sprintf("<!DOCTYPE %s%s [\n%s\n]>", $root, $ref, $internalDtd);
-
 
1078
        }
-
 
1079
    }
-
 
1080
 
-
 
1081
   /**
-
 
1082
    * create string representation of an attribute list
-
 
1083
    *
-
 
1084
    * <code>
-
 
1085
    * require_once 'XML/Util.php';
-
 
1086
    * 
-
 
1087
    * // build an attribute string
-
 
1088
    * $att = array(
-
 
1089
    *              "foo"   =>  "bar",
-
 
1090
    *              "argh"  =>  "tomato"
-
 
1091
    *            );
-
 
1092
    *
-
 
1093
    * $attList = PEAR_PackageFile_Generator_v2_XML_Util::attributesToString($att);    
-
 
1094
    * </code>
-
 
1095
    *
-
 
1096
    * @access   public
-
 
1097
    * @static
-
 
1098
    * @param    array         $attributes        attribute array
-
 
1099
    * @param    boolean|array $sort              sort attribute list alphabetically, may also be an assoc array containing the keys 'sort', 'multiline', 'indent', 'linebreak' and 'entities'
-
 
1100
    * @param    boolean       $multiline         use linebreaks, if more than one attribute is given
-
 
1101
    * @param    string        $indent            string used for indentation of multiline attributes
-
 
1102
    * @param    string        $linebreak         string used for linebreaks of multiline attributes
881
            settype($tag['content'], 'string');
1103
    * @param    integer       $entities          setting for entities in attribute values (one of PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_NONE, PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML, PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML_REQUIRED, PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_HTML)
-
 
1104
    * @return   string                           string representation of the attributes
-
 
1105
    * @uses     PEAR_PackageFile_Generator_v2_XML_Util::replaceEntities() to replace XML entities in attribute values
-
 
1106
    * @todo     allow sort also to be an options array
-
 
1107
    */
-
 
1108
    function attributesToString($attributes, $sort = true, $multiline = false, $indent = '    ', $linebreak = "\n", $entities = PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML)
-
 
1109
    {
-
 
1110
        /**
-
 
1111
         * second parameter may be an array
-
 
1112
         */
-
 
1113
        if (is_array($sort)) {
-
 
1114
            if (isset($sort['multiline'])) {
-
 
1115
                $multiline = $sort['multiline'];
-
 
1116
            }
-
 
1117
            if (isset($sort['indent'])) {
-
 
1118
                $indent = $sort['indent'];
-
 
1119
            }
-
 
1120
            if (isset($sort['linebreak'])) {
-
 
1121
                $multiline = $sort['linebreak'];
-
 
1122
            }
-
 
1123
            if (isset($sort['entities'])) {
-
 
1124
                $entities = $sort['entities'];
-
 
1125
            }
-
 
1126
            if (isset($sort['sort'])) {
-
 
1127
                $sort = $sort['sort'];
-
 
1128
            } else {
-
 
1129
                $sort = true;
-
 
1130
            }
-
 
1131
        }
-
 
1132
        $string = '';
-
 
1133
        if (is_array($attributes) && !empty($attributes)) {
-
 
1134
            if ($sort) {
-
 
1135
                ksort($attributes);
-
 
1136
            }
-
 
1137
            if( !$multiline || count($attributes) == 1) {
-
 
1138
                foreach ($attributes as $key => $value) {
-
 
1139
                    if ($entities != PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_NONE) {
-
 
1140
                        $value = PEAR_PackageFile_Generator_v2_XML_Util::replaceEntities($value, $entities);
-
 
1141
                    }
-
 
1142
                    $string .= ' '.$key.'="'.$value.'"';
-
 
1143
                }
-
 
1144
            } else {
-
 
1145
                $first = true;
-
 
1146
                foreach ($attributes as $key => $value) {
-
 
1147
                    if ($entities != PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_NONE) {
-
 
1148
                        $value = PEAR_PackageFile_Generator_v2_XML_Util::replaceEntities($value, $entities);
-
 
1149
                    }
-
 
1150
                    if ($first) {
-
 
1151
                        $string .= " ".$key.'="'.$value.'"';
-
 
1152
                        $first = false;
-
 
1153
                    } else {
-
 
1154
                        $string .= $linebreak.$indent.$key.'="'.$value.'"';
-
 
1155
                    }
-
 
1156
                }
-
 
1157
            }
-
 
1158
        }
-
 
1159
        return $string;
-
 
1160
    }
-
 
1161
 
-
 
1162
   /**
-
 
1163
    * create a tag
-
 
1164
    *
-
 
1165
    * This method will call PEAR_PackageFile_Generator_v2_XML_Util::createTagFromArray(), which
-
 
1166
    * is more flexible.
-
 
1167
    *
-
 
1168
    * <code>
-
 
1169
    * require_once 'XML/Util.php';
-
 
1170
    * 
-
 
1171
    * // create an XML tag:
-
 
1172
    * $tag = PEAR_PackageFile_Generator_v2_XML_Util::createTag("myNs:myTag", array("foo" => "bar"), "This is inside the tag", "http://www.w3c.org/myNs#");
-
 
1173
    * </code>
-
 
1174
    *
-
 
1175
    * @access   public
-
 
1176
    * @static
-
 
1177
    * @param    string  $qname             qualified tagname (including namespace)
-
 
1178
    * @param    array   $attributes        array containg attributes
-
 
1179
    * @param    mixed   $content
-
 
1180
    * @param    string  $namespaceUri      URI of the namespace
-
 
1181
    * @param    integer $replaceEntities   whether to replace XML special chars in content, embedd it in a CData section or none of both
-
 
1182
    * @param    boolean $multiline         whether to create a multiline tag where each attribute gets written to a single line
-
 
1183
    * @param    string  $indent            string used to indent attributes (_auto indents attributes so they start at the same column)
-
 
1184
    * @param    string  $linebreak         string used for linebreaks
-
 
1185
    * @param    string  $encoding          encoding that should be used to translate content
-
 
1186
    * @return   string  $string            XML tag
-
 
1187
    * @see      PEAR_PackageFile_Generator_v2_XML_Util::createTagFromArray()
-
 
1188
    * @uses     PEAR_PackageFile_Generator_v2_XML_Util::createTagFromArray() to create the tag
-
 
1189
    */
-
 
1190
    function createTag($qname, $attributes = array(), $content = null, $namespaceUri = null, $replaceEntities = PEAR_PackageFile_Generator_v2_XML_Util_REPLACE_ENTITIES, $multiline = false, $indent = "_auto", $linebreak = "\n", $encoding = PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML)
-
 
1191
    {
-
 
1192
        $tag = array(
-
 
1193
                     "qname"      => $qname,
-
 
1194
                     "attributes" => $attributes
-
 
1195
                    );
-
 
1196
 
-
 
1197
        // add tag content
-
 
1198
        if ($content !== null) {
-
 
1199
            $tag["content"] = $content;
-
 
1200
        }
-
 
1201
        
-
 
1202
        // add namespace Uri
-
 
1203
        if ($namespaceUri !== null) {
-
 
1204
            $tag["namespaceUri"] = $namespaceUri;
-
 
1205
        }
-
 
1206
 
-
 
1207
        return PEAR_PackageFile_Generator_v2_XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $linebreak, $encoding);
-
 
1208
    }
-
 
1209
 
-
 
1210
   /**
-
 
1211
    * create a tag from an array
-
 
1212
    * this method awaits an array in the following format
-
 
1213
    * <pre>
-
 
1214
    * array(
-
 
1215
    *  "qname"        => $qname         // qualified name of the tag
-
 
1216
    *  "namespace"    => $namespace     // namespace prefix (optional, if qname is specified or no namespace)
-
 
1217
    *  "localpart"    => $localpart,    // local part of the tagname (optional, if qname is specified)
-
 
1218
    *  "attributes"   => array(),       // array containing all attributes (optional)
-
 
1219
    *  "content"      => $content,      // tag content (optional)
-
 
1220
    *  "namespaceUri" => $namespaceUri  // namespaceUri for the given namespace (optional)
-
 
1221
    *   )
-
 
1222
    * </pre>
-
 
1223
    *
-
 
1224
    * <code>
-
 
1225
    * require_once 'XML/Util.php';
-
 
1226
    * 
-
 
1227
    * $tag = array(
-
 
1228
    *           "qname"        => "foo:bar",
-
 
1229
    *           "namespaceUri" => "http://foo.com",
-
 
1230
    *           "attributes"   => array( "key" => "value", "argh" => "fruit&vegetable" ),
-
 
1231
    *           "content"      => "I'm inside the tag",
-
 
1232
    *            );
-
 
1233
    * // creating a tag with qualified name and namespaceUri
-
 
1234
    * $string = PEAR_PackageFile_Generator_v2_XML_Util::createTagFromArray($tag);
-
 
1235
    * </code>
-
 
1236
    *
-
 
1237
    * @access   public
-
 
1238
    * @static
-
 
1239
    * @param    array   $tag               tag definition
-
 
1240
    * @param    integer $replaceEntities   whether to replace XML special chars in content, embedd it in a CData section or none of both
-
 
1241
    * @param    boolean $multiline         whether to create a multiline tag where each attribute gets written to a single line
-
 
1242
    * @param    string  $indent            string used to indent attributes (_auto indents attributes so they start at the same column)
-
 
1243
    * @param    string  $linebreak         string used for linebreaks
-
 
1244
    * @return   string  $string            XML tag
-
 
1245
    * @see      PEAR_PackageFile_Generator_v2_XML_Util::createTag()
-
 
1246
    * @uses     PEAR_PackageFile_Generator_v2_XML_Util::attributesToString() to serialize the attributes of the tag
-
 
1247
    * @uses     PEAR_PackageFile_Generator_v2_XML_Util::splitQualifiedName() to get local part and namespace of a qualified name
-
 
1248
    */
-
 
1249
    function createTagFromArray($tag, $replaceEntities = PEAR_PackageFile_Generator_v2_XML_Util_REPLACE_ENTITIES, $multiline = false, $indent = "_auto", $linebreak = "\n", $encoding = PEAR_PackageFile_Generator_v2_XML_Util_ENTITIES_XML)
-
 
1250
    {
-
 
1251
        if (isset($tag["content"]) && !is_scalar($tag["content"])) {
-
 
1252
            return PEAR_PackageFile_Generator_v2_XML_Util::raiseError( "Supplied non-scalar value as tag content", PEAR_PackageFile_Generator_v2_XML_Util_ERROR_NON_SCALAR_CONTENT );
882
            $tag = XML_Util::createTagFromArray($tag, $replaceEntities);
1253
        }
-
 
1254
 
-
 
1255
        if (!isset($tag['qname']) && !isset($tag['localPart'])) {
-
 
1256
            return PEAR_PackageFile_Generator_v2_XML_Util::raiseError( 'You must either supply a qualified name (qname) or local tag name (localPart).', PEAR_PackageFile_Generator_v2_XML_Util_ERROR_NO_TAG_NAME );
-
 
1257
        }
-
 
1258
 
-
 
1259
        // if no attributes hav been set, use empty attributes
-
 
1260
        if (!isset($tag["attributes"]) || !is_array($tag["attributes"])) {
-
 
1261
            $tag["attributes"] = array();
-
 
1262
        }
-
 
1263
        
-
 
1264
        // qualified name is not given
-
 
1265
        if (!isset($tag["qname"])) {
-
 
1266
            // check for namespace
-
 
1267
            if (isset($tag["namespace"]) && !empty($tag["namespace"])) {
-
 
1268
                $tag["qname"] = $tag["namespace"].":".$tag["localPart"];
-
 
1269
            } else {
-
 
1270
                $tag["qname"] = $tag["localPart"];
-
 
1271
            }
-
 
1272
        // namespace URI is set, but no namespace
-
 
1273
        } elseif (isset($tag["namespaceUri"]) && !isset($tag["namespace"])) {
-
 
1274
            $parts = PEAR_PackageFile_Generator_v2_XML_Util::splitQualifiedName($tag["qname"]);
-
 
1275
            $tag["localPart"] = $parts["localPart"];
-
 
1276
            if (isset($parts["namespace"])) {
-
 
1277
                $tag["namespace"] = $parts["namespace"];
-
 
1278
            }
-
 
1279
        }
-
 
1280
 
-
 
1281
        if (isset($tag["namespaceUri"]) && !empty($tag["namespaceUri"])) {
-
 
1282
            // is a namespace given
-
 
1283
            if (isset($tag["namespace"]) && !empty($tag["namespace"])) {
-
 
1284
                $tag["attributes"]["xmlns:".$tag["namespace"]] = $tag["namespaceUri"];
-
 
1285
            } else {
-
 
1286
                // define this Uri as the default namespace
-
 
1287
                $tag["attributes"]["xmlns"] = $tag["namespaceUri"];
-
 
1288
            }
-
 
1289
        }
-
 
1290
 
-
 
1291
        // check for multiline attributes
-
 
1292
        if ($multiline === true) {
-
 
1293
            if ($indent === "_auto") {
-
 
1294
                $indent = str_repeat(" ", (strlen($tag["qname"])+2));
-
 
1295
            }
-
 
1296
        }
-
 
1297
        
-
 
1298
        // create attribute list
-
 
1299
        $attList    =   PEAR_PackageFile_Generator_v2_XML_Util::attributesToString($tag["attributes"], true, $multiline, $indent, $linebreak );
-
 
1300
        if (!isset($tag["content"]) || (string)$tag["content"] == '') {
-
 
1301
            $tag    =   sprintf("<%s%s />", $tag["qname"], $attList);
-
 
1302
        } else {
-
 
1303
            if ($replaceEntities == PEAR_PackageFile_Generator_v2_XML_Util_REPLACE_ENTITIES) {
-
 
1304
                $tag["content"] = PEAR_PackageFile_Generator_v2_XML_Util::replaceEntities($tag["content"], $encoding);
-
 
1305
            } elseif ($replaceEntities == PEAR_PackageFile_Generator_v2_XML_Util_CDATA_SECTION) {
-
 
1306
                $tag["content"] = PEAR_PackageFile_Generator_v2_XML_Util::createCDataSection($tag["content"]);
-
 
1307
            }
-
 
1308
            $tag    =   sprintf("<%s%s>%s</%s>", $tag["qname"], $attList, $tag["content"], $tag["qname"] );
-
 
1309
        }        
883
        }
1310
        return  $tag;
884
        return  $tag;
1311
    }
-
 
1312
 
-
 
1313
   /**
-
 
1314
    * create a start element
-
 
1315
    *
-
 
1316
    * <code>
-
 
1317
    * require_once 'XML/Util.php';
-
 
1318
    * 
-
 
1319
    * // create an XML start element:
-
 
1320
    * $tag = PEAR_PackageFile_Generator_v2_XML_Util::createStartElement("myNs:myTag", array("foo" => "bar") ,"http://www.w3c.org/myNs#");
-
 
1321
    * </code>
-
 
1322
    *
-
 
1323
    * @access   public
-
 
1324
    * @static
-
 
1325
    * @param    string  $qname             qualified tagname (including namespace)
-
 
1326
    * @param    array   $attributes        array containg attributes
-
 
1327
    * @param    string  $namespaceUri      URI of the namespace
-
 
1328
    * @param    boolean $multiline         whether to create a multiline tag where each attribute gets written to a single line
-
 
1329
    * @param    string  $indent            string used to indent attributes (_auto indents attributes so they start at the same column)
-
 
1330
    * @param    string  $linebreak         string used for linebreaks
-
 
1331
    * @return   string  $string            XML start element
-
 
1332
    * @see      PEAR_PackageFile_Generator_v2_XML_Util::createEndElement(), PEAR_PackageFile_Generator_v2_XML_Util::createTag()
-
 
1333
    */
-
 
1334
    function createStartElement($qname, $attributes = array(), $namespaceUri = null, $multiline = false, $indent = '_auto', $linebreak = "\n")
-
 
1335
    {
-
 
1336
        // if no attributes hav been set, use empty attributes
-
 
1337
        if (!isset($attributes) || !is_array($attributes)) {
-
 
1338
            $attributes = array();
-
 
1339
        }
-
 
1340
        
-
 
1341
        if ($namespaceUri != null) {
-
 
1342
            $parts = PEAR_PackageFile_Generator_v2_XML_Util::splitQualifiedName($qname);
-
 
1343
        }
-
 
1344
 
-
 
1345
        // check for multiline attributes
-
 
1346
        if ($multiline === true) {
-
 
1347
            if ($indent === "_auto") {
-
 
1348
                $indent = str_repeat(" ", (strlen($qname)+2));
-
 
1349
            }
-
 
1350
        }
-
 
1351
 
-
 
1352
        if ($namespaceUri != null) {
-
 
1353
            // is a namespace given
-
 
1354
            if (isset($parts["namespace"]) && !empty($parts["namespace"])) {
-
 
1355
                $attributes["xmlns:".$parts["namespace"]] = $namespaceUri;
-
 
1356
            } else {
-
 
1357
                // define this Uri as the default namespace
-
 
1358
                $attributes["xmlns"] = $namespaceUri;
-
 
1359
            }
-
 
1360
        }
-
 
1361
 
-
 
1362
        // create attribute list
-
 
1363
        $attList    =   PEAR_PackageFile_Generator_v2_XML_Util::attributesToString($attributes, true, $multiline, $indent, $linebreak);
-
 
1364
        $element    =   sprintf("<%s%s>", $qname, $attList);
-
 
1365
        return  $element;
-
 
1366
    }
-
 
1367
 
-
 
1368
   /**
-
 
1369
    * create an end element
-
 
1370
    *
-
 
1371
    * <code>
-
 
1372
    * require_once 'XML/Util.php';
-
 
1373
    * 
-
 
1374
    * // create an XML start element:
-
 
1375
    * $tag = PEAR_PackageFile_Generator_v2_XML_Util::createEndElement("myNs:myTag");
-
 
1376
    * </code>
-
 
1377
    *
-
 
1378
    * @access   public
-
 
1379
    * @static
-
 
1380
    * @param    string  $qname             qualified tagname (including namespace)
-
 
1381
    * @return   string  $string            XML end element
-
 
1382
    * @see      PEAR_PackageFile_Generator_v2_XML_Util::createStartElement(), PEAR_PackageFile_Generator_v2_XML_Util::createTag()
-
 
1383
    */
-
 
1384
    function createEndElement($qname)
-
 
1385
    {
-
 
1386
        $element    =   sprintf("</%s>", $qname);
-
 
1387
        return  $element;
-
 
1388
    }
-
 
1389
    
-
 
1390
   /**
-
 
1391
    * create an XML comment
-
 
1392
    *
-
 
1393
    * <code>
-
 
1394
    * require_once 'XML/Util.php';
-
 
1395
    * 
-
 
1396
    * // create an XML start element:
-
 
1397
    * $tag = PEAR_PackageFile_Generator_v2_XML_Util::createComment("I am a comment");
-
 
1398
    * </code>
-
 
1399
    *
-
 
1400
    * @access   public
-
 
1401
    * @static
-
 
1402
    * @param    string  $content           content of the comment
-
 
1403
    * @return   string  $comment           XML comment
-
 
1404
    */
-
 
1405
    function createComment($content)
-
 
1406
    {
-
 
1407
        $comment    =   sprintf("<!-- %s -->", $content);
-
 
1408
        return  $comment;
-
 
1409
    }
-
 
1410
    
-
 
1411
   /**
-
 
1412
    * create a CData section
-
 
1413
    *
-
 
1414
    * <code>
-
 
1415
    * require_once 'XML/Util.php';
-
 
1416
    * 
-
 
1417
    * // create a CData section
-
 
1418
    * $tag = PEAR_PackageFile_Generator_v2_XML_Util::createCDataSection("I am content.");
-
 
1419
    * </code>
-
 
1420
    *
-
 
1421
    * @access   public
-
 
1422
    * @static
-
 
1423
    * @param    string  $data              data of the CData section
-
 
1424
    * @return   string  $string            CData section with content
-
 
1425
    */
-
 
1426
    function createCDataSection($data)
-
 
1427
    {
-
 
1428
        return  sprintf("<![CDATA[%s]]>", $data);
-
 
1429
    }
-
 
1430
 
-
 
1431
   /**
-
 
1432
    * split qualified name and return namespace and local part
-
 
1433
    *
-
 
1434
    * <code>
-
 
1435
    * require_once 'XML/Util.php';
-
 
1436
    * 
-
 
1437
    * // split qualified tag
-
 
1438
    * $parts = PEAR_PackageFile_Generator_v2_XML_Util::splitQualifiedName("xslt:stylesheet");
-
 
1439
    * </code>
-
 
1440
    * the returned array will contain two elements:
-
 
1441
    * <pre>
-
 
1442
    * array(
-
 
1443
    *       "namespace" => "xslt",
-
 
1444
    *       "localPart" => "stylesheet"
-
 
1445
    *      );
-
 
1446
    * </pre>
-
 
1447
    *
-
 
1448
    * @access public
-
 
1449
    * @static
-
 
1450
    * @param  string    $qname      qualified tag name
-
 
1451
    * @param  string    $defaultNs  default namespace (optional)
-
 
1452
    * @return array     $parts      array containing namespace and local part
-
 
1453
    */
-
 
1454
    function splitQualifiedName($qname, $defaultNs = null)
-
 
1455
    {
-
 
1456
        if (strstr($qname, ':')) {
-
 
1457
            $tmp = explode(":", $qname);
-
 
1458
            return array(
-
 
1459
                          "namespace" => $tmp[0],
-
 
1460
                          "localPart" => $tmp[1]
-
 
1461
                        );
-
 
1462
        }
-
 
1463
        return array(
-
 
1464
                      "namespace" => $defaultNs,
-
 
1465
                      "localPart" => $qname
-
 
1466
                    );
-
 
1467
    }
-
 
1468
 
-
 
1469
   /**
-
 
1470
    * check, whether string is valid XML name
-
 
1471
    *
-
 
1472
    * <p>XML names are used for tagname, attribute names and various
-
 
1473
    * other, lesser known entities.</p>
-
 
1474
    * <p>An XML name may only consist of alphanumeric characters,
-
 
1475
    * dashes, undescores and periods, and has to start with a letter
-
 
1476
    * or an underscore.
-
 
1477
    * </p>
-
 
1478
    *
-
 
1479
    * <code>
-
 
1480
    * require_once 'XML/Util.php';
-
 
1481
    * 
-
 
1482
    * // verify tag name
-
 
1483
    * $result = PEAR_PackageFile_Generator_v2_XML_Util::isValidName("invalidTag?");
-
 
1484
    * if (PEAR_PackageFile_Generator_v2_XML_Util::isError($result)) {
-
 
1485
    *    print "Invalid XML name: " . $result->getMessage();
-
 
1486
    * }
-
 
1487
    * </code>
-
 
1488
    *
-
 
1489
    * @access  public
-
 
1490
    * @static
-
 
1491
    * @param   string  $string string that should be checked
-
 
1492
    * @return  mixed   $valid  true, if string is a valid XML name, PEAR error otherwise
-
 
1493
    * @todo    support for other charsets
-
 
1494
    */
-
 
1495
    function isValidName($string)
-
 
1496
    {
-
 
1497
        // check for invalid chars
-
 
1498
        if (!preg_match("/^[[:alnum:]_\-.]$/", $string{0})) {
-
 
1499
            return PEAR_PackageFile_Generator_v2_XML_Util::raiseError( "XML names may only start with letter or underscore", PEAR_PackageFile_Generator_v2_XML_Util_ERROR_INVALID_START );
-
 
1500
        }
-
 
1501
        
-
 
1502
        // check for invalid chars
-
 
1503
        if (!preg_match("/^([a-zA-Z_]([a-zA-Z0-9_\-\.]*)?:)?[a-zA-Z_]([a-zA-Z0-9_\-\.]+)?$/", $string)) {
-
 
1504
            return PEAR_PackageFile_Generator_v2_XML_Util::raiseError( "XML names may only contain alphanumeric chars, period, hyphen, colon and underscores", PEAR_PackageFile_Generator_v2_XML_Util_ERROR_INVALID_CHARS );
-
 
1505
         }
-
 
1506
        // XML name is valid
-
 
1507
        return true;
-
 
1508
    }
-
 
1509
 
-
 
1510
   /**
-
 
1511
    * replacement for PEAR_PackageFile_Generator_v2_XML_Util::raiseError
-
 
1512
    *
-
 
1513
    * Avoids the necessity to always require
-
 
1514
    * PEAR.php
-
 
1515
    *
-
 
1516
    * @access   public
-
 
1517
    * @param    string      error message
-
 
1518
    * @param    integer     error code
-
 
1519
    * @return   object PEAR_Error
-
 
1520
    */
-
 
1521
    function raiseError($msg, $code)
-
 
1522
    {
-
 
1523
        require_once 'PEAR.php';
-
 
1524
        return PEAR::raiseError($msg, $code);
-
 
1525
    }
885
    }
1526
}
-
 
1527
?>
886
}