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:rng="http://relaxng.org/ns/structure/1.0"
27    xmlns:xalan="http://xml.apache.org/xalan"
28    exclude-result-prefixes = "xalan"
29    xml:indent="true">
30  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
31
32
33  <xsl:key name="resources"
34           match="resource[not(@generated)]" use="@name" />
35
36  <xsl:key name="resourcetags"
37           match="resource/@tag" use="ancestor::resource/@name"/>
38
39  <xsl:template name="generateresource">
40    <xsl:param name="resource"/>
41    <xsl:element name="resource">
42      <xsl:attribute name="name">
43        <xsl:value-of select="@name"/>
44      </xsl:attribute>
45      <xsl:attribute name="resource">
46        <xsl:value-of select="$resource"/>
47      </xsl:attribute>
48      <xsl:attribute name="generated">
49        <xsl:text>yes</xsl:text>
50      </xsl:attribute>
51      <xsl:for-each select="key('resourcetags', @name)">
52        <xsl:attribute name="tag">
53          <xsl:value-of select="."/>
54        </xsl:attribute>
55      </xsl:for-each>
56    </xsl:element>
57  </xsl:template>
58
59  <xsl:key name="nsaliases" match="//namespace-alias" use="@name"/>
60
61  <xsl:template name="resourcevalues">
62    <xsl:variable name="definename" select="@name"/>
63    <xsl:variable name="namespace" select="key('nsaliases', ancestor::namespace/rng:grammar/@ns)/@alias"/>
64    <xsl:element name="resource">
65      <xsl:attribute name="name">
66        <xsl:value-of select="@name"/>
67      </xsl:attribute>
68      <xsl:attribute name="resource">List</xsl:attribute>
69      <xsl:attribute name="generated">yes</xsl:attribute>
70      <xsl:for-each select="key('resourcetags', @name)">
71        <xsl:attribute name="tag">
72          <xsl:value-of select="."/>
73        </xsl:attribute>
74      </xsl:for-each>
75      <xsl:for-each select=".//rng:value">
76        <xsl:element name="value">
77          <xsl:attribute name="name">
78            <xsl:value-of select="translate(., '-+ ,', 'mp__')"/>
79          </xsl:attribute>
80          <xsl:attribute name="tokenid">
81            <xsl:text>ooxml:Value_</xsl:text>
82            <xsl:value-of select="translate($namespace, '-', '_')"/>
83            <xsl:text>_</xsl:text>
84            <xsl:value-of select="$definename"/>
85            <xsl:text>_</xsl:text>
86            <xsl:value-of select="translate(., '-+ ,', 'mp__')"/>
87          </xsl:attribute>
88          <xsl:value-of select="."/>
89        </xsl:element>
90      </xsl:for-each>
91    </xsl:element>
92  </xsl:template>
93
94    <xsl:template name="typeofdefine">
95        <xsl:for-each select="rng:data">
96            <xsl:choose>
97                <xsl:when test="@type='base64Binary'">
98                    <xsl:text>String</xsl:text>
99                </xsl:when>
100                <xsl:when test="@type='boolean'">
101                    <xsl:text>Boolean</xsl:text>
102                </xsl:when>
103                <xsl:when test="@type='byte'">
104                    <xsl:text>Integer</xsl:text>
105                </xsl:when>
106                <xsl:when test="@type='dateTime'">
107                    <xsl:text>String</xsl:text>
108                </xsl:when>
109                <xsl:when test="@type='decimal'">
110                    <xsl:text>Integer</xsl:text>
111                </xsl:when>
112                <xsl:when test="@type='float'">
113                    <xsl:text>Float</xsl:text>
114                </xsl:when>
115                <xsl:when test="@type='hexBinary'">
116                    <xsl:text>Hex</xsl:text>
117                </xsl:when>
118                <xsl:when test="@type='int'">
119                    <xsl:text>Integer</xsl:text>
120                </xsl:when>
121                <xsl:when test="@type='integer'">
122                    <xsl:text>Integer</xsl:text>
123                </xsl:when>
124                <xsl:when test="@type='positiveInteger'">
125                    <xsl:text>Integer</xsl:text>
126                </xsl:when>
127                <xsl:when test="@type='string'">
128                    <xsl:text>String</xsl:text>
129                </xsl:when>
130                <xsl:when test="@type='token'">
131                    <xsl:text>Integer</xsl:text>
132                </xsl:when>
133                <xsl:when test="@type='unsignedInt'">
134                    <xsl:text>Integer</xsl:text>
135                </xsl:when>
136                <xsl:when test="@type='unsignedLong'">
137                    <xsl:text>Integer</xsl:text>
138                </xsl:when>
139                <xsl:otherwise>
140                    <xsl:text>Unknown</xsl:text>
141                </xsl:otherwise>
142            </xsl:choose>
143        </xsl:for-each>
144    </xsl:template>
145
146    <xsl:template name="typeofattribute">
147        <xsl:for-each select="rng:ref">
148            <xsl:variable name="name" select="@name"/>
149            <xsl:for-each select="ancestor::namespace/rng:grammar/rng:define[@name=$name]">
150                <xsl:call-template name="typeofdefine"/>
151            </xsl:for-each>
152        </xsl:for-each>
153    </xsl:template>
154
155    <xsl:template name="generatevalueresource">
156        <xsl:variable name="name" select="@name"/>
157        <xsl:variable name="ns_id" select="generate-id(ancestor::namespace)"/>
158        <resource>
159            <xsl:attribute name="name">
160                <xsl:value-of select="@name"/>
161            </xsl:attribute>
162            <xsl:attribute name="resource">Value</xsl:attribute>
163            <xsl:attribute name="generated">yes</xsl:attribute>
164            <xsl:for-each select="key('resourcetags', @name)[generate-id(ancestor::namespace) = $ns_id]">
165              <xsl:attribute name="tag">
166                <xsl:value-of select="."/>
167              </xsl:attribute>
168            </xsl:for-each>
169            <xsl:for-each select=".//rng:attribute">
170                <xsl:variable name="type">
171                    <xsl:choose>
172                        <xsl:when test="rng:ref[@name='ST_OnOff']">
173                            <xsl:text>Boolean</xsl:text>
174                        </xsl:when>
175                        <xsl:when test="rng:text">
176                            <xsl:text>String</xsl:text>
177                        </xsl:when>
178                        <xsl:otherwise>
179                            <xsl:call-template name="typeofattribute"/>
180                        </xsl:otherwise>
181                    </xsl:choose>
182                </xsl:variable>
183                <attribute>
184                    <xsl:attribute name="name">
185                        <xsl:value-of select="@name"/>
186                    </xsl:attribute>
187                    <xsl:attribute name="tokenid">
188                        <xsl:text>ooxml:</xsl:text>
189                        <xsl:value-of select="$name"/>
190                        <xsl:text>_</xsl:text>
191                        <xsl:value-of select="@name"/>
192                    </xsl:attribute>
193                    <xsl:attribute name="action">
194                        <xsl:text>setValue</xsl:text>
195                    </xsl:attribute>
196                </attribute>
197                <xsl:if test="string-length($type) > 0">
198                    <action name="start">
199                        <xsl:attribute name="action">
200                            <xsl:text>setDefault</xsl:text>
201                            <xsl:value-of select="$type"/>
202                            <xsl:text>Value</xsl:text>
203                        </xsl:attribute>
204                    </action>
205                </xsl:if>
206            </xsl:for-each>
207        </resource>
208    </xsl:template>
209
210  <xsl:template match="namespace">
211    <xsl:variable name="nsid" select="generate-id(.)"/>
212    <xsl:element name="namespace">
213      <xsl:copy-of select="@*"/>
214      <xsl:copy-of select=".//start"/>
215      <xsl:copy-of select="./rng:grammar"/>
216
217      <xsl:for-each select=".//rng:define">
218        <xsl:variable name="resources" select="key('resources', @name)[generate-id(ancestor::namespace) = $nsid]"/>
219        <xsl:copy-of select="$resources"/>
220        <xsl:if test="count($resources) = 0">
221          <xsl:if test="substring(@name, 1, 3) = 'CT_'">
222            <xsl:if test="./rng:attribute[@name='val']">
223              <xsl:call-template name="generatevalueresource"/>
224            </xsl:if>
225          </xsl:if>
226          <xsl:if test="substring(@name, 1, 3) = 'ST_'">
227            <xsl:if test="./rng:data[@type='int']">
228              <xsl:call-template name="generateresource">
229                <xsl:with-param name="resource">Integer</xsl:with-param>
230              </xsl:call-template>
231            </xsl:if>
232            <xsl:if test="./rng:data[@type='integer']">
233              <xsl:call-template name="generateresource">
234                <xsl:with-param name="resource">Integer</xsl:with-param>
235              </xsl:call-template>
236            </xsl:if>
237            <xsl:if test="./rng:data[@type='long']">
238              <xsl:call-template name="generateresource">
239                <xsl:with-param name="resource">Integer</xsl:with-param>
240              </xsl:call-template>
241            </xsl:if>
242            <xsl:if test="./rng:data[@type='unsignedInt']">
243              <xsl:call-template name="generateresource">
244                <xsl:with-param name="resource">Integer</xsl:with-param>
245              </xsl:call-template>
246            </xsl:if>
247            <xsl:if test="./rng:data[@type='unsignedLong']">
248              <xsl:call-template name="generateresource">
249                <xsl:with-param name="resource">Integer</xsl:with-param>
250              </xsl:call-template>
251            </xsl:if>
252            <xsl:if test="./rng:data[@type='boolean']">
253              <xsl:call-template name="generateresource">
254                <xsl:with-param name="resource">Boolean</xsl:with-param>
255              </xsl:call-template>
256            </xsl:if>
257            <xsl:if test="./rng:data[@type='token']">
258              <xsl:call-template name="generateresource">
259                <xsl:with-param name="resource">String</xsl:with-param>
260              </xsl:call-template>
261            </xsl:if>
262            <xsl:if test="./rng:data[@type='string']">
263              <xsl:call-template name="generateresource">
264                <xsl:with-param name="resource">String</xsl:with-param>
265              </xsl:call-template>
266            </xsl:if>
267            <xsl:if test="./rng:data[@type='dateTime']">
268              <xsl:call-template name="generateresource">
269                <xsl:with-param name="resource">String</xsl:with-param>
270              </xsl:call-template>
271            </xsl:if>
272            <xsl:if test="./rng:data[@type='hexBinary']">
273              <xsl:call-template name="generateresource">
274                <xsl:with-param name="resource">Hex</xsl:with-param>
275              </xsl:call-template>
276            </xsl:if>
277            <xsl:if test="./rng:list">
278              <xsl:call-template name="resourcevalues"/>
279            </xsl:if>
280          </xsl:if>
281        </xsl:if>
282      </xsl:for-each>
283    </xsl:element>
284  </xsl:template>
285
286  <xsl:template match="namespace-alias">
287    <namespace-alias>
288      <xsl:for-each select="@*">
289        <xsl:copy-of select="."/>
290      </xsl:for-each>
291    </namespace-alias>
292  </xsl:template>
293
294  <xsl:template match="token">
295    <token>
296      <xsl:for-each select="@*">
297        <xsl:copy-of select="."/>
298      </xsl:for-each>
299    </token>
300  </xsl:template>
301
302  <xsl:template match="fasttoken">
303    <xsl:copy-of select="."/>
304  </xsl:template>
305
306  <xsl:template match="/">
307    <model>
308      <xsl:apply-templates select=".//namespace-alias">
309        <xsl:sort select="@id" data-type="number"/>
310      </xsl:apply-templates>
311      <xsl:apply-templates select=".//token"/>
312      <xsl:apply-templates select=".//fasttoken"/>
313      <xsl:apply-templates select=".//namespace"/>
314    </model>
315  </xsl:template>
316</xsl:stylesheet>
317