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
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
:
18 getElementsByTagNamesNS(node
, ns
, names
, 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;
33 if (pFactory->getElementId(nDefine, Element, nResource, nElementId))
35 const Id nId = pFactory->getResourceId(nDefine, Element);
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));
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))
73 for namespace
in [ns
.getAttribute("name") for ns
in model
.getElementsByTagName("namespace")]:
74 id = namespace
.replace('-', '_')
76 pResult = OOXMLFactory_%s::getInstance();
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;
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));
110 def fastTokenToId(model
):
112 std::string fastTokenToId(sal_uInt32 nToken)
117 switch (oox::getNamespace(nToken))
121 for alias
in sorted(ooxUrlAliases
.values()):
122 if alias
not in aliases
:
123 aliases
.append(alias
)
124 print(""" case oox::NMSP_%s:
126 break;""" % (alias
, alias
))
129 switch (nToken & 0xffff)
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
:
136 print(""" case oox::XML_%s:
138 break;""" % (token
, token
))
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);
161 for url
in sorted(ooxUrlAliases
.keys()):
162 print(""" mxFastParser->registerNamespace("%s", oox::NMSP_%s);""" % (url
, ooxUrlAliases
[url
]))
172 def createImpl(model
):
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"
181 for namespace
in [ns
.getAttribute("name") for ns
in model
.getElementsByTagName("namespace")]:
182 print('#include "OOXMLFactory_%s.hxx"' % namespace
)
184 print("""namespace writerfilter {
187 using namespace com::sun::star;
192 createFastChildContextFromFactory(model
)
193 getFactoryForNamespace(model
)
194 createFastChildContextFromStart(model
)
199 def parseNamespaces(fro
):
201 for i
in sock
.readlines():
203 alias
, url
= line
.split(' ')[1:] # first column is ID, not interesting for us
204 ooxUrlAliases
[url
] = alias
208 namespacesPath
= sys
.argv
[1]
210 parseNamespaces(namespacesPath
)
211 modelPath
= sys
.argv
[2]
212 model
= minidom
.parse(modelPath
)
215 # vim:set shiftwidth=4 softtabstop=4 expandtab: