ODFPY 1.2.0 |

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