# HG changeset patch
# User Michael Stahl <mst@openoffice.org>
# Date 1284998235 -7200
# Node ID 55fae6015ce88deebbb563109b7cd00a2b4a8851
# Parent  25e9f4025104892ee9f34d624b548b98fdb8b700
rtftok01: writerfilter: some XSLT to generate tokens and gperf hash function

diff --git a/writerfilter/source/rtftok/makefile.mk b/writerfilter/source/rtftok/makefile.mk
--- a/writerfilter/source/rtftok/makefile.mk
+++ b/writerfilter/source/rtftok/makefile.mk
@@ -45,6 +45,7 @@
 # --- Files --------------------------------------------------------
 
 SLOFILES= \
+	$(SLO)$/RTFTokenToId.obj \
 	$(SLO)$/RTFScanner.obj \
 	$(SLO)$/RTFParseException.obj \
 	$(SLO)$/RTFDocumentImpl.obj \
@@ -83,15 +84,64 @@
 
 .INCLUDE :	target.mk
 
+RTFHXXOUTDIR=$(INCCOM)/rtftok
+RTFHXXOUTDIRCREATED=$(RTFHXXOUTDIR)/created
+
 RTFSCANNERCXX=$(MISC)/RTFScanner.cxx
+RTFTOKENS=$(MISC)/rtftokens.xml
+RTFTOKENSHXX=$(RTFHXXOUTDIR)/RTFTokens.hxx
+RTFGPERFFASTTOKENHXX=$(RTFHXXOUTDIR)/gperffasttoken.hxx
+RTFTOKENTOIDCXX=$(MISC)/RTFTokenToId.cxx
 
-GENERATEDFILES=$(RTFSCANNERCXX)
+RTFTOKENXSL=rtftoken.xsl
+RTFTOKENHEADERXSL=rtftokenheader.xsl
+RTFGPERFFASTTOKENXSL=rtfgperf.xsl
+RTFTOKENTOIDXSL=rtftokentoid.xsl
+
+MODELPROCESSED=rtfmodel.xml
+
+GENERATEDHEADERS= \
+	$(RTFTOKENSHXX) \
+	$(RTFGPERFFASTTOKENHXX) \
+
+GENERATEDFILES= \
+    $(GENERATEDHEADERS) \
+	$(RTFTOKENTOIDCXX) \
+    $(RTFSCANNERCXX) \
+
+$(RTFHXXOUTDIRCREATED):
+	$(MKDIRHIER) $(RTFHXXOUTDIR)
+	@$(TOUCH) $@
+
+$(GENERATEDHEADERS): $(RTFHXXOUTDIRCREATED)
+
+$(RTFTOKENS): $(RTFTOKENXSL) $(MODELPROCESSED)
+	@echo "Making:   " $(@:f)
+	$(COMMAND_ECHO)$(XSLTPROC) $(RTFTOKENXSL) $(MODELPROCESSED) > $@
+
+$(RTFTOKENSHXX): $(RTFTOKENHEADERXSL) $(RTFTOKENS)
+	@echo "Making:   " $(@:f)
+	$(COMMAND_ECHO)$(XSLTPROC) $(RTFTOKENHEADERXSL) $(RTFTOKENS) > $@
+
+$(RTFGPERFFASTTOKENHXX): $(RTFGPERFFASTTOKENXSL) $(RTFTOKENS)
+	@echo "Making:   " $(@:f)
+	$(COMMAND_ECHO)$(XSLTPROC) $(RTFGPERFFASTTOKENXSL) $(RTFTOKENS) \
+		| tr -d '\r' | gperf -I -t -E -S1 -c -G -LC++ > $@
+
+$(RTFTOKENTOIDCXX): $(RTFTOKENTOIDXSL) $(RTFTOKENS)
+	@echo "Making:   " $(@:f)
+	$(COMMAND_ECHO)$(XSLTPROC) $(RTFTOKENTOIDXSL) $(RTFTOKENS) > $@
 
 $(RTFSCANNERCXX): RTFScanner.lex
 	/usr/bin/flex -o$@ RTFScanner.lex
 
 $(SLO)/RTFScanner.obj: $(RTFSCANNERCXX)
 
+$(SLO)/RTFTokenToId.obj: $(RTFTOKENTOIDCXX)
+
+$(SLOFILES): $(GENERATEDHEADERS)
+
+
 .PHONY: genmake genclean
 
 genmake: $(GENERATEDFILES)
diff --git a/writerfilter/source/rtftok/rtfgperf.xsl b/writerfilter/source/rtftok/rtfgperf.xsl
new file mode 100644
--- /dev/null
+++ b/writerfilter/source/rtftok/rtfgperf.xsl
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!--
+   DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+
+   Copyright 2000, 2010 Oracle and/or its affiliates.
+
+   OpenOffice.org - a multi-platform office productivity suite
+
+   This file is part of OpenOffice.org.
+
+   OpenOffice.org is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License version 3
+   only, as published by the Free Software Foundation.
+
+   OpenOffice.org is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License version 3 for more details
+   (a copy is included in the LICENSE file that accompanied this code).
+
+   You should have received a copy of the GNU Lesser General Public License
+   version 3 along with OpenOffice.org.  If not, see
+   <http://www.openoffice.org/license.html>
+   for a copy of the LGPLv3 License.
+-->
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+  <xsl:output method="text"/>
+
+  <xsl:template match="/">
+
+    <!-- header -->
+<xsl:text>
+%{
+/*
+    THIS FILE IS GENERATED! DO NOT EDIT!
+*/
+
+#include &lt;RTFTokens.hxx&gt;
+
+namespace writerfilter { namespace rtftok { namespace tokenmap {
+
+%}
+
+struct token { char const* name; Token_t nToken; };
+
+%%
+</xsl:text>
+
+    <!-- controls -->
+    <xsl:for-each select="/tokens/token">
+      <xsl:sort select="text()"/>
+      <xsl:variable name="control">
+          <xsl:value-of select="text()"/>
+      </xsl:variable>
+      <xsl:value-of select="$control"/>
+      <xsl:text>, RTF_</xsl:text>
+      <xsl:value-of select="$control"/>
+      <xsl:text>&#xA;</xsl:text>
+    </xsl:for-each>
+
+    <!-- footer -->
+<xsl:text>%%
+
+Token_t
+GetToken(char const*const string, unsigned long const len)
+{
+    struct token const*const pToken( Perfect_Hash::in_word_set(string, len) );
+    return (pToken) ? pToken->nToken : RTF_TOKENS_END;
+}
+
+} } } // namespace writerfilter::rtftok::tokenmap
+</xsl:text>
+
+  </xsl:template>
+</xsl:stylesheet>
diff --git a/writerfilter/source/rtftok/rtftoken.xsl b/writerfilter/source/rtftok/rtftoken.xsl
new file mode 100644
--- /dev/null
+++ b/writerfilter/source/rtftok/rtftoken.xsl
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!--
+   DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+
+   Copyright 2000, 2010 Oracle and/or its affiliates.
+
+   OpenOffice.org - a multi-platform office productivity suite
+
+   This file is part of OpenOffice.org.
+
+   OpenOffice.org is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License version 3
+   only, as published by the Free Software Foundation.
+
+   OpenOffice.org is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License version 3 for more details
+   (a copy is included in the LICENSE file that accompanied this code).
+
+   You should have received a copy of the GNU Lesser General Public License
+   version 3 along with OpenOffice.org.  If not, see
+   <http://www.openoffice.org/license.html>
+   for a copy of the LGPLv3 License.
+-->
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
+
+  <xsl:template match="*">
+  </xsl:template>
+
+  <xsl:key name="names" match="//control|//destination|//flag|//toggle|//value|//symbol" use="@name"/>
+
+  <xsl:template match="/">
+      <!--
+      <xsl:for-each select="key('names', *)">
+          -->
+    <tokens>
+    <xsl:comment> THIS FILE IS GENERATED! DO NOT EDIT! </xsl:comment>
+    <xsl:text>&#xA;</xsl:text>
+
+    <xsl:for-each select="//control|//destination|//flag|//toggle|//value|//symbol">
+      <xsl:sort select="@name"/>
+      <xsl:if test="generate-id(.) = generate-id(key('names',@name)[1])">
+        <!-- filter out various symbols that are handled by the lexer -->
+        <xsl:if test="not(starts-with(@name, &quot;&apos;&quot;) or @name = '*' or @name = '-' or @name = ':' or @name = '_' or @name = '~' or @name = '|')">
+          <xsl:variable name="control">
+            <xsl:choose>
+              <xsl:when test="substring(@name, string-length(@name))='N'">
+                  <xsl:value-of select="substring(@name, 1, string-length(@name)- 1)"/>
+              </xsl:when>
+              <xsl:otherwise>
+                  <xsl:value-of select="@name"/>
+              </xsl:otherwise>
+            </xsl:choose>
+          </xsl:variable>
+          <xsl:if test="string-length(@name)&gt;0">
+            <xsl:text>  </xsl:text>
+            <token>
+              <xsl:value-of select="$control"/>
+            </token>
+            <xsl:text>&#xA;</xsl:text>
+          </xsl:if>
+        </xsl:if>
+      </xsl:if>
+    </xsl:for-each>
+    </tokens>
+  </xsl:template>
+
+</xsl:stylesheet>
diff --git a/writerfilter/source/rtftok/rtftokenheader.xsl b/writerfilter/source/rtftok/rtftokenheader.xsl
new file mode 100644
--- /dev/null
+++ b/writerfilter/source/rtftok/rtftokenheader.xsl
@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!--
+   DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+
+   Copyright 2000, 2010 Oracle and/or its affiliates.
+
+   OpenOffice.org - a multi-platform office productivity suite
+
+   This file is part of OpenOffice.org.
+
+   OpenOffice.org is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License version 3
+   only, as published by the Free Software Foundation.
+
+   OpenOffice.org is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License version 3 for more details
+   (a copy is included in the LICENSE file that accompanied this code).
+
+   You should have received a copy of the GNU Lesser General Public License
+   version 3 along with OpenOffice.org.  If not, see
+   <http://www.openoffice.org/license.html>
+   for a copy of the LGPLv3 License.
+-->
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+  <xsl:output method="text"/>
+
+  <xsl:template match="/">
+
+    <!-- header -->
+<xsl:text>
+/*
+    THIS FILE IS GENERATED! DO NOT EDIT!
+*/
+
+#ifndef INCLUDED_RTF_TOKENS_HXX
+#define INCLUDED_RTF_TOKENS_HXX
+
+#include &lt;string&gt;
+
+#include &lt;sal/types.h&gt;
+
+
+namespace writerfilter {
+namespace rtftok {
+
+
+typedef sal_uInt32 Token_t;
+
+</xsl:text>
+
+    <!-- controls -->
+    <xsl:for-each select="/tokens/token">
+      <xsl:sort select="text()"/>
+      <xsl:variable name="control">
+          <xsl:value-of select="text()"/>
+      </xsl:variable>
+      <xsl:text>Token_t const RTF_</xsl:text>
+      <xsl:value-of select="$control"/>
+      <xsl:text> = </xsl:text>
+      <xsl:value-of select="position() - 1"/>
+      <xsl:text>;&#xA;</xsl:text>
+    </xsl:for-each>
+    <xsl:text>&#xA;</xsl:text>
+    <xsl:text>Token_t const RTF_TOKENS_END = </xsl:text>
+    <xsl:value-of select="count(/tokens/token)"/>
+    <xsl:text>;&#xA;</xsl:text>
+
+    <!-- footer -->
+<xsl:text>
+
+char const* TokenToId(Token_t const nToken);
+
+} } // namespace writerfilter::rtftok
+
+#endif // INCLUDED_RTF_TOKENS_HXX
+</xsl:text>
+
+  </xsl:template>
+</xsl:stylesheet>
diff --git a/writerfilter/source/rtftok/rtftokentoid.xsl b/writerfilter/source/rtftok/rtftokentoid.xsl
new file mode 100644
--- /dev/null
+++ b/writerfilter/source/rtftok/rtftokentoid.xsl
@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!--
+   DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+
+   Copyright 2000, 2010 Oracle and/or its affiliates.
+
+   OpenOffice.org - a multi-platform office productivity suite
+
+   This file is part of OpenOffice.org.
+
+   OpenOffice.org is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License version 3
+   only, as published by the Free Software Foundation.
+
+   OpenOffice.org is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License version 3 for more details
+   (a copy is included in the LICENSE file that accompanied this code).
+
+   You should have received a copy of the GNU Lesser General Public License
+   version 3 along with OpenOffice.org.  If not, see
+   <http://www.openoffice.org/license.html>
+   for a copy of the LGPLv3 License.
+-->
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+  <xsl:output method="text"/>
+
+  <xsl:template match="/">
+
+    <!-- header -->
+<xsl:text>
+/*
+    THIS FILE IS GENERATED! DO NOT EDIT!
+*/
+
+#include &lt;RTFTokens.hxx&gt;
+
+#include &lt;osl/diagnose.h&gt;
+
+
+namespace writerfilter {
+namespace rtftok {
+
+
+char const*
+TokenToId(Token_t const nToken)
+{
+    switch (nToken)
+    {
+</xsl:text>
+
+    <!-- controls -->
+    <xsl:for-each select="/tokens/token">
+      <xsl:sort select="text()"/>
+      <xsl:variable name="control">
+          <xsl:value-of select="text()"/>
+      </xsl:variable>
+      <xsl:text>        case RTF_</xsl:text>
+      <xsl:value-of select="$control"/>
+      <xsl:text>: return "</xsl:text>
+      <xsl:value-of select="$control"/>
+      <xsl:text>";&#xA;</xsl:text>
+    </xsl:for-each>
+    <xsl:text>&#xA;</xsl:text>
+    <xsl:text>        default:&#xA;</xsl:text>
+    <xsl:text>            OSL_ASSERT(false);&#xA;</xsl:text>
+    <xsl:text>            return "";&#xA;</xsl:text>
+
+    <!-- footer -->
+<xsl:text>
+    } // switch (nToken)
+}
+
+} } // namespace writerfilter::rtftok
+
+</xsl:text>
+
+  </xsl:template>
+</xsl:stylesheet>
+
