/[cvs]/nfo/php/libs/net.php.pear/Tree/Memory/XML.php
ViewVC logotype

Annotation of /nfo/php/libs/net.php.pear/Tree/Memory/XML.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Thu Feb 27 16:49:59 2003 UTC (21 years, 6 months ago) by joko
Branch: MAIN
+ initial commit, from PEAR

1 joko 1.1 <?php
2     //
3     // +----------------------------------------------------------------------+
4     // | PHP Version 4 |
5     // +----------------------------------------------------------------------+
6     // | Copyright (c) 1997-2003 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: Wolfram Kriesing <wolfram@kriesing.de> |
17     // +----------------------------------------------------------------------+
18     //
19     // Id: XML.php,v 1.6 2003/01/04 11:56:28 mj Exp
20     // $Id: XML.php,v 1.6 2003/01/04 11:56:28 mj Exp $
21    
22     require_once "XML/Parser.php";
23    
24     /**
25     * the XML interface for the tree class
26     *
27     * @package Tree
28     * @author
29     * @version
30     * @access public
31     */
32     class Tree_Memory_XML extends XML_Parser
33     {
34    
35     /**
36     * @var array $data the first element has to be empty, so we can use the parentId=0 as "no parent"
37     */
38     var $data = array(0=>NULL);
39    
40     /**
41     * @var integer $level
42     */
43     var $level = 0;
44    
45     /**
46     * @var array $parentIdOnLevel
47     */
48     var $parentIdOnLevel = array();
49    
50     /**
51     * @var boolean $folding set case folding for the XML_Parser to false
52     */
53     var $folding = false; // turn off case folding
54    
55     /**
56     * @var boolean if true it converts all attributes and tag names etc to lower case
57     * this is default, since i dont see no way of case insensitive comparison
58     * in the tree class, since you can access the internal data directly
59     * or you get them returned ... i know this is not 100% proper OOP but that's how it is right now
60     */
61     var $_toLower = true;
62    
63     /**
64     *
65     *
66     * @version 2002/01/17
67     * @access public
68     * @author Wolfram Kriesing <wolfram@kriesing.de>
69     * @return boolean true on success
70     */
71     function Tree_Memory_XML( $dsn , $options )
72     {
73     $handle = $dsn;
74    
75     $this->XML_Parser();
76    
77     if (@is_resource($handle)) {
78     $this->setInput($handle);
79     } elseif ($handle != "") {
80     $this->setInputFile($handle);
81     } else {
82     return $this->raiseError("No filename passed.");
83     }
84     }
85    
86     /**
87     *
88     *
89     * @version 2002/01/17
90     * @access public
91     * @author Wolfram Kriesing <wolfram@kriesing.de>
92     * @return boolean true on success
93     */
94     function startHandler($parser, $element, $attribs)
95     {
96     $elementBeforeId = sizeof($this->data)-1;
97     $curId = sizeof($this->data);
98    
99     $this->data[$curId]['id'] = $curId;
100     $this->data[$curId]['name'] = $this->_toLower ? strtolower($element) : $element;
101     $this->data[$curId]['level'] = $this->level;
102     $this->data[$curId]['attributes'] = $attribs;
103     if( $this->_toLower )
104     {
105     $this->data[$curId]['attributes'] = array();
106     foreach( $attribs as $key=>$value )
107     $this->data[$curId]['attributes'][strtolower($key)] = $value;
108     }
109    
110     if( isset($this->data[$elementBeforeId]['level']) &&
111     $this->level == $this->data[$elementBeforeId]['level'] ) // is that a new child, or just a 'next' of a child?
112     {
113     $this->data[$curId]['parentId'] = $this->data[$elementBeforeId]['parentId'];
114     }
115     else // set stuff for the first child !!!
116     {
117     if( $this->level>0 ) // the root has no parent
118     {
119     $parentId = $this->parentIdOnLevel[$this->level-1];
120     $this->data[$curId]['parentId'] = $parentId;
121     }
122     else
123     {
124     $this->data[$curId]['parentId'] = 0;
125     }
126     }
127     $this->parentIdOnLevel[$this->level] = $curId;
128    
129     #print "$curId $element ".$this->data[$curId]['parentId'].'<br>';
130     $this->level++;
131     }
132    
133     /**
134     *
135     *
136     * @version 2002/01/17
137     * @access public
138     * @author Wolfram Kriesing <wolfram@kriesing.de>
139     * @return boolean true on success
140     */
141     function endHandler($parser, $element)
142     {
143     $this->level--;
144     }
145    
146     /**
147     *
148     *
149     * @version 2002/01/17
150     * @access public
151     * @author Wolfram Kriesing <wolfram@kriesing.de>
152     * @return boolean true on success
153     */
154     function cdataHandler($parser, $cdata)
155     {
156     # QUESTION: why is this method called multiple times for one element?
157     # is every space a cdata ???
158     # ANSWER: if you call xml_parse($parser, "foo ", false) and then
159     # xml_parse($parser, "bar", true), callbacks are done once
160     # for each xml_parse() call.
161     if( !isset($this->data[ sizeof($this->data)-1 ]['cdata']) )
162     $this->data[ sizeof($this->data)-1 ]['cdata'] = '';
163     #print "cdata = '$cdata'\r\n";
164     $this->data[ sizeof($this->data)-1 ]['cdata'].= $cdata;
165     }
166    
167     /**
168     *
169     *
170     * @version 2002/01/17
171     * @access public
172     * @author Wolfram Kriesing <wolfram@kriesing.de>
173     * @return boolean true on success
174     */
175     function defaultHandler($parser, $cdata)
176     {
177     # $this->data[ sizeof($this->data)-1 ]['cdata'] = $cdata;
178     # not in use yet :-( is that ok??
179     }
180    
181    
182    
183    
184     /**
185     * read the data from the xml file and prepare them so the tree
186     * class can work with it, the preparation is mainly done in startHandler
187     *
188     * @version 2002/01/17
189     * @access public
190     * @author Wolfram Kriesing <wolfram@kriesing.de>
191     * @return boolean true on success
192     */
193     function setup()
194     {
195     $this->parse();
196    
197     return $this->data;
198     } // end of function
199    
200     /**
201     * read the data from an xml string and prepare them so the tree
202     * class can work with it, the preparation is mainly done in startHandler
203     *
204     * @version 2002/02/05
205     * @access public
206     * @author Wolfram Kriesing <wolfram@kriesing.de>
207     * @return boolean true on success
208     */
209     function setupByRawData( $xmlString )
210     {
211     $this->parseString( $xmlString , true );
212    
213     return $this->data;
214     }
215    
216     /**
217     * TO BE IMPLEMNTED
218     * adds _one_ new element in the tree under the given parent
219     * the values' keys given have to match the db-columns, because the
220     * value gets inserted in the db directly
221     * to add an entire node containing children and so on see 'addNode()'
222     *
223     * @see addNode()
224     * @version 2001/10/09
225     * @access public
226     * @author Wolfram Kriesing <wolfram@kriesing.de>
227     * @param array $newValues this array contains the values that shall be inserted in the db-table
228     * @return mixed either boolean false on failure or the id of the inserted row
229     */
230     /* function add( $newValues )
231     {
232     // add the data in the internal structure $this->data
233     $this->data[sizeof($this->data)] = $newValues;
234    
235     # i am thinking if it might be a good solution to walk the data-array
236     # and write each line singlely until the one to add comes, write it and
237     # keep on writing the data-array
238     # but that means writing the entire file every time any method that
239     # changes the xml-file's structure the entire file is written,
240     # can that not be done somehow better ???
241    
242     # // and regenerate the xml file
243     # $this->_writeFile();
244    
245     } // end of function
246     */
247     /**
248     * TO BE IMPLEMNTED
249     * removes the given node
250     *
251     * @version 2001/10/09
252     * @access public
253     * @author Wolfram Kriesing <wolfram@kriesing.de>
254     * @param mixed $id the id of the node to be removed
255     * @return boolean true on success
256     */
257     /* function remove( $id )
258     {
259     // remove the data from this->data
260     unset($this->data[$id]);
261    
262     # see comment in "add"-method
263     } // end of function
264     */
265     /**
266     * TO BE IMPLEMNTED
267     * move an entry under a given parent or behind a given entry
268     *
269     * @version 2001/10/10
270     * @access public
271     * @author Wolfram Kriesing <wolfram@kriesing.de>
272     * @param
273     * @param
274     * @param integer if prevId is given the element with the id idToMove shall be moved _behind_ element with id=prevId
275     * before would be easier, but then no element could be inserted at the end :-/
276     * @return boolean true for success
277     */
278     /* function move( $idToMove , $newParentId , $prevId=0 )
279     {
280     $this->data[$idToMove]['parentId'] = $newParentId;
281     $this->data[$idToMove]['prevId'] = $prevId;
282    
283     # see comment in "add"-method
284     } // end of function
285     */
286    
287     }
288     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed