nss: upgrade to release 3.73
[LibreOffice.git] / writerfilter / source / ooxml / factoryimpl.py
blob4fdbfda2ee4bbf1f8400c2fd816b73d38891841e
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 xml.dom import minidom
11 import sys
14 def getElementsByTagNamesNS(parent, ns, names, ret=minidom.NodeList()):
15 for node in parent.childNodes:
16 if node.nodeType == minidom.Node.ELEMENT_NODE and node.namespaceURI == ns and node.tagName in names:
17 ret.append(node)
18 getElementsByTagNamesNS(node, ns, names, ret)
19 return ret
22 def createFastChildContextFromFactory(model):
23 print("""uno::Reference<xml::sax::XFastContextHandler> OOXMLFactory::createFastChildContextFromFactory
24 (OOXMLFastContextHandler* pHandler, OOXMLFactory_ns::Pointer_t pFactory, Token_t Element)
26 uno::Reference <xml::sax::XFastContextHandler> aResult;
27 const Id nDefine = pHandler->getDefine();
29 if (pFactory.get() != NULL)
31 ResourceType nResource;
32 Id nElementId;
33 if (pFactory->getElementId(nDefine, Element, nResource, nElementId))
35 const Id nId = pFactory->getResourceId(nDefine, Element);
37 switch (nResource)
38 {""")
39 resources = [
40 "List", "Integer", "Hex", "HexColor", "String",
41 "TwipsMeasure_asSigned", "TwipsMeasure_asZero",
42 "HpsMeasure", "Boolean", "MeasurementOrPercent",
44 for resource in [r.getAttribute("resource") for r in model.getElementsByTagName("resource")]:
45 if resource not in resources:
46 resources.append(resource)
47 print(""" case ResourceType::%s:
48 aResult.set(OOXMLFastHelper<OOXMLFastContextHandler%s>::createAndSetParentAndDefine(pHandler, Element, nId, nElementId));
49 break;""" % (resource, resource))
50 print(""" case ResourceType::Any:
51 aResult.set(createFastChildContextFromStart(pHandler, Element));
52 break;
53 default:
54 break;
60 return aResult;
62 """)
65 def getFactoryForNamespace(model):
66 print("""OOXMLFactory_ns::Pointer_t OOXMLFactory::getFactoryForNamespace(Id nId)
68 OOXMLFactory_ns::Pointer_t pResult;
70 switch (oox::getNamespace(nId))
71 {""")
73 for namespace in [ns.getAttribute("name") for ns in model.getElementsByTagName("namespace")]:
74 id = namespace.replace('-', '_')
75 print(""" case NN_%s:
76 pResult = OOXMLFactory_%s::getInstance();
77 break;""" % (id, id))
78 print(""" default:
79 break;
82 return pResult;
84 """)
87 def createFastChildContextFromStart(model):
88 print("""uno::Reference<xml::sax::XFastContextHandler> OOXMLFactory::createFastChildContextFromStart
89 (OOXMLFastContextHandler* pHandler, Token_t Element)
91 uno::Reference<xml::sax::XFastContextHandler> aResult;
92 OOXMLFactory_ns::Pointer_t pFactory;
94 """)
96 for namespace in [ns.getAttribute("name") for ns in model.getElementsByTagName("namespace")]:
97 id = namespace.replace('-', '_')
98 print(""" if (!aResult.is())
100 pFactory = getFactoryForNamespace(NN_%s);
101 aResult.set(createFastChildContextFromFactory(pHandler, pFactory, Element));
102 }""" % id)
104 print("""
105 return aResult;
107 """)
110 def fastTokenToId(model):
111 print("""
112 std::string fastTokenToId(sal_uInt32 nToken)
114 std::string sResult;
115 #ifdef DBG_UTIL
117 switch (oox::getNamespace(nToken))
118 {""")
120 aliases = []
121 for alias in sorted(ooxUrlAliases.values()):
122 if alias not in aliases:
123 aliases.append(alias)
124 print(""" case oox::NMSP_%s:
125 sResult += "%s:";
126 break;""" % (alias, alias))
127 print(""" }
129 switch (nToken & 0xffff)
130 {""")
132 tokens = [""]
133 for token in [t.getAttribute("localname") for t in getElementsByTagNamesNS(model, "http://relaxng.org/ns/structure/1.0", ["element", "attribute"])]:
134 if token not in tokens:
135 tokens.append(token)
136 print(""" case oox::XML_%s:
137 sResult += "%s";
138 break;""" % (token, token))
140 print(""" }
141 #else
142 (void)nToken;
143 #endif
144 return sResult;
146 """)
149 def getFastParser():
150 print("""uno::Reference <xml::sax::XFastParser> OOXMLStreamImpl::getFastParser()
152 if (!mxFastParser.is())
154 mxFastParser = css::xml::sax::FastParser::create(mxContext);
155 // the threaded parser is about 20% slower loading writer documents
156 css::uno::Reference< css::lang::XInitialization > xInit( mxFastParser, css::uno::UNO_QUERY_THROW );
157 css::uno::Sequence< css::uno::Any > args(1);
158 args[0] <<= OUString("DisableThreadedParser");
159 xInit->initialize(args);
160 """)
161 for url in sorted(ooxUrlAliases.keys()):
162 print(""" mxFastParser->registerNamespace("%s", oox::NMSP_%s);""" % (url, ooxUrlAliases[url]))
163 print(""" }
165 return mxFastParser;
168 /// @endcond
169 }}""")
172 def createImpl(model):
173 print("""
174 #include <com/sun/star/xml/sax/FastParser.hpp>
175 #include <com/sun/star/lang/XInitialization.hpp>
176 #include "ooxml/OOXMLFactory.hxx"
177 #include "ooxml/OOXMLFastHelper.hxx"
178 #include "ooxml/OOXMLStreamImpl.hxx"
179 """)
181 for namespace in [ns.getAttribute("name") for ns in model.getElementsByTagName("namespace")]:
182 print('#include "OOXMLFactory_%s.hxx"' % namespace)
184 print("""namespace writerfilter {
185 namespace ooxml {
187 using namespace com::sun::star;
189 /// @cond GENERATED
190 """)
192 createFastChildContextFromFactory(model)
193 getFactoryForNamespace(model)
194 createFastChildContextFromStart(model)
195 fastTokenToId(model)
196 getFastParser()
199 def parseNamespaces(fro):
200 sock = open(fro)
201 for i in sock.readlines():
202 line = i.strip()
203 alias, url = line.split(' ')[1:] # first column is ID, not interesting for us
204 ooxUrlAliases[url] = alias
205 sock.close()
208 namespacesPath = sys.argv[1]
209 ooxUrlAliases = {}
210 parseNamespaces(namespacesPath)
211 modelPath = sys.argv[2]
212 model = minidom.parse(modelPath)
213 createImpl(model)
215 # vim:set shiftwidth=4 softtabstop=4 expandtab: