| <?php | |
| /******************************************************************************* | |
| * Copyright (c) 2012 Eclipse Foundation and others. | |
| * All rights reserved. This program and the accompanying materials | |
| * are made available under the terms of the Eclipse Public License v1.0 | |
| * which accompanies this distribution, and is available at | |
| * http://www.eclipse.org/legal/epl-v10.html | |
| * | |
| * Contributors: | |
| * Wayne Beaton (Eclipse Foundation)- initial API and implementation | |
| *******************************************************************************/ | |
| /** | |
| * The ProjectPlanParser turns an XML-based project plan into an array. | |
| */ | |
| class ProjectPlanParser { | |
| var $parser; | |
| var $stack; | |
| static function getPlanFromFile($file) { | |
| if (!$file) return null; | |
| if (!file_exists($file)) return null; | |
| if (!$contents = file_get_contents($file)) return null; | |
| return self::getPlanFromString($contents); | |
| } | |
| static function getPlanFromString($string) { | |
| if (!$string) return null; | |
| $parser = new self(); | |
| return $parser->parse($string); | |
| } | |
| function __construct() { | |
| $this->parser = xml_parser_create_ns(); | |
| xml_set_object($this->parser, $this); | |
| xml_set_element_handler($this->parser, "start", "end"); | |
| xml_set_character_data_handler($this->parser, "characters"); | |
| } | |
| function parse($data) { | |
| $this->stack = array(array()); | |
| xml_parse($this->parser, $data, true); | |
| $top = array_pop($this->stack); | |
| return $top['plan'][0]; | |
| } | |
| function start($parser, $tag, $attributes) { | |
| if (preg_match('/^HTTP:\/\/WWW\.ECLIPSE\.ORG\/PROJECT\/PLAN:(.+)$/', $tag, $matches)) { | |
| $nested = array(); | |
| foreach($attributes as $key => $value) $nested[strtolower($key)] = $value; | |
| array_push($this->stack, $nested); | |
| } else { | |
| preg_match('/^http:\/\/[^:]+:(.+)$/i', $tag, $matches); | |
| $tag = strtolower($matches[1]); | |
| foreach($attributes as $key => $value) | |
| $tag .= ' ' . strtolower($key) . "=\"$value\""; | |
| $this->characters($parser, "<$tag>"); | |
| } | |
| } | |
| function characters($parser, $data) { | |
| if (!trim($data)) return; | |
| $top = array_pop($this->stack); | |
| $top['#text'] .= $data; | |
| array_push($this->stack, $top); | |
| } | |
| function end($parser, $tag) { | |
| if (preg_match('/^HTTP:\/\/WWW\.ECLIPSE\.ORG\/PROJECT\/PLAN:(.+)$/', $tag, $matches)) { | |
| $tag = strtolower($matches[1]); | |
| $nested = array_pop($this->stack); | |
| if (isset($nested['#text'])) | |
| $nested['#text'] = preg_replace('/\s+/', ' ', trim($nested['#text'])); | |
| $top = array_pop($this->stack); | |
| $top[$tag][] = $nested; | |
| array_push($this->stack, $top); | |
| } else { | |
| preg_match('/^http:\/\/[^:]+:(.+)$/i', $tag, $matches); | |
| $tag = strtolower($matches[1]); | |
| $this->characters($parser, "</$tag>"); | |
| } | |
| } | |
| } | |
| ?> |