Imported Upstream version 2.8.0
[platform/upstream/libxml2.git] / test / namespaces / reconcile / tests-to-c.xsl
1 <?xml version="1.0"?>
2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3         <xsl:output method="text"/>
4
5         <xsl:template match="/">
6                 <xsl:text><![CDATA[
7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
9 #include <libxml/xpath.h>
10 #include <libxml/xpathInternals.h>
11 ]]>
12 </xsl:text>
13                 <xsl:call-template name="serializer"/>          
14                 <xsl:apply-templates select="tests/test"/>
15                 <xsl:text>
16
17 int main(int argc, char **argv) {&#xA;</xsl:text>
18                 <xsl:apply-templates select="tests/test" mode="call"/>
19                 <xsl:text>
20         /* printf("finished.\n"); */
21         return (0);
22 }
23 </xsl:text>
24         </xsl:template> 
25
26         <xsl:template match="tests/test" mode="call">
27                 <xsl:text>      nsTest_</xsl:text>
28                 <xsl:value-of select="@name"/>
29                 <xsl:text>();&#xA;</xsl:text>
30         </xsl:template>
31
32         <xsl:template name="xml-text">
33                 <xsl:param name="text"/>
34                 <xsl:call-template name="replace-string">
35                         <!-- Substitute #10 for " -->                                   
36                 <xsl:with-param name="from" select="'&#10;'"/>
37                 <xsl:with-param name="to" select="'&quot;&#10;&quot;'"/>
38                         <xsl:with-param name="text">
39                                 <xsl:call-template name="replace-string">
40                                         <!-- Substitute " for \" -->
41                                 <xsl:with-param name="from" select="'&quot;'"/>
42                                 <xsl:with-param name="to" select="'\&quot;'"/>
43                                         <xsl:with-param name="text">
44                                                 <xsl:call-template name="replace-string">
45                                                         <!-- Remove tabs. -->
46                                                 <xsl:with-param name="from" select="'&#9;'"/>
47                                                 <xsl:with-param name="to" select="''"/>
48                                                         <xsl:with-param name="text" select="$text"/>
49                                         </xsl:call-template>
50                                         </xsl:with-param>
51                         </xsl:call-template>
52                         </xsl:with-param>
53         </xsl:call-template>
54                 
55         </xsl:template>
56
57         <xsl:template match="doc" mode="define">
58                 <xsl:text>      xmlDocPtr </xsl:text>
59                 <xsl:value-of select="@name"/>
60                 <xsl:text>;&#xA;</xsl:text>
61                 <xsl:text>      const char * </xsl:text>
62                 <xsl:value-of select="@name"/>
63                 <xsl:text>_str = "</xsl:text>
64                 <xsl:call-template name="xml-text">
65                 <xsl:with-param name="text" select="."/>
66         </xsl:call-template>            
67                 <xsl:text>";&#xA;</xsl:text>
68         </xsl:template>
69
70         <xsl:template match="expected" mode="define">
71                 <xsl:text>      const char * </xsl:text>        
72                 <xsl:text>exp_str = "</xsl:text>
73                 <xsl:call-template name="xml-text">
74                 <xsl:with-param name="text" select="."/>
75         </xsl:call-template>            
76                 <xsl:text>";&#xA;</xsl:text>
77         </xsl:template>
78
79         <xsl:template match="doc">
80                 <xsl:text>      </xsl:text>
81                 <xsl:value-of select="@name"/>
82                 <xsl:text> = xmlReadDoc(BAD_CAST </xsl:text>
83                 <xsl:value-of select="@name"/>
84                 <xsl:text>_str, NULL, NULL, 0);&#xA;</xsl:text>
85                         
86                 <xsl:apply-templates select="following-sibling::*[1]"/>
87
88                 <xsl:text>      xmlFreeDoc(</xsl:text>
89                 <xsl:value-of select="@name"/>
90                 <xsl:text>);&#xA;</xsl:text>
91         </xsl:template>
92
93         <xsl:template match="xpath">
94         </xsl:template>
95
96         <xsl:template match="var" mode="define">
97                 <xsl:text>      xmlNodePtr </xsl:text>
98                 <xsl:value-of select="@name"/>
99                 <xsl:text>;&#xA;</xsl:text>
100         </xsl:template>
101
102         <xsl:template match="var">
103                 <xsl:if test="xpath">
104                         <!-- Create XPath context. -->
105                         <xsl:text>      /* Selecting node "</xsl:text><xsl:value-of select="@name"/><xsl:text>". */&#xA;</xsl:text>
106                         <xsl:text>      xp = xmlXPathNewContext(</xsl:text>
107                         <xsl:value-of select="xpath/@doc"/>
108                         <xsl:text>);&#xA;</xsl:text>
109                         <!-- Register namespaces. -->
110                         <xsl:for-each select="xpath/reg-ns">
111                                 <xsl:text>      xmlXPathRegisterNs(xp, BAD_CAST "</xsl:text>
112                                 <xsl:value-of select="@prefix"/>
113                                 <xsl:text>", BAD_CAST "</xsl:text>
114                                 <xsl:value-of select="@ns"/>
115                                 <xsl:text>");&#xA;</xsl:text>
116                         </xsl:for-each>
117                         <!-- Evaluate. -->
118                         <xsl:text>      </xsl:text>
119                         <xsl:value-of select="@name"/>
120                         <xsl:text> = nsSelectNode(xp, "</xsl:text>
121                         <xsl:value-of select="xpath/@select-node"/>
122                         <xsl:text>");&#xA;</xsl:text>
123                         <xsl:text>      xmlXPathFreeContext(xp);&#xA;</xsl:text>
124                 </xsl:if>
125                 <xsl:apply-templates select="following-sibling::*[1]"/>
126         </xsl:template>
127
128         <xsl:template match="reconcile-ns">
129                 <xsl:text>      /* Reconcile node "</xsl:text><xsl:value-of select="@ref"/><xsl:text>". */&#xA;</xsl:text>
130                 <xsl:text>      xmlDOMWrapReconcileNamespaces(NULL, </xsl:text>
131                 <xsl:value-of select="@node"/>
132                 <xsl:text>, 0);&#xA;</xsl:text>
133                 <xsl:apply-templates select="following-sibling::*[1]"/>
134         </xsl:template>
135
136         <xsl:template match="remove">
137                 <xsl:text>      xmlDOMWrapRemoveNode(NULL, </xsl:text>
138                 <xsl:value-of select="@node"/>
139                 <xsl:text>->doc, </xsl:text>
140                 <xsl:value-of select="@node"/>
141                 <xsl:text>, 0);&#xA;</xsl:text>
142                 <xsl:apply-templates select="following-sibling::*[1]"/>
143         </xsl:template>
144
145         <xsl:template match="adopt">
146                 <xsl:text>      /* Adopt "</xsl:text><xsl:value-of select="@node"/><xsl:text>". */&#xA;</xsl:text>
147                 <xsl:text>      xmlDOMWrapAdoptNode(NULL, </xsl:text>
148                 <xsl:value-of select="@node"/>
149                 <xsl:text>->doc, </xsl:text>
150                 <xsl:value-of select="@node"/>
151                 <xsl:text>, </xsl:text>
152                 <xsl:value-of select="@dest-doc"/>
153                 <xsl:text>, </xsl:text>
154                 <xsl:choose>
155                         <xsl:when test="@dest-parent">
156                                 <xsl:value-of select="@dest-parent"/>
157                         </xsl:when>
158                         <xsl:otherwise>
159                                 <xsl:text>NULL</xsl:text>
160                         </xsl:otherwise>
161                 </xsl:choose>           
162                 <xsl:text>, 0);&#xA;</xsl:text>
163                 <xsl:apply-templates select="following-sibling::*[1]"/>
164         </xsl:template>
165
166         <xsl:template match="append-child">
167                 <xsl:text>      xmlAddChild(</xsl:text>
168                 <xsl:value-of select="@parent"/>
169                 <xsl:text>, </xsl:text>
170                 <xsl:value-of select="@child"/>
171                 <xsl:text>);&#xA;</xsl:text>
172                 <xsl:apply-templates select="following-sibling::*[1]"/>
173         </xsl:template>
174
175         <xsl:template match="expected">         
176                 <xsl:text>      /* Serialize "</xsl:text><xsl:value-of select="@doc"/><xsl:text>". */&#xA;</xsl:text>
177                 <xsl:text>      result_str = nsSerializeNode(xmlDocGetRootElement(</xsl:text>
178                 <xsl:value-of select="@doc"/>
179                 <xsl:text>));&#xA;</xsl:text>
180                 <xsl:text>      /* Compare result. */
181         if (! xmlStrEqual(BAD_CAST result_str, BAD_CAST exp_str)) {
182                 printf("FAILED\n");
183                 printf("%s\n", (const char *) result_str);
184                 printf("- - -\n");
185                 printf("Expected:\n%s\n", exp_str);
186                 printf("= = =\n");
187         }
188         xmlFree(result_str);&#xA;</xsl:text>
189         </xsl:template>
190
191         <!--********
192             * TEST *
193             ********-->
194         <xsl:template match="test">             
195                 <xsl:text>void nsTest_</xsl:text>
196                 <xsl:value-of select="@name"/>
197                 <xsl:text>(void) {
198         xmlChar * result_str;
199         xmlXPathContextPtr xp;
200         int memory;&#xA;</xsl:text>
201                 <xsl:apply-templates select="*" mode="define"/>
202                 <xsl:text>
203         memory = xmlMemUsed();
204         xmlInitParser();&#xA;&#xA;</xsl:text>
205                 <xsl:apply-templates select="child::*[1]"/>
206                 <xsl:text>
207         xmlCleanupParser();
208         memory = xmlMemUsed() - memory;
209
210         if (memory != 0) {              
211                 printf("## '%s' MEMORY leak: %d\n", "</xsl:text>
212                 <xsl:value-of select="@name"/>
213                 <xsl:text>", memory);
214     }           
215 }
216 </xsl:text>     
217         </xsl:template>
218
219         <xsl:template name="serializer">
220                 <xsl:text>
221                 
222 xmlChar * nsSerializeNode(xmlNodePtr node) {
223         xmlChar * ret;
224
225         xmlOutputBufferPtr buf;
226         buf = xmlAllocOutputBuffer(NULL);
227         xmlNodeDumpOutput(buf, node->doc, node, 0, 0, NULL);
228         xmlOutputBufferFlush(buf);
229         ret = (xmlChar *) buf->buffer->content;
230         buf->buffer->content = NULL;
231         (void) xmlOutputBufferClose(buf);
232         return (ret);
233 }
234
235 xmlNodePtr nsSelectNode(xmlXPathContextPtr xp, const char * xpath) {
236         xmlXPathObjectPtr xpres;
237         xmlNodePtr ret; 
238                 
239         xpres = xmlXPathEval(BAD_CAST xpath, xp);
240         ret = xpres->nodesetval->nodeTab[0];
241         xmlXPathFreeObject(xpres);
242         return (ret);
243 }
244
245 </xsl:text>
246         </xsl:template>
247
248         <xsl:template name="replace-string">
249     <xsl:param name="text"/>
250     <xsl:param name="from"/>
251     <xsl:param name="to"/>
252
253     <xsl:choose>
254       <xsl:when test="contains($text, $from)">
255
256         <xsl:variable name="before" select="substring-before($text, $from)"/>
257         <xsl:variable name="after" select="substring-after($text, $from)"/>
258         <xsl:variable name="prefix" select="concat($before, $to)"/>
259
260         <xsl:value-of select="$before"/>
261         <xsl:value-of select="$to"/>
262         <xsl:call-template name="replace-string">
263           <xsl:with-param name="text" select="$after"/>
264           <xsl:with-param name="from" select="$from"/>
265           <xsl:with-param name="to" select="$to"/>
266         </xsl:call-template>
267       </xsl:when> 
268       <xsl:otherwise>
269         <xsl:value-of select="$text"/>  
270       </xsl:otherwise>
271     </xsl:choose>            
272  </xsl:template>
273                 
274         
275 </xsl:stylesheet>