+'''
+Created on 02.08.2019
+
+@author: mdoc
+'''
+
+from mutil.XmlExtractor import XmlExtractor
+from mutil import XmlExtractor as XMLE
+import re
+
+def outfirst(outfile, tpllayer, attrs, value):
+ if tpllayer["value"]:
+ ostr = tpllayer["value"]
+ if "$value" in ostr:
+ ostr = re.sub(r"\$value", value, ostr)
+ print (ostr, end="", file=outfile)
+
+def outlast(outfile, tpllayer, attrs, value):
+ if tpllayer["value"]:
+ print (file=outfile)
+
+def process_file_layer(source, outfile, globals, hierarc):
+
+
+ (ctl,elem, value, attrs) = source.extractElement(XMLE.EC_BEG)
+ while ctl != XMLE.EC_END:
+ item = None
+ nexthiera = {}
+ if elem in hierarc:
+ item = hierarc[elem]
+ nexthiera = hierarc[elem]["items"]
+ elif elem in globals:
+ item = globals[elem]
+ else:
+ print ("Warning: No mapping for item:" +elem)
+ if item:
+ outfirst(outfile, item, attrs, value)
+ process_file_layer(source, outfile, globals, nexthiera)
+ if item:
+ outlast(outfile, item, attrs, value)
+ (ctl,elem, value, attrs) = source.extractElement(ctl)
+
+def convert (file, format):
+
+ source = XmlExtractor()
+ source.openInput(file)
+
+ # read whole template
+ tple = XmlExtractor()
+ tpl = tple.readFile("tpl/"+format+".tpl")
+ globals = tpl["mdoc"]["items"]["global"]["items"]
+ hierarc = tpl["mdoc"]["items"]["hierarchical"]["items"]
+
+ # open output file
+ ext = tpl["mdoc"]["attrs"]["extension"]
+ outname = re.sub(r"\.xml", "."+ext, file)
+ outfile = open(outname, "w")
+ #print ("hallo", file=outfile)
+
+ process_file_layer(source, outfile, globals, hierarc)
+