1<!--***********************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 ***********************************************************-->
21
22
23<xsl:stylesheet
24    version="1.0"
25    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
26    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
27    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
28    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
29    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
30    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
31    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
32    xmlns:xlink="http://www.w3.org/1999/xlink"
33    xmlns:dc="http://purl.org/dc/elements/1.1/"
34    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
35    xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
36    xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
37    xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
38    xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
39    xmlns:math="http://www.w3.org/1998/Math/MathML"
40    xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
41    xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
42    xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0"
43    xmlns:ooo="http://openoffice.org/2004/office"
44    xmlns:ooow="http://openoffice.org/2004/writer"
45    xmlns:oooc="http://openoffice.org/2004/calc"
46    xmlns:dom="http://www.w3.org/2001/xml-events"
47    xmlns:xforms="http://www.w3.org/2002/xforms"
48    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
49    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
50    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
51    xmlns:rng="http://relaxng.org/ns/structure/1.0"
52    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
53    xmlns:UML = 'org.omg.xmi.namespace.UML' xml:space="default">
54  <xsl:output method="xml" />
55
56  <xsl:key name="namespace-aliases" match="//namespace-alias" use="@name"/>
57
58  <xsl:template match="/">
59    <xsl:apply-templates/>
60  </xsl:template>
61
62  <xsl:template name="prefixfromurl">
63    <xsl:param name="url"/>
64    <xsl:variable name="prefix" select="key('namespace-aliases', $url)/@alias"/>
65    <xsl:choose>
66      <xsl:when test="string-length($prefix) > 0">
67        <xsl:value-of select="$prefix"/>
68      </xsl:when>
69      <xsl:otherwise>
70        <xsl:value-of select="translate(substring-after($url, 'http://'), '/.', '__')"/>
71      </xsl:otherwise>
72    </xsl:choose>
73  </xsl:template>
74
75  <xsl:template name="prefixforgrammar">
76    <xsl:variable name="ns" select="ancestor::namespace/rng:grammar/@ns"/>
77    <xsl:variable name="prefix" select="key('namespace-aliases', $ns)/@alias"/>
78    <xsl:choose>
79      <xsl:when test="string-length($prefix) > 0">
80        <xsl:value-of select="$prefix"/>
81      </xsl:when>
82      <xsl:otherwise>
83        <xsl:call-template name="prefixfromurl">
84          <xsl:with-param name="url" select="$ns"/>
85        </xsl:call-template>
86      </xsl:otherwise>
87    </xsl:choose>
88  </xsl:template>
89
90  <xsl:template name="nsforgrammar">
91    <xsl:value-of select="ancestor::namespace/rng:grammar/@ns"/>
92  </xsl:template>
93
94  <xsl:template match="rng:element[@name|./rng:anyName] | rng:attribute[@name] | element | attribute">
95    <xsl:variable name="prefix">
96      <xsl:choose>
97        <xsl:when test="contains(@name, ':')">
98          <xsl:variable name="myname" select="@name"/>
99          <xsl:call-template name="prefixfromurl">
100            <xsl:with-param name="url" select="string(namespace::*[local-name(.) = substring-before($myname, ':')])"/>
101          </xsl:call-template>
102        </xsl:when>
103        <xsl:when test="name(.)='attribute'">
104          <xsl:if test="ancestor::namespace/rng:grammar/@attributeFormDefault='qualified'">
105            <xsl:call-template name="prefixforgrammar"/>
106          </xsl:if>
107        </xsl:when>
108        <xsl:otherwise>
109          <xsl:call-template name="prefixforgrammar"/>
110        </xsl:otherwise>
111      </xsl:choose>
112    </xsl:variable>
113    <xsl:variable name="ns">
114      <xsl:choose>
115        <xsl:when test="contains(@name, ':')">
116          <xsl:variable name="myname" select="@name"/>
117          <xsl:value-of select="string(namespace::*[local-name(.) = substring-before($myname, ':')])"/>
118        </xsl:when>
119        <xsl:when test="name(.)='attribute'">
120          <xsl:if test="ancestor::rng:grammar/@attributeFormDefault='qualified'">
121            <xsl:call-template name="nsforgrammar"/>
122          </xsl:if>
123        </xsl:when>
124        <xsl:otherwise>
125          <xsl:call-template name="nsforgrammar"/>
126        </xsl:otherwise>
127      </xsl:choose>
128    </xsl:variable>
129    <xsl:variable name="localname">
130      <xsl:choose>
131        <xsl:when test="contains(@name, ':')">
132          <xsl:value-of select="substring-after(@name, ':')"/>
133        </xsl:when>
134	<xsl:when test="./rng:anyName">
135	  <xsl:text>FAST_TOKENS_END</xsl:text>
136	</xsl:when>
137        <xsl:otherwise>
138          <xsl:value-of select="@name"/>
139        </xsl:otherwise>
140      </xsl:choose>
141    </xsl:variable>
142    <xsl:copy>
143      <xsl:apply-templates select="@*"/>
144      <xsl:attribute name="enumname">
145        <xsl:if test="string-length($prefix) > 0">
146          <xsl:value-of select="$prefix"/>
147          <xsl:text>:</xsl:text>
148        </xsl:if>
149        <xsl:value-of select="$localname"/>
150      </xsl:attribute>
151      <xsl:attribute name="qname">
152        <xsl:if test="string-length($ns) > 0">
153          <xsl:value-of select="$ns"/>
154          <xsl:text>:</xsl:text>
155        </xsl:if>
156        <xsl:value-of select="$localname"/>
157      </xsl:attribute>
158      <xsl:attribute name="prefix">
159        <xsl:value-of select="$prefix"/>
160      </xsl:attribute>
161      <xsl:attribute name="localname">
162        <xsl:value-of select="$localname"/>
163      </xsl:attribute>
164      <xsl:apply-templates/>
165    </xsl:copy>
166  </xsl:template>
167
168  <xsl:template match="rng:grammar">
169   <xsl:copy>
170     <xsl:apply-templates select="@*"/>
171     <xsl:attribute name="application">
172       <xsl:value-of select="substring-before(substring-after(@ns, 'http://schemas.openxmlformats.org/'), '/')"/>
173     </xsl:attribute>
174     <xsl:apply-templates/>
175   </xsl:copy>
176  </xsl:template>
177
178  <xsl:template match="node()|@*">
179   <xsl:copy>
180   <xsl:apply-templates select="@*"/>
181   <xsl:apply-templates/>
182   </xsl:copy>
183  </xsl:template>
184
185  <xsl:template match="model">
186   <xsl:copy>
187   <xsl:apply-templates select="@*"/>
188   <xsl:apply-templates/>
189   </xsl:copy>
190  </xsl:template>
191
192  <xsl:template match="rng:define|rng:ref">
193   <xsl:copy>
194     <xsl:apply-templates select="@*"/>
195     <xsl:attribute name="classfordefine">
196       <xsl:variable name="name" select="@name"/>
197       <xsl:choose>
198         <xsl:when test="(starts-with(@name, 'CT_') or starts-with(@name, 'EG_') or starts-with(@name, 'AG_'))">1</xsl:when>
199         <xsl:when test="ancestor::namespace//start[@name=$name]">1</xsl:when>
200         <xsl:otherwise>0</xsl:otherwise>
201       </xsl:choose>
202     </xsl:attribute>
203     <xsl:apply-templates/>
204   </xsl:copy>
205  </xsl:template>
206
207  <xsl:template match="namespace">
208    <xsl:variable name="ns" select=".//rng:grammar/@ns"/>
209   <xsl:copy>
210     <xsl:apply-templates select="@*"/>
211     <xsl:attribute name="namespacealias">
212       <xsl:value-of select="key('namespace-aliases', $ns)/@alias"/>
213     </xsl:attribute>
214     <xsl:attribute name="prefix"><xsl:value-of select="translate(substring-after($ns, 'http://schemas.openxmlformats.org/'), '/-', '__')"/></xsl:attribute>
215     <xsl:apply-templates/>
216   </xsl:copy>
217  </xsl:template>
218</xsl:stylesheet>
219