summaryrefslogtreecommitdiff
path: root/writerfilter/source/ooxml/modelpreprocess.py
blob: f81496094d473e2663305b97df2d1dd9787d1e38 (plain)
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
#!/usr/bin/env python
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

from __future__ import print_function
from xml.dom import minidom
import sys


def prefixForGrammar(namespace):
    ns = namespace.getElementsByTagName("grammar")[0].getAttribute("ns")
    return ooxUrlAliases[ns]


def parseNamespaceAliases(node):
    ret = {}
    for k, v in list(node.attributes.items()):
        if k.startswith("xmlns:"):
            ret[k.replace('xmlns:', '')] = v
    return ret


def parseNamespaces(fro):
    sock = open(fro)
    for i in sock.readlines():
        line = i.strip()
        alias, url = line.split(' ')[1:]
        ooxUrlAliases[url] = alias
    sock.close()


def check(model):
    defines = [i.getAttribute("name") for i in model.getElementsByTagName("define")]
    for reference in [i.getAttribute("name") for i in model.getElementsByTagName("ref")]:
        if reference not in defines:
            raise Exception("Unknown define element with name '%s'" % reference)
    for start in [i.getAttribute("name") for i in model.getElementsByTagName("start")]:
        if start not in defines:
            raise Exception("Unknown start element with name '%s'" % start)


def preprocess(model):
    modelNode = [i for i in model.childNodes if i.localName == "model"][0]
    # Alias -> URL, based on "xmlns:" attributes.
    modelNamespaceAliases = parseNamespaceAliases(modelNode)
    for i in modelNode.getElementsByTagName("namespace"):
        grammarprefix = prefixForGrammar(i)

        grammar = i.getElementsByTagName("grammar")[0]

        for j in i.getElementsByTagName("element") + i.getElementsByTagName("attribute"):
            # prefix
            prefix = ""
            if ":" in j.getAttribute("name"):
                nameprefix = j.getAttribute("name").split(':')[0]
                prefix = ooxUrlAliases[modelNamespaceAliases[nameprefix]]
            elif j.localName == "attribute":
                if grammar.getAttribute("attributeFormDefault") == "qualified":
                    prefix = grammarprefix
            else:
                prefix = grammarprefix

            # localname
            if ":" in j.getAttribute("name"):
                localname = j.getAttribute("name").split(':')[1]
            else:
                localname = j.getAttribute("name")

            # set the attributes
            j.setAttribute("prefix", prefix)
            j.setAttribute("localname", localname)


namespacesPath = sys.argv[1]
modelPath = sys.argv[2]

# URL -> alias, from oox
ooxUrlAliases = {}
parseNamespaces(namespacesPath)

model = minidom.parse(modelPath)
check(model)
preprocess(model)
model.writexml(sys.stdout)

# vim:set shiftwidth=4 softtabstop=4 expandtab: