From 1d1ebdd4c09a7354e16f64a0347e2b0cb0bbc991 Mon Sep 17 00:00:00 2001 From: Michael Wiebusch Date: Mon, 15 Jul 2013 17:48:21 +0200 Subject: [PATCH] put xml operations in separate perl module xmlOperations --- tools/jtageditor.pl | 371 +--------------------------------------- tools/xmlOperations.pm | 379 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 380 insertions(+), 370 deletions(-) create mode 100644 tools/xmlOperations.pm diff --git a/tools/jtageditor.pl b/tools/jtageditor.pl index c569aa6..0b78c20 100755 --- a/tools/jtageditor.pl +++ b/tools/jtageditor.pl @@ -35,6 +35,7 @@ use POSIX; use CGI::Carp qw(fatalsToBrowser); use HTML::Entities; require Common; +require xmlOperations; @@ -107,7 +108,6 @@ if ( $cgiHash{'print'} eq 'settree' ) { integrateAncestry(); - print_ancestorInfo(); print_registers($configFile); @@ -156,376 +156,7 @@ if ( defined $cgiHash{'action'} ) { #################### SUBLAND ###################### -############################### -## xml file operations -############################### - -sub deleteFile { - my $configFileName = $_[0]; - my $configFile = $confDir . "/" . $configFileName; - unless ( -e $configFile ) { - print "

selected file not found

"; - exit; - } - unlink($configFile); - print "

config file was deleted

"; -} -#args:configFileName #globs:$confDir - -sub createConfigFile { - $configFileName = $_[0]; - - $specFileName = $_[1]; #config file is based on this specification! - $configFile = $confDir . "/" . $configFileName; - $specFile = $specDir . "/" . $specFileName; - - $configTree = XML::LibXML->createDocument; - $specTree = $parser->parse_file($specFile); - - my $configMaps = $configTree->createElementNS( "", "MAPS" ); - $configTree->setDocumentElement($configMaps); - - my $specMaps = $specTree->findnodes("/MAPS")->shift(); - - my $mapsType = $specMaps->findvalue("./\@type"); - - $configMaps->setAttribute( "type", $mapsType ); - $configMaps->setAttribute( "specDbFile", $specFileName ); - -} -# args: configFileName,specFileName -# globs:$confDir,$specDir,$parser,$configFile,$specFile,$configFileName,$specFileName -# description: create new configFileTree - -sub parseConfigAndSpec { - parseConfigFile( $_[0] ); - $specFileName = $configTree->findvalue("/MAPS/\@specDbFile"); - $specFile = $specDir . "/" . $specFileName; - unless ( -e $specFile ) { - print -"

specification file \"$specFileName\" could not be found in the specification directory \"$specDir\"

"; - exit; - } - $specTree = $parser->parse_file($specFile); -} -#args: configFileName #globs: $specFileName,$configTree,$specFile,$specDir,$specTree,$parser #calls: parseConfigFile() - -sub parseConfigFile { - $configFileName = $_[0]; - $configFile = $confDir . "/" . $_[0]; - $configTree = $parser->parse_file($configFile); - $configMapsType = $configTree->findvalue("/MAPS/\@type"); -} -#args: configFileName #globs: $confDir,$configTree,$parser,$configFile,$configMapsType,$configFileName - -sub buildAncestry { - # the file for which the ancestry shall be built - my $xmlfileName = $_[0]; - # the file that referred $xmlfileName as its ancestor - # should be "" if you start building the ancestry from the bottom - my $descendantXmlFileName = $_[1]; - - # no target file, no action! - if ( $xmlfileName eq "" ) { - return; - } - - # check if you are not including yourself - if ( $descendantXmlFileName eq $xmlfileName ) { - print "

It's a bad idea to try to include yourself!

"; - print "

The faulty include directive was removed.

"; - print "

"; - changeAncestor(""); # break the evil circle where it was closed! - writeConfigFile(); - exit; - } - - # check against circular dependencies - for (@ancestryList) { # suppres circular dependencies! - if ( $xmlfileName eq $_ ) { - - print "

No circular includes, please!

"; - print "

"; - print $descendantXmlFileName. "->" - . join( "->", @ancestryList ) . "->" - . $configFileName; - print "

"; - print "

The faulty include directive was removed.

"; - print - "

"; - changeAncestor(""); # break the evil circle where it was closed! - writeConfigFile(); - exit; - - #die "no circular includes please! $!"; - return; - } - } - - my $xmlfile = $confDir . "/" . $xmlfileName; - - # check if all files in the ancestry really exist! - unless ( -e $xmlfile ) { - - print "

You are trying to include a file that does not exist

"; - print "

"; - print "" - . $xmlfileName - . "" . "->" - . join( "->", @ancestryList ) . "->" - . $configFileName; - print "

"; - print "

The faulty include directive was removed.

"; - print "

"; - changeAncestor(""); # break the evil circle where it was closed! - writeConfigFile(); - 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 -"

the included config file does not comply with the MAPS type of the current config file

"; - print "

The faulty include directive was removed.

"; - print "

"; - changeAncestor(""); # break the evil circle where it was closed! - writeConfigFile(); - exit; - } - - # compliance checking specDbFile - unless ( $currentSpecFileName eq $specFileName ) { - print -"

the included config file is not based on the same specification file as current config file

"; - print "

The faulty include directive was removed.

"; - print "

"; - changeAncestor(""); # break the evil circle where it was closed! - writeConfigFile(); - 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; - } -} -# args: xmlfileName, -# parentXmlFileName - -# globs: @ancestryList, -# $ancestryTree, -# $configFileName, -# $confDir, -# $configTree, -# $configMapsType, -# %fileLevelHash - -# calls: changeAncestor(), -# mergeTrees() - -# usage: buildAncestry($configFile,""), do not use directly, use integrateAncestry()! -# description: recursively adds the contents of the ancestor files of $configFile to $ancestryTree - - -sub integrateAncestry { - - buildAncestry($configFileName,""); - - my $counter = 1; - for my $file ( reverse(@ancestryList) ) { - $fileLevelHash{$file} = $counter; - if ( $counter < 6 ) { - $counter++; - } - } - - $configTree = $ancestryTree; -} -# globs: @ancestryList,%fileLevelHash,$configFileName,$configTree,$ancestryTree -# description: this function extends $configTree to include all information that is held -# by its ancestor (and ancestor's ancestor ... and so on) - -sub changeAncestor { - my $ancestorFileName = $_[0]; - my $xmltree = $configTree; - my $xmlfile = $configFile; - my $maps = $xmltree->findnodes("/MAPS")->shift(); - - $maps->setAttribute( "inheritSettingsFrom", $ancestorFileName ); - -# open( SCHREIBEN, "> $xmlfile" ) -# or print "could not open file $xmlfile for writing: $!\n"; -# -# print SCHREIBEN $xmltree->toString(); -# close SCHREIBEN; - -} -# args: ancestorFileName -# globs: $configTree,$configFile -# description: change the "inheritSettingsFrom" tag in the current configTree to "ancestorFileName" -# make change permanent by calling writeConfigFile() hereafter. - -sub del { - - my $registerName = $_[0]; - my $fieldName = $_[1]; - my $xmlfile = $configFile; - my $xmltree = $configTree; - my $maps = $xmltree->findnodes("/MAPS")->shift(); - my $register = - $xmltree->findnodes( "/MAPS/register[\@name='" . $registerName . "']" ) - ->shift(); - - if ( $fieldName eq "" ) { # no field specified, remove whole register - unless ( $register eq "" ) { - $maps->removeChild($register); - } - print "deleted whole register"; - } - else { - - my $field = - $xmltree->findnodes( "/MAPS/register[\@name='" - . $registerName - . "']/field[\@name='" - . $fieldName - . "']" )->shift(); - $register->removeChild($field); - print "deleted field
"; - unless ( $register->hasChildNodes() ) { - $maps->removeChild($register); - print "deleted register as well
"; - } - } -# open( SCHREIBEN, "> $xmlfile" ) -# or print "could not open file $xmlfile for writing: $!\n"; -# -# print SCHREIBEN $xmltree->toString(); -# close SCHREIBEN; -} -# args: registerName,fieldName, -# globs: $configFile,$configTree -# description: deletes field "fieldName" in register "registerName" in $configTree -# make change permanent by calling writeConfigFile() hereafter. - - -sub save { - - my $registerName = $_[0]; - my $fieldName = $_[1]; - my $xmlfile = $configFile; - my $newValue = $_[2]; - - my $xmltree = $configTree; - my $maps = $xmltree->findnodes("/MAPS")->shift(); - my $register = - $xmltree->findnodes( "/MAPS/register[\@name='" . $registerName . "']" ) - ->shift(); - - if ( $register eq "" ) { - $register = $maps->addNewChild( "", "register" ); - $register->setAttribute( "name", $registerName ); - } - - my $field = - $xmltree->findnodes( "/MAPS/register[\@name='" - . $registerName - . "']/field[\@name='" - . $fieldName - . "']" )->shift(); - - if ( $field eq "" ) { - $field = $register->addNewChild( "", "field" ); - $field->setAttribute( "name", $fieldName ); - - } - - $field->setAttribute( "value", $newValue ); - print $field->findvalue("./\@value"); -# open( SCHREIBEN, "> $xmlfile" ) -# or print "could not open file $xmlfile for writing: $!\n"; -# -# print SCHREIBEN $xmltree->toString(); -# close SCHREIBEN; -} -# args: registerName,fieldName,newValue -# globs: $configFile,$configTree, -# description: saves value "newValue" in field "fieldName" in register "registerName" (in the $configTree) -# if field or register does not exist yet, it is created. -# make change permanent by calling writeConfigFile() hereafter. - -sub copyDefaultRegister { - my $registerName = $_[0]; - #my $configTree = $parser->parse_file($configFile); - #my $specTree = $parser->parse_file($specFile); - my $configmaps = $configTree->findnodes("/MAPS")->shift(); - my $specmaps = $specTree->findnodes("/MAPS")->shift(); - - my $specRegister = - $specTree->findnodes( "/MAPS/register[\@name='" . $registerName . "']" ) - ->shift(); - - my $configRegister = $configmaps->addNewChild( "", "register" ); - $configRegister->setAttribute( "name", $registerName ); - - my @specFields = $specRegister->findnodes("./field"); - - for my $specField (@specFields) { - my $fieldName = $specField->findvalue("./\@name"); - my $fieldValue = $specField->findvalue("./\@defaultValue"); - my $configField = $configRegister->addNewChild( "", "field" ); - $configField->setAttribute( "name", $fieldName ); - $configField->setAttribute( "value", $fieldValue ); - print $configField->findvalue("./\@value"); - } - - #writeConfigFile(); -} -# args: registerName -# globs: $configTree,$specTree,$configFile -# description: copys register "registerName" from $specTree to $configTree -# make change permanent by calling writeConfigFile() hereafter. - -sub writeConfigFile(){ - open( SCHREIBEN, "> $configFile" ) - or print "could not open file $configFile for writing: $!\n"; - - print SCHREIBEN $configTree->toString(); - close SCHREIBEN; -} -# globs: $configFile,$configTree -# description: writes the current $configTree to $configFile ############################### ## subs generating html output diff --git a/tools/xmlOperations.pm b/tools/xmlOperations.pm new file mode 100644 index 0000000..54d6deb --- /dev/null +++ b/tools/xmlOperations.pm @@ -0,0 +1,379 @@ +#!/usr/bin/perl -w + + + +############################### +## xml file operations +############################### + +sub deleteFile { + my $configFileName = $_[0]; + my $configFile = $confDir . "/" . $configFileName; + unless ( -e $configFile ) { + print "

selected file not found

"; + exit; + } + unlink($configFile); + print "

config file was deleted

"; +} +#args:configFileName #globs:$confDir + +sub createConfigFile { + $configFileName = $_[0]; + + $specFileName = $_[1]; #config file is based on this specification! + $configFile = $confDir . "/" . $configFileName; + $specFile = $specDir . "/" . $specFileName; + + $configTree = XML::LibXML->createDocument; + $specTree = $parser->parse_file($specFile); + + my $configMaps = $configTree->createElementNS( "", "MAPS" ); + $configTree->setDocumentElement($configMaps); + + my $specMaps = $specTree->findnodes("/MAPS")->shift(); + + my $mapsType = $specMaps->findvalue("./\@type"); + + $configMaps->setAttribute( "type", $mapsType ); + $configMaps->setAttribute( "specDbFile", $specFileName ); + +} +# args: configFileName,specFileName +# globs:$confDir,$specDir,$parser,$configFile,$specFile,$configFileName,$specFileName +# description: create new configFileTree + +sub parseConfigAndSpec { + parseConfigFile( $_[0] ); + $specFileName = $configTree->findvalue("/MAPS/\@specDbFile"); + $specFile = $specDir . "/" . $specFileName; + unless ( -e $specFile ) { + print +"

specification file \"$specFileName\" could not be found in the specification directory \"$specDir\"

"; + exit; + } + $specTree = $parser->parse_file($specFile); +} +#args: configFileName #globs: $specFileName,$configTree,$specFile,$specDir,$specTree,$parser #calls: parseConfigFile() + +sub parseConfigFile { + $configFileName = $_[0]; + $configFile = $confDir . "/" . $_[0]; + $configTree = $parser->parse_file($configFile); + $configMapsType = $configTree->findvalue("/MAPS/\@type"); +} +#args: configFileName #globs: $confDir,$configTree,$parser,$configFile,$configMapsType,$configFileName + +sub buildAncestry { + # the file for which the ancestry shall be built + my $xmlfileName = $_[0]; + # the file that referred $xmlfileName as its ancestor + # should be "" if you start building the ancestry from the bottom + my $descendantXmlFileName = $_[1]; + + # no target file, no action! + if ( $xmlfileName eq "" ) { + return; + } + + # check if you are not including yourself + if ( $descendantXmlFileName eq $xmlfileName ) { + print "

It's a bad idea to try to include yourself!

"; + print "

The faulty include directive was removed.

"; + print "

"; + changeAncestor(""); # break the evil circle where it was closed! + writeConfigFile(); + exit; + } + + # check against circular dependencies + for (@ancestryList) { # suppres circular dependencies! + if ( $xmlfileName eq $_ ) { + + print "

No circular includes, please!

"; + print "

"; + print $descendantXmlFileName. "->" + . join( "->", @ancestryList ) . "->" + . $configFileName; + print "

"; + print "

The faulty include directive was removed.

"; + print + "

"; + changeAncestor(""); # break the evil circle where it was closed! + writeConfigFile(); + exit; + + #die "no circular includes please! $!"; + return; + } + } + + my $xmlfile = $confDir . "/" . $xmlfileName; + + # check if all files in the ancestry really exist! + unless ( -e $xmlfile ) { + + print "

You are trying to include a file that does not exist

"; + print "

"; + print "" + . $xmlfileName + . "" . "->" + . join( "->", @ancestryList ) . "->" + . $configFileName; + print "

"; + print "

The faulty include directive was removed.

"; + print "

"; + changeAncestor(""); # break the evil circle where it was closed! + writeConfigFile(); + 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 +"

the included config file does not comply with the MAPS type of the current config file

"; + print "

The faulty include directive was removed.

"; + print "

"; + changeAncestor(""); # break the evil circle where it was closed! + writeConfigFile(); + exit; + } + + # compliance checking specDbFile + unless ( $currentSpecFileName eq $specFileName ) { + print +"

the included config file is not based on the same specification file as current config file

"; + print "

The faulty include directive was removed.

"; + print "

"; + changeAncestor(""); # break the evil circle where it was closed! + writeConfigFile(); + 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; + } +} +# args: xmlfileName, +# parentXmlFileName + +# globs: @ancestryList, +# $ancestryTree, +# $configFileName, +# $confDir, +# $configTree, +# $configMapsType, +# %fileLevelHash + +# calls: changeAncestor(), +# mergeTrees() + +# usage: buildAncestry($configFile,""), do not use directly, use integrateAncestry()! +# description: recursively adds the contents of the ancestor files of $configFile to $ancestryTree + + +sub integrateAncestry { + + buildAncestry($configFileName,""); + + my $counter = 1; + for my $file ( reverse(@ancestryList) ) { + $fileLevelHash{$file} = $counter; + if ( $counter < 6 ) { + $counter++; + } + } + + $configTree = $ancestryTree; +} +# globs: @ancestryList,%fileLevelHash,$configFileName,$configTree,$ancestryTree +# description: this function extends $configTree to include all information that is held +# by its ancestor (and ancestor's ancestor ... and so on) + +sub changeAncestor { + my $ancestorFileName = $_[0]; + my $xmltree = $configTree; + my $xmlfile = $configFile; + my $maps = $xmltree->findnodes("/MAPS")->shift(); + + $maps->setAttribute( "inheritSettingsFrom", $ancestorFileName ); + +# open( SCHREIBEN, "> $xmlfile" ) +# or print "could not open file $xmlfile for writing: $!\n"; +# +# print SCHREIBEN $xmltree->toString(); +# close SCHREIBEN; + +} +# args: ancestorFileName +# globs: $configTree,$configFile +# description: change the "inheritSettingsFrom" tag in the current configTree to "ancestorFileName" +# make change permanent by calling writeConfigFile() hereafter. + +sub del { + + my $registerName = $_[0]; + my $fieldName = $_[1]; + my $xmlfile = $configFile; + my $xmltree = $configTree; + my $maps = $xmltree->findnodes("/MAPS")->shift(); + my $register = + $xmltree->findnodes( "/MAPS/register[\@name='" . $registerName . "']" ) + ->shift(); + + if ( $fieldName eq "" ) { # no field specified, remove whole register + unless ( $register eq "" ) { + $maps->removeChild($register); + } + print "deleted whole register"; + } + else { + + my $field = + $xmltree->findnodes( "/MAPS/register[\@name='" + . $registerName + . "']/field[\@name='" + . $fieldName + . "']" )->shift(); + $register->removeChild($field); + print "deleted field
"; + unless ( $register->hasChildNodes() ) { + $maps->removeChild($register); + print "deleted register as well
"; + } + } +# open( SCHREIBEN, "> $xmlfile" ) +# or print "could not open file $xmlfile for writing: $!\n"; +# +# print SCHREIBEN $xmltree->toString(); +# close SCHREIBEN; +} +# args: registerName,fieldName, +# globs: $configFile,$configTree +# description: deletes field "fieldName" in register "registerName" in $configTree +# make change permanent by calling writeConfigFile() hereafter. + + +sub save { + + my $registerName = $_[0]; + my $fieldName = $_[1]; + my $xmlfile = $configFile; + my $newValue = $_[2]; + + my $xmltree = $configTree; + my $maps = $xmltree->findnodes("/MAPS")->shift(); + my $register = + $xmltree->findnodes( "/MAPS/register[\@name='" . $registerName . "']" ) + ->shift(); + + if ( $register eq "" ) { + $register = $maps->addNewChild( "", "register" ); + $register->setAttribute( "name", $registerName ); + } + + my $field = + $xmltree->findnodes( "/MAPS/register[\@name='" + . $registerName + . "']/field[\@name='" + . $fieldName + . "']" )->shift(); + + if ( $field eq "" ) { + $field = $register->addNewChild( "", "field" ); + $field->setAttribute( "name", $fieldName ); + + } + + $field->setAttribute( "value", $newValue ); + print $field->findvalue("./\@value"); +# open( SCHREIBEN, "> $xmlfile" ) +# or print "could not open file $xmlfile for writing: $!\n"; +# +# print SCHREIBEN $xmltree->toString(); +# close SCHREIBEN; +} +# args: registerName,fieldName,newValue +# globs: $configFile,$configTree, +# description: saves value "newValue" in field "fieldName" in register "registerName" (in the $configTree) +# if field or register does not exist yet, it is created. +# make change permanent by calling writeConfigFile() hereafter. + +sub copyDefaultRegister { + my $registerName = $_[0]; + #my $configTree = $parser->parse_file($configFile); + #my $specTree = $parser->parse_file($specFile); + my $configmaps = $configTree->findnodes("/MAPS")->shift(); + my $specmaps = $specTree->findnodes("/MAPS")->shift(); + + my $specRegister = + $specTree->findnodes( "/MAPS/register[\@name='" . $registerName . "']" ) + ->shift(); + + my $configRegister = $configmaps->addNewChild( "", "register" ); + $configRegister->setAttribute( "name", $registerName ); + + my @specFields = $specRegister->findnodes("./field"); + + for my $specField (@specFields) { + my $fieldName = $specField->findvalue("./\@name"); + my $fieldValue = $specField->findvalue("./\@defaultValue"); + my $configField = $configRegister->addNewChild( "", "field" ); + $configField->setAttribute( "name", $fieldName ); + $configField->setAttribute( "value", $fieldValue ); + print $configField->findvalue("./\@value"); + } + + #writeConfigFile(); +} +# args: registerName +# globs: $configTree,$specTree,$configFile +# description: copys register "registerName" from $specTree to $configTree +# make change permanent by calling writeConfigFile() hereafter. + +sub writeConfigFile(){ + open( SCHREIBEN, "> $configFile" ) + or print "could not open file $configFile for writing: $!\n"; + + print SCHREIBEN $configTree->toString(); + close SCHREIBEN; +} +# globs: $configFile,$configTree +# description: writes the current $configTree to $configFile + + +1; + +__END__ -- 2.43.0