def testWfEntDtd(filename, id): global error_nr global error_msg global log error_nr = 0 error_msg = '' ctxt = libxml2.createFileParserCtxt(filename) if ctxt == None: return -1 ctxt.replaceEntities(1) ctxt.loadSubset(1) ret = ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None if doc == None or ret != 0 or ctxt.wellFormed() == 0: print "%s: error: wrongly failed to parse the document" % (id) log.write("%s: error: wrongly failed to parse the document\n" % (id)) if doc != None: doc.freeDoc() return 0 if error_nr != 0: print "%s: warning: WF document generated an error msg" % (id) log.write("%s: error: WF document generated an error msg\n" % (id)) doc.freeDoc() return 2 doc.freeDoc() return 1
def testError(filename, id): global error_nr global error_msg global log error_nr = 0 error_msg = '' ctxt = libxml2.createFileParserCtxt(filename) if ctxt is None: return -1 ctxt.replaceEntities(1) ctxt.loadSubset(1) ret = ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None if doc is not None: doc.freeDoc() if ctxt.wellFormed() == 0: print "%s: warning: failed to parse the document but accepted" % (id) log.write("%s: warning: failed to parse the document but accepte\n" % (id)) return 2 if error_nr != 0: print "%s: warning: WF document generated an error msg" % (id) log.write("%s: error: WF document generated an error msg\n" % (id)) return 2 return 1
def __init__(self, filename, base_path, app): self.filename = filename self.app = app self.expand_entities = self.app.options.get('expand_entities') self.ignored_tags = self.app.current_mode.getIgnoredTags() # Remove the part of the path that redirects from build dir to # source dir since that part depends on personal setup and can # cause a lot of unnecessary changes in commits. self.base_path = base_path self.source_filename = self.filename if self.source_filename.startswith(self.base_path): self.source_filename = self.source_filename[len(self.base_path):] ctxt = libxml2.createFileParserCtxt(filename) ctxt.lineNumbers(1) if self.app.options.get('expand_all_entities'): ctxt.replaceEntities(1) try: ctxt.parseDocument() except Exception as e: print("Error parsing XML file '%s': %s" % (filename, str(e)), file=sys.stderr) sys.exit(1) self.doc = ctxt.doc() if self.doc.name != filename: raise Exception( "Error: I tried to open '%s' but got '%s' -- how did that happen?" % (filename, self.doc.name)) if self.app.msg: self.app.msg.setFilename(self.source_filename) self.isFinalNode = self.app.current_mode.isFinalNode
def testNotWfEnt(filename, id): global error_nr global error_msg global log error_nr = 0 error_msg = '' ctxt = libxml2.createFileParserCtxt(filename) if ctxt is None: return -1 ctxt.replaceEntities(1) ret = ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None if doc is not None: doc.freeDoc() if ret == 0 or ctxt.wellFormed() != 0: print "%s: error: Well Formedness error not detected" % (id) log.write("%s: error: Well Formedness error not detected\n" % (id)) return 0 return 1
def __init__(self, config_dir, data_dir): self.doc = None self.config_dir = config_dir self.data_dir = data_dir os.chdir(data_dir) libxml2.initializeCatalog() libxml2.loadCatalog(os.path.join(data_dir, "catalog.xml")) parser = libxml2.createFileParserCtxt( os.path.join(config_dir, "jjigw.xml")) parser.validate(1) parser.parseDocument() if not parser.isValid(): raise JJIGWFatalError, "Invalid configuration" self.doc = parser.doc() self.connect = ConnectConfig(self.doc.xpathEval("jjigw/connect")[0]) self.jid = None self.networks = {} for n in self.doc.xpathEval("jjigw/network"): network = NetworkConfig(n) if not self.jid: self.jid = network.jid self.networks[network.jid.domain] = network spidentd = self.doc.xpathEval("jjigw/spidentd") if spidentd: self.spidentd = SPIdentDConfig(spidentd[0]) else: self.spidentd = None self.admins = [] for n in self.doc.xpathEval("jjigw/admin"): self.admins.append(JID(n.getContent()))
def testNotWfEntDtd(filename, id): global error_nr global error_msg global log error_nr = 0 error_msg = '' ctxt = libxml2.createFileParserCtxt(filename) if ctxt == None: return -1 ctxt.replaceEntities(1) ctxt.loadSubset(1) ret = ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None if doc != None: doc.freeDoc() if ret == 0 or ctxt.wellFormed() != 0: print("%s: error: Well Formedness error not detected" % (id)) log.write("%s: error: Well Formedness error not detected\n" % (id)) return 0 return 1
def testError(filename, id): global error_nr global error_msg global log error_nr = 0 error_msg = '' ctxt = libxml2.createFileParserCtxt(filename) if ctxt == None: return -1 ctxt.replaceEntities(1) ctxt.loadSubset(1) ret = ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None if doc != None: doc.freeDoc() if ctxt.wellFormed() == 0: print("%s: warning: failed to parse the document but accepted" % (id)) log.write("%s: warning: failed to parse the document but accepte\n" % (id)) return 2 if error_nr != 0: print("%s: warning: WF document generated an error msg" % (id)) log.write("%s: error: WF document generated an error msg\n" % (id)) return 2 return 1
def testValid(filename, id): global error_nr global error_msg error_nr = 0 error_msg = '' ctxt = libxml2.createFileParserCtxt(filename) if ctxt == None: return -1 ctxt.validate(1) ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None valid = ctxt.isValid() if doc == None: print("%s: error: wrongly failed to parse the document" % (id)) log.write("%s: error: wrongly failed to parse the document\n" % (id)) return 0 if valid != 1: print("%s: error: Validity check failed" % (id)) log.write("%s: error: Validity check failed\n" % (id)) doc.freeDoc() return 0 if error_nr != 0 or valid != 1: print("%s: warning: valid document reported an error" % (id)) log.write("%s: warning: valid document reported an error\n" % (id)) doc.freeDoc() return 2 doc.freeDoc() return 1
def testValid(filename, id): global error_nr global error_msg error_nr = 0 error_msg = '' ctxt = libxml2.createFileParserCtxt(filename) if ctxt is None: return -1 ctxt.validate(1) ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None valid = ctxt.isValid() if doc is None: print "%s: error: wrongly failed to parse the document" % (id) log.write("%s: error: wrongly failed to parse the document\n" % (id)) return 0 if valid != 1: print "%s: error: Validity check failed" % (id) log.write("%s: error: Validity check failed\n" % (id)) doc.freeDoc() return 0 if error_nr != 0 or valid != 1: print "%s: warning: valid document reported an error" % (id) log.write("%s: warning: valid document reported an error\n" % (id)) doc.freeDoc() return 2 doc.freeDoc() return 1
def __init__(self,config_dir,data_dir): self.doc=None self.config_dir=config_dir self.data_dir=data_dir os.chdir(data_dir) libxml2.initializeCatalog() libxml2.loadCatalog(os.path.join(data_dir,"catalog.xml")) parser=libxml2.createFileParserCtxt(os.path.join(config_dir,"jjigw.xml")) parser.validate(1) parser.parseDocument() if not parser.isValid(): raise JJIGWFatalError,"Invalid configuration" self.doc=parser.doc() self.connect=ConnectConfig(self.doc.xpathEval("jjigw/connect")[0]) self.jid=None self.networks={} for n in self.doc.xpathEval("jjigw/network"): network=NetworkConfig(n) if not self.jid: self.jid=network.jid self.networks[network.jid.domain]=network spidentd=self.doc.xpathEval("jjigw/spidentd") if spidentd: self.spidentd=SPIdentDConfig(spidentd[0]) else: self.spidentd=None self.admins=[] for n in self.doc.xpathEval("jjigw/admin"): self.admins.append(JID(n.getContent()))
def main(filename, removetag, copyfrom, copyto): try: ctxt = libxml2.createFileParserCtxt(filename) ctxt.lineNumbers(1) #ctxt.replaceEntities(1) ctxt.parseDocument() doc = ctxt.doc() if doc.name != filename: print >> sys.stderr, "Error: I tried to open '%s' but got '%s' -- how did that happen?" % (filename, doc.name) sys.exit(4) except: print >> sys.stderr, "Error: cannot open file '%s'." % (filename) sys.exit(1) root = doc.getRootElement() # <translation-status> # go through all releases and modules node = root.children while node: next = node.next if node.name == 'release': tag = node.prop("name") if tag == removetag: node.unlinkNode() node.freeNode() elif tag == copyfrom: newnode = node.copyNodeList() newnode.setProp("name", copyto) node.addNextSibling(newnode) elif node.name == 'module': child = node.children while child: vnext = child.next if child.name=="version": rel = child.prop("release") if rel == removetag: child.unlinkNode() child.freeNode() elif rel == copyfrom: newnode = child.copyNodeList() newnode.setProp("release", copyto) find = newnode.children while find: if find.name == "component": findbranch = find.children while findbranch: if findbranch.name == "branch": findbranch.setProp("name", "HEAD") findbranch = findbranch.next find = find.next child.addNextSibling(newnode) child = vnext node = next print doc.serialize('utf-8')
def __init__(self,config_file): self.doc=None self.config_file=config_file parser=libxml2.createFileParserCtxt(os.path.join(config_file)) parser.validate(0) parser.parseDocument() if not parser.isValid(): raise FatalError,"Invalid configuration" self.doc=parser.doc() self.source=ConnectionConfig(self.doc.xpathEval("xdbcopy/source")[0]) self.destination=ConnectionConfig(self.doc.xpathEval("xdbcopy/destination")[0]) self.ns=NSConfig(self.doc.xpathEval("xdbcopy/xdbns")[0])
def loadNoentDoc(filename): ctxt = libxml2.createFileParserCtxt(filename) if ctxt is None: return None ctxt.replaceEntities(1) ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None if ctxt.wellFormed() != 1: doc.freeDoc() return None return doc
def loadNoentDoc(filename): ctxt = libxml2.createFileParserCtxt(filename) if ctxt == None: return None ctxt.replaceEntities(1) ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None if ctxt.wellFormed() != 1: doc.freeDoc() return None return doc
def validate(self, fn): """ Validate XML file. XML file has to be file with specified XML document type. @param fn: XML filename. """ ctxt = libxml2.createFileParserCtxt(fn) ctxt.validate(1) ctxt.parseDocument() doc = ctxt.doc() doc.freeDoc() valid = ctxt.isValid() != 0 del ctxt return valid
def __init__(self, filename, app): self.app = app self.expand_entities = self.app.options.get('expand_entities') self.ignored_tags = self.app.current_mode.getIgnoredTags() ctxt = libxml2.createFileParserCtxt(filename) ctxt.lineNumbers(1) if self.app.options.get('expand_all_entities'): ctxt.replaceEntities(1) ctxt.parseDocument() self.doc = ctxt.doc() if self.doc.name != filename: raise Exception( "Error: I tried to open '%s' but got '%s' -- how did that happen?" % (filename, self.doc.name)) if self.app.msg: self.app.msg.setFilename(filename) self.isFinalNode = self.app.current_mode.isFinalNode
def formatXML(self, fn): """ Format XML file. @param fn: XML filename. """ ctxt = libxml2.createFileParserCtxt(fn) ctxt.parseDocument() doc = ctxt.doc() # save formatted file f = open(fn, 'w') buf = libxml2.createOutputBuffer(f, 'UTF-8') doc.saveFormatFileTo(buf, 'UTF-8', 1) f.close() doc.freeDoc() del ctxt
def testNotWf(filename, id): global error_nr global error_msg global log error_nr = 0 error_msg = '' ctxt = libxml2.createFileParserCtxt(filename) if ctxt == None: return -1 ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None if error_nr == 0 or ctxt.wellFormed() != 0: print "%s: error: Well Formedness error not detected" % (id) log.write("%s: error: Well Formedness error not detected\n" % (id)) doc.freeDoc() return 0 return 1
def testInvalid(filename, id): global error_nr global error_msg global log error_nr = 0 error_msg = '' ctxt = libxml2.createFileParserCtxt(filename) if ctxt == None: return -1 ctxt.validate(1) ret = ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None valid = ctxt.isValid() if doc == None: print("%s: error: wrongly failed to parse the document" % (id)) log.write("%s: error: wrongly failed to parse the document\n" % (id)) return 0 if valid == 1: print("%s: error: Validity error not detected" % (id)) log.write("%s: error: Validity error not detected\n" % (id)) doc.freeDoc() return 0 if error_nr == 0: print("%s: warning: Validity error not reported" % (id)) log.write("%s: warning: Validity error not reported\n" % (id)) doc.freeDoc() return 2 doc.freeDoc() return 1
def testInvalid(filename, id): global error_nr global error_msg global log error_nr = 0 error_msg = '' ctxt = libxml2.createFileParserCtxt(filename) if ctxt is None: return -1 ctxt.validate(1) ret = ctxt.parseDocument() try: doc = ctxt.doc() except: doc = None valid = ctxt.isValid() if doc is None: print "%s: error: wrongly failed to parse the document" % (id) log.write("%s: error: wrongly failed to parse the document\n" % (id)) return 0 if valid == 1: print "%s: error: Validity error not detected" % (id) log.write("%s: error: Validity error not detected\n" % (id)) doc.freeDoc() return 0 if error_nr == 0: print "%s: warning: Validity error not reported" % (id) log.write("%s: warning: Validity error not reported\n" % (id)) doc.freeDoc() return 2 doc.freeDoc() return 1
#!/usr/bin/python -u import sys import libxml2 # Memory debug specific libxml2.debugMemory(1) ctxt = libxml2.createFileParserCtxt("valid.xml") ctxt.validate(1) ctxt.parseDocument() doc = ctxt.doc() valid = ctxt.isValid() if doc.name != "valid.xml": print "doc.name failed" sys.exit(1) root = doc.children if root.name != "doc": print "root.name failed" sys.exit(1) if valid != 1: print "validity chec failed" sys.exit(1) doc.freeDoc() i = 1000 while i > 0: ctxt = libxml2.createFileParserCtxt("valid.xml") ctxt.validate(1) ctxt.parseDocument() doc = ctxt.doc()
# I'm not particularly happy about making any of these global, # but I don't want to bother too much with it right now semitrans = {} PlaceHolder = 0 if origxml == '': msg = MessageOutput() else: filenames.append(origxml) msg = MessageOutput(1) for filename in filenames: try: if filename == origxml: msg.translationsFollow() ctxt = libxml2.createFileParserCtxt(filename) ctxt.lineNumbers(1) if expand_all_entities: ctxt.replaceEntities(1) ctxt.parseDocument() doc = ctxt.doc() if doc.name != filename: print >> sys.stderr, "Error: I tried to open '%s' but got '%s' -- how did that happen?" % (filename, doc.name) sys.exit(4) except: print >> sys.stderr, "Error: cannot open file '%s'." % (filename) sys.exit(1) msg.setFilename(filename) if CurrentXmlMode and origxml=='': CurrentXmlMode.preProcessXml(doc,msg)
def parseDoc(filename): parser = libxml2.createFileParserCtxt(filename) parser.ctxtUseOptions(libxml2.XML_PARSE_NSCLEAN | libxml2.XML_PARSE_DTDLOAD & ~libxml2.XML_PARSE_DTDATTR) parser.parseDocument() return parser.doc()
def __init__(self, filename, *hooks): """ Given a filename and a list of hooks, parse the file (using libxml2) and generates the associated Xot structure. """ apply_all_hooks = False if len(hooks) == 1 and hooks[0] == ':all:': apply_all_hooks = True self.hooks = {'xml': {}, 'sql': {}, 'dot': {}, 'xot': {}, 'ref': {}, 'key': {}, } self.filename = filename self.table_groups = {} ctxt = libxml2.createFileParserCtxt(filename) ctxt.pedantic(True) #ctxt.validate(True) ctxt.parseDocument() if not ctxt.isValid(): sys.exit(1) doc = ctxt.doc() # read in the hooks for hook_def in doc.xpathEval('/database/hook_defs/hook_def'): name = hook_def.prop('name') for hook_impl in hook_def.xpathEval('hook_impl'): target = hook_impl.prop('target') assert target in self.hooks hook = Hook.new(self, name, target, hook_impl) self.hooks[target].setdefault(name, []).append(hook) # the xot now has to restructure the xml slightly: all tables # must go in table groups, so that transformations brought on # by xml hooks that split individual tables into several (to # implement missing features in the db engine, for example) # don't wreak havoc with (symbolic) references other tables # might have tables = doc.xpathEval('/database/tables')[0] for table in tables.xpathEval('table'): name = table.prop('name') parent = table.prop('inherits') tg = libxml2.newNode('table_group') tg.setProp('name', name) if parent: tg.setProp('inherits', parent) table.replaceNode(tg) tg.addChild(table) h = table.xpathEval('hooks') if (h): table.addNextSibling(h[0]) # done; now do the xml hooks requested (i.e. listed as # arguments to the constructor) g=globals() l=locals() for tg in tables.xpathEval('table_group'): l['self'] = tg for hook in tg.xpathEval('hooks/hook'): name = hook.prop('name') if self.hooks['xml'].has_key(name) and \ (apply_all_hooks or name in hooks): for i in self.hooks['xml'][name]: try: i.sub(g, l) except Exception, err: print "while hooking table-group %s:" % tg.prop('name') raise