/*
This class reads an xml file describing the schema of an xml file and
creates an group data structure representing that schema. It allows the
user the option of either providing a list of the children of the root
node or letting the program automatically search for them. The schema
file is then rewritten to include the children of the root node.
*/
include_once("group.php5");
include_once("fields_loader.php5");
class hierarchy_loader {
public $parser;
public $cur_group = false;
public $find_fields;
public $get_field;
public $data;
public $id_field;
public $group_fields = array();
public $cur_count;
public $fields;
function __construct()
{
$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");
}
function startHandler($xp, $element, $attribs)
{
$element = strtolower($element);
if($element == "group") {
$name = $attribs["NAME"];
if(!$this->cur_group) {
$parent = false;
$this->cur_group = new group($name,$parent);
}
else {
$parent = $this->cur_group;
unset($this->cur_group);
}
if(array_key_exists("FIND_FIELDS",$attribs)) {
$find_fields = trim($attribs["FIND_FIELDS"]);
if($find_fields == "true") {
$this->find_fields = true;
$this->id_field = $name;
}
else {
$this->find_fields = false;
}
}
$this->cur_group = new group($name, $parent);
//echo " ".$this->cur_group->name."
";
}
else {
$this->get_field = true;
$this->data = "";
if(array_key_exists("COUNT",$attribs)){
$this->count = trim($attribs["COUNT"]);
}
else {
$this->count = 1;
}
}
}
function endHandler($xp, $element)
{
$element = strtolower($element);
if($element == "group") {
//echo "moving up from ".$this->cur_group->name." to";
$child = $this->cur_group;
if($child->parent === false) {
//echo " the root
";
}
else {
unset($this->cur_group);
$this->cur_group = $child->parent;
$this->cur_group->add_group($child);
//echo " ".$this->cur_group->name."
";
}
}
else if($this->get_field) {
$this->cur_group->add_field($this->data,$this->count);
if($this->find_fields) {
if(!in_array($this->data,$this->group_fields)) {
$this->group_fields[$this->data] = $this->cur_group->name;
}
}
$this->get_field = false;
$this->count = 1;
}
}
function cDataHandler($xp, $data)
{
if($this->get_field) {
$this->data .= trim($data);
}
}
function create_group_structure($hierarchy_xml,$lexicon_xml)
{
set_time_limit(1000);
//$start = date("r")."
";
$in = fopen($hierarchy_xml,"r");
$xml = "";
while($line = fgets($in)) {
$xml = ereg_replace("&","&",$line);
$good_parse = xml_parse($this->parser,$xml,false);
if(!$good_parse) {
echo "BAD PARSE: ".xml_get_current_line_number($this->parser)."
";
}
}
if($this->find_fields) {
$fl = new fields_loader($lexicon_xml,$this->id_field,$this->group_fields);
$fields = $fl->get_root_fields();
$this->cur_group->fields = $fields;
}
return $this->cur_group;
}
function write_schema($filename) {
$out = fopen($filename,"w");
fwrite($out,$this->cur_group->structure_to_xml());
fclose($out);
}
}
/*
$hl = new hierarchy_loader();
$g = $hl->create_group_structure("new_hierarchy.xml","test.xml");
echo $g->structure_to_xml();
*/
?>