2 #***********************************************************************
4 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 # Copyright 2008 by Sun Microsystems, Inc.
8 # OpenOffice.org - a multi-platform office productivity suite
10 # $RCSfile: parse.py,v $
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 #***********************************************************************
35 localeNames
= {'fr': 'French', 'hu': 'Hungarian', 'de': 'German'}
36 def getLocaleName (code
):
38 if localeNames
.has_key(code
):
39 return localeNames
[code
]
41 return "(unknown locale)"
49 class LocaleData(object):
50 def __init__ (self
, locale
):
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
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(\""
72 chars
+= "\"), OUString(), OUString());\n\n"
74 # pre instantiations of localized function names.
75 funcs
= self
.funcList
.keys()
77 chars
+= "// pre instantiations of localized function names\n"
79 for item
in self
.funcList
[func
]:
80 chars
+= "static const sal_Unicode " + self
.getLocaleFuncVarName(func
, item
) + "[] = {\n"
83 # Dump the UTF-16 bytes.
89 chars
+= "0x%.4X"%uval
91 # Don't forget to null-terminate the string.
98 # map item instantiations
100 chars
+= "static const TransItem p" + self
.locale
.capitalize() + "[] = {\n"
102 for item
in self
.funcList
[func
]:
104 chars
+= "{%s, \"%s\", %s},\n"%(self
.getLocaleFuncVarName(func
, item
),
106 "oc"+func
.capitalize())
108 chars
+= " {NULL, NULL, ocNone}\n"
112 chars
+= "addToMap(%s, %s);\n"%(
113 "p"+self
.locale
.capitalize(), "a"+self
.locale
.capitalize())
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"
125 self
.infile
= args
[1]
127 self
.outfile
= args
[2]
130 # Assume little endian.
131 bh
= ord(self
.bytes
[self
.i
])
132 bl
= ord(self
.bytes
[self
.i
+1])
137 def parseLine (self
):
139 while self
.i
< self
.size
:
140 dbyte
= self
.getDByte()
146 def dumpBuf (self
, buf
, linefeed
=True):
148 sys
.stdout
.write(chr(item
))
154 file = open(self
.infile
, 'r')
155 self
.bytes
= file.read()
158 self
.size
= len(self
.bytes
)
161 localeList
= [] # stores an array of locale data objects.
166 while self
.i
< self
.size
:
167 dbyte
= self
.getDByte()
168 if dbyte
== 0xFEFF and self
.i
== 2:
169 # unicode signature - ignore it.
171 elif dbyte
== 0x0024:
173 buf
= self
.parseLine()
174 locale
= getAscii(buf
)
175 localeList
.append(LocaleData(locale
))
177 elif dbyte
== 0x0040:
179 buf
= self
.parseLine()
180 funcName
= getAscii(buf
)
182 elif dbyte
== 0x002C:
183 # , - comma separator
185 wordPair
.append(word
)
187 elif dbyte
== 0x000A:
190 wordPair
.append(word
)
192 if len(wordPair
) >= 2:
193 localeList
[-1].addKeywordMap(funcName
, wordPair
[0], wordPair
[1])
195 elif dbyte
in [0x0009, 0x0020]:
196 # whitespace - ignore it.
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')
210 if __name__
=='__main__':
211 parser
= Parser(sys
.argv
)