def verify(self, value): if self._is_enumeration: if value not in self.a_type: raise POM.ValidationError( "Enumeration has wrong value for %r. %r is not one of %r." % (self.name, value, self.a_type)) elif self.a_decl == FIXED: if value != self.default: raise POM.ValidationError( "Bad value for FIXED attrib for %r. %r must be %r." % (self.name, value, self.default)) return True
def startElement(self, name, atts): "Handle an event for the beginning of an element." try: klass = self._get_class(name) except AttributeError: raise POM.ValidationError("Undefined element tag: " + name) attr = {} for name, value in atts.items(): attr[keyword_identifier( POM.normalize_unicode(name))] = POM.unescape(value) obj = klass(**attr) self.stack.append(obj)
def endDocument(self): if self.stack: # stack should be empty now raise POM.ValidationError("unbalanced document!") if self.doc is None: self.doc = self._doc_factory(encoding=self.encoding) root = self.msg self.msg = None # Parser strips out namespaces, have to add them back in. if self._prefixes: for uri, prefix in self._prefixes.items(): root.set_attribute(u"xmlns:%s" % prefix, uri) self.doc.set_root(root)
def resolveEntity(self, publicId, systemId): for modname, doctype in dtds.DOCTYPES.items(): if doctype.public == publicId: if self.doc is None: self.doc = self._doc_factory(doctype=modname, encoding=self.encoding) else: self.doc.set_doctype(modname) break else: raise POM.ValidationError("unknown DOCTYPE: %r" % (publicId, )) # Have to fake a file-like object for the XML parser to not # actually get an external entity. return FakeFile(systemId)
def verify(self, value): raise POM.ValidationError("Can't validate unknown attribute: %r" % (self.name, ))