Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / writerfilter / source / ooxml / factoryimpl.py
blob41b42929563a9f3247a86ecf47a4da3250654b3e
1 #!/usr/bin/env python
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 from __future__ import print_function
11 from xml.dom import minidom
12 import sys
15 def getElementsByTagNamesNS(parent, ns, names, ret=minidom.NodeList()):
16 for node in parent.childNodes:
17 if node.nodeType == minidom.Node.ELEMENT_NODE and node.namespaceURI == ns and node.tagName in names:
18 ret.append(node)
19 getElementsByTagNamesNS(node, ns, names, ret)
20 return ret
23 def createFastChildContextFromFactory(model):
24 print("""uno::Reference<xml::sax::XFastContextHandler> OOXMLFactory::createFastChildContextFromFactory
25 (OOXMLFastContextHandler* pHandler, OOXMLFactory_ns::Pointer_t pFactory, Token_t Element)
27 uno::Reference <xml::sax::XFastContextHandler> aResult;
28 const Id nDefine = pHandler->getDefine();
30 if (pFactory.get() != NULL)
32 ResourceType nResource;
33 Id nElementId;
34 if (pFactory->getElementId(nDefine, Element, nResource, nElementId))
36 const Id nId = pFactory->getResourceId(nDefine, Element);
38 switch (nResource)
39 {""")
40 resources = [
41 "List", "Integer", "Hex", "HexColor", "String",
42 "TwipsMeasure_asSigned", "TwipsMeasure_asZero",
43 "HpsMeasure", "Boolean", "MeasurementOrPercent",
45 for resource in [r.getAttribute("resource") for r in model.getElementsByTagName("resource")]:
46 if resource not in resources:
47 resources.append(resource)
48 print(""" case ResourceType::%s:
49 aResult.set(OOXMLFastHelper<OOXMLFastContextHandler%s>::createAndSetParentAndDefine(pHandler, Element, nId, nElementId));
50 break;""" % (resource, resource))
51 print(""" case ResourceType::Any:
52 aResult.set(createFastChildContextFromStart(pHandler, Element));
53 break;
54 default:
55 break;
61 return aResult;
63 """)
66 def getFactoryForNamespace(model):
67 print("""OOXMLFactory_ns::Pointer_t OOXMLFactory::getFactoryForNamespace(Id nId)
69 OOXMLFactory_ns::Pointer_t pResult;
71 switch (oox::getNamespace(nId))
72 {""")
74 for namespace in [ns.getAttribute("name") for ns in model.getElementsByTagName("namespace")]:
75 id = namespace.replace('-', '_')
76 print(""" case NN_%s:
77 pResult = OOXMLFactory_%s::getInstance();
78 break;""" % (id, id))
79 print(""" default:
80 break;
83 return pResult;
85 """)
88 def createFastChildContextFromStart(model):
89 print("""uno::Reference<xml::sax::XFastContextHandler> OOXMLFactory::createFastChildContextFromStart
90 (OOXMLFastContextHandler* pHandler, Token_t Element)
92 uno::Reference<xml::sax::XFastContextHandler> aResult;
93 OOXMLFactory_ns::Pointer_t pFactory;
95 """)
97 for namespace in [ns.getAttribute("name") for ns in model.getElementsByTagName("namespace")]:
98 id = namespace.replace('-', '_')
99 print(""" if (!aResult.is())
101 pFactory = getFactoryForNamespace(NN_%s);
102 aResult.set(createFastChildContextFromFactory(pHandler, pFactory, Element));
103 }""" % id)
105 print("""
106 return aResult;
108 """)
111 def fastTokenToId(model):
112 print("""
113 std::string fastTokenToId(sal_uInt32 nToken)
115 std::string sResult;
116 #ifdef DBG_UTIL
118 switch (oox::getNamespace(nToken))
119 {""")
121 aliases = []
122 for alias in sorted(ooxUrlAliases.values()):
123 if alias not in aliases:
124 aliases.append(alias)
125 print(""" case oox::NMSP_%s:
126 sResult += "%s:";
127 break;""" % (alias, alias))
128 print(""" }
130 switch (nToken & 0xffff)
131 {""")
133 tokens = [""]
134 for token in [t.getAttribute("localname") for t in getElementsByTagNamesNS(model, "http://relaxng.org/ns/structure/1.0", ["element", "attribute"])]:
135 if token not in tokens:
136 tokens.append(token)
137 print(""" case oox::XML_%s:
138 sResult += "%s";
139 break;""" % (token, token))
141 print(""" }
142 #else
143 (void)nToken;
144 #endif
145 return sResult;
147 """)
150 def getFastParser():
151 print("""uno::Reference <xml::sax::XFastParser> OOXMLStreamImpl::getFastParser()
153 if (!mxFastParser.is())
155 mxFastParser = css::xml::sax::FastParser::create(mxContext);
156 // the threaded parser is about 20% slower loading writer documents
157 css::uno::Reference< css::lang::XInitialization > xInit( mxFastParser, css::uno::UNO_QUERY_THROW );
158 css::uno::Sequence< css::uno::Any > args(1);
159 args[0] <<= OUString("DisableThreadedParser");
160 xInit->initialize(args);
161 """)
162 for url in sorted(ooxUrlAliases.keys()):
163 print(""" mxFastParser->registerNamespace("%s", oox::NMSP_%s);""" % (url, ooxUrlAliases[url]))
164 print(""" }
166 return mxFastParser;
169 /// @endcond
170 }}""")
173 def createImpl(model):
174 print("""
175 #include <com/sun/star/xml/sax/FastParser.hpp>
176 #include <com/sun/star/lang/XInitialization.hpp>
177 #include "ooxml/OOXMLFactory.hxx"
178 #include "ooxml/OOXMLFastHelper.hxx"
179 #include "ooxml/OOXMLStreamImpl.hxx"
180 """)
182 for namespace in [ns.getAttribute("name") for ns in model.getElementsByTagName("namespace")]:
183 print('#include "OOXMLFactory_%s.hxx"' % namespace)
185 print("""namespace writerfilter {
186 namespace ooxml {
188 using namespace com::sun::star;
190 /// @cond GENERATED
191 """)
193 createFastChildContextFromFactory(model)
194 getFactoryForNamespace(model)
195 createFastChildContextFromStart(model)
196 fastTokenToId(model)
197 getFastParser()
200 def parseNamespaces(fro):
201 sock = open(fro)
202 for i in sock.readlines():
203 line = i.strip()
204 alias, url = line.split(' ')[1:] # first column is ID, not interesting for us
205 ooxUrlAliases[url] = alias
206 sock.close()
209 namespacesPath = sys.argv[1]
210 ooxUrlAliases = {}
211 parseNamespaces(namespacesPath)
212 modelPath = sys.argv[2]
213 model = minidom.parse(modelPath)
214 createImpl(model)
216 # vim:set shiftwidth=4 softtabstop=4 expandtab: