android: Update app-specific/MIME type icons
[LibreOffice.git] / writerfilter / source / ooxml / factoryimpl.py
blobc68d5ba06160cccb14b8d9a831899331fa1b720d
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{ css::uno::Any(OUString("DisableThreadedParser")) };
158 xInit->initialize(args);
159 """)
160 for url in sorted(ooxUrlAliases.keys()):
161 print(""" mxFastParser->registerNamespace("%s", oox::NMSP_%s);""" % (url, ooxUrlAliases[url]))
162 print(""" }
164 return mxFastParser;
167 /// @endcond
168 }}""")
171 def createImpl(model):
172 print("""
173 #include <com/sun/star/xml/sax/FastParser.hpp>
174 #include <com/sun/star/lang/XInitialization.hpp>
175 #include "ooxml/OOXMLFactory.hxx"
176 #include "ooxml/OOXMLFastHelper.hxx"
177 #include "ooxml/OOXMLStreamImpl.hxx"
178 """)
180 for namespace in [ns.getAttribute("name") for ns in model.getElementsByTagName("namespace")]:
181 print('#include "OOXMLFactory_%s.hxx"' % namespace)
183 print("""namespace writerfilter {
184 namespace ooxml {
186 using namespace com::sun::star;
188 /// @cond GENERATED
189 """)
191 createFastChildContextFromFactory(model)
192 getFactoryForNamespace(model)
193 createFastChildContextFromStart(model)
194 fastTokenToId(model)
195 getFastParser()
198 def parseNamespaces(fro):
199 sock = open(fro)
200 for i in sock.readlines():
201 line = i.strip()
202 alias, url = line.split(' ')[1:] # first column is ID, not interesting for us
203 ooxUrlAliases[url] = alias
204 sock.close()
207 namespacesPath = sys.argv[1]
208 ooxUrlAliases = {}
209 parseNamespaces(namespacesPath)
210 modelPath = sys.argv[2]
211 model = minidom.parse(modelPath)
212 createImpl(model)
214 # vim:set shiftwidth=4 softtabstop=4 expandtab: