| 1 |
#!/usr/bin/env python |
| 2 |
# Copyright (C) 2007-2008 Søren Roug, European Environment Agency |
| 3 |
# |
| 4 |
# This library is free software; you can redistribute it and/or |
| 5 |
# modify it under the terms of the GNU Lesser General Public |
| 6 |
# License as published by the Free Software Foundation; either |
| 7 |
# version 2.1 of the License, or (at your option) any later version. |
| 8 |
# |
| 9 |
# This library is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# Lesser General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU Lesser General Public |
| 15 |
# License along with this library; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
# |
| 18 |
# Contributor(s): |
| 19 |
# |
| 20 |
|
| 21 |
# This script is to be embedded in opendocument.py later |
| 22 |
# The purpose is to read an ODT/ODP/ODS file and create the datastructure |
| 23 |
# in memory. The user should then be able to make operations and then save |
| 24 |
# the structure again. |
| 25 |
|
| 26 |
|
| 27 |
from xml.sax import handler |
| 28 |
|
| 29 |
from .element import Element |
| 30 |
from .namespaces import OFFICENS |
| 31 |
|
| 32 |
# |
| 33 |
# Parse the XML files |
| 34 |
# |
| 35 |
|
| 36 |
|
| 37 |
class LoadParser(handler.ContentHandler): |
| 38 |
"""Extract headings from content.xml of an ODT file""" |
| 39 |
|
| 40 |
triggers = ( |
| 41 |
(OFFICENS, 'automatic-styles'), |
| 42 |
(OFFICENS, 'body'), |
| 43 |
(OFFICENS, 'font-face-decls'), |
| 44 |
(OFFICENS, 'master-styles'), |
| 45 |
(OFFICENS, 'meta'), |
| 46 |
(OFFICENS, 'scripts'), |
| 47 |
(OFFICENS, 'settings'), |
| 48 |
(OFFICENS, 'styles'), |
| 49 |
) |
| 50 |
|
| 51 |
def __init__(self, document): |
| 52 |
self.doc = document |
| 53 |
self.data = [] |
| 54 |
self.level = 0 |
| 55 |
self.parse = False |
| 56 |
|
| 57 |
def characters(self, content): |
| 58 |
if self.parse is False: |
| 59 |
return |
| 60 |
self.data.append(content) |
| 61 |
|
| 62 |
def startElementNS(self, name, qname, attrs): |
| 63 |
if name in self.triggers: |
| 64 |
self.parse = True |
| 65 |
if self.doc._parsing != 'styles.xml' and name == (OFFICENS, 'font-face-decls'): |
| 66 |
self.parse = False |
| 67 |
if self.parse is False: |
| 68 |
return |
| 69 |
|
| 70 |
self.level = self.level + 1 |
| 71 |
# Add any accumulated text content |
| 72 |
content = ''.join(self.data) |
| 73 |
if len(content.strip()) > 0: |
| 74 |
assert self.parent is not None |
| 75 |
self.parent.addText(content, check_grammar=False) |
| 76 |
self.data = [] |
| 77 |
# Create the element |
| 78 |
attrdict = {} |
| 79 |
for att, value in attrs.items(): |
| 80 |
attrdict[att] = value |
| 81 |
try: |
| 82 |
e = Element(qname=name, qattributes=attrdict, check_grammar=False) |
| 83 |
self.curr = e |
| 84 |
except AttributeError as v: |
| 85 |
print(f'Error: {v}') |
| 86 |
|
| 87 |
if name == (OFFICENS, 'automatic-styles'): |
| 88 |
e = self.doc.automaticstyles |
| 89 |
elif name == (OFFICENS, 'body'): |
| 90 |
e = self.doc.body |
| 91 |
elif name == (OFFICENS, 'master-styles'): |
| 92 |
e = self.doc.masterstyles |
| 93 |
elif name == (OFFICENS, 'meta'): |
| 94 |
e = self.doc.meta |
| 95 |
elif name == (OFFICENS, 'scripts'): |
| 96 |
e = self.doc.scripts |
| 97 |
elif name == (OFFICENS, 'settings'): |
| 98 |
e = self.doc.settings |
| 99 |
elif name == (OFFICENS, 'styles'): |
| 100 |
e = self.doc.styles |
| 101 |
elif self.doc._parsing == 'styles.xml' and name == (OFFICENS, 'font-face-decls'): |
| 102 |
e = self.doc.fontfacedecls |
| 103 |
elif hasattr(self, 'parent'): |
| 104 |
assert self.parent is not None |
| 105 |
self.parent.addElement(e, check_grammar=False) |
| 106 |
self.parent = e |
| 107 |
|
| 108 |
def endElementNS(self, name, qname): |
| 109 |
if self.parse is False: |
| 110 |
return |
| 111 |
self.level = self.level - 1 |
| 112 |
# Changed by Kovid to deal with <span> tags with only whitespace |
| 113 |
# content. |
| 114 |
data = q = ''.join(self.data) |
| 115 |
tn = getattr(self.curr, 'tagName', '') |
| 116 |
try: |
| 117 |
do_strip = not tn.startswith('text:') |
| 118 |
except Exception: |
| 119 |
do_strip = True |
| 120 |
if do_strip: |
| 121 |
q = q.strip() |
| 122 |
if q: |
| 123 |
assert self.curr is not None |
| 124 |
self.curr.addText(data, check_grammar=False) |
| 125 |
self.data = [] |
| 126 |
assert self.curr is not None |
| 127 |
self.curr = self.curr.parentNode |
| 128 |
self.parent = self.curr |
| 129 |
if name in self.triggers: |
| 130 |
self.parse = False |