File: gennames.py

package info (click to toggle)
kde4libs 4%3A4.14.2-5%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 82,428 kB
  • ctags: 99,415
  • sloc: cpp: 761,864; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (294 lines) | stat: -rwxr-xr-x 9,427 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/python
# -*- coding: utf-8 -*-

# List of namespaces.The 2nd part in the array states whether the attributes
# should be in the namespace
namespaces = {
    "xhtml": ['"http://www.w3.org/1999/xhtml"', False, 0 ],
    "empty": [ "DOMString()", False, 1 ],
    "svg":   ['"http://www.w3.org/2000/svg"', False, 2 ],
    "xlink": ['"http://www.w3.org/1999/xlink"', True, 3 ],
    "xmlns":   ['"http://www.w3.org/2000/xmlns/"', True, 4 ],
    "xml":   ['"http://www.w3.org/XML/1998/namespace"', True, 5 ],
}

cache = {}
namesList = []

additionalCaseSensitiveAttrs = []
lastCaseInsensitiveAttr      = 0


def constName(prefix, ns, i):
    if namespaces[ns][1]:
        return (prefix + "_%s_%s") % (ns.upper(), i.upper().replace("-", "_"));
    else:
        return (prefix + "_%s") % i.upper().replace("-", "_");

def parseFile(fname, ns, isAttrs, isHtmlAttrs):
    global cache
    global namesList

    global additionalCaseSensitiveAttrs
    global lastCaseInsensitiveAttr

    prefix = "ID"
    if isAttrs:
        prefix = "ATTR"

    f = open(fname, "r")
    for i in f.xreadlines():
        i = i.strip()
        if i.startswith("#") or i == "": continue
        if isHtmlAttrs and i.startswith("END_CI"):
            lastCaseInsensitiveAttr = len(cache)
            continue

        # don't add it twice to names list
        cons = constName(prefix, ns, i)
        alreadyDefined = [k for k in namesList if k[2] == cons]
        if len(alreadyDefined) > 0:
            continue

        if i.upper() == "TEXT" and isAttrs: # hack to get ID_TEXT and ATTR_TEXT be different
            key = len(cache) + 1
            cache["TEXT#"] = -1
        else:
            key = cache.get(i, len(cache) + 1)

        if isHtmlAttrs and lastCaseInsensitiveAttr > 0 and key <= len(cache):
            additionalCaseSensitiveAttrs.append(cons)

        cache[i] = key
        namesList.append([key, i, cons, ns])
    f.close();

def parseTags(fname, ns):
    parseFile(fname, ns, False, False)

def parseAttrs(fname, ns, isHtml):
    parseFile(fname, ns, True, isHtml)

# parse htmltags.in
parseTags("htmltags.in", "xhtml");
lastTagId = len(cache)

# parse htmlattrs.in file
parseAttrs("htmlattrs.in", "xhtml", True)
lastAttrId = len(cache)

# parse SVG tags, attrs
parseTags("../svg/svgtags.in", "svg")
parseAttrs("../svg/svgattrs.in", "svg", False)

# parse XLink attrs
parseAttrs("../svg/xlinkattrs.in", "xlink", False)

# and XML attrs
parseAttrs("xmlattrs.in", "xml", False)

# sort the list
def func(a, b):
    return cmp(a[0], b[0]) or -cmp(a[2], b[2])
namesList.sort(func)

########################################
# END OF PARSING, START WRITING FILES
########################################

out = open("htmlnames.h", "w")
out.write("/* This file is automatically generated from htmltags.in and htmlattrs.in by gennames.py, do not edit */\n")
out.write("/* Copyright 2008 Vyacheslav Tokarev */\n")
out.write("\n")
out.write("#ifndef HTMLNames_h\n")
out.write("#define HTMLNames_h\n")
out.write("\n")
out.write("#include \"misc/idstring.h\"\n")
out.write("\n")

