[Libreoffice-commits] core.git: writerfilter/CustomTarget_source.mk writerfilter/source
Miklos Vajna
vmiklos at collabora.co.uk
Sat Jul 12 01:18:59 PDT 2014
writerfilter/CustomTarget_source.mk | 6
writerfilter/source/ooxml/factoryimpl.py | 204 +++++++++++++++++++++
writerfilter/source/ooxml/factoryimpl.xsl | 274 -----------------------------
writerfilter/source/ooxml/factorytools.xsl | 20 --
4 files changed, 207 insertions(+), 297 deletions(-)
New commits:
commit a7706f8a4e79fd36a296e988f7f852dfd549a16f
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date: Sat Jul 12 10:09:53 2014 +0200
writerfilter: convert factoryimpl to Python
Change-Id: I2065215db5da0a379e902a74eff419d5c6068d21
diff --git a/writerfilter/CustomTarget_source.mk b/writerfilter/CustomTarget_source.mk
index 989a623..9fdaf00 100644
--- a/writerfilter/CustomTarget_source.mk
+++ b/writerfilter/CustomTarget_source.mk
@@ -79,9 +79,9 @@ writerfilter_SRC_ooxml_Preprocess_py=$(writerfilter_SRC)/ooxml/modelpreprocess.p
writerfilter_SRC_ooxml_QNameToStr_py=$(writerfilter_SRC)/ooxml/qnametostr.py
writerfilter_SRC_ooxml_ResourceIds_py=$(writerfilter_SRC)/ooxml/resourceids.py
-$(writerfilter_GEN_ooxml_Factory_cxx) : $(writerfilter_SRC)/ooxml/factoryimpl.xsl $(writerfilter_GEN_ooxml_Model_processed)
- $(call gb_Output_announce,$(subst $(WORKDIR)/,,$@),build,XSL,1)
- $(call gb_Helper_abbreviate_dirs, $(writerfilter_XSLTCOMMAND) $< $(writerfilter_GEN_ooxml_Model_processed)) > $@
+$(writerfilter_GEN_ooxml_Factory_cxx) : $(writerfilter_SRC)/ooxml/factoryimpl.py $(writerfilter_GEN_ooxml_Model_processed)
+ $(call gb_Output_announce,$(subst $(WORKDIR)/,,$@),build,PY ,1)
+ $(call gb_Helper_abbreviate_dirs, $(writerfilter_PYTHONCOMMAND) $< $(writerfilter_GEN_ooxml_Model_processed)) > $@
$(writerfilter_GEN_ooxml_Factory_hxx) : $(writerfilter_SRC)/ooxml/factoryinc.py $(writerfilter_GEN_ooxml_Model_processed)
$(call gb_Output_announce,$(subst $(WORKDIR)/,,$@),build,PY ,1)
diff --git a/writerfilter/source/ooxml/factoryimpl.py b/writerfilter/source/ooxml/factoryimpl.py
new file mode 100644
index 0000000..0fb9144
--- /dev/null
+++ b/writerfilter/source/ooxml/factoryimpl.py
@@ -0,0 +1,204 @@
+#!/usr/bin/env python
+#
+# This file is part of the LibreOffice project.
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+
+from __future__ import print_function
+from xml.dom import minidom
+import sys
+
+
+def getElementsByTagNamesNS(parent, ns, names, ret=minidom.NodeList()):
+ for node in parent.childNodes:
+ if node.nodeType == minidom.Node.ELEMENT_NODE and node.namespaceURI == ns and node.tagName in names:
+ ret.append(node)
+ getElementsByTagNamesNS(node, ns, names, ret)
+ return ret
+
+
+def createFastChildContextFromFactory(model):
+ print("""uno::Reference<xml::sax::XFastContextHandler> OOXMLFactory::createFastChildContextFromFactory
+(OOXMLFastContextHandler* pHandler, OOXMLFactory_ns::Pointer_t pFactory, Token_t Element)
+{
+ uno::Reference <xml::sax::XFastContextHandler> aResult;
+ Id nDefine = pHandler->getDefine();
+
+ if (pFactory.get() != NULL)
+ {
+ CreateElementMapPointer pMap = pFactory->getCreateElementMap(nDefine);
+ TokenToIdMapPointer pTokenMap = pFactory->getTokenToIdMap(nDefine);
+
+ if (pMap.get() != NULL)
+ {
+ Id nId = (*pTokenMap)[Element];
+ CreateElement aCreateElement = (*pMap)[Element];
+
+ switch (aCreateElement.m_nResource)
+ {""")
+ resources = ["List", "Integer", "Hex", "String", "UniversalMeasure", "Boolean"]
+ for resource in [r.getAttribute("resource") for r in model.getElementsByTagName("resource")]:
+ if resource not in resources:
+ resources.append(resource)
+ print(""" case RT_%s:
+ aResult.set(OOXMLFastHelper<OOXMLFastContextHandler%s>::createAndSetParentAndDefine(pHandler, Element, nId, aCreateElement.m_nId));
+ break;""" % (resource, resource))
+ print(""" case RT_Any:
+ aResult.set(createFastChildContextFromStart(pHandler, Element));
+ break;
+ default:
+ break;
+ }
+
+ }
+ }
+
+ return aResult;
+}
+""")
+
+
+def getFactoryForNamespace(model):
+ print("""OOXMLFactory_ns::Pointer_t OOXMLFactory::getFactoryForNamespace(Id nId)
+{
+ OOXMLFactory_ns::Pointer_t pResult;
+
+ switch (nId & 0xffff0000)
+ {""")
+
+ for namespace in [ns.getAttribute("name") for ns in model.getElementsByTagName("namespace")]:
+ id = namespace.replace('-', '_')
+ print(""" case NN_%s:
+ pResult = OOXMLFactory_%s::getInstance();
+ break;""" % (id, id))
+ print(""" default:
+ break;
+ }
+
+ return pResult;
+}
+""")
+
+
+def createFastChildContextFromStart(model):
+ print("""uno::Reference<xml::sax::XFastContextHandler> OOXMLFactory::createFastChildContextFromStart
+(OOXMLFastContextHandler* pHandler, Token_t Element)
+{
+ uno::Reference<xml::sax::XFastContextHandler> aResult;
+ OOXMLFactory_ns::Pointer_t pFactory;
+
+""")
+
+ for namespace in [ns.getAttribute("name") for ns in model.getElementsByTagName("namespace")]:
+ id = namespace.replace('-', '_')
+ print(""" if (!aResult.is())
+ {
+ pFactory = getFactoryForNamespace(NN_%s);
+ aResult.set(createFastChildContextFromFactory(pHandler, pFactory, Element));
+ }""" % id)
+
+ print("""
+ return aResult;
+}
+""")
+
+
+def fastTokenToId(model):
+ print("""namespace tokenmap {
+struct token { const char* name; Token_t nToken; };
+class Perfect_Hash
+{
+private:
+ static inline unsigned int hash (const char* str, unsigned int len);
+public:
+ static struct token* in_word_set (const char* str, unsigned int len);
+};
+}
+
+#ifdef DEBUG_DOMAINMAPPER
+string fastTokenToId(sal_uInt32 nToken)
+{
+
+ string sResult;
+
+ switch (nToken & 0xffff0000)
+ {""")
+
+ aliases = []
+ for alias in [a.getAttribute("alias") for a in model.getElementsByTagName("namespace-alias")]:
+ if not alias in aliases:
+ aliases.append(alias)
+ print(""" case NS_%s:
+ sResult += "%s:";
+ break;""" % (alias, alias))
+ print(""" }
+
+ switch (nToken & 0xffff)
+ {""")
+
+ tokens = [""]
+ for token in [t.getAttribute("localname") for t in getElementsByTagNamesNS(model, "http://relaxng.org/ns/structure/1.0", ["element", "attribute"])]:
+ if not token in tokens:
+ tokens.append(token)
+ print(""" case OOXML_%s:
+ sResult += "%s";
+ break;""" % (token, token))
+
+ print(""" }
+
+ return sResult;
+}
+#endif
+""")
+
+
+def getFastParser(model):
+ print("""uno::Reference <xml::sax::XFastParser> OOXMLStreamImpl::getFastParser()
+{
+ if (!mxFastParser.is())
+ {
+ mxFastParser = css::xml::sax::FastParser::create(mxContext);
+""")
+ for alias in model.getElementsByTagName("namespace-alias"):
+ print(""" mxFastParser->registerNamespace("%s", NS_%s);""" % (alias.getAttribute("name"), alias.getAttribute("alias")))
+ print(""" }
+
+ return mxFastParser;
+}
+
+/// @endcond
+}}""")
+
+
+def createImpl(model):
+ print("""
+#include <com/sun/star/xml/sax/FastParser.hpp>
+#include "ooxml/OOXMLFactory.hxx"
+#include "ooxml/OOXMLFastHelper.hxx"
+#include "ooxml/OOXMLStreamImpl.hxx"
+""")
+
+ for namespace in [ns.getAttribute("name") for ns in model.getElementsByTagName("namespace")]:
+ print('#include "OOXMLFactory_%s.hxx"' % namespace)
+
+ print("""namespace writerfilter {
+namespace ooxml {
+
+/// @cond GENERATED
+""")
+
+ createFastChildContextFromFactory(model)
+ getFactoryForNamespace(model)
+ createFastChildContextFromStart(model)
+ fastTokenToId(model)
+ getFastParser(model)
+
+
+modelPath = sys.argv[1]
+model = minidom.parse(modelPath)
+createImpl(model)
+
+# vim:set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/writerfilter/source/ooxml/factoryimpl.xsl b/writerfilter/source/ooxml/factoryimpl.xsl
deleted file mode 100644
index cea6198..0000000
--- a/writerfilter/source/ooxml/factoryimpl.xsl
+++ /dev/null
@@ -1,274 +0,0 @@
-<!--
- * This file is part of the LibreOffice project.
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- *
- * This file incorporates work covered by the following license notice:
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed
- * with this work for additional information regarding copyright
- * ownership. The ASF licenses this file to you under the Apache
- * License, Version 2.0 (the "License"); you may not use this file
- * except in compliance with the License. You may obtain a copy of
- * the License at http://www.apache.org/licenses/LICENSE-2.0 .
--->
-<xsl:stylesheet
- version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
- xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
- xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
- xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
- xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
- xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
- xmlns:xlink="http://www.w3.org/1999/xlink"
- xmlns:dc="http://purl.org/dc/elements/1.1/"
- xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
- xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
- xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
- xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
- xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
- xmlns:math="http://www.w3.org/1998/Math/MathML"
- xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
- xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
- xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0"
- xmlns:ooo="http://openoffice.org/2004/office"
- xmlns:ooow="http://openoffice.org/2004/writer"
- xmlns:oooc="http://openoffice.org/2004/calc"
- xmlns:dom="http://www.w3.org/2001/xml-events"
- xmlns:xforms="http://www.w3.org/2002/xforms"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
- xmlns:rng="http://relaxng.org/ns/structure/1.0"
- xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
- xmlns:UML = 'org.omg.xmi.namespace.UML' xml:space="default">
- <xsl:output method="text" />
- <xsl:param name="prefix"/>
-
- <xsl:include href="factorytools.xsl"/>
-
- <xsl:key name="sprms-with-code" match="//resource/element[@tokenid]" use="@tokenid"/>
-
-<xsl:template name="factorycreatecontextfromfactory">
- <xsl:text>
-uno::Reference< xml::sax::XFastContextHandler > OOXMLFactory::createFastChildContextFromFactory
-(OOXMLFastContextHandler * pHandler, OOXMLFactory_ns::Pointer_t pFactory, Token_t Element)
-{
- uno::Reference < xml::sax::XFastContextHandler > aResult;
- Id nDefine = pHandler->getDefine();
-
- if (pFactory.get() != NULL)
- {
- CreateElementMapPointer pMap = pFactory->getCreateElementMap(nDefine);
- TokenToIdMapPointer pTokenMap = pFactory->getTokenToIdMap(nDefine);
-
- if (pMap.get() != NULL)
- {
- Id nId = (*pTokenMap)[Element];
- CreateElement aCreateElement = (*pMap)[Element];
-
- switch (aCreateElement.m_nResource)
- {</xsl:text>
- <xsl:for-each select="/model/namespace/resource">
- <xsl:if test="generate-id(key('resources', @resource)) = generate-id(.)">
- <xsl:if test="not(@resource = 'Hex' or
- @resource = 'Integer' or
- @resource = 'UniversalMeasure' or
- @resource = 'Boolean' or
- @resource = 'List' or
- @resource = 'String')">
- <xsl:text>
- case RT_</xsl:text>
- <xsl:value-of select="@resource"/>
- <xsl:text>:
- aResult.set(OOXMLFastHelper<OOXMLFastContextHandler</xsl:text>
- <xsl:value-of select="@resource"/>
- <xsl:text>>::createAndSetParentAndDefine(pHandler, Element, nId, aCreateElement.m_nId));
- break;</xsl:text>
- </xsl:if>
- </xsl:if>
- </xsl:for-each>
- <xsl:text>
- case RT_Any:
- aResult.set(createFastChildContextFromStart(pHandler, Element));
- break;
- default:
- break;
- }
-
- }
- }
-
- return aResult;
-}
-</xsl:text>
-</xsl:template>
-
-<xsl:template name="factoryfornamespace">
- <xsl:text>
-OOXMLFactory_ns::Pointer_t OOXMLFactory::getFactoryForNamespace(Id nId)
-{
- OOXMLFactory_ns::Pointer_t pResult;
-
- switch (nId & 0xffff0000)
- {</xsl:text>
- <xsl:for-each select="/model/namespace">
- <xsl:text>
- case </xsl:text>
- <xsl:call-template name="idfornamespace"/>
- <xsl:text>:
- pResult = </xsl:text>
- <xsl:call-template name="factoryclassname"/>
- <xsl:text>::getInstance();
- break;</xsl:text>
- </xsl:for-each>
- <xsl:text>
- default:
- break;
- }
-
- return pResult;
-}
-</xsl:text>
-</xsl:template>
-
-<xsl:template name="factorycreatefromstart">
- <xsl:text>
-uno::Reference< xml::sax::XFastContextHandler > OOXMLFactory::createFastChildContextFromStart
-(OOXMLFastContextHandler * pHandler, Token_t Element)
-{
- uno::Reference < xml::sax::XFastContextHandler > aResult;
- OOXMLFactory_ns::Pointer_t pFactory;
-
-</xsl:text>
- <xsl:for-each select="/model/namespace">
- <xsl:text>
- if (! aResult.is())
- {
- pFactory = getFactoryForNamespace(</xsl:text>
- <xsl:call-template name="idfornamespace"/>
- <xsl:text>);
- aResult.set(createFastChildContextFromFactory(pHandler, pFactory, Element));
- }</xsl:text>
- </xsl:for-each>
- <xsl:text>
-
- return aResult;
-}
-</xsl:text>
-</xsl:template>
-
-<xsl:key name="namespaces-by-id" match="namespace-alias" use="@id"/>
-<xsl:template name="fasttokentoid">
- <xsl:text>
-namespace tokenmap {
-struct token { const char * name; Token_t nToken; };
-class Perfect_Hash
-{
-private:
- static inline unsigned int hash (const char *str, unsigned int len);
-public:
- static struct token *in_word_set (const char *str, unsigned int len);
-};
-}
-
-#ifdef DEBUG_DOMAINMAPPER
-string fastTokenToId(sal_uInt32 nToken)
-{
- </xsl:text>
- <xsl:text>
- string sResult;
-
- switch (nToken & 0xffff0000)
- {</xsl:text>
- <xsl:for-each select="//namespace-alias[generate-id() = generate-id(key('namespaces-by-id', @id)[1])]">
- <xsl:text>
- case NS_</xsl:text>
- <xsl:value-of select="@alias"/>
- <xsl:text>:
- sResult += "</xsl:text>
- <xsl:value-of select="@alias"/>
- <xsl:text>:";
- break;</xsl:text>
- </xsl:for-each>
- <xsl:text>
- }
-
- switch (nToken & 0xffff)
- {</xsl:text>
- <xsl:for-each select=".//rng:element[@localname]|.//rng:attribute[@localname]">
- <xsl:variable name="localname" select="@localname"/>
- <xsl:if test="generate-id(.) = generate-id(key('same-token-name', $localname)[1])">
- <xsl:text>
- case </xsl:text>
- <xsl:call-template name="fastlocalname"/>
- <xsl:text>:
- sResult += "</xsl:text>
- <xsl:value-of select="$localname"/>
- <xsl:text>";
- break;</xsl:text>
- </xsl:if>
- </xsl:for-each>
-<xsl:text>
- }
-
- return sResult;
-}
-#endif
- </xsl:text>
-</xsl:template>
-
-<xsl:template name="getfastparser">
-<xsl:text>
-uno::Reference < xml::sax::XFastParser > OOXMLStreamImpl::getFastParser()
-{
- if (! mxFastParser.is())
- {
- mxFastParser = css::xml::sax::FastParser::create(mxContext);
-</xsl:text>
-<xsl:for-each select="//namespace-alias">
- <xsl:text>
- mxFastParser->registerNamespace("</xsl:text>
- <xsl:value-of select="@name"/>
- <xsl:text>", </xsl:text>
- <xsl:call-template name="namespaceid"/>
- <xsl:text>);</xsl:text>
-</xsl:for-each>
-<xsl:text>
- }
-
- return mxFastParser;
-}
-</xsl:text>
-</xsl:template>
-
- <xsl:template match="/">
- <xsl:text>
-#include <com/sun/star/xml/sax/FastParser.hpp>
-#include "ooxml/OOXMLFactory.hxx"
-#include "ooxml/OOXMLFastHelper.hxx"
-#include "ooxml/OOXMLStreamImpl.hxx"
-</xsl:text>
- <xsl:call-template name="factoryincludes"/>
- <xsl:text>
-namespace writerfilter {
-namespace ooxml {
-
-/// @cond GENERATED
- </xsl:text>
- <xsl:call-template name="factorycreatecontextfromfactory"/>
- <xsl:call-template name="factoryfornamespace"/>
- <xsl:call-template name="factorycreatefromstart"/>
- <xsl:call-template name="fasttokentoid"/>
- <xsl:call-template name="getfastparser"/>
- <xsl:text>
-/// @endcond
-}}
-</xsl:text>
-</xsl:template>
-
-</xsl:stylesheet>
diff --git a/writerfilter/source/ooxml/factorytools.xsl b/writerfilter/source/ooxml/factorytools.xsl
index 7968796..337d258 100644
--- a/writerfilter/source/ooxml/factorytools.xsl
+++ b/writerfilter/source/ooxml/factorytools.xsl
@@ -214,17 +214,6 @@ case NN_<namesapce/@name> | DEFINE_<rng:define/@name>:
</xsl:template>
<!--
- Returns the identifier for a namespace.
-
-NS_<namespace/@alias>
-
--->
-<xsl:template name="namespaceid">
- <xsl:text>NS_</xsl:text>
- <xsl:value-of select="@alias"/>
-</xsl:template>
-
-<!--
Returns the value of the @resource attribute of the <resource>
node according to the current <define>.
-->
@@ -281,13 +270,4 @@ NS_<namespace/@alias>
<xsl:value-of select="translate(@name, '-', '_')"/>
</xsl:template>
-<xsl:template name="factoryincludes">
- <xsl:for-each select="/model/namespace">
- <xsl:text>
-#include "OOXMLFactory_</xsl:text>
- <xsl:value-of select="@name"/>
- <xsl:text>.hxx"</xsl:text>
- </xsl:for-each>
-</xsl:template>
-
</xsl:stylesheet>
More information about the Libreoffice-commits
mailing list