Пример #1
0
 def perform(self):
     try:
         etree.fromstring(
             self.probe.response.content,
             parser=etree.XMLParser(huge_tree=CONFIG['GHC_LARGE_XML']))
     except Exception:
         self.set_result(False, str(sys.exc_info()))
Пример #2
0
    def loadWms(self, wmsUrl, wmsXml, additionalItems=[]):
        """Add an OWSLib WebMapService to this NafiTreeViewModel based on the capabilities XML."""

        # etree.fromstring internally for some reason can't handle the XML declaration, 
        # so it gets hacked off here
        wmsXml = re.sub("<\\?xml.*\\?>", "", wmsXml)
        contents = OrderedDict()

        try: 
            parser = etree.XMLParser(dtd_validation=False, load_dtd=False, no_network=True, recover=True, resolve_entities=False)
            capabilities = etree.fromstring(wmsXml, parser)

            capabilityElement = capabilities.find("Capability")

            if capabilityElement is None: 
                raise RuntimeError("Missing 'Capability' Element in parsed XML capabilities")

            # recursively gather content metadata for all layer elements, this is stolen 
            # from OWSLib because it won't let us configure the parser the way we need to
            # to avoid unwanted network activity, entity resolutions etc
            # see https://github.com/geopython/OWSLib/blob/8a94500c2137082dfc4e59acd15389312bcb63fb/owslib/map/wms111.py#L113
            
            # TODO merge this gather with the other tree manipulation functions below
            
            def gatherLayers(parentElement, parentMetadata):
                layers = []
                for index, elem in enumerate(parentElement.findall('Layer')):
                    cm = ContentMetadata(elem, parent=parentMetadata,
                                            index=index + 1,
                                            parse_remote_metadata=False)
                    if cm.id:
                        layers.append(cm)
                        contents[cm.id] = cm
                    cm.children = gatherLayers(elem, cm)
                return layers
            gatherLayers(capabilityElement, None)
        
        except (etree.ParserError, RuntimeError) as pe:
            capabilitiesError(str(pe), wmsXml)
            return

        # clear all rows
        self.removeRows(0, self.rowCount())
        # the OWSLib structure is not properly organised via its "children" properties, need to fix it up
        owsLayers = [contents[layerName] for layerName in list(contents)]
        # check we've got at least one layer
        assert (len(owsLayers) > 0)
        # calculate our root layer
        rootLayer = NafiTreeViewModel.groupByRootLayers(owsLayers)[0]
        # add layer hierarchy to our tree model
        NafiTreeViewModel.addOwsLayerToTreeViewModel(self, wmsUrl, rootLayer, self.unwantedLayers)
        # add some extras if present
        self.loadAdditionalItems(additionalItems)
Пример #3
0
def parse_dov_xml(xml_data):
    """Parse the given XML data into an ElementTree.

    Parameters
    ----------
    xml_data : bytes
        The raw XML data of a DOV object as bytes.

    Returns
    -------
    tree : etree.ElementTree
        Parsed XML tree of the DOV object.

    """
    try:
        parser = etree.XMLParser(ns_clean=True, recover=True)
    except TypeError:
        parser = etree.XMLParser()

    try:
        tree = etree.fromstring(xml_data, parser=parser)
        return tree
    except Exception:
        raise XmlParseError("Failed to parse XML record.")