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