xref: /aoo42x/main/l10ntools/scripts/tool/xhtex.py (revision ae54856b)
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 = unicode(str(sdfdata[obj.get_id()].text),"utf8")
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", unicode(str(sdfdata[obj.get_id()].text),"utf8"))
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+")
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+")
94            str = dom.toxml()
95            f.write(str.encode("utf-8"))
96        except IOError:
97            print "ERROR: Can not write file " + outputfilename
98            sys.exit(-1)
99        else:
100            f.close()
101
102    ##### Helper for parse-once-use-often like parsing a xml file is needed implement it here
103    def parse_file(self, filename):
104        document = ""
105        try:
106            f = open(filename,"r")
107            document = f.read()
108        except IOError:
109            print "ERROR: Can not read file " + filename
110        else:
111            f.close()
112        return xml.dom.minidom.parseString(document)
113
114    ##### Extract a single File
115    def extract_file(self, inputfile):
116        sdf_data = []
117        dom = self.parse_file(inputfile)
118        sdf_data.extend(self.extract_topic(dom.getElementsByTagName("topic"), inputfile))
119        sdf_data.extend(self.extract_title(dom.getElementsByTagName("help_section"), inputfile))
120        sdf_data.extend(self.extract_title(dom.getElementsByTagName("node"), inputfile))
121        return ''.join([str(line)+"\n" for line in sdf_data])
122
123    def prepare_sdf_line(self, inputfile="", lang="" , id="" , text=""):
124        if lang == "":
125            lang = self._source_language
126        return SdfEntity(project=self._options.project_name, source_file=self.get_filename_string(inputfile),
127                         resource_type=self._resource_type, gid=id, lid="", langid=lang,text=text)
128
129run = Xhtex()
130