# Print out namespace string constants
for n in namespaces.keys():
    uri = namespaces[n][0];
    if uri.startswith('"'):
        out.write("#define %s_NAMESPACE %s\n" % (n.upper(), uri))

# Now the main macros and constants
out.write("namespace DOM {\n\
\n\
#define NodeImpl_IdNSMask    0xffff0000\n\
#define NodeImpl_IdLocalMask 0x0000ffff\n\
\n");

for n in namespaces.keys():
    out.write("const quint32 %sNamespace = %d;\n" % (n, namespaces[n][2]));

out.write("const quint16 anyNamespace = 0xffff;\n\
const quint16 anyLocalName = 0xffff;\n\
const quint16 emptyPrefix = 0;\n\
const quint16 xmlPrefix = 1;\n\
const quint16 xmlnsPrefix = 2;\n\
\n\
inline quint16 localNamePart(quint32 id) { return id & NodeImpl_IdLocalMask; }\n\
inline quint16 namespacePart(quint32 id) { return (((unsigned int)id) & NodeImpl_IdNSMask) >> 16; }\n\
inline quint32 makeId(quint16 n, quint16 l) { return (n << 16) | l; }\n\
\n\
const quint32 anyQName = makeId(anyNamespace, anyLocalName);\n\
\n\
}\n")
out.write("\n")


# Now the ATTR_ and ID_ constants
out.write("\n")
for i in namesList:
    if i[2].startswith("ATTR"):
        attrNS = i[3]
        useNS  = "empty"
        if namespaces[attrNS][1]:
            useNS = attrNS
        out.write("#define %s ((DOM::%s << 16) | %d)\n" % (i[2], useNS + "Namespace", i[0]))
    else:
        out.write("#define %s %d\n" % (i[2], i[0]))
out.write("#define ID_LAST_TAG %d\n" % (lastTagId))
out.write("#define ID_CLOSE_TAG 16384\n")
out.write("#define ATTR_LAST_ATTR %d\n" % (lastAttrId))
out.write("#define ATTR_LAST_CI_ATTR %d\n" % (lastCaseInsensitiveAttr))
out.write("\n")
s = "((localNamePart(id)) > ATTR_LAST_CI_ATTR"
for i in additionalCaseSensitiveAttrs:
    s = s + " || (id) == %s" % i
s = s + ")"
out.write("#define caseSensitiveAttr(id) (%s)\n" % s)
out.write("\n")
out.write("namespace khtml {\n\
\n\
class NamespaceFactory {\n\
public:\n\
    static IDTable<NamespaceFactory>* idTable() {\n\
        return s_idTable;\n\
    }\n\
    static IDTable<NamespaceFactory>* initIdTable();\n\
protected:\n\
    static IDTable<NamespaceFactory>* s_idTable;\n\
};\n\
\n\
class LocalNameFactory {\n\
public:\n\
    static IDTable<LocalNameFactory>* idTable() {\n\
        return s_idTable;\n\
    }\n\
    static IDTable<LocalNameFactory>* initIdTable();\n\
protected:\n\
    static IDTable<LocalNameFactory>* s_idTable;\n\
};\n\
\n\
class PrefixFactory {\n\
public:\n\
    static IDTable<PrefixFactory>* idTable() {\n\
        return s_idTable;\n\
    }\n\
    static IDTable<PrefixFactory>* initIdTable();\n\
protected:\n\
    static IDTable<PrefixFactory>* s_idTable;\n\
};\n")
out.write("\n")
out.write("}\n")
out.write("\n")
out.write("namespace DOM {\n\
\n\
    typedef khtml::IDString<khtml::NamespaceFactory> NamespaceName;\n\
    typedef khtml::IDString<khtml::LocalNameFactory> LocalName;\n\
    typedef khtml::IDString<khtml::PrefixFactory> PrefixName;\n\
    extern PrefixName emptyPrefixName;\n\
    extern LocalName emptyLocalName;\n\
    extern NamespaceName emptyNamespaceName;\n\
\n\
    QString getPrintableName(int id);\n\n")
out.write("}\n\n")
out.write("#endif\n")
out.close()

temp = ""
prev = 0
for i in namesList:
    if prev and prev[0] == i[0]: continue
    if i[2] == "ID_TEXT" or i[2] == "ID_COMMENT":
        temp = temp + ("    s_idTable->addHiddenMapping(%s, \"%s\");\n" % (i[2], i[1]))
    else:
        temp = temp + ("    s_idTable->addStaticMapping(localNamePart(%s), \"%s\");\n" % (i[2], i[1]))
    prev = i

out = open("htmlnames.cpp", "w")
out.write("#include \"misc/htmlnames.h\"\n")
out.write("#include \"dom/dom_string.h\"\n")
out.write("\nusing namespace DOM;\n")
out.write("\n")
out.write("namespace khtml {\n\n")
out.write("IDTable<NamespaceFactory>* NamespaceFactory::s_idTable;\n\
IDTable<NamespaceFactory>* NamespaceFactory::initIdTable()\n\
{\n\
    if (s_idTable) return s_idTable; // Can happen if KHTMLGlobal was recreated..\n\
    s_idTable = new IDTable<NamespaceFactory>();\n");
for n in namespaces.keys():
    uri = namespaces[n][0];
    if uri.startswith('"'):
        out.write("    s_idTable->addStaticMapping(DOM::%sNamespace, %s_NAMESPACE);\n" % (n, n.upper()))
    else:
        out.write("    s_idTable->addStaticMapping(DOM::%sNamespace, %s);\n" % (n, uri))

out.write("    return s_idTable;\n\
}\n\
\n\
IDTable<LocalNameFactory>* LocalNameFactory::s_idTable;\n\
IDTable<LocalNameFactory>* LocalNameFactory::initIdTable()\n\
{\n\
    if (s_idTable) return s_idTable; // Can happen if KHTMLGlobal was recreated..\n\
    s_idTable = new IDTable<LocalNameFactory>();\n\
    s_idTable->addStaticMapping(0, DOMString());\n\
%s\
    return s_idTable;\n\
}\n\
\n\
IDTable<PrefixFactory>* PrefixFactory::s_idTable;\n\
IDTable<PrefixFactory>* PrefixFactory::initIdTable()\n\
{\n\
    if (s_idTable) return s_idTable; // Can happen if KHTMLGlobal was recreated..\n\
    s_idTable = new IDTable<PrefixFactory>();\n\
    s_idTable->addStaticMapping(DOM::emptyPrefix, DOMString());\n\
    s_idTable->addStaticMapping(DOM::xmlPrefix, \"xml\");\n\
    s_idTable->addStaticMapping(DOM::xmlnsPrefix, \"xmlns\");\n\
    return s_idTable;\n\
}\n" % temp)
out.write("\n}\n")
out.write("\n")
out.write("namespace DOM {\n\n")
out.write("LocalName emptyLocalName;// = LocalName::fromId(0);\n")
out.write("PrefixName emptyPrefixName;// = PrefixName::fromId(0);\n")
out.write("NamespaceName emptyNamespaceName;// = NamespaceName::fromId(0);\n")
out.write("\n")
out.write("""QString getPrintableName(int id) {
    QString local = QString("null");
    QString namespacename = QString("null");

    if (localNamePart(id) != anyLocalName) {
        DOMString localName = LocalName::fromId(localNamePart(id)).toString();
        if (localName.implementation())
            local = localName.string();
    } else {
        local = "*";
    }

    if (namespacePart(id) != anyNamespace) {
        DOMString namespaceName = NamespaceName::fromId(namespacePart(id)).toString();
        if (namespaceName.implementation())
            namespacename = namespaceName.string();
    } else {
        namespacename = "*";
    }
    return "{ns:" + QString::number(namespacePart(id)) + ",[" + namespacename + "] local:" + QString::number(localNamePart(id)) + ",[" + local + "]}";
}\n""")
out.write("\n}\n")
out.close()

# kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;