metabase/metabase_parser.php - view - 1.4
1: <?
2: /*
3: * metabase_parser.php
4: *
5: * @(#) $Header: /opt2/mlemos/cvs/metal/metabase/metabase_parser.php,v 1.4 1999/01/04 04:05:05 mlemos Exp $
6: *
7: */
8:
9: /*
10: * Parser error numbers:
11: *
12: * 1 - Could not create the XML parser
13: * 2 - Could not parse data
14: * 3 - Could not read from input stream
15: * 4 - Metabase format syntax error
16: *
17: */
18:
19: $metabase_handlers=array();
20:
21: Function metabase_start_element_handler($parser,$name,$attrs)
22: {
23: global $metabase_handlers;
24:
25: if($metabase_handlers[$parser]->error=="")
26: $metabase_handlers[$parser]->StartElement($name,$attrs);
27: }
28:
29: Function metabase_end_element_handler($parser,$name)
30: {
31: global $metabase_handlers;
32:
33: if($metabase_handlers[$parser]->error=="")
34: $metabase_handlers[$parser]->EndElement($name);
35: }
36:
37: class metabase_handler_class
38: {
39: var $xml_parser;
40: var $depth=0;
41: var $error_number=0;
42: var $error="";
43: var $error_code=0;
44: var $error_line,$error_column,$error_byte_index;
45: var $tables;
46:
47: Function SetError($error_number,$error)
48: {
49: $this->error_number=$error_number;
50: $this->error=$error;
51: $this->error_line=xml_get_current_line_number($this->xml_parser);
52: $this->error_column=xml_get_current_column_number($this->xml_parser);
53: $this->error_byte_index=xml_get_current_byte_index($this->xml_parser);
54: }
55:
56: Function StartElement($name,&$attrs)
57: {
58: $this->depth++;
59: switch($this->depth)
60: {
61: case 1:
62: if($name!="DATABASE")
63: {
64: $this->SetError(4,"Root element is not DATABASE");
65: break;
66: }
67: $this->database["name"]=(IsSet($attrs["NAME"]) ? $attrs["NAME"] : "");
68: $this->database["tables"]=array();
69: break;
70: case 2:
71: if($name!="TABLE")
72: {
73: $this->SetError(4,"DATABASE element is not a TABLE");
74: break;
75: }
76: if(!IsSet($attrs["NAME"])
77: || ($this->table=$attrs["NAME"])=="")
78: {
79: $this->SetError(4,"It was not defined the NAME of the TABLE");
80: break;
81: }
82: if(IsSet($this->database["tables"][$this->table]))
83: {
84: $this->SetError(4,"TABLE is already defined");
85: break;
86: }
87: $this->database["tables"][$this->table]=array("fields"=>array());
88: break;
89: case 3:
90: if($name!="FIELD")
91: {
92: $this->SetError(4,"TABLE element is not a FIELD");
93: break;
94: }
95: if(!IsSet($attrs["NAME"])
96: || ($this->field=$attrs["NAME"])=="")
97: {
98: $this->SetError(4,"It was not defined the NAME of the FIELD");
99: break;
100: }
101: if(IsSet($this->database["tables"][$this->table]["fields"][$this->field]))
102: {
103: $this->SetError(4,"FIELD is already defined");
104: break;
105: }
106: if(!IsSet($attrs["TYPE"]))
107: {
108: $this->SetError(4,"it was not defined the TYPE of the FIELD");
109: break;
110: }
111: switch($type=$attrs["TYPE"])
112: {
113: case "INTEGER":
114: case "TEXT":
115: case "BOOLEAN":
116: $field=array("TYPE"=>$type);
117: break;
118: default:
119: $type="";
120: }
121: if($type=="")
122: {
123: $this->SetError(4,"it was not defined a valid FIELD TYPE");
124: break;
125: }
126: switch($type)
127: {
128: case "INTEGER":
129: if(IsSet($attrs["UNSIGNED"]))
130: $field["UNSIGNED"]=1;
131: break;
132: }
133: $this->database["tables"][$this->table]["fields"][$this->field]=$field;
134: break;
135: default:
136: $this->SetError(4,"invalid tag");
137: break;
138: }
139: }
140:
141: Function EndElement($name)
142: {
143: switch($this->depth)
144: {
145: case 1:
146: if(count($this->database["tables"])==0)
147: $this->SetError(4,"DATABASE has no TABLEs");
148: break;
149: case 2:
150: if(count($this->database["tables"][$this->table]["fields"])==0)
151: $this->SetError(4,"TABLE has no FIELDs");
152: break;
153: }
154: $this->depth--;
155: }
156: };
157:
158: class metabase_parser_class
159: {
160: /* PUBLIC DATA */
161: var $stream_buffer_size=4096;
162: var $error_number=0;
163: var $error="";
164: var $error_code=0;
165: var $error_line,$error_column,$error_byte_index;
166: var $tables;
167:
168: /* PRIVATE DATA */
169: var $xml_parser;
170:
171: /* PRIVATE METHODS */
172:
173: Function SetError($error_number,$error)
174: {
175: $this->error_number=$error_number;
176: $this->error=$error;
177: if($this->xml_parser)
178: {
179: $this->error_line=xml_get_current_line_number($this->xml_parser);
180: $this->error_column=xml_get_current_column_number($this->xml_parser);
181: $this->error_byte_index=xml_get_current_byte_index($this->xml_parser);
182: }
183: else
184: {
185: $this->error_line=0;
186: $this->error_column=0;
187: $this->error_byte_index=0;
188: }
189: }
190:
191:
192: /* PUBLIC METHODS */
193:
194: Function Parse($data,$end_of_data)
195: {
196: global $metabase_handlers;
197:
198: if($this->error!="")
199: return($this->error);
200: if(!$this->xml_parser)
201: {
202:
203: if(!($this->xml_parser=xml_parser_create()))
204: {
205: $this->SetError(1,"Could not create the XML parser");
206: return($this->error);
207: }
208: xml_set_element_handler($this->xml_parser,"metabase_start_element_handler","metabase_end_element_handler");
209: $metabase_handlers[$this->xml_parser]=new metabase_handler_class;
210: $metabase_handlers[$this->xml_parser]->xml_parser=$this->xml_parser;
211: }
212: $parser_ok=xml_parse($this->xml_parser,$data,$end_of_data);
213: if($metabase_handlers[$this->xml_parser]->error=="")
214: {
215: if($parser_ok)
216: {
217: if($end_of_data)
218: {
219: $this->database=$metabase_handlers[$this->xml_parser]->database;
220: Unset($metabase_handlers[$this->xml_parser]);
221: xml_parser_free($this->xml_parser);
222: $this->xml_parser=0;
223: }
224: }
225: else
226: $this->SetError(2,"Could not parse data: ".xml_error_string($this->error_code=xml_get_error_code($this->xml_parser)));
227: }
228: else
229: {
230: $this->error_number=$metabase_handlers[$this->xml_parser]->error_number;
231: $this->error=$metabase_handlers[$this->xml_parser]->error;
232: $this->error_code=0;
233: $this->error_line=$metabase_handlers[$this->xml_parser]->error_line;
234: $this->error_column=$metabase_handlers[$this->xml_parser]->error_column;
235: $this->error_byte_index=$metabase_handlers[$this->xml_parser]->error_byte_index;
236: }
237: return($this->error);
238: }
239:
240: Function ParseStream($stream)
241: {
242: if($this->error!="")
243: return($this->error);
244: do
245: {
246: if(!($data=fread($stream,$this->stream_buffer_size)))
247: {
248: $this->SetError(3,"Could not read from input stream");
249: break;
250: }
251: if(($error=$this->Parse($data,feof($stream)))!="")
252: break;
253: }
254: while(!feof($stream));
255: return($this->error);
256: }
257: }
258:
259: ?>
|