1a0428e9eSAndrew Rist#************************************************************** 2a0428e9eSAndrew Rist# 3a0428e9eSAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 4a0428e9eSAndrew Rist# or more contributor license agreements. See the NOTICE file 5a0428e9eSAndrew Rist# distributed with this work for additional information 6a0428e9eSAndrew Rist# regarding copyright ownership. The ASF licenses this file 7a0428e9eSAndrew Rist# to you under the Apache License, Version 2.0 (the 8a0428e9eSAndrew Rist# "License"); you may not use this file except in compliance 9a0428e9eSAndrew Rist# with the License. You may obtain a copy of the License at 10a0428e9eSAndrew Rist# 11a0428e9eSAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 12a0428e9eSAndrew Rist# 13a0428e9eSAndrew Rist# Unless required by applicable law or agreed to in writing, 14a0428e9eSAndrew Rist# software distributed under the License is distributed on an 15a0428e9eSAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16a0428e9eSAndrew Rist# KIND, either express or implied. See the License for the 17a0428e9eSAndrew Rist# specific language governing permissions and limitations 18a0428e9eSAndrew Rist# under the License. 19a0428e9eSAndrew Rist# 20a0428e9eSAndrew Rist#************************************************************** 21cdf0e10cSrcweir 22cdf0e10cSrcweirfrom l10ntool import AbstractL10nTool 23cdf0e10cSrcweirfrom sdf import SdfEntity 24cdf0e10cSrcweirimport sys 25cdf0e10cSrcweirimport xml.dom.minidom 26cdf0e10cSrcweir 27cdf0e10cSrcweirclass Xhtex(AbstractL10nTool): 28*ae54856bSPedro Giffuni _resource_type = "xht" 29cdf0e10cSrcweir _sdfdata = () 30cdf0e10cSrcweir _lang = "" 31*ae54856bSPedro Giffuni 32cdf0e10cSrcweir # Extract methods 33cdf0e10cSrcweir def extract_topic(self, list, inputfile): 34cdf0e10cSrcweir topics = [] 35*ae54856bSPedro Giffuni for elem in list: 36cdf0e10cSrcweir if elem.childNodes[0].nodeType == elem.TEXT_NODE and len(elem.childNodes[0].data.strip()): 37*ae54856bSPedro Giffuni topics.append(self.prepare_sdf_line(id=elem.getAttribute("id").strip(), text=elem.childNodes[0].data, inputfile=inputfile)) 38cdf0e10cSrcweir return topics 39*ae54856bSPedro Giffuni 40cdf0e10cSrcweir def extract_title(self, list, inputfile): 41cdf0e10cSrcweir titles = [] 42cdf0e10cSrcweir for elem in list: 43cdf0e10cSrcweir if len(elem.getAttribute("title").strip()): 44cdf0e10cSrcweir titles.append(self.prepare_sdf_line(id=elem.getAttribute("id").strip(), text=elem.getAttribute("title").strip(), inputfile=inputfile)) 45cdf0e10cSrcweir return titles 46*ae54856bSPedro Giffuni 47cdf0e10cSrcweir # Merge methods 48cdf0e10cSrcweir def merge_topic(self, list, sdfdata, lang, inputfilename, dom): 49cdf0e10cSrcweir for elem in list: 50cdf0e10cSrcweir if elem.childNodes[0].nodeType == elem.TEXT_NODE and elem.getAttribute("id").strip(): 51cdf0e10cSrcweir obj = self.prepare_sdf_line(inputfile=inputfilename, lang=lang, id=elem.getAttribute("id").strip()) 52cdf0e10cSrcweir if sdfdata[obj.get_id()]: 53cdf0e10cSrcweir elem.childNodes[0].data = unicode(str(sdfdata[obj.get_id()].text),"utf8") 54cdf0e10cSrcweir 55*ae54856bSPedro Giffuni 56cdf0e10cSrcweir def merge_title(self, list, sdfdata, lang, inputfilename): 57cdf0e10cSrcweir for elem in list: 58cdf0e10cSrcweir obj = self.prepare_sdf_line(inputfile=inputfilename, lang=lang, id=elem.getAttribute("id").strip()) 59*ae54856bSPedro Giffuni if elem.getAttribute("id").strip() and sdfdata[obj.get_id()]: 60cdf0e10cSrcweir elem.setAttribute("title", unicode(str(sdfdata[obj.get_id()].text),"utf8")) 61cdf0e10cSrcweir 62*ae54856bSPedro Giffuni # L10N tool 63cdf0e10cSrcweir def __init__(self): 64cdf0e10cSrcweir AbstractL10nTool.__init__(self) 65*ae54856bSPedro Giffuni 66cdf0e10cSrcweir def parse_file(self, filename): 67cdf0e10cSrcweir document = "" 68cdf0e10cSrcweir try: 69cdf0e10cSrcweir f = open(filename, "r+") 70cdf0e10cSrcweir document = f.read() 71cdf0e10cSrcweir except IOError: 72cdf0e10cSrcweir print "ERROR: Can not read file " + filename 73cdf0e10cSrcweir sys.exit(-1) 74cdf0e10cSrcweir else: 75cdf0e10cSrcweir f.close() 76cdf0e10cSrcweir return xml.dom.minidom.parseString(document) 77cdf0e10cSrcweir 78*ae54856bSPedro Giffuni 79cdf0e10cSrcweir def merge_file(self, inputfilename, outputfilename, parsed_file_ref, lang,is_forced_lang, sdfdata): 80*ae54856bSPedro Giffuni if lang == "en-US": 81*ae54856bSPedro Giffuni mod_outputfilename = outputfilename.replace("_en-US",'') 82cdf0e10cSrcweir self.make_dirs(mod_outputfilename) 83cdf0e10cSrcweir self.copy_file(inputfilename, mod_outputfilename) 84cdf0e10cSrcweir return 85cdf0e10cSrcweir dom = parsed_file_ref.cloneNode(True) 86cdf0e10cSrcweir #dom = self.parse_file(inputfilename) # in case cloneNode is buggy just parse it always 87*ae54856bSPedro Giffuni 88cdf0e10cSrcweir self.merge_topic(dom.getElementsByTagName("topic"), sdfdata, lang, inputfilename, dom) 89cdf0e10cSrcweir self.merge_title(dom.getElementsByTagName("node"), sdfdata, lang, inputfilename) 90cdf0e10cSrcweir self.merge_title(dom.getElementsByTagName("help_section"), sdfdata, lang, inputfilename) 91cdf0e10cSrcweir self.make_dirs(outputfilename) 92cdf0e10cSrcweir try: 93cdf0e10cSrcweir f = open(outputfilename, "w+") 94cdf0e10cSrcweir str = dom.toxml() 95cdf0e10cSrcweir f.write(str.encode("utf-8")) 96cdf0e10cSrcweir except IOError: 97cdf0e10cSrcweir print "ERROR: Can not write file " + outputfilename 98cdf0e10cSrcweir sys.exit(-1) 99cdf0e10cSrcweir else: 100cdf0e10cSrcweir f.close() 101*ae54856bSPedro Giffuni 102cdf0e10cSrcweir ##### Helper for parse-once-use-often like parsing a xml file is needed implement it here 103cdf0e10cSrcweir def parse_file(self, filename): 104cdf0e10cSrcweir document = "" 105cdf0e10cSrcweir try: 106cdf0e10cSrcweir f = open(filename,"r") 107cdf0e10cSrcweir document = f.read() 108cdf0e10cSrcweir except IOError: 109cdf0e10cSrcweir print "ERROR: Can not read file " + filename 110cdf0e10cSrcweir else: 111*ae54856bSPedro Giffuni f.close() 112cdf0e10cSrcweir return xml.dom.minidom.parseString(document) 113*ae54856bSPedro Giffuni 114cdf0e10cSrcweir ##### Extract a single File 115cdf0e10cSrcweir def extract_file(self, inputfile): 116cdf0e10cSrcweir sdf_data = [] 117*ae54856bSPedro Giffuni dom = self.parse_file(inputfile) 118cdf0e10cSrcweir sdf_data.extend(self.extract_topic(dom.getElementsByTagName("topic"), inputfile)) 119cdf0e10cSrcweir sdf_data.extend(self.extract_title(dom.getElementsByTagName("help_section"), inputfile)) 120*ae54856bSPedro Giffuni sdf_data.extend(self.extract_title(dom.getElementsByTagName("node"), inputfile)) 121*ae54856bSPedro Giffuni return ''.join([str(line)+"\n" for line in sdf_data]) 122cdf0e10cSrcweir 123cdf0e10cSrcweir def prepare_sdf_line(self, inputfile="", lang="" , id="" , text=""): 124cdf0e10cSrcweir if lang == "": 125cdf0e10cSrcweir lang = self._source_language 126cdf0e10cSrcweir return SdfEntity(project=self._options.project_name, source_file=self.get_filename_string(inputfile), 127cdf0e10cSrcweir resource_type=self._resource_type, gid=id, lid="", langid=lang,text=text) 128*ae54856bSPedro Giffuni 129cdf0e10cSrcweirrun = Xhtex() 130