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