/*
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 program
provides this function as Jonathan Amiths database has around 100 fields
at the root node. This avoids the hassle of having to write every
field in the schema.
The schema file is then rewritten to include the children of the root node.
*/
include_once("group.php5");
include_once("fields_loader.php5");
class schema_loader {
public $parser;
public $cur_group;
public $find_fields;
public $get_field;
public $data;
public $id_field;
public $group_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");
$this->cur_group = false;
$this->find_fields = false;
$this->group_fields = array();
}
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);
}
}
/*
$sl = new schema_loader();
$refgroup = $sl->create_group_structure("AN_schema.xml","");
$segroup = $refgroup->groups["segroup"];
$segroup->parent->name = "foo";
echo "segroup's parent's name: $refgroup->name \n";
$pnagroup = $segroup->groups["pnagroup"];
$pnagroup->parent->name = "goo";
echo "pnagroup's parent's name: $segroup->name \n";
*/
?>