# HG changeset patch
# User Henning Brinkmann <hbrinkm@openoffice.org>
# Date 1282750817 -7200
# Node ID e8b9206324e018baa890b2ed1e476117a88578cf
# Parent  d87fcd24d47c7fa2f9895dae2e35b0688c9811d0
rtftok01: preprocess right side setting some parentheses

diff --git a/writerfilter/source/rtftok/spectobnf.xsl b/writerfilter/source/rtftok/spectobnf.xsl
--- a/writerfilter/source/rtftok/spectobnf.xsl
+++ b/writerfilter/source/rtftok/spectobnf.xsl
@@ -112,6 +112,74 @@
   </xsl:choose>
 </xsl:template>
 
+<xsl:template name="parensbyprecedence">
+  <xsl:param name="string"/>
+  <xsl:param name="prev"/>
+
+  <xsl:variable name="token">
+    <xsl:call-template name="nexttoken">
+      <xsl:with-param name="string" select="$string"/>
+    </xsl:call-template>
+  </xsl:variable>
+  <xsl:variable name="rest" select="substring-after($string, $token)"/>
+
+  <!--
+  <xsl:text>&#xa;string:</xsl:text>
+  <xsl:value-of select="$string"/>
+  <xsl:text>&#xa;rest:</xsl:text>
+  <xsl:value-of select="$rest"/>
+  <xsl:text>&#xa;token:</xsl:text>
+  <xsl:value-of select="$token"/>
+  -->
+  
+  <xsl:choose>
+    <xsl:when test="string-length($token) &gt; 0">
+      <xsl:choose>
+        <xsl:when test="translate($token, '?+*', 'aaa') = 'a'">
+          <xsl:call-template name="parensbyprecedence">
+            <xsl:with-param name="string" select="$rest"/>
+            <xsl:with-param name="prev">
+              <xsl:text>(</xsl:text>
+              <xsl:value-of select="$prev"/>
+              <xsl:value-of select="$token"/>
+              <xsl:text>)</xsl:text>
+            </xsl:with-param>
+          </xsl:call-template>
+        </xsl:when>
+        <xsl:when test="starts-with($token, '(')">
+          <xsl:variable name="braceinner">
+            <xsl:call-template name="braceinner">
+              <xsl:with-param name="string" select="substring($token,2)"/>
+              <xsl:with-param name="depth">1</xsl:with-param>
+            </xsl:call-template>
+          </xsl:variable>
+          <xsl:call-template name="parensbyprecedence">
+            <xsl:with-param name="string" select="$rest"/>
+            <xsl:with-param name="prev">
+              <xsl:text>(</xsl:text>
+              <xsl:call-template name="parensbyprecedence">
+                <xsl:with-param name="string" select="$braceinner"/>
+              </xsl:call-template>
+              <xsl:text>)</xsl:text>
+            </xsl:with-param>
+          </xsl:call-template>
+        </xsl:when>
+        <xsl:otherwise>
+          <xsl:value-of select="$prev"/>
+          <xsl:call-template name="parensbyprecedence">
+            <xsl:with-param name="string" select="$rest"/>
+            <xsl:with-param name="prev" select="$token"/>
+          </xsl:call-template>
+        </xsl:otherwise>
+      </xsl:choose>
+    </xsl:when>
+    <xsl:otherwise>
+      <xsl:value-of select="$prev"/>
+      <xsl:value-of select="$string"/>
+    </xsl:otherwise>
+  </xsl:choose>
+</xsl:template>
+
 <xsl:template name="nexttokens">
   <xsl:param name="string"/>
   <xsl:param name="precedence"/>
@@ -171,6 +239,12 @@
     <xsl:when test="starts-with($string, '#PCDATA')">
       <xsl:text>#PCDATA</xsl:text>
     </xsl:when>
+    <xsl:when test="starts-with($string, '\*\')">
+      <xsl:text>\*\</xsl:text>
+      <xsl:call-template name="control">
+        <xsl:with-param name="string" select="substring($string,4)"/>
+      </xsl:call-template>
+    </xsl:when>
     <xsl:when test="starts-with($string, '\')">
       <xsl:text>\</xsl:text>
       <xsl:call-template name="control">
@@ -242,6 +316,19 @@
             </xsl:with-param>
           </xsl:call-template>
         </xsl:when>
+        <xsl:when test="starts-with($token, '\*\')">
+          <xsl:copy-of select="$prev"/>
+          <xsl:call-template name="parseright">
+            <xsl:with-param name="right" select="$rest"/>
+            <xsl:with-param name="prev">
+              <destination>
+                <xsl:attribute name="name">
+                  <xsl:value-of select="substring($token, 4)"/>
+                </xsl:attribute>
+              </destination>
+            </xsl:with-param>
+          </xsl:call-template>
+        </xsl:when>
         <xsl:when test="starts-with($token, '\')">
           <xsl:copy-of select="$prev"/>
           <xsl:call-template name="parseright">
@@ -311,10 +398,8 @@
         </xsl:when>
         <xsl:when test="starts-with($token, '|')">
           <xsl:variable name="myright">
-            <xsl:call-template name="nexttokens">
+            <xsl:call-template name="nexttoken">
               <xsl:with-param name="string" select="$rest"/>
-              <xsl:with-param name="precedence" select="1"/>
-              <xsl:with-param name="guard" select="10"/>
             </xsl:call-template>
           </xsl:variable>
           <xsl:variable name="myrest" select="substring-after($rest, $myright)"/>
@@ -332,10 +417,8 @@
         </xsl:when>
         <xsl:when test="starts-with($token, '&amp;')">
           <xsl:variable name="myright">
-            <xsl:call-template name="nexttokens">
+            <xsl:call-template name="nexttoken">
               <xsl:with-param name="string" select="$rest"/>
-              <xsl:with-param name="precedence" select="1"/>
-              <xsl:with-param name="guard" select="10"/>
             </xsl:call-template>
           </xsl:variable>
           <xsl:variable name="myrest" select="substring-after($rest, $myright)"/>
@@ -356,11 +439,9 @@
           <xsl:call-template name="parseright">
             <xsl:with-param name="right" select="$rest"/>
             <xsl:with-param name="prev">
-              <paren>
-                <xsl:call-template name="parseright">
-                  <xsl:with-param name="right" select="substring($token, 2, string-length($token) - 2)"/>
-                </xsl:call-template>
-              </paren>
+              <xsl:call-template name="parseright">
+                <xsl:with-param name="right" select="substring($token, 2, string-length($token) - 2)"/>
+              </xsl:call-template>
             </xsl:with-param>
           </xsl:call-template>
         </xsl:when>
@@ -389,9 +470,11 @@
     </xsl:when>
     <xsl:otherwise>
       <xsl:copy-of select="$prev"/>
-      <notoken>
-        <xsl:value-of select="$right"/>
-      </notoken>
+      <xsl:if test="string-length($right) > 0">
+        <notoken>
+          <xsl:value-of select="$right"/>
+        </notoken>
+      </xsl:if>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
@@ -611,9 +694,19 @@
             <xsl:comment>
               <xsl:value-of select="$right"/>
             </xsl:comment>
+            <xsl:variable name="rightpreprocessed">
+              <xsl:call-template name="parensbyprecedence">
+                <xsl:with-param name="string" select="$right"/>
+              </xsl:call-template>
+            </xsl:variable>
+            <xsl:comment>
+              <xsl:value-of select="$rightpreprocessed"/>
+            </xsl:comment>            
+
             <xsl:call-template name="parseright">
-              <xsl:with-param name="right" select="$right"/>
+              <xsl:with-param name="right" select="$rightpreprocessed"/>
             </xsl:call-template>
+
           </define>
         </xsl:if>
       </xsl:for-each>
