2 # -*- coding: utf-8 -*-
4 # Copyright 2007 Zuza Software Foundation
6 # This file is part of translate.
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 """This class implements the functionality for handling OpenDocument files.
25 from translate
.misc
import xmlwrapper
26 from translate
.storage
import base
29 class ODFUnit(base
.TranslationUnit
):
30 """This class represents an OpenDocument translatable snippet"""
32 class ODFFile(base
.TranslationStore
, xmlwrapper
.XMLWrapper
):
33 """This class represents an OpenDocument file"""
35 def __init__(self
, inputfile
):
36 base
.TranslationStore
.__init
__(self
, unitclass
=self
.UnitClass
)
37 self
.filename
= getattr(inputfile
, 'name', '')
39 z
= zipfile
.ZipFile(self
.filename
, 'r')
40 contents
= z
.read("content.xml")
41 except (ValueError, zipfile
.BadZipfile
):
42 contents
= open(self
.filename
, 'r').read()
43 root
= xmlwrapper
.BuildTree(contents
)
44 xmlwrapper
.XMLWrapper
.__init
__(self
, root
)
45 if self
.tag
!= "document-content": raise ValueError("root %r != 'document-content'" % self
.tag
)
46 self
.body
= self
.getchild("body")
48 def excludeiterator(self
, obj
, excludetags
):
50 for node
in obj
._children
:
51 if xmlwrapper
.splitnamespace(node
.tag
)[1] not in excludetags
:
53 nodes
.extend(self
.excludeiterator(node
, excludetags
))
57 nodes
= self
.excludeiterator(self
.body
.obj
, ["tracked-changes"])
60 childns
, childtag
= xmlwrapper
.splitnamespace(node
.tag
)
61 if childtag
== "p" or childtag
== "h":
62 paragraphs
.append(xmlwrapper
.XMLWrapper(node
))
63 for child
in paragraphs
:
64 text
= child
.gettexts().strip()
65 self
.addsourceunit(text
)