2019-09-17 10:22:56 +01:00
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
_copyright = """/*
|
|
|
|
|
* Copyright 2011-2014 Software Freedom Conservancy
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the \"License\");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an \"AS IS\" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def get_atom_name(name):
|
2024-12-28 11:39:00 +01:00
|
|
|
# We had a todo here to convert camelCase and snake_case to BIG_SNAKE_CASE, but this code
|
|
|
|
|
# will be removed when BiDi is the default, so we're not going to bother with that.
|
2019-09-17 10:22:56 +01:00
|
|
|
name = os.path.basename(name)
|
|
|
|
|
return name.upper()
|
|
|
|
|
|
2019-11-29 05:18:34 -08:00
|
|
|
def write_atom_literal(out, name, contents, lang, utf8):
|
2019-09-17 10:22:56 +01:00
|
|
|
# Escape the contents of the file so it can be stored as a literal.
|
2019-10-17 13:56:30 -07:00
|
|
|
contents = contents.replace("\\", "\\\\")
|
2019-09-17 10:22:56 +01:00
|
|
|
contents = contents.replace("\f", "\\f")
|
|
|
|
|
contents = contents.replace("\n", "\\n")
|
|
|
|
|
contents = contents.replace("\r", "\\r")
|
|
|
|
|
contents = contents.replace("\t", "\\t")
|
|
|
|
|
contents = contents.replace('"', '\\"')
|
|
|
|
|
|
|
|
|
|
if "cc" == lang or "hh" == lang:
|
2019-11-29 05:18:34 -08:00
|
|
|
if utf8:
|
|
|
|
|
line_format = " \"{}\",\n"
|
2020-04-27 00:56:10 -07:00
|
|
|
else:
|
|
|
|
|
line_format = " L\"{}\",\n"
|
2019-09-17 10:22:56 +01:00
|
|
|
elif "java" == lang:
|
|
|
|
|
line_format = " .append\(\"{}\")\n"
|
|
|
|
|
else:
|
|
|
|
|
raise RuntimeError("Unknown language: %s " % lang)
|
|
|
|
|
|
|
|
|
|
name = get_atom_name(name)
|
|
|
|
|
|
|
|
|
|
if "cc" == lang or "hh" == lang:
|
2020-04-27 00:56:10 -07:00
|
|
|
string_type = "std::string" if utf8 else "std::wstring"
|
|
|
|
|
char_type = "char" if utf8 else "wchar_t"
|
2019-11-29 05:18:34 -08:00
|
|
|
out.write("const %s* const %s[] = {\n" % (char_type, name))
|
2019-09-17 10:22:56 +01:00
|
|
|
elif "java" == lang:
|
|
|
|
|
out.write(" %s(new StringBuilder()\n" % name)
|
|
|
|
|
else:
|
|
|
|
|
raise RuntimeError("Unknown language: %s " % lang)
|
|
|
|
|
|
|
|
|
|
# Make the header file play nicely in a terminal: limit lines to 80
|
|
|
|
|
# characters, but make sure we don't cut off a line in the middle
|
|
|
|
|
# of an escape sequence.
|
2019-10-17 13:56:30 -07:00
|
|
|
while len(contents) > 70:
|
|
|
|
|
diff = 70
|
2019-09-17 10:22:56 +01:00
|
|
|
while contents[diff-1] == "\\":
|
|
|
|
|
diff = diff -1
|
|
|
|
|
line = contents[0:diff]
|
|
|
|
|
contents = contents[diff:]
|
|
|
|
|
|
|
|
|
|
out.write(line_format.format(line))
|
|
|
|
|
if len(contents) > 0:
|
|
|
|
|
out.write(line_format.format(contents))
|
|
|
|
|
|
|
|
|
|
if "cc" == lang or "hh" == lang:
|
|
|
|
|
out.write(" NULL\n};\n\n")
|
|
|
|
|
elif "java" == lang:
|
|
|
|
|
out.write(" .toString()),\n")
|
|
|
|
|
|
2019-11-29 05:18:34 -08:00
|
|
|
def generate_header(file_name, out, js_map, just_declare, utf8):
|
2019-09-17 10:22:56 +01:00
|
|
|
define_guard = "WEBDRIVER_%s" % os.path.basename(file_name.upper()).replace(".", "_")
|
2020-04-27 00:56:10 -07:00
|
|
|
include_stddef = "" if utf8 else "\n#include <stddef.h> // For wchar_t."
|
2019-09-17 10:22:56 +01:00
|
|
|
out.write("""%s
|
2024-12-28 11:39:00 +01:00
|
|
|
|
2019-09-17 10:22:56 +01:00
|
|
|
/* AUTO GENERATED - DO NOT EDIT BY HAND */
|
|
|
|
|
#ifndef %s
|
|
|
|
|
#define %s
|
2019-11-29 05:18:34 -08:00
|
|
|
%s
|
2019-09-17 10:22:56 +01:00
|
|
|
#include <string> // For std::(w)string.
|
|
|
|
|
|
|
|
|
|
namespace webdriver {
|
|
|
|
|
namespace atoms {
|
2024-12-28 11:39:00 +01:00
|
|
|
|
2019-11-29 05:18:34 -08:00
|
|
|
""" % (_copyright, define_guard, define_guard, include_stddef))
|
2019-09-17 10:22:56 +01:00
|
|
|
|
2020-04-27 00:56:10 -07:00
|
|
|
string_type = "std::string" if utf8 else "std::wstring"
|
|
|
|
|
char_type = "char" if utf8 else "wchar_t"
|
2024-12-28 11:39:00 +01:00
|
|
|
|
2019-09-17 10:22:56 +01:00
|
|
|
for (name, file) in js_map.items():
|
|
|
|
|
if just_declare:
|
2019-11-29 05:18:34 -08:00
|
|
|
out.write("extern const %s* const %s[];\n" % (char_type, name.upper()))
|
2019-09-17 10:22:56 +01:00
|
|
|
else:
|
|
|
|
|
contents = open(file, "r").read()
|
2019-11-29 05:18:34 -08:00
|
|
|
write_atom_literal(out, name, contents, "hh", utf8)
|
2019-09-17 10:22:56 +01:00
|
|
|
|
|
|
|
|
out.write("""
|
2019-10-17 13:56:30 -07:00
|
|
|
static inline %s asString(const %s* const atom[]) {
|
|
|
|
|
%s source;
|
2019-09-17 10:22:56 +01:00
|
|
|
for (int i = 0; atom[i] != NULL; i++) {
|
|
|
|
|
source += atom[i];
|
|
|
|
|
}
|
|
|
|
|
return source;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace atoms
|
|
|
|
|
} // namespace webdriver
|
2024-12-28 11:39:00 +01:00
|
|
|
|
2019-09-17 10:22:56 +01:00
|
|
|
#endif // %s
|
2019-10-17 13:56:30 -07:00
|
|
|
""" % (string_type, char_type, string_type, define_guard))
|
2019-09-17 10:22:56 +01:00
|
|
|
|
2019-11-29 05:18:34 -08:00
|
|
|
def generate_cc_source(out, js_map, utf8):
|
2019-09-17 10:22:56 +01:00
|
|
|
out.write("""%s
|
|
|
|
|
|
|
|
|
|
/* AUTO GENERATED - DO NOT EDIT BY HAND */
|
|
|
|
|
|
|
|
|
|
#include <stddef.h> // For NULL.
|
|
|
|
|
#include "atoms.h"
|
|
|
|
|
|
|
|
|
|
namespace webdriver {
|
|
|
|
|
namespace atoms {
|
2024-12-28 11:39:00 +01:00
|
|
|
|
2019-09-17 10:22:56 +01:00
|
|
|
""" % _copyright)
|
|
|
|
|
|
|
|
|
|
for (name, file) in js_map.items():
|
|
|
|
|
contents = open(file, "r").read()
|
2019-11-29 05:18:34 -08:00
|
|
|
write_atom_literal(out, name, contents, "cc", utf8)
|
2019-09-17 10:22:56 +01:00
|
|
|
|
|
|
|
|
out.write("""
|
|
|
|
|
} // namespace atoms
|
|
|
|
|
} // namespace webdriver
|
|
|
|
|
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
def generate_java_source(file_name, out, preamble, js_map):
|
|
|
|
|
if not file_name.endswith(".java"):
|
|
|
|
|
raise RuntimeError("File name must end in .java")
|
|
|
|
|
class_name = os.path.basename(file_name[:-5])
|
|
|
|
|
|
|
|
|
|
out.write(_copyright)
|
|
|
|
|
out.write("\n")
|
|
|
|
|
out.write(preamble)
|
|
|
|
|
out.write("")
|
|
|
|
|
out.write("""
|
|
|
|
|
public enum %s {
|
|
|
|
|
|
|
|
|
|
// AUTO GENERATED - DO NOT EDIT BY HAND
|
|
|
|
|
""" % class_name)
|
|
|
|
|
|
|
|
|
|
for (name, file) in js_map.items():
|
|
|
|
|
contents = open(file, "r").read()
|
2019-11-29 05:18:34 -08:00
|
|
|
write_atom_literal(out, name, contents, "java", True)
|
2019-09-17 10:22:56 +01:00
|
|
|
|
|
|
|
|
out.write("""
|
|
|
|
|
;
|
|
|
|
|
private final String value;
|
|
|
|
|
|
|
|
|
|
public String getValue() {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
|
return getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
%s(String value) {
|
|
|
|
|
this.value = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
""" % class_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv=[]):
|
|
|
|
|
lang = argv[1]
|
|
|
|
|
file_name = argv[2]
|
|
|
|
|
preamble = argv[3]
|
2019-11-29 05:18:34 -08:00
|
|
|
utf8 = (argv[4] == "true")
|
2019-09-17 10:22:56 +01:00
|
|
|
|
|
|
|
|
js_map = {}
|
2019-11-29 05:18:34 -08:00
|
|
|
for i in range(5, len(argv), 2):
|
2019-09-17 10:22:56 +01:00
|
|
|
js_map[argv[i]] = argv[i + 1]
|
|
|
|
|
|
|
|
|
|
with open(file_name, "w") as out:
|
|
|
|
|
if "cc" == lang:
|
2019-11-29 05:18:34 -08:00
|
|
|
generate_cc_source(out, js_map, utf8)
|
2019-09-17 10:22:56 +01:00
|
|
|
elif "hdecl" == lang:
|
2019-11-29 05:18:34 -08:00
|
|
|
generate_header(file_name, out, js_map, True, utf8)
|
2019-09-17 10:22:56 +01:00
|
|
|
elif "hh" == lang:
|
2019-11-29 05:18:34 -08:00
|
|
|
generate_header(file_name, out, js_map, False, utf8)
|
2019-09-17 10:22:56 +01:00
|
|
|
elif "java" == lang:
|
|
|
|
|
generate_java_source(file_name, out, preamble, js_map)
|
|
|
|
|
else:
|
|
|
|
|
raise RuntimeError("Unknown lang: %s" % lang)
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main(sys.argv)
|