1#!/usr/bin/env python 2#*********************************************************************** 3# 4# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5# 6# Copyright 2000, 2010 Oracle and/or its affiliates. 7# 8# OpenOffice.org - a multi-platform office productivity suite 9# 10# This file is part of OpenOffice.org. 11# 12# OpenOffice.org is free software: you can redistribute it and/or modify 13# it under the terms of the GNU Lesser General Public License version 3 14# only, as published by the Free Software Foundation. 15# 16# OpenOffice.org is distributed in the hope that it will be useful, 17# but WITHOUT ANY WARRANTY; without even the implied warranty of 18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19# GNU Lesser General Public License version 3 for more details 20# (a copy is included in the LICENSE file that accompanied this code). 21# 22# You should have received a copy of the GNU Lesser General Public License 23# version 3 along with OpenOffice.org. If not, see 24# <http://www.openoffice.org/license.html> 25# for a copy of the LGPLv3 License. 26# 27#*********************************************************************** 28 29import sys 30 31localeNames = {'fr': 'French', 'hu': 'Hungarian', 'de': 'German'} 32def getLocaleName (code): 33 global localeNames 34 if localeNames.has_key(code): 35 return localeNames[code] 36 else: 37 return "(unknown locale)" 38 39def getAscii (ords): 40 ascii = '' 41 for c in ords: 42 ascii += chr(c) 43 return ascii 44 45class LocaleData(object): 46 def __init__ (self, locale): 47 self.locale = locale 48 self.funcList = {} 49 50 def addKeywordMap (self, funcName, localeName, engName): 51 if not self.funcList.has_key(funcName): 52 self.funcList[funcName] = [] 53 54 self.funcList[funcName].append([localeName, engName]) 55 56 def getLocaleFuncVarName (self, func, pair): 57 return func.lower() + "_" + getAscii(pair[1]).lower() + "_" + self.locale 58 59 def dumpCode (self): 60 chars = "" 61 62 # locale output 63 chars += "// " + "-"*75 + "\n" 64 chars += "// %s language locale (automatically generated)\n"%getLocaleName(self.locale) 65 chars += "// " + "-"*75 + "\n" 66 chars += "static const Locale a" + self.locale.capitalize() + "(OUString::createFromAscii(\"" 67 chars += self.locale 68 chars += "\"), OUString(), OUString());\n\n" 69 70 # pre instantiations of localized function names. 71 funcs = self.funcList.keys() 72 funcs.sort() 73 chars += "// pre instantiations of localized function names\n" 74 for func in funcs: 75 for item in self.funcList[func]: 76 chars += "static const sal_Unicode " + self.getLocaleFuncVarName(func, item) + "[] = {\n" 77 chars += " " 78 isFirst = True 79 # Dump the UTF-16 bytes. 80 for uval in item[0]: 81 if isFirst: 82 isFirst = False 83 else: 84 chars += ", " 85 chars += "0x%.4X"%uval 86 87 # Don't forget to null-terminate the string. 88 if not isFirst: 89 chars += ", " 90 chars += "0x0000" 91 92 chars += "};\n" 93 94 # map item instantiations 95 chars += "\n" 96 chars += "static const TransItem p" + self.locale.capitalize() + "[] = {\n" 97 for func in funcs: 98 for item in self.funcList[func]: 99 chars += " " 100 chars += "{%s, \"%s\", %s},\n"%(self.getLocaleFuncVarName(func, item), 101 getAscii(item[1]), 102 "oc"+func.capitalize()) 103 104 chars += " {NULL, NULL, ocNone}\n" 105 chars += "};\n\n" 106 107 # addToMap call 108 chars += "addToMap(%s, %s);\n"%( 109 "p"+self.locale.capitalize(), "a"+self.locale.capitalize()) 110 111 return chars 112 113class Parser(object): 114 115 def __init__ (self, args): 116 # default input & output files. 117 self.infile = "./keywords_utf16.txt" 118 self.outfile = "../../source/core/tool/cellkeywords.inl" 119 120 if len(args) >= 2: 121 self.infile = args[1] 122 if len(args) >= 3: 123 self.outfile = args[2] 124 125 def getDByte (self): 126 # Assume little endian. 127 bh = ord(self.bytes[self.i]) 128 bl = ord(self.bytes[self.i+1]) 129 dbyte = bl*256 + bh 130 self.i += 2 131 return dbyte 132 133 def parseLine (self): 134 buf = [] 135 while self.i < self.size: 136 dbyte = self.getDByte() 137 if dbyte == 0x000A: 138 break 139 buf.append(dbyte) 140 return buf 141 142 def dumpBuf (self, buf, linefeed=True): 143 for item in buf: 144 sys.stdout.write(chr(item)) 145 if linefeed: 146 print '' 147 148 def parse (self): 149 150 file = open(self.infile, 'r') 151 self.bytes = file.read() 152 file.close() 153 154 self.size = len(self.bytes) 155 self.i = 0 156 157 localeList = [] # stores an array of locale data objects. 158 funcName = None 159 word = [] 160 wordPair = [] 161 162 while self.i < self.size: 163 dbyte = self.getDByte() 164 if dbyte == 0xFEFF and self.i == 2: 165 # unicode signature - ignore it. 166 pass 167 elif dbyte == 0x0024: 168 # $ - locale name 169 buf = self.parseLine() 170 locale = getAscii(buf) 171 localeList.append(LocaleData(locale)) 172 173 elif dbyte == 0x0040: 174 # @ - function name 175 buf = self.parseLine() 176 funcName = getAscii(buf) 177 178 elif dbyte == 0x002C: 179 # , - comma separator 180 if len(word) > 0: 181 wordPair.append(word) 182 word = [] 183 elif dbyte == 0x000A: 184 # linefeed 185 if len(word) > 0: 186 wordPair.append(word) 187 word = [] 188 if len(wordPair) >= 2: 189 localeList[-1].addKeywordMap(funcName, wordPair[0], wordPair[1]) 190 wordPair = [] 191 elif dbyte in [0x0009, 0x0020]: 192 # whitespace - ignore it. 193 pass 194 else: 195 word.append(dbyte) 196 197 chars = "// This file has been automatically generated. Do not hand-edit this!\n" 198 for obj in localeList: 199 chars += "\n" + obj.dumpCode() 200 201 # Write to output file. 202 file = open(self.outfile, 'w') 203 file.write(chars) 204 file.close() 205 206if __name__=='__main__': 207 parser = Parser(sys.argv) 208 parser.parse() 209 210