#use bignum qw/hex/;
require Common;
+require xmlOperations;
-
+##################################
# paste a lot of stuff/global variables from cgitest.pl
-my $parser = XML::LibXML->new();
-# defined earlier
-my $specFile = "";
-#my $configFile = "";
-my $specFileName = "";
-my $configFileName = "";
-my $specTree;
-my $configTree;
-my $configMapsType;
-
-my %fileLevelHash;
+# now obsolete because we use xmlOperations now!
+
+# my $parser = XML::LibXML->new();
+# # defined earlier
+# my $specFile = "";
+# #my $configFile = "";
+# my $specFileName = "";
+my $configFileName = ""; #don't get it confused with $configFileName in xmlOperations.pm
+# my $specTree;
+# my $configTree;
+# my $configMapsType;
+#
+# my %fileLevelHash;
+#
+# my $ancestryTree;
+# my @ancestryList;
-my $ancestryTree;
-my @ancestryList;
+###############################
-my $confDir = '../config'; # default, gets overwritten anyway
-my $specDir = '../specs';
+our $confDir = '../config'; # default, gets overwritten anyway
+our $specDir = '../specs';
# separate configFileName and the containing directory path
my @configFilePath = split("/",$configFile);
# parse config and spec file
parseConfigAndSpec( $configFileName );
# parse and integrate all ancestors of configFile into configTree
-buildAncestry( $configFileName, "" );
-$configTree = $ancestryTree; # not so elegant, but works
+# buildAncestry( $configFileName, "" ); # obsolete use integrateAncestry() now
+# $configTree = $ancestryTree; # not so elegant, but works
+
+integrateAncestry();
+
+# now we will need to copy some variables from xmlOperations to here
+ $configFile = getConfigFile();
+ my $specFile = getSpecFile();
+ $configFileName = getConfigFileName();
+ my $specFileName = getSpecFileName();
+ my $configTree = getConfigTree();
+ my $specTree = getSpecTree();
print VERBOSE "specFileName: $specFileName\n specDir: $specDir\n";
-###### all my SUBS ###########
-
-sub parseConfigAndSpec {
- parseConfig( $_[0] );
- $specFileName = $configTree->findvalue("/MAPS/\@specDbFile");
- $specFile = $specDir . "/" . $specFileName;
- unless ( -e $specFile ) {
- print
-"<p>specification file \"$specFileName\" could not be found in the specification directory \"$specDir\"</p>";
- exit;
- }
- $specTree = $parser->parse_file($specFile);
-}
-
-sub parseConfig {
- $configFileName = $_[0];
- $configFile = $confDir . "/" . $_[0];
- $configTree = $parser->parse_file($configFile);
- $configMapsType = $configTree->findvalue("/MAPS/\@type");
-}
-
-sub buildAncestry { # recursive
- my $xmlfileName = $_[0];
- my $parentXmlFileName = $_[1];
- if ( $xmlfileName eq "" ) {
- return;
- }
-
- # check if you are not including yourself
- if ( $parentXmlFileName eq $xmlfileName ) {
- print "<p>It's a bad idea to try to include yourself!</p>";
- print "<p>The faulty include directive was removed.</p>";
- print "<p><input type='button' onclick='loadFile()' value='back'></p>";
- changeAncestor(""); # break the evil circle where it was closed!
- exit;
- }
-
- # check against circular dependencies
- for (@ancestryList) { # suppres circular dependencies!
- if ( $xmlfileName eq $_ ) {
-
- print "<p>No circular includes, please!</p>";
- print "<p>";
- print $parentXmlFileName. "->"
- . join( "->", @ancestryList ) . "->"
- . $configFileName;
- print "</p>";
- print "<p>The faulty include directive was removed.</p>";
- print
- "<p><input type='button' onclick='loadFile()' value='back'></p>";
- changeAncestor(""); # break the evil circle where it was closed!
- exit;
-
- #die "no circular includes please! $!";
- return;
- }
- }
-
- my $xmlfile = $confDir . "/" . $xmlfileName;
-
- # check if all files in the ancestry really exist!
- unless ( -e $xmlfile ) {
-
- print "<p>You are trying to include a file that does not exist</p>";
- print "<p>";
- print "<strike>"
- . $xmlfileName
- . "</strike>" . "->"
- . join( "->", @ancestryList ) . "->"
- . $configFileName;
- print "</p>";
- print "<p>The faulty include directive was removed.</p>";
- print "<p><input type='button' onclick='loadFile()' value='back'></p>";
- changeAncestor(""); # break the evil circle where it was closed!
- exit;
- return;
- }
-
- my $xmltree;
- if ( $xmlfile eq $configFile ) {
- $xmltree = $configTree;
- }
- else {
-
- # we got an ancestor here, write him to the ancestor list
- unshift( @ancestryList, $xmlfileName );
- $xmltree = $parser->parse_file($xmlfile);
-
- # tag all the fields with an heritage attribute
- for my $field ( $xmltree->findnodes("//field") ) {
- $field->setAttribute( "isHeritageFrom", "$xmlfileName" );
- }
- }
-
- my $ancestorFileName = $xmltree->findvalue("/MAPS/\@inheritSettingsFrom")
- || "";
- my $currentMapsType = $xmltree->findvalue("/MAPS/\@type");
- my $currentSpecFileName = $xmltree->findvalue("/MAPS/\@specDbFile");
-
- # compliance checking MAPS type
- unless ( $currentMapsType eq $configMapsType ) {
- print
-"<p>the included config file does not comply with the MAPS type of the current config file</p>";
- print "<p>The faulty include directive was removed.</p>";
- print "<p><input type='button' onclick='loadFile()' value='back'></p>";
- changeAncestor(""); # break the evil circle where it was closed!
- exit;
- }
-
- # compliance checking specDbFile
- unless ( $currentSpecFileName eq $specFileName ) {
- print
-"<p>the included config file is not based on the same specification file as current config file</p>";
- print "<p>The faulty include directive was removed.</p>";
- print "<p><input type='button' onclick='loadFile()' value='back'></p>";
- changeAncestor(""); # break the evil circle where it was closed!
- exit;
- }
- unless ( $ancestorFileName eq "" ) {
-
- # this block is executed when an ancestor is found
- buildAncestry( $ancestorFileName, $xmlfileName )
- ; # recursion, second argument is the target file from THIS parent recursion call
-
- #integrate the current tree into the ancestryTree, overwrite older settings
- mergeTrees( $ancestryTree, $xmltree );
- }
- else {
-
-# this block is called when you are at the root of the ancestry -> you are THE Father
-# begin the ancestry tree
-
- $ancestryTree = $xmltree;
- }
-
- my $counter = 1;
- for my $file ( reverse(@ancestryList) ) {
- $fileLevelHash{$file} = $counter;
- if ( $counter < 6 ) {
- $counter++;
- }
- }
-
-}
-
-sub mergeTrees
-{ # give me two trees, tree1 overwrites everything in tree0 and will consequently be integrated in tree0
- my $tree0 = $_[0];
- my $tree1 = $_[1];
- my $maps0 = $tree0->findnodes("/MAPS")->shift();
- my $maps1 = $tree1->findnodes("/MAPS")->shift();
-
- my @registers0 = $tree0->findnodes("/MAPS/register");
- my @registers1 = $tree1->findnodes("/MAPS/register");
-
- for my $register1 (@registers1) {
- my $registerName = $register1->findvalue("./\@name");
- if ( $registerName eq "" ) {
- die "registerName in tree1 undefined!";
- }
-
- # if current register does not exist in tree0, create it
- my $register0 =
- $maps0->findnodes( "./register[\@name='" . $registerName . "']" )
- ->shift();
- unless( defined($register0) ) {
- $register0 = $maps0->addNewChild( "", "register" );
- $register0->setAttribute( "name", $registerName );
- }
-
- my @fields1 = $register1->findnodes("./field");
- for my $field1 (@fields1) {
- my $fieldName = $field1->findvalue("./\@name");
-
- # if field with same name exists in tree0/register0 delete it!
- my $field0 =
- $register0->findnodes( "./field[\@name='" . $fieldName . "']" )
- ->shift();
- if ( defined($field0) ) {
- $register0->removeChild($field0);
- }
-
- # copy current field from tree1/register1 to tree0/register0
-
- $register0->addChild($field1);
- }
-
- }
-}
###############################
#### Feierabend!