62 function &
parse($data, $file, $archive =
false)
64 if (!extension_loaded(
'xml')) {
65 return PEAR::raiseError(
'Cannot create xml parser for parsing package.xml, no xml extension');
67 $xp = xml_parser_create();
69 $a = &PEAR::raiseError(
'Cannot create xml parser for parsing package.xml');
72 xml_set_object($xp, $this);
73 xml_set_element_handler($xp,
'_element_start_1_0',
'_element_end_1_0');
74 xml_set_character_data_handler($xp,
'_pkginfo_cdata_1_0');
75 xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING,
false);
77 $this->element_stack = array();
78 $this->_packageInfo = array(
'provides' => array());
79 $this->current_element =
false;
80 unset($this->dir_install);
81 $this->_packageInfo[
'filelist'] = array();
82 $this->filelist =& $this->_packageInfo[
'filelist'];
83 $this->dir_names = array();
84 $this->in_changelog =
false;
87 $this->_isValid =
true;
89 if (!xml_parse($xp, $data, 1)) {
90 $code = xml_get_error_code($xp);
91 $line = xml_get_current_line_number($xp);
93 $a = PEAR::raiseError(sprintf(
"XML error: %s at line %d",
94 $str = xml_error_string($code), $line), 2);
102 if (isset($this->_logger)) {
103 $pf->setLogger($this->_logger);
105 $pf->setPackagefile($file, $archive);
106 $pf->fromArray($this->_packageInfo);
121 $str = preg_replace(
'/^[\r\n]+/',
'', $str);
123 $indent_len = strspn($str,
" \t");
124 $indent = substr($str, 0, $indent_len);
127 foreach (explode(
"\n", $str) as $line) {
128 if (substr($line, 0, $indent_len) == $indent) {
129 $data .= substr($line, $indent_len) .
"\n";
130 } elseif (trim(substr($line, 0, $indent_len))) {
131 $data .= ltrim($line);
153 array_push($this->element_stack, $name);
154 $this->current_element = $name;
155 $spos =
sizeof($this->element_stack) - 2;
156 $this->prev_element = ($spos >= 0) ? $this->element_stack[$spos] :
'';
157 $this->current_attributes = $attribs;
161 if ($this->in_changelog) {
164 if (array_key_exists(
'name', $attribs) && $attribs[
'name'] !=
'/') {
165 $attribs[
'name'] = preg_replace(array(
'!\\\\+!',
'!/+!'), array(
'/',
'/'),
167 if (strrpos($attribs[
'name'],
'/') === strlen($attribs[
'name']) - 1) {
168 $attribs[
'name'] = substr($attribs[
'name'], 0,
169 strlen($attribs[
'name']) - 1);
171 if (strpos($attribs[
'name'],
'/') === 0) {
172 $attribs[
'name'] = substr($attribs[
'name'], 1);
174 $this->dir_names[] = $attribs[
'name'];
176 if (isset($attribs[
'baseinstalldir'])) {
177 $this->dir_install = $attribs[
'baseinstalldir'];
179 if (isset($attribs[
'role'])) {
180 $this->dir_role = $attribs[
'role'];
184 if ($this->in_changelog) {
187 if (isset($attribs[
'name'])) {
189 if (count($this->dir_names)) {
190 foreach ($this->dir_names as $dir) {
194 $path .= preg_replace(array(
'!\\\\+!',
'!/+!'), array(
'/',
'/'),
196 unset($attribs[
'name']);
197 $this->current_path = $path;
198 $this->filelist[$path] = $attribs;
200 if (!isset($this->filelist[$path][
'baseinstalldir']) &&
201 isset($this->dir_install))
203 $this->filelist[$path][
'baseinstalldir'] = $this->dir_install;
206 if (!isset($this->filelist[$path][
'role']) && isset($this->dir_role)) {
207 $this->filelist[$path][
'role'] = $this->dir_role;
212 if (!$this->in_changelog) {
213 $this->filelist[$this->current_path][
'replacements'][] = $attribs;
217 $this->_packageInfo[
'maintainers'] = array();
222 if (!isset($this->_packageInfo[
'maintainers'])) {
223 $this->_packageInfo[
'maintainers'] = array();
226 $this->_packageInfo[
'maintainers'][$this->m_i] = array();
227 $this->current_maintainer =& $this->_packageInfo[
'maintainers'][$this->m_i];
230 $this->_packageInfo[
'changelog'] = array();
232 $this->in_changelog =
true;
235 if ($this->in_changelog) {
236 $this->_packageInfo[
'changelog'][$this->c_i] = array();
237 $this->current_release = &$this->_packageInfo[
'changelog'][$this->c_i];
239 $this->current_release = &$this->_packageInfo;
243 if (!$this->in_changelog) {
244 $this->_packageInfo[
'release_deps'] = array();
249 if (!$this->in_changelog) {
251 isset($attribs[
'type']) ? ($attribs[
'type'] = strtolower($attribs[
'type'])) :
false;
252 $this->_packageInfo[
'release_deps'][$this->d_i] = $attribs;
255 case 'configureoptions':
256 if (!$this->in_changelog) {
257 $this->_packageInfo[
'configure_options'] = array();
260 case 'configureoption':
261 if (!$this->in_changelog) {
262 $this->_packageInfo[
'configure_options'][] = $attribs;
266 if (empty($attribs[
'type']) || empty($attribs[
'name'])) {
269 $attribs[
'explicit'] =
true;
270 $this->_packageInfo[
'provides'][
"$attribs[type];$attribs[name]"] = $attribs;
273 if (isset($attribs[
'version'])) {
274 $this->_packageInfo[
'xsdversion'] = trim($attribs[
'version']);
276 $this->_packageInfo[
'xsdversion'] =
'1.0';
278 if (isset($attribs[
'packagerversion'])) {
279 $this->_packageInfo[
'packagerversion'] = $attribs[
'packagerversion'];
301 $data = trim($this->cdata);
304 switch ($this->prev_element) {
306 $this->_packageInfo[
'package'] = $data;
309 $this->current_maintainer[
'name'] = $data;
314 $this->_packageInfo[
'extends'] = $data;
317 $this->_packageInfo[
'summary'] = $data;
321 $this->_packageInfo[
'description'] = $data;
324 $this->current_maintainer[
'handle'] = $data;
327 $this->current_maintainer[
'email'] = $data;
330 $this->current_maintainer[
'role'] = $data;
333 if ($this->in_changelog) {
334 $this->current_release[
'version'] = $data;
336 $this->_packageInfo[
'version'] = $data;
340 if ($this->in_changelog) {
341 $this->current_release[
'release_date'] = $data;
343 $this->_packageInfo[
'release_date'] = $data;
350 $data = rtrim($this->
_unIndent($this->cdata));
351 if ($this->in_changelog) {
352 $this->current_release[
'release_notes'] = $data;
354 $this->_packageInfo[
'release_notes'] = $data;
358 if ($this->in_changelog) {
359 $this->current_release[
'release_warnings'] = $data;
361 $this->_packageInfo[
'release_warnings'] = $data;
365 if ($this->in_changelog) {
366 $this->current_release[
'release_state'] = $data;
368 $this->_packageInfo[
'release_state'] = $data;
372 if ($this->in_changelog) {
373 $this->current_release[
'release_license'] = $data;
375 $this->_packageInfo[
'release_license'] = $data;
379 if ($data && !$this->in_changelog) {
380 $this->_packageInfo[
'release_deps'][$this->d_i][
'name'] = $data;
384 if ($this->in_changelog) {
387 array_pop($this->dir_names);
390 if ($this->in_changelog) {
395 if (count($this->dir_names)) {
396 foreach ($this->dir_names as $dir) {
401 $this->filelist[$path] = $this->current_attributes;
403 if (!isset($this->filelist[$path][
'baseinstalldir']) &&
404 isset($this->dir_install))
406 $this->filelist[$path][
'baseinstalldir'] = $this->dir_install;
409 if (!isset($this->filelist[$path][
'role']) && isset($this->dir_role)) {
410 $this->filelist[$path][
'role'] = $this->dir_role;
415 if (empty($this->_packageInfo[
'maintainers'][$this->m_i][
'role'])) {
416 $this->_packageInfo[
'maintainers'][$this->m_i][
'role'] =
'lead';
421 if ($this->in_changelog) {
426 $this->in_changelog =
false;
429 array_pop($this->element_stack);
430 $spos =
sizeof($this->element_stack) - 1;
431 $this->current_element = ($spos > 0) ? $this->element_stack[$spos] :
'';