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
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
:
19 getElementsByTagNamesNS(node
, ns
, names
, 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;
34 if (pFactory->getElementId(nDefine, Element, nResource, nElementId))
36 const Id nId = pFactory->getResourceId(nDefine, Element);
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));
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))
74 for namespace
in [ns
.getAttribute("name") for ns
in model
.getElementsByTagName("namespace")]:
75 id = namespace
.replace('-', '_')
77 pResult = OOXMLFactory_%s::getInstance();
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;
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));
111 def fastTokenToId(model
):
113 std::string fastTokenToId(sal_uInt32 nToken)
118 switch (oox::getNamespace(nToken))
122 for alias
in sorted(ooxUrlAliases
.values()):
123 if alias
not in aliases
:
124 aliases
.append(alias
)
125 print(""" case oox::NMSP_%s:
127 break;""" % (alias
, alias
))
130 switch (nToken & 0xffff)
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
:
137 print(""" case oox::XML_%s:
139 break;""" % (token
, token
))
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);
162 for url
in sorted(ooxUrlAliases
.keys()):
163 print(""" mxFastParser->registerNamespace("%s", oox::NMSP_%s);""" % (url
, ooxUrlAliases
[url
]))
173 def createImpl(model
):
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"
182 for namespace
in [ns
.getAttribute("name") for ns
in model
.getElementsByTagName("namespace")]:
183 print('#include "OOXMLFactory_%s.hxx"' % namespace
)
185 print("""namespace writerfilter {
188 using namespace com::sun::star;
193 createFastChildContextFromFactory(model
)
194 getFactoryForNamespace(model
)
195 createFastChildContextFromStart(model
)
200 def parseNamespaces(fro
):
202 for i
in sock
.readlines():
204 alias
, url
= line
.split(' ')[1:] # first column is ID, not interesting for us
205 ooxUrlAliases
[url
] = alias
209 namespacesPath
= sys
.argv
[1]
211 parseNamespaces(namespacesPath
)
212 modelPath
= sys
.argv
[2]
213 model
= minidom
.parse(modelPath
)
216 # vim:set shiftwidth=4 softtabstop=4 expandtab: