1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
# -*- coding: utf-8 -*-
# Copyright (C) 2000-2004 Juan David Ibáñez Palomar <jdavid@itaapy.com>
# 2003 Roberto Quero, Eduardo Corrales
# 2004 Søren Roug
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# NOTE:
# This parser doesn't understand the ALT-TRANS element.
from xml.sax.handler import ContentHandler
from xml.sax import *
from cStringIO import StringIO
from types import StringType, UnicodeType
#constants
_FILE_ATTRS = ['original', 'source-language', 'datatype', 'date',
'target-language', 'product-name', 'product-version', 'build-num']
_PHASE_ATTRS = ['phase-name', 'process-name', 'tool', 'date', 'contact-name',
'contact-email', 'company-name']
class XLIFFHandler(ContentHandler):
""" This is used to parse the xliff file
"""
def __init__(self):
"""constructor """
self.__currentTag = ''
self.__filetag = []
self.__phase_group = []
self.__source = 0
self.__body = {}
self.__data = []
self.__inside_alttrans = 0
self.__tuid = ''
#functions related with <file> tag
def getFileTag(self):
return self.__filetag
def setFileTag(self, dict):
self.__filetag.extend(dict)
#functions related with <phase-group> tag
def getPhaseGroup(self):
return self.__phase_group
def setPhaseGroup(self, dict):
self.__phase_group.append(dict)
def getBody(self):
return self.__body
def setBody(self, key, value):
self.__body[key] = value
def startElement(self, name, attrs):
self.__currentTag = name
if name == 'alt-trans':
self.__inside_alttrans = 1
# Make the attributes available
# Implicit assumption: There is only one <file> element.
if name == 'file':
tmp = attrs.items()
for i in [elem for elem in attrs.keys() if elem not in _FILE_ATTRS]:
tmp.remove((i, attrs[i]))
self.setFileTag(tmp)
if name == 'phase':
tmp = attrs.items()
for i in [elem for elem in attrs.keys() if elem not in _PHASE_ATTRS]:
tmp.remove((i, attrs[i]))
self.setPhaseGroup(tmp)
if name == 'trans-unit':
self.__tuid = attrs['id']
self.__source = u''
self.__target = u''
self.__note = u''
def endElement(self, name):
if name == 'alt-trans':
self.__inside_alttrans = 0
if name == 'source' and self.__inside_alttrans == 0:
content = u''.join(self.__data).strip()
self.__data = []
self.__source = content
if name == 'target' and self.__inside_alttrans == 0:
content = u''.join(self.__data).strip()
self.__data = []
self.__target = content
if name == 'note' and self.__inside_alttrans == 0:
content = u''.join(self.__data).strip()
self.__data = []
self.__note = content
if name == 'trans-unit':
self.setBody(self.__tuid, {'source':self.__source,
'target':self.__target, 'note':self.__note})
self.__currentTag = ''
def characters(self, content):
currentTag = self.__currentTag
if currentTag in ( 'source', 'target', 'note'):
self.__data.append(content)
class HandleXliffParsing:
""" class for parse xliff files """
def __init__(self):
""" """
pass
def parseXLIFFSTring(self, xml_string):
""" """
chandler = XLIFFHandler()
parser = make_parser()
# Tell the parser to use our handler
parser.setContentHandler(chandler)
# Don't load the DTD from the Internet
parser.setFeature(handler.feature_external_ges, 0)
inpsrc = InputSource()
inpsrc.setByteStream(StringIO(xml_string))
try:
parser.parse(inpsrc)
return chandler
except:
return None
def parseXLIFFFile(self, file):
# Create a parser
parser = make_parser()
chandler = XLIFFHandler()
# Tell the parser to use our handler
parser.setContentHandler(chandler)
# Don't load the DTD from the Internet
parser.setFeature(handler.feature_external_ges, 0)
inputsrc = InputSource()
try:
if type(file) is StringType:
inputsrc.setByteStream(StringIO(file))
else:
filecontent = file.read()
inputsrc.setByteStream(StringIO(filecontent))
parser.parse(inputsrc)
return chandler
except:
return None
|