Tryag File Manager
Home
-
Turbo Force
Current Path :
/
home
/
cluster1
/
data
/
bu01
/
1121861
/
html
/
jlex
/
php4
/
Upload File :
New :
File
Dir
//home/cluster1/data/bu01/1121861/html/jlex/php4/flat_to_hierarchy_converter.php4
<? /* This class takes in a flat xml file and an xml schema file and converts the flat xml file into an xml file observing the schema. Note that if the is not complete schema file, i.e. contains the children fields of the root node, the program will do so in the hierarchy_loader object, taking extra time. */ include_once("group.php4"); include_once("fields_loader.php4"); include_once("schema_loader.php4"); class flat_to_hierarchy_converter { var $head_tag; var $parser; var $cur_group; var $get_entry = false; var $cur_tag; var $get_val; var $template; var $out; var $cur_data; function startHandler($xp, $element, $attribs) { $element = strtolower($element); //echo "START: $element \n"; if($element == $this->head_tag) { $this->cur_group->destroy_subgroups(); $this->cur_group->init_ids(); $this->cur_group->assign_ids($this->cur_group->ids[$this->head_tag], $this->cur_group->ids[$this->head_tag], $this->cur_group->ids[$this->head_tag]); $this->template = ""; $this->get_entry = true; } else if ($this->get_entry){ $found = false; while(!$found) { //First check to see if the element exists in the current group. Note that in; //subgroups, a field can only occur once. Otherwise, it is not possible; //to determine if a field marks the beginning of a new group or exists within; //the current group. The flexibility may be able to be improved for this in; //later versions.; if(array_key_exists($element,$this->cur_group->fields)) { /* The following code handles the case when the max number of fields allowed; per group is reached. In this event, a new group of the same type of as cur_group; is created. */ if(count($this->cur_group->field_vals[$element]) == $this->cur_group->fields[$element]) { $this->template .= "/".$this->cur_group->name." ".$this->cur_group->name." "; $name = $this->cur_group->name; $this->cur_group = &$this->cur_group->parent; $new_group = $this->cur_group->groups[$name]; $new_group->assign_ids($this->cur_group->main_id, $this->cur_group->id, $this->cur_group->name); $new_group->parent = &$this->cur_group; $this->cur_group->group_vals[$new_group->name][] = &$new_group; $this->cur_group = &$new_group; //$new_group->parent = NULL; unset($new_group); } $this->template .= "$element "; $this->cur_tag = $element; $this->get_val = true; $found = true; } //Then check to see if the element exists in any of the subgroups of the current group; //If the element is not in any of the subgroups then it must mean there are no more elements; //in the current group. I have set the condition that a group must contain at least one non-group; //element and it must be the first item in the group. Otherwise, it is not possible to determine; //to which group a field belongs. For example, a pna field, which is a field of a subgroup of; //se and sea, can not precede the sense it belongs to. Otherwise, it is not possible to determine; //whether the pna field belongs to the sense field before it or after it (if one exists). //$new_group = false; else { foreach($this->cur_group->groups as $new_group) { if(array_key_exists($element,$new_group->fields)) { //echo "moving down from ".$this->cur_group->name." to "; $new_group->assign_ids($this->cur_group->main_id, $this->cur_group->id, $this->cur_group->name); $new_group->parent = &$this->cur_group; $this->cur_group->group_vals[$new_group->name][] = &$new_group; $this->cur_group = &$new_group; unset($new_group); //echo $this->cur_group->name."<BR>"; $this->cur_tag = $element; $this->get_val = true; $this->template .= $this->cur_group->name." ".$element." "; $found = true; break; } } //If the element did no exist in any subgroups, ascend up the hierarchy. If the element is; //is not a member of the parent group, the variable 'new_group' will remain false and the; //while loop will begin again though with the parent group as the current group. I suppose; //this leads to some redundancy as the code will check in places it has alread looked, namely; //the current group. However, I believe the efficiency of the recursion in general makes up for it; if(!$found) { if($this->cur_group->name != $this->head_tag) { $this->template .= "/".$this->cur_group->name." "; } if($this->cur_group->parent) { $this->cur_group = &$this->cur_group->parent; //echo $this->cur_group->name." \n"; } else { $ref = $this->cur_group->field_vals["ref"][0]; $error = "LINE ".xml_get_current_line_number($this->parser); $error .= ": ERROR: XML does not match schema. $element : ref: $ref \n"; echo $error; break; } } } } } } function endHandler($xp, $element) { $element = strtolower($element); //echo "END: $element \n"; if($element == $this->head_tag) { while($this->cur_group->name != $this->head_tag) { $this->template .= "/".$this->cur_group->name." "; $this->cur_group = &$this->cur_group->parent; } $this->template = trim($this->template); $this->cur_group->template = $this->template; $fields = explode(" ", $this->template); fwrite($this->out,$this->cur_group->to_xml_3($fields,0)."\n"); $this->cur_group->destroy_subgroups(); $this->get_entry = false; } else if($this->get_entry && $this->get_val) { $this->cur_group->add_value($element,$this->cur_data); $this->get_val = false; $this->cur_tag = ""; $this->cur_data = ""; } } function cDataHandler($xp, $data) { $data = trim($data); if($this->get_val && ($data != "")) { $data = ereg_replace("&","&",$data); $data = ereg_replace("<","<",$data); $data = ereg_replace(">",">",$data); $this->cur_data .= $data; } } function produce_xml_with_hierarchy($database,$db_file,$schema,$new_database_name,$new_schema_name) { $this->parser = xml_parser_create(); xml_set_object($this->parser,$this); xml_set_element_handler($this->parser,"startHandler","endHandler"); xml_set_character_data_handler($this->parser,"cDataHandler"); $sl = new schema_loader(); $this->cur_group = $sl->create_group_structure($schema,$db_file); $sl->write_schema($new_schema_name); $this->head_tag = $this->cur_group->name; $this->out = fopen($new_database_name,"w"); fwrite($this->out,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); fwrite($this->out, "<database>\n"); $in = fopen($db_file,"r"); while($line = fgets($in)) { xml_parse($this->parser,$line,false); } fwrite($this->out,"</database>\n"); fclose($this->out); xml_parser_free($this->parser); } } /* //error_reporting(0); set_time_limit(0); $converter = new flat_to_hierarchy_converter(); $converter->produce_xml_with_hierarchy("nahuatl","stripped.xml","hierarchy.xml", "full.xml","AN_schema.xml"); */ ?>