OSDN Git Service

前回のコミットの補遺
[nucleus-jp/nucleus-next.git] / nucleus / convert / PRAX.php
1 <?php
2
3 /*
4
5 PRAX - PHP Record-oriented API for XML
6
7 Affords a database recordset-like view of an XML document
8 in documents which lend themselves to such interpretation.
9
10 A port of the Perl XML::RAX module by Robert Hanson 
11 (http://search.cpan.org/search?mode=module&query=rax)
12 based on the RAX API created by Sean McGrath 
13 (http://www.xml.com/pub/2000/04/26/rax)
14
15 Copyright (c) 2000 Rael Dornfest <rael@oreilly.com>,
16 All Rights Reserved.
17
18 License is granted to use or modify this software ("PRAX")
19 for commercial or non-commercial use provided the copyright 
20 of the author is preserved in any distributed or derivative 
21 work.
22
23 XML::RAX Copyright (c) 2000 Robert Hanson.  All rights
24 reserved.  This program ("XML::RAX") is free software; you 
25 can redistribute and/or modify it under the terms of the 
26 Perl "Artistic License."
27 (http://www.perl.com/language/misc/Artistic.html)
28
29 For a usage synopsis, see this distribution's README.txt.
30 Take a gander at sample.php (using sample.xml) for a 
31 live example.
32
33 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED
34 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
37 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 POSSIBILITY OF SUCH DAMAGE.
44
45
46 MODIFICATIONS:
47 Slightly modified by Wouter Demuynck (wouter@demuynck.org)
48 */
49
50
51 class RAX {
52
53         function RAX () {
54
55                 $this->record_delim = '';
56                 $this->fields = array();
57                 $this->records = array();
58                 $this->parser;
59                 $this->in_rec = 0;
60                 $this->in_field = 0;
61                 $this->field_data = '';
62                 $this->tag_stack = array();
63                 $this->xml = '';
64                 $this->xml_file;
65                 $this->rax_opened = 0;
66                 $this->debug = 0;
67                 $this->version = '0.1';
68
69         }
70
71
72         function open ($xml) {
73
74                 $this->debug("open(\"$xml\")");
75
76                 if ($this->rax_opened) return 0;
77
78                 $this->xml = $xml;
79                 $this->rax_opened = 1;
80         }
81
82
83         function openfile ($filename) {
84
85                 $this->debug("openfile(\"$filename\")");
86
87                 if ($this->rax_opened) return 0;
88
89                 $fp = fopen($filename, "r");
90
91                 if ($fp) {
92                         $this->xml_file = $fp;
93                         $this->rax_opened = 1;
94                         return 1;
95                 }
96
97                 return 0;
98         }
99
100
101         function startparse () {
102
103                 $this->debug("startparse()");
104
105                 $this->parser = xml_parser_create();
106
107                 xml_set_object($this->parser,$this);
108                 xml_set_element_handler($this->parser,  "startElement",  "endElement");
109                 xml_set_character_data_handler($this->parser,  "characterData");
110                 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
111
112                 if (xml_parse($this->parser, '')) {
113                         $this->parse_started = 1;
114                         return 1;
115                 }
116
117                 return 0;
118         }
119
120
121         function parse () {
122                 
123                 $this->debug("parse()");
124
125                 if (!$this->rax_opened) return 0;
126                 if ($this->parse_done) return 0;
127
128                 if (!$this->parse_started) 
129                         if (!$this->startparse()) return 0;
130
131                 if ($this->xml_file) {
132
133                         $buffer = fread($this->xml_file, 4096);
134
135                         if ( $buffer )
136                                 xml_parse( $this->parser, $buffer, feof($this->xml_file) );
137                         else {
138                                 $this->parse_done = 1;
139                         }
140
141                 }
142                 else {
143                         xml_parse($this->parser, $this->xml, 1);
144                         $this->parse_done = 1;
145                 }
146
147                 return 1;
148         }
149
150
151         function startElement($parser, $name, $attrs) {
152                 
153                 $this->debug("startElement($name)");
154
155                 array_push($this->tag_stack, $name);
156
157                 if ( !$this->in_rec and !strcmp($name, $this->record_delim) ) {
158                         $this->in_rec = 1;
159                         $this->rec_lvl = sizeof($this->tag_stack);
160                         $this->field_lvl = $this->rec_lvl + 1;
161                 }
162                 else if ( $this->in_rec and sizeof($this->tag_stack) == $this->field_lvl ) {
163                         $this->in_field = 1;
164                 }
165
166         }
167
168
169         function endElement($parser, $name) {
170
171                 $this->debug("endElement($name)");
172
173                 array_pop($this->tag_stack);
174
175                 if ( $this->in_rec ) {
176
177                         if ( sizeof($this->tag_stack) < $this->rec_lvl ) {
178                                 $this->in_rec = 0;
179                                 array_push( $this->records, new RAX_Record( $this->fields ) );
180                                 $this->fields = array();
181                         }
182                         else if ( sizeof($this->tag_stack) < $this->field_lvl ) {
183                                 $this->in_field = 0;
184                                 $this->fields[$name] = $this->field_data;
185                                 $this->field_data = '';
186                         }
187
188                 }
189
190         }
191
192
193         function characterData ($parser, $data) {
194
195                 $this->debug("characterData($data)");
196
197                 if ( $this->in_field ) 
198                         $this->field_data .= $data;
199
200         }
201
202
203         function setRecord ($delim) {
204
205                 $this->debug("setRecord($delim)");
206
207                 if ($this->parse_started) return 0;
208
209                 $this->record_delim = $delim;
210
211                 return 1;
212         }
213
214
215         function readRecord () {
216
217                 $this->debug("readRecord()");
218
219                 while ( !sizeof($this->records) and !$this->parse_done ) $this->parse();
220
221                 return array_shift($this->records);
222         }
223
224
225         function debug ($msg) {
226                 if ($this->debug) print "$msg<br />\n";
227         }
228         
229         // added by Wouter Demuynck
230         function close() {
231                 if ($this->rax_opened) {
232                         $this->debug("Closing RAX");
233                         
234                         fclose($this->xml_file);
235                         $this->xml_file = 0;
236                         $this->rax_opened = 0;
237                 }
238         }
239
240 }
241
242
243 class RAX_Record {
244
245         function RAX_Record ( $fields ) {
246
247                 $this->fields = $fields;
248
249                 $this->debug = 0;
250         }
251
252
253         function getFieldnames () {
254                 
255                 $this->debug("getFieldnames()");
256
257                 return array_keys( $this->fields );
258         }
259
260
261         function getField ( $field ) {
262                 
263                 $this->debug("getField($field)");
264
265                 return trim( $this->fields[$field] );
266         }
267
268
269         function getFields () {
270                 
271                 $this->debug("getFields()");
272
273                 return array_values( $this->fields );
274         }
275
276
277         function getRow () {
278                 
279                 $this->debug("getFields()");
280
281                 return $this->fields;
282         }
283
284
285         function debug ($msg) {
286                 if ($this->debug) print "$msg<br />\n";
287         }
288
289 }