def _parse(self): parser = etree.XMLParser() data = StringIO(self._xml_data) try: self._xml_doc = etree.parse(data, parser) except Exception, e: raise InvalidObjectError("Invalid XML data. (%s)" % str(e))
class XmlTreeDoc(XmlDoc): """ XML document using lxml's element tree parser. """ implements(IXmlTreeDoc) def __init__(self, xml_data=None, blocking=False): XmlDoc.__init__(self) self.errors = list() self.options = { 'blocking': blocking, } if isinstance(xml_data, basestring): self._xml_data = xml_data else: raise InvalidObjectError("No xml data str was given: %s" % xml_data) self._parse() def _parse(self): parser = etree.XMLParser() data = StringIO(self._xml_data) try: self._xml_doc = etree.parse(data, parser) except Exception, e: raise InvalidObjectError("Invalid XML data. (%s)" % str(e)) self.errors = parser.error_log if self.options['blocking'] and len(self.errors) > 0: raise InvalidObjectError(self.errors) return True
def __init__(self, xml_data=None, blocking=False): XmlDoc.__init__(self) self.errors = list() self.options = { 'blocking': blocking, } if isinstance(xml_data, basestring): self._xml_data = xml_data else: raise InvalidObjectError("No xml data str was given: %s" % xml_data) self._parse()
def deleteIndex(self, xmlindex): """ Delete an XMLIndex and all related indexed data from the catalog. """ if not xmlindex._id: msg = "DeleteIndex: an XmlIndex has no id." raise InvalidObjectError(msg) resourcetype = xmlindex.resourcetype self.flushIndex(xmlindex) self.drop(XmlIndex, _id=xmlindex._id) # cache self._deleteFromCache(xmlindex) # refresh index view self.updateIndexView(resourcetype)
def validate(self, xml_doc): if not IXmlDoc.providedBy(xml_doc): raise DoesNotImplement(IXmlDoc) doc = xml_doc.getXml_doc() try: self.schema.assertValid(doc) except AttributeError: valid = self.schema.validate(doc) if not valid: msg = "Could not validate document." raise SeisHubError(msg) except etree.DocumentInvalid, e: msg = "Could not validate document. (%s)" raise InvalidObjectError(msg % str(e))
def validateResource(self, resource): """ Do a schema validation of a given resource. This validates against all schemas of the corresponding resourcetype. """ pid = resource.package.package_id rid = resource.resourcetype.resourcetype_id schemas = self.env.registry.schemas.get(pid, rid) for schema in schemas: try: schema.validate(resource) except Exception, e: msg = "Resource-validation against schema '%s' failed. (%s)" raise InvalidObjectError(msg % (str(schema.getResource().name), str(e)))