2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Alan Knowles <alan@akbkhome.com> |
17 // +----------------------------------------------------------------------+
19 // $Id: php_pear_headers,v 1.1 2002/04/22 09:51:27 alan_k Exp $
21 // This is a wrapper class for XML_Tree to lets you add callbacks to xml tags
22 // to enable data morphing (so you can get associative arrays and the like from
27 require_once 'XML/Tree.php';
37 * require_once 'XML/Tree/Morph.php';
40 * $x = new XML_Tree_Morph(
45 * //tag // either toObject/toArray or your function/method etc.
46 * 'project' => 'toObject',
47 * 'widget' => 'toObject',
48 * 'child' => 'toObject',
53 * $tree = $x->getTreeFromFile();
54 * print_r($tree->children[0]['project']);
55 * print_r($tree->children[1]['widget']);
61 * [name] => validatemanager
62 * [program_name] => validatemanage
64 * [source_directory] => src
65 * [pixmaps_directory] => pixmaps
67 * [gnome_support] => False
68 * [gettext_support] => False
72 * [class] => GtkWindow
74 * [title] => Gtk_ValidateManager
75 * [type] => GTK_WINDOW_TOPLEVEL
76 * [position] => GTK_WIN_POS_CENTER
78 * [default_width] => 600
79 * [default_height] => 400
80 * [allow_shrink] => False
81 * [allow_grow] => True
82 * [auto_shrink] => False
83 * [widget] => stdClass Object
87 * @version $Id: php_phpdoc_class,v 1.1 2002/04/22 10:20:29 alan_k Exp $
89 class XML_Tree_Morph extends XML_Tree {
100 * @param string Filename
101 * @param array $options
105 * tagname => callback
107 * where callback can be
108 * - toObject|toArary = built in converters
109 * - 'function', array($object,'method') or
110 * array('class','staticMethod');
116 function __construct($filename,$options) {
118 parent::__construct($filename,'1.0');
120 $this->_morphOptions = $options;
125 * Overridden endHandler which relays into callbacks..
127 * @see XML_Tree:endHandler();
132 function endHandler($xp, $elem) {
135 $obj_id = 'obj' . $this->i;
136 // recover the node created in StartHandler
137 $node = $this->$obj_id;
139 if (count($node->children) > 0) {
140 if (trim($this->cdata)) {
141 $node->children[] = new XML_Tree_Node(null, $this->cdata);
144 $node->setContent($this->cdata);
146 $parent_id = 'obj' . ($this->i - 1);
147 $parent = $this->$parent_id;
148 // attach the node to its parent node children array
150 if (isset($this->_morphOptions['filter'][$node->name])) {
151 $f = &$this->_morphOptions['filter'][$node->name];
152 if (is_string($f) && method_exists($this,'morph'.$f)) {
153 $parent->children[] = $this->{'morph'.$this->_morphOptions['filter'][$node->name]}($node);
157 if (is_callable($f)) {
158 $parent->children[] = call_user_func($f, $node);
166 if (@$this->_morphOptions['debug']) {
167 echo "SKIP: {$node->name}\n";
\r
170 $parent->children[] = $node;
181 * Converts standard <xxx>vvvv</xxxx> into
185 * @param object XML_Tree_Node
188 * @return array (name => array(keys=>values)
191 function morphToArray($node) {
193 foreach($node->children as $cnode) {
195 if (is_a($cnode,'xml_tree_node')) {
196 $ret[$cnode->name] = $cnode->content;
199 // otherwise it's an array...
202 foreach($cnode as $k=>$o) {
203 if (empty($ret[$k])) {
206 if (!is_array($ret[$k])) {
207 echo "OPPS: $k already in array ";print_r($ret);
208 echo "TRYING TO ADD "; print_r($cnode);
215 foreach($ret as $k=>$v) {
216 if (is_array($v) && count($v) == 1 ) {
222 return array($node->name => $ret);
227 * Converts standard <xxx>vvvv</xxxx> into
231 * @param object XML_Tree_Node
234 * @return array ($node->name => $object);
237 function morphToObject($node) {
239 foreach($node->children as $cnode) {
241 if (is_a($cnode,'xml_tree_node')) {
242 $ret->{$cnode->name} = $cnode->content;
245 // otherwise it's an array...
248 foreach($cnode as $k=>$o) {
249 if (empty($ret->{$k})) {
252 if (!is_array($ret->{$k})) {
253 echo "OPPS: $k already in array ";print_r($ret);
254 echo "TRYING TO ADD "; print_r($cnode);
261 foreach($ret as $k=>$v) {
262 if (is_array($v) && count($v) == 1 ) {
268 return array($node->name => $ret);