| 1 |
# Reads an XML file and returns XML elements. |
| 2 |
# Based on inputs.fileinput.XmlElementStreamFileInput. |
| 3 |
# |
| 4 |
# Author: Frank Steggink |
| 5 |
# |
| 6 |
from copy import deepcopy |
| 7 |
|
| 8 |
from stetl.component import Config |
| 9 |
from stetl.filter import Filter |
| 10 |
from stetl.util import Util, etree |
| 11 |
from stetl.packet import FORMAT |
| 12 |
|
| 13 |
log = Util.get_log('xmlelementreader') |
| 14 |
|
| 15 |
|
| 16 |
class XmlElementReader(Filter): |
| 17 |
""" |
| 18 |
Extracts XML elements from a file, outputs each feature element in Packet. |
| 19 |
Parsing is streaming (no internal DOM buildup) so any file size can be handled. |
| 20 |
Use this class for your big GML files! |
| 21 |
|
| 22 |
consumes=FORMAT.string, produces=FORMAT.etree_element |
| 23 |
""" |
| 24 |
|
| 25 |
# Start attribute config meta |
| 26 |
@Config(ptype=list, default=None, required=True) |
| 27 |
def element_tags(self): |
| 28 |
""" |
| 29 |
Comma-separated string of XML (feature) element tag names of the elements that should be extracted |
| 30 |
and added to the output element stream. |
| 31 |
""" |
| 32 |
pass |
| 33 |
|
| 34 |
@Config(ptype=bool, default=False, required=False) |
| 35 |
def strip_namespaces(self): |
| 36 |
""" |
| 37 |
should namespaces be removed from the input document and thus not be present in the output element stream? |
| 38 |
""" |
| 39 |
pass |
| 40 |
|
| 41 |
# End attribute config meta |
| 42 |
|
| 43 |
# Constructor |
| 44 |
def __init__(self, configdict, section): |
| 45 |
Filter.__init__(self, configdict, section, consumes=FORMAT.string, produces=FORMAT.etree_element) |
| 46 |
self.context = None |
| 47 |
self.root = None |
| 48 |
self.cur_file_path = None |
| 49 |
self.elem_count = 0 |
| 50 |
log.info("Element tags to be matched: %s" % self.element_tags) |
| 51 |
|
| 52 |
def invoke(self, packet): |
| 53 |
if packet.data is None: |
| 54 |
log.info("No XML file given") |
| 55 |
return packet |
| 56 |
|
| 57 |
if self.cur_file_path is None: |
| 58 |
self.cur_file_path = packet.data |
| 59 |
|
| 60 |
event = None |
| 61 |
packet.data = None |
| 62 |
|
| 63 |
if self.context is None: |
| 64 |
# Open file |
| 65 |
fd = open(self.cur_file_path, 'rb') |
| 66 |
self.elem_count = 0 |
| 67 |
log.info("file opened : %s" % self.cur_file_path) |
| 68 |
self.context = etree.iterparse(fd, events=("start", "end")) |
| 69 |
self.context = iter(self.context) |
| 70 |
event, self.root = next(self.context) |
| 71 |
|
| 72 |
packet = self.process_xml(packet) |
| 73 |
|
| 74 |
return packet |
| 75 |
|
| 76 |
def process_xml(self, packet): |
| 77 |
while self.context is not None: |
| 78 |
# while not packet.is_end_of_doc(): |
| 79 |
try: |
| 80 |
event, elem = next(self.context) |
| 81 |
except (etree.XMLSyntaxError, StopIteration): |
| 82 |
# workaround for etree.XMLSyntaxError https://bugs.launchpad.net/lxml/+bug/1185701 |
| 83 |
self.context = None |
| 84 |
|
| 85 |
if self.context is None: |
| 86 |
# Always end of doc |
| 87 |
# TODO: is this still useful for a non-input component? |
| 88 |
packet.set_end_of_doc() |
| 89 |
log.info("End of doc: %s elem_count=%d" % (self.cur_file_path, self.elem_count)) |
| 90 |
|
| 91 |
return packet |
| 92 |
|
| 93 |
# Filter out Namespace from the tag |
| 94 |
# this is the easiest way to go for now |
| 95 |
tag = elem.tag.split('}') |
| 96 |
if len(tag) == 2: |
| 97 |
# Namespaced tag: 2nd is tag |
| 98 |
tag = tag[1] |
| 99 |
else: |
| 100 |
# Non-namespaced tag: first |
| 101 |
tag = tag[0] |
| 102 |
|
| 103 |
if tag in self.element_tags: |
| 104 |
if event == "start": |
| 105 |
pass |
| 106 |
elif event == "end": |
| 107 |
packet.data = deepcopy(elem) |
| 108 |
self.elem_count += 1 |
| 109 |
|
| 110 |
if self.strip_namespaces: |
| 111 |
packet.data = Util.stripNamespaces(elem).getroot() |
| 112 |
|
| 113 |
# Clear the element which has been read. Don't clear the root document, |
| 114 |
# since the last element hasn't been processed yet. |
| 115 |
elem.clear() |
| 116 |
|
| 117 |
# If there is a next component, let it process |
| 118 |
if self.next: |
| 119 |
# Hand-over data (line, doc whatever) to the next component |
| 120 |
packet.format = self._output_format |
| 121 |
packet = self.next.process(packet) |
| 122 |
|
| 123 |
return packet |