LibreOffice Module oox (master)  1
namespaces.py
Go to the documentation of this file.
1 # This file is part of the LibreOffice project.
2 #
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 #
7 # This file incorporates work covered by the following license notice:
8 #
9 # Licensed to the Apache Software Foundation (ASF) under one or more
10 # contributor license agreements. See the NOTICE file distributed
11 # with this work for additional information regarding copyright
12 # ownership. The ASF licenses this file to you under the Apache
13 # License, Version 2.0 (the "License"); you may not use this file
14 # except in compliance with the License. You may obtain a copy of
15 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
16 #
17 
18 import sys, re
19 
20 infile_name = sys.argv[1]
21 id_out_name = sys.argv[2]
22 name_out_name = sys.argv[3]
23 txt_out_name = sys.argv[4]
24 instrict_name = sys.argv[5]
25 strict_out_name = sys.argv[6]
26 
27 # parse input file
28 
29 namespaces = {}
30 
31 with open(infile_name) as infile:
32  for line in infile:
33  line = line.strip()
34  # trim comments
35  line = line.split('#')[0]
36  # skip empty lines
37  if line:
38  # check for valid characters
39  m = re.match(r'([a-zA-Z][a-zA-Z0-9]*)\s+([a-zA-Z0-9-.:\/]+)\s*$', line)
40  if not m:
41  sys.exit('Invalid character in input data: ' + line)
42  namespaces[m.group(1)] = m.group(2)
43 
44 # OOXML strict namespaces
45 
46 namespaces_strict = {}
47 with open(instrict_name) as infile_strict:
48  for line in infile_strict:
49  line = line.strip()
50  # trim comments
51  line = line.split('#')[0]
52  # skip empty lines
53  if line:
54  # check for valid characters
55  m = re.match(r'([a-zA-Z][a-zA-Z0-9]*)\s+([a-zA-Z0-9-.:\/]+)\s*$', line)
56  if not m:
57  sys.exit("Error: invalid character in input data: " + line)
58  namespaces_strict[m.group(1)] = m.group(2)
59 
60 # generate output files
61 
62 idfile = open(id_out_name, 'w')
63 namefile = open(name_out_name, 'w')
64 txtfile = open(txt_out_name, 'w')
65 namefile_strict = open(strict_out_name, 'w')
66 
67 # number of bits to shift the namespace identifier
68 shift = 16
69 
70 idfile.write("const size_t NMSP_SHIFT = {};\n".format(shift))
71 
72 i = 1;
73 for token in sorted(namespaces.keys()):
74  idfile.write("const sal_Int32 NMSP_{} = {} << NMSP_SHIFT;\n".format(token, i))
75  cur_id = i << shift
76  namefile.write("{{ {}, \"{}\" }},\n".format(cur_id, namespaces[token]))
77  namefile_strict.write("{{ {}, \"{}\" }},\n".format(cur_id, namespaces_strict[token]))
78  txtfile.write("{} {} {}\n".format(cur_id, token, namespaces[token]))
79  txtfile.write("{} {} {}\n".format(cur_id, token, namespaces_strict[token]))
80  i += 1
81 
82 idfile.close()
83 namefile.close()
84 namefile_strict.close()
85 txtfile.close